@Override public String createNewGame(String player1Id, String player2Id) { IGame game = createGame(player1Id, player2Id); Logger.info( String.format( "Player %s and %s has started a new multi player game.", player1Id, player2Id)); return game.getGameID(); }
/** Creates new game for a given player and returns the gameId as String */ @Override public String createNewGame(String playerId) { PlayerAI cp = new SimplePlayerAI(UUID.randomUUID().toString()); Logger.info(String.format("Player %s has started a new single player game.", playerId)); IGame game = createGame(playerId, cp.getId()); game.addObserver(cp); game.notifyObservers(); return game.getGameID(); }
/** Tries to create a multi player game if at least two players are waiting */ private void tryCreateMultiPlayerGame() { Logger.info( String.format( "Try to create a new multi player game. Queue length: %d", waitingPlayers.size())); if (waitingPlayers.size() >= 2) { String p1 = waitingPlayers.poll(); String p2 = waitingPlayers.poll(); IGame game = createGame(p1, p2); String gid = game.getGameID(); games.put(gid, game); mpGames.put(p1, gid); mpGames.put(p2, gid); Logger.info(String.format("Multi player game %s created.", gid)); } }
@Override public void abortGame(String gameId, String playerId) { try { IGame game = getGame(gameId); Logger.info(String.format("Player %s has aborted game %s.", playerId, gameId)); game.setAborted(playerId); // Delete the game if both players have aborted. if (game.isFinished()) { games.remove(game.getGameID()); Logger.info(String.format("Game %s has been removed.", gameId)); } else { game.notifyObservers(); } } catch (UnknownPlayerException ex) { Logger.error(ex.toString()); } }