1
\$\begingroup\$

I'm currently about to create my first networking game, and it's supposed to be two player and turn-based. I wrote the client using the LibGDX library, and the server with Kryonet.

It should be possible for multiple users to play the games, at once, I feel the need to create something like a "game room". I thought about creating a class with two connections (the two players), adding a listener to each, and to handle the messages within the game room.

However, I am not sure how to permanently update the game rooms. At the moment, my server only listens for new connections, and registers them in a list of connected clients. Maybe, I should create a new thread for each match?


How do I create such game rooms within the game server, itself?

Edit:

So after doing some more research, this is how I currently do it: - I store the players, that are currently searching for a game in a LinkedList (more precise, their Connections) and once there are more than two, I do this in my Server class:

if (connectedPlayers.size() >= 2) {
            GameRoom newGame = new GameRoom(connectedPlayers.removeFirst(), connectedPlayers.removeFirst());
            gameRooms.add(newGame); //some list, storing all the current games
            newGame.start();
}

and my GameRoom looks like this:

public class GameRoom implements Runnable {
Thread t;
Connection playerOne;
Connection playerTwo;
//more variables your GameRoom might need

public GameRoom(Connection playerOne, Connection playerTwo) {
    this.playerOne = playerOne;
    this.playerTwo = playerTwo;
    //whatever more variables you need to use can be declared here, e.g.
}

@Override
public void run() {
    while (true) {
        //Game logic goes here
    }
}

public void start() {
    t = new Thread(this);
    t.start();
}

} I am not really into threading and this is what I set up after a little more reading into that topic, but I assume this is not really scaleable - because 1000 games would mean 1000 more threads, which I think is not so good. Please let me know, whether this is the correct approach and if not, just stick to my question and help me with it, if you can and feel like it.

\$\endgroup\$
  • \$\begingroup\$ Note the changes I made to your question. Asking "which is better" can be tricky, as different users may have different opinions. Simply asking how do do it works better. You get options, and votes should give you an idea of which answer should work better. I have also removed your pseudo-request for tutorials; such requests can make a question off topic. Note that a good answer may still point you to additional tutorials or alternate reading. \$\endgroup\$ – Gnemlock Feb 8 '17 at 0:44

Your Answer

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

Browse other questions tagged or ask your own question.