if (game.getId() >= 0) {
assertAmMod(session); // only admin can edit existing games.
try {
lobby.database.startTransaction();
GameRoom gameRoom = lobby.database.getGame(game.getId());
if (!gameRoom.getName().equals(game.getName())) {
gameRoom.setName( game.getName() );
lobby.database.saveGame(gameRoom);
lobby.gameChanged(gameRoom);
}
// TODO we may want to resign/add players
}
finally {
lobby.database.endTransaction();
}
}
else {
if (game.getPlayers().isEmpty()) {
GameRoom gameRoom = lobby.createGame(game);
lobby.joinGame(session.getUsername(), gameRoom.getId());
}
else {
Set<Player> players = game.getPlayers();
game.setPlayers(new HashSet());
GameRoom gameRoom = lobby.createGame(game);
// TODO somehow need to tell the DB this is a private game
for (Player player: players) {
gameRoom = lobby.joinGame(player.getName(), gameRoom.getId(), session);
}
// tell everyone about the game.
lobby.gameChanged(gameRoom);
}
}