i have a problem implementing a turn-based game server in java.

What i want is a way or a technic that let the server keep all the games state that are created in order to the client recover at the same state when an disconnection occurs.

Until now i have developed the server-client programmes and they work fine, but i didn't know that TCP can't recover over some situations of connection lose. So i get so many "connection timed out" errors, and my programmes don't have a reconnect and recover technic. Now i need to think a way the server keeps some info of all the games.

My (for now) solution is to have a class that all threads have a reference to, inside the class we have many ArrayLists to store every info we need so threads can access and handle them.

For Example:

Game-123 has the score 100 so an ArrayList scoreOfGame[123]=100

Game-3 PlayerB name is stored at ArrayList PlayersB_Names[3]=name.

If there is a better way to keep track of all game states seperately on the server, i would appreciate it!!!!

  • \$\begingroup\$ How long do.your games last? Are these rounds played in a single session over a few minutes/hours, or are turns spread over days/weeks? \$\endgroup\$ – DMGregory Oct 6 '17 at 11:46
  • \$\begingroup\$ Few minutes to hours not days....its a simple card game \$\endgroup\$ – never give Oct 6 '17 at 12:57

Is the name just a string or is it a unique identifier for each player.

If it is a uid you can just iterate over the player name arrays to see if the player is currently involved in a running game and then the keeps on playing that game. I can't get into more details without knowing your architecture but I bet you get the point.

On the other side I would recommend something like the following:

Server-side you could introduce a "session" object. Each currently running game holds it's own (and only it's own) game state within this session object. Instead global arrays holding the states (scoreOfGame[123]).

e.g. (since you mentioned each session lives in a thread)

class Session implements IRunnable() {
    private List<Player> players;
    private int score;

    //other stuff

You also would then have a session manager class handling your sessions. If a client looses a connection you can either ask the session manager if the player has a running session and then (re)join it to this session.

The benefit is that you do not need the arrays holding all the states of the other game instances.

Which may makes the code a little easier and gives it a slightly better structure/architecture.

If players are unique identifiable you could have a player class and the matching player manager which holds the players.

Then you could give the player class a member Session currentSession. When it logs on to your server you would have the current session already referenced and would not need to search all current session for the player.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.