Hello!
I’m trying to code a server like this:
The main class is the queue.
Queue has one slot where user awaits for his opponent.
When queue reaches two players, new ‘GameRoom’ object is created and both of them are passed to it’s constructor (we don’t need them in queue anymore)
In this ‘GameRoom’ we make a gameloop that handles their 1v1 match.
In the same time, queue is empty and awaits for the same situation - for two players etc…
First of all, I’m a newbie with networking, but I figured out that I can’t open a listener ‘behind’ the main listener (in a GameRoom) cause it would be unreachable.
server.addListener(new Listener() {
public void received (Connection c, Object object) {
//c.setIdleThreshold(1);
if(numberOfPlayers == 2){
System.out.println("NEW GAME ROOM CREATED");
GameRoom gameRoom;
gameRoom = new GameRoom(player_list.get(0), player_list.get(1), gameID++);
numberOfPlayers = numberOfPlayers - 2;
}
if (object instanceof Login) {
System.out.println("LOGIN FROM: " + c.getRemoteAddressTCP());
}
}
}
private boolean isValid (String value) {
if (value == null) return false;
value = value.trim();
if (value.length() == 0) return false;
return true;
}
public void disconnected (Connection c) {
numberOfPlayers--;
}
});
server.bind(Network.port, 8889);
server.start();
}
This is the listener code - it’s just ‘taken’ from Kryonet.
My question is: How should I handle this kind of game?
Can I somehow separate listeners/connections to queue and gamerooms?
I thought of something like this:
User connects and gets his ‘GameRoom Unique Number’. When a packet with for ex. position comes to the server it reads to which GameRoom this user belongs and passes this position to him, but I think this method is not efficient, cause let’s see if we have 10 000 players. There are 10 000 iterations over the players list (with only single packet).
I hope that you understood what I’m trying to accomplish. If there is anyone that could help me with this idea - just point me - how should I code it - I would be grateful!