public boolean isMatchTableStillValid() { // check only normal match table with state != Finished if (!table.isTournament()) { int humanPlayers = 0; int aiPlayers = 0; int validHumanPlayers = 0; if (!(table.getState().equals(TableState.WAITING) || table.getState().equals(TableState.STARTING) || table.getState().equals(TableState.READY_TO_START))) { if (match == null) { logger.debug("- Match table with no match:"); logger.debug("-- matchId:" + match.getId() + " [" + match.getName() + "]"); // return false; } else if (match.isDoneSideboarding() && match.getGame() == null) { // no sideboarding and not active game -> match seems to hang (maybe the Draw bug) logger.debug("- Match with no active game and not in sideboard state:"); logger.debug("-- matchId:" + match.getId() + " [" + match.getName() + "]"); // return false; } } // check for active players for (Map.Entry<UUID, UUID> userPlayerEntry : userPlayerMap.entrySet()) { MatchPlayer matchPlayer = match.getPlayer(userPlayerEntry.getValue()); if (matchPlayer == null) { logger.debug("- Match player not found:"); logger.debug("-- matchId:" + match.getId()); logger.debug("-- userId:" + userPlayerEntry.getKey()); logger.debug("-- playerId:" + userPlayerEntry.getValue()); continue; } if (matchPlayer.getPlayer().isHuman()) { humanPlayers++; if ((table.getState().equals(TableState.WAITING) || table.getState().equals(TableState.STARTING) || table.getState().equals(TableState.READY_TO_START)) || !match.isDoneSideboarding() || (!matchPlayer.hasQuit() && match.getGame() != null && matchPlayer.getPlayer().isInGame())) { User user = UserManager.getInstance().getUser(userPlayerEntry.getKey()); if (user == null) { logger.debug("- Active user of match is missing: " + matchPlayer.getName()); logger.debug("-- matchId:" + match.getId()); logger.debug("-- userId:" + userPlayerEntry.getKey()); logger.debug("-- playerId:" + userPlayerEntry.getValue()); return false; } // user exits on the server and match player has not quit -> player is valid validHumanPlayers++; } } else { aiPlayers++; } } // if at least 2 human players are valid (multiplayer) or all human players are valid the // table is valid or it's an AI match return validHumanPlayers >= 2 || validHumanPlayers == humanPlayers || aiPlayers > 1; } return true; }
private void autoSideboard() { for (MatchPlayer player : match.getPlayers()) { if (!player.isDoneSideboarding()) { match.submitDeck(player.getPlayer().getId(), player.generateDeck()); } } }
private void sideboard() { table.sideboard(); setupTimeout(Match.SIDEBOARD_TIME); if (table.isTournamentSubTable()) { for (MatchPlayer matchPlayer : match.getPlayers()) { if (!matchPlayer.hasQuit()) { TournamentPlayer tournamentPlayer = table.getTournament().getPlayer(matchPlayer.getPlayer().getId()); if (tournamentPlayer != null) { tournamentPlayer.setStateInfo("sideboarding"); } } } } match.sideboard(); cancelTimeout(); if (table.isTournamentSubTable()) { for (MatchPlayer matchPlayer : match.getPlayers()) { TournamentPlayer tournamentPlayer = table.getTournament().getPlayer(matchPlayer.getPlayer().getId()); if (tournamentPlayer != null && tournamentPlayer.getStateInfo().equals("sideboarding")) { tournamentPlayer.setStateInfo(""); } } } }
public boolean isUserStillActive(UUID userId) { UUID playerId = userPlayerMap.get(userId); if (playerId != null) { if (tournament != null) { TournamentPlayer tournamentPlayer = tournament.getPlayer(playerId); if (tournamentPlayer != null) { return tournamentPlayer.isInTournament(); } } else if (match != null) { MatchPlayer matchPlayer = match.getPlayer(playerId); return matchPlayer != null && !matchPlayer.hasQuit(); } } return false; }
private void matchEnd() { if (match != null) { for (Entry<UUID, UUID> entry : userPlayerMap.entrySet()) { MatchPlayer matchPlayer = match.getPlayer(entry.getValue()); // opponent(s) left during sideboarding if (matchPlayer != null) { if (!matchPlayer.hasQuit()) { User user = UserManager.getInstance().getUser(entry.getKey()); if (user != null) { if (table.getState().equals(TableState.SIDEBOARDING)) { StringBuilder sb = new StringBuilder(); if (table.isTournamentSubTable()) { sb.append("Your tournament match of round "); sb.append(table.getTournament().getRounds().size()); sb.append(" is over. "); } else { sb.append("Match [").append(match.getName()).append("] is over. "); } if (match.getPlayers().size() > 2) { sb.append("All your opponents have lost or quit the match."); } else { sb.append("Your opponent has quit the match."); } user.showUserMessage("Match info", sb.toString()); } // remove table from user - table manager holds table for display of finished matches if (!table.isTournamentSubTable()) { user.removeTable(entry.getValue()); } } } } } // free resources no longer needed match.cleanUpOnMatchEnd( ConfigSettings.getInstance().isSaveGameActivated(), table.isTournament()); } }
public synchronized boolean submitDeck(UUID userId, DeckCardLists deckList) throws MageException { UUID playerId = userPlayerMap.get(userId); if (table.isTournament()) { TournamentPlayer player = tournament.getPlayer(playerId); if (player == null || player.hasQuit()) { return true; // so the construct panel closes after submit } } else if (table.getMatch() != null) { MatchPlayer mPlayer = table.getMatch().getPlayer(playerId); if (mPlayer == null || mPlayer.hasQuit()) { return true; // so the construct panel closes after submit } if (table.isTournamentSubTable()) { TournamentPlayer tournamentPlayer = table.getTournament().getPlayer(mPlayer.getPlayer().getId()); if (tournamentPlayer != null) { tournamentPlayer.setStateInfo(""); // reset sideboarding state } } } if (table.getState() != TableState.SIDEBOARDING && table.getState() != TableState.CONSTRUCTING) { return false; } Deck deck = Deck.load(deckList, false, false); if (table.getState() == TableState.SIDEBOARDING && table.getMatch() != null) { MatchPlayer mPlayer = table.getMatch().getPlayer(playerId); if (mPlayer != null) { deck.setName(mPlayer.getDeck().getName()); } } if (!Main.isTestMode() && !table.getValidator().validate(deck)) { throw new InvalidDeckException( "Invalid deck for this format", table.getValidator().getInvalid()); } submitDeck(userId, playerId, deck); return true; }
public MatchView(Match match) { this.matchId = match.getId(); this.matchName = match.getName(); this.gameType = match.getOptions().getGameType(); this.deckType = match.getOptions().getDeckType(); for (Game game : match.getGames()) { games.add(game.getId()); } StringBuilder sb1 = new StringBuilder(); StringBuilder sb2 = new StringBuilder(); for (MatchPlayer player : match.getPlayers()) { sb1.append(player.getPlayer().getName()).append(", "); sb2.append(player.getPlayer().getName()) .append(" ") .append(player.getWins()) .append("-") .append(player.getLoses()) .append(", "); players = sb1.substring(0, sb1.length() - 2); result = sb2.substring(0, sb2.length() - 2); } this.startTime = match.getStartTime(); this.endTime = match.getEndTime(); }
/** Called by eliminate tournaments after each match */ public void eliminatePlayers() { if (match.hasEnded()) { MatchPlayer mPlayer1 = match.getPlayer(player1.getPlayer().getId()); MatchPlayer mPlayer2 = match.getPlayer(player2.getPlayer().getId()); if (mPlayer1.hasQuit() || !mPlayer1.isMatchWinner()) { player1.setEliminated(); } if (mPlayer2.hasQuit() || !mPlayer2.isMatchWinner()) { player2.setEliminated(); } } }
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); } } }