/** * Ends the current game and starts if neccessary the next game * * @return true if table can be closed */ public boolean endGameAndStartNextGame() { // get player that chooses who goes first Game game = match.getGame(); if (game == null) { return true; } UUID choosingPlayerId = match.getChooser(); match.endGame(); if (ConfigSettings.getInstance().isSaveGameActivated() && !game.isSimulation()) { if (GameManager.getInstance().saveGame(game.getId())) { match.setReplayAvailable(true); } } GameManager.getInstance().removeGame(game.getId()); try { if (!match.hasEnded()) { if (match.getGame() != null && match.getGame().getGameType().isSideboardingAllowed()) { sideboard(); } if (!match.hasEnded()) { startGame(choosingPlayerId); } else { closeTable(); } } else { closeTable(); } } catch (GameException ex) { logger.fatal(null, ex); } return match.hasEnded(); }
// remove user from all sub tables of a tournament public void userQuitTournamentSubTables(UUID tournamentId, UUID userId) { for (TableController controller : controllers.values()) { if (controller.getTable().isTournamentSubTable() && controller.getTable().getTournament().getId().equals(tournamentId)) { Match match = controller.getTable().getMatch(); if (match != null) { if (match.getGame() != null) { GameManager.getInstance().quitMatch(match.getGame().getId(), userId); } } } } }
private void startGame(UUID choosingPlayerId) throws GameException { try { match.startGame(); table.initGame(); GameOptions gameOptions = new GameOptions(); gameOptions.rollbackTurnsAllowed = match.getOptions().isRollbackTurnsAllowed(); match.getGame().setGameOptions(gameOptions); GameManager.getInstance() .createGameSession( match.getGame(), userPlayerMap, table.getId(), choosingPlayerId, gameOptions); String creator = null; StringBuilder opponent = new StringBuilder(); for (Entry<UUID, UUID> entry : userPlayerMap.entrySet()) { // no AI players if (match.getPlayer(entry.getValue()) != null && !match.getPlayer(entry.getValue()).hasQuit()) { User user = UserManager.getInstance().getUser(entry.getKey()); if (user != null) { user.ccGameStarted(match.getGame().getId(), entry.getValue()); if (creator == null) { creator = user.getName(); } else { if (opponent.length() > 0) { opponent.append(" - "); } opponent.append(user.getName()); } } else { logger.error( "Unable to find user: "******" playerId: " + entry.getValue()); MatchPlayer matchPlayer = match.getPlayer(entry.getValue()); if (matchPlayer != null && !matchPlayer.hasQuit()) { matchPlayer.setQuit(true); } } } } // Append AI opponents to the log file for (MatchPlayer mPlayer : match.getPlayers()) { if (!mPlayer.getPlayer().isHuman()) { if (opponent.length() > 0) { opponent.append(" - "); } opponent.append(mPlayer.getName()); } } ServerMessagesUtil.getInstance().incGamesStarted(); // log about game started logger.info( "GAME started " + (match.getGame() != null ? match.getGame().getId() : "no Game") + " [" + match.getName() + "] " + creator + " - " + opponent.toString()); logger.debug("- matchId: " + match.getId() + " [" + match.getName() + "]"); if (match.getGame() != null) { logger.debug("- chatId: " + GameManager.getInstance().getChatId(match.getGame().getId())); } } catch (Exception ex) { logger.fatal("Error starting game table: " + table.getId(), ex); if (table != null) { TableManager.getInstance().removeTable(table.getId()); } if (match != null) { Game game = match.getGame(); if (game != null) { GameManager.getInstance().removeGame(game.getId()); } } } }
public synchronized void leaveTable(UUID userId) { if (table == null) { logger.error("No table object - userId: " + userId); return; } if (table.isTournament() && tournament == null) { logger.error("No tournament object - userId: " + userId + " table: " + table.getId()); return; } if (table != null && this.userId != null && this.userId.equals(userId) // tourn. sub tables have no creator user && (table.getState().equals(TableState.WAITING) || table.getState().equals(TableState.READY_TO_START))) { // table not started yet and user is the owner, remove the table TableManager.getInstance().removeTable(table.getId()); } else { UUID playerId = userPlayerMap.get(userId); if (playerId != null) { if (table.getState() == TableState.WAITING || table.getState() == TableState.READY_TO_START) { table.leaveNotStartedTable(playerId); if (table.isTournament()) { tournament.removePlayer(playerId); } else { match.quitMatch(playerId); } User user = UserManager.getInstance().getUser(userId); if (user != null) { ChatManager.getInstance() .broadcast( chatId, user.getName(), "has left the table", ChatMessage.MessageColor.BLUE, true, ChatMessage.MessageType.STATUS, ChatMessage.SoundToPlay.PlayerLeft); if (!table.isTournamentSubTable()) { user.removeTable(playerId); } } else { logger.debug("User not found - userId: " + userId + " tableId:" + table.getId()); } userPlayerMap.remove(userId); } else if (!table.getState().equals(TableState.FINISHED)) { if (table.isTournament()) { logger.debug("Quit tournament sub tables for userId: " + userId); TableManager.getInstance().userQuitTournamentSubTables(tournament.getId(), userId); logger.debug( "Quit tournament Id: " + table.getTournament().getId() + "(" + table.getTournament().getTournamentState() + ")"); TournamentManager.getInstance().quit(tournament.getId(), userId); } else { MatchPlayer matchPlayer = match.getPlayer(playerId); if (matchPlayer != null && !match.hasEnded() && !matchPlayer.hasQuit()) { Game game = match.getGame(); if (game != null && !game.hasEnded()) { Player player = match.getPlayer(playerId).getPlayer(); if (player != null && player.isInGame()) { GameManager.getInstance().quitMatch(game.getId(), userId); } match.quitMatch(playerId); } else { if (table.getState().equals(TableState.SIDEBOARDING)) { if (!matchPlayer.isDoneSideboarding()) { // submit deck to finish sideboarding and trigger match start / end matchPlayer.submitDeck(matchPlayer.getDeck()); } } match.quitMatch(playerId); } } } } } else { logger.error("No playerId found for userId: " + userId); } } }