Game Development Reference
In-Depth Information
The
connsForGame
list is empty for now, but will be populated as players con-
nect:
private Game createGame(){
Game game = new Game();
game.setId(nextGameId++);
games.put(game.getId(), game);
List<HostedConnection> connsForGame = new
ArrayList<HostedConnection>();
connectionFilters.put(game.getId(), connsForGame);
return game;
}
6. The first thing the
addPlayer
method does is create a new
Player
object and
then set the ID of it. We use
WelcomeMessage
to send the ID back to the play-
er:
private void addPlayer(Game game, HostedConnection
conn){
Player player = new Player();
player.setId(nextPlayerId++);
7. The server broadcasts this message using the client's connection as a filter, ensur-
ing it's the only recipient of the message, as follows:
WelcomeMessage welcomeMessage = new
WelcomeMessage();
welcomeMessage.setMyPlayerId(player.getId());
server.broadcast(Filters.in(conn), welcomeMessage);
8. It then decides whether the player is the first or second to connect to the game,
and adds the player's
HostedConnection
instance to the list of connections
associated with this game, as shown in the following code snippet:
if(game.getPlayerOne() == null){
game.setPlayerOne(player);
} else {
game.setPlayerTwo(player);
}
List<HostedConnection> connsForGame =