public synchronized void startTournament(UUID userId) { try { if (userId.equals(this.userId) && table.getState().equals(TableState.STARTING)) { tournament.setStartTime(); TournamentManager.getInstance() .createTournamentSession(tournament, userPlayerMap, table.getId()); for (Entry<UUID, UUID> entry : userPlayerMap.entrySet()) { User user = UserManager.getInstance().getUser(entry.getKey()); if (user != null) { logger.info( new StringBuilder("User ") .append(user.getName()) .append(" tournament started: ") .append(tournament.getId()) .append(" userId: ") .append(user.getId())); user.ccTournamentStarted(tournament.getId(), entry.getValue()); } } ServerMessagesUtil.getInstance().incTournamentsStarted(); } } catch (Exception ex) { logger.fatal("Error starting tournament", ex); TableManager.getInstance().removeTable(table.getId()); TournamentManager.getInstance().quit(tournament.getId(), userId); } }
@Override public TableView createTournamentTable(UUID userId, TournamentOptions options) { Table table = TableManager.getInstance().createTournamentTable(this.getRoomId(), userId, options); tables.put(table.getId(), table); return new TableView(table); }
public synchronized void startMatch() { if (table.getState().equals(TableState.STARTING)) { try { if (table.isTournamentSubTable()) { logger.info( "Tourn. match started id:" + match.getId() + " tournId: " + table.getTournament().getId()); } else { User user = UserManager.getInstance().getUser(userId); logger.info( "MATCH started [" + match.getName() + "] " + match.getId() + "(" + user.getName() + ")"); logger.debug( "- " + match.getOptions().getGameType() + " - " + match.getOptions().getDeckType()); } match.startMatch(); startGame(null); } catch (GameException ex) { logger.fatal("Error starting match ", ex); match.endGame(); } } }
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 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(""); } } } }
private void submitDeck(UUID userId, UUID playerId, Deck deck) { if (table.getState() == TableState.SIDEBOARDING) { match.submitDeck(playerId, deck); UserManager.getInstance().getUser(userId).removeSideboarding(table.getId()); } else { TournamentManager.getInstance().submitDeck(tournament.getId(), playerId, deck); UserManager.getInstance().getUser(userId).removeConstructing(playerId); } }
public void updateDeck(UUID userId, DeckCardLists deckList) throws MageException { UUID playerId = userPlayerMap.get(userId); if (table.getState() != TableState.SIDEBOARDING && table.getState() != TableState.CONSTRUCTING) { return; } Deck deck = Deck.load(deckList, false, false); updateDeck(userId, playerId, deck); }
// used for tournaments public MatchView(Table table) { this.matchId = table.getTournament().getId(); this.matchName = table.getName(); this.gameType = table.getGameType(); this.deckType = table.getDeckType(); StringBuilder sb1 = new StringBuilder(); for (TournamentPlayer tPlayer : table.getTournament().getPlayers()) { sb1.append(tPlayer.getPlayer().getName()) .append(" (") .append(tPlayer.getPoints()) .append(" P.) "); } this.players = sb1.toString(); StringBuilder sb2 = new StringBuilder(); if (table.getTournament().getRounds().size() > 0) { for (TournamentPlayer tPlayer : table.getTournament().getPlayers()) { sb2.append(tPlayer.getPlayer().getName()) .append(": ") .append(tPlayer.getResults()) .append(" "); } } else { sb2.append("Canceled"); } this.result = sb2.toString(); this.startTime = table.getTournament().getStartTime(); this.endTime = table.getTournament().getEndTime(); }
@Override public void removeTable(UUID tableId) { Table table = tables.get(tableId); if (table != null) { table.cleanUp(); tables.remove(tableId); if (logger.isDebugEnabled()) { logger.debug("Table removed: " + tableId); } } }
public synchronized boolean changeTableStateToStarting() { if (!getTable().getState().equals(TableState.READY_TO_START)) { // tournament is not ready, can't start return false; } if (!table.allSeatsAreOccupied()) { logger.debug("Not alle Seats are occupied: stop start tableId:" + table.getId()); return false; } getTable().setState(TableState.STARTING); return true; }
private void updateDeck(UUID userId, UUID playerId, Deck deck) { if (table.isTournament()) { if (tournament != null) { TournamentManager.getInstance().updateDeck(tournament.getId(), playerId, deck); } else { logger.fatal("Tournament == null table: " + table.getId() + " userId: " + userId); } } else if (TableState.SIDEBOARDING.equals(table.getState())) { match.updateDeck(playerId, deck); } else { // deck was meanwhile submitted so the autoupdate can be ignored } }
public void addPlayer(UUID userId, Player player, String playerType, Deck deck) throws GameException { if (table.getState() != TableState.WAITING) { return; } Seat seat = table.getNextAvailableSeat(playerType); if (seat == null) { throw new GameException("No available seats."); } match.addPlayer(player, deck); table.joinTable(player, seat); if (player.isHuman()) { userPlayerMap.put(userId, player.getId()); } }
public TableController(UUID roomId, UUID userId, TournamentOptions options) { this.userId = userId; tournament = TournamentFactory.getInstance().createTournament(options.getTournamentType(), options); if (userId != null) { User user = UserManager.getInstance().getUser(userId); if (user == null) { logger.fatal( new StringBuilder("User for userId ") .append(userId) .append(" could not be retrieved from UserManager") .toString()); controllerName = "[unknown]"; } else { controllerName = user.getName(); } } else { controllerName = "System"; } table = new Table( roomId, options.getTournamentType(), options.getName(), controllerName, DeckValidatorFactory.getInstance() .createDeckValidator(options.getMatchOptions().getDeckType()), options.getPlayerTypes(), TableRecorderImpl.getInstance(), tournament); chatId = ChatManager.getInstance().createChatSession("Tourn. table " + table.getId()); }
public void leaveTable(UUID userId, UUID tableId) { TableController tableController = controllers.get(tableId); if (tableController != null) { // table not started yet and user is the owner, remove the table Table table = getTable(tableId); if (table != null && isTableOwner(tableId, userId) && (table.getState().equals(TableState.WAITING) || table.getState().equals(TableState.STARTING))) { removeTable(tableId); } else { logger.debug("TABLE leave - userId: " + userId + " tableId: " + tableId); tableController.leaveTable(userId); } } }
public synchronized boolean replaceDraftPlayer( Player oldPlayer, String name, String playerType, int skill) { Player newPlayer = createPlayer(name, playerType, skill); if (newPlayer == null || table.getState() != TableState.DRAFTING) { return false; } TournamentPlayer oldTournamentPlayer = tournament.getPlayer(oldPlayer.getId()); tournament.removePlayer(oldPlayer.getId()); tournament.addPlayer(newPlayer, playerType); TournamentPlayer newTournamentPlayer = tournament.getPlayer(newPlayer.getId()); newTournamentPlayer.setState(oldTournamentPlayer.getState()); newTournamentPlayer.setReplacedTournamentPlayer(oldTournamentPlayer); DraftManager.getInstance().getController(table.getId()).replacePlayer(oldPlayer, newPlayer); return true; }
public boolean watchTable(UUID userId) { if (table.isTournament()) { UserManager.getInstance().getUser(userId).ccShowTournament(table.getTournament().getId()); return true; } else { if (table.isTournamentSubTable() && !table.getTournament().getOptions().isWatchingAllowed()) { return false; } if (table.getState() != TableState.DUELING) { return false; } // you can't watch your own game if (userPlayerMap.get(userId) != null) { return false; } return UserManager.getInstance().getUser(userId).ccWatchGame(match.getGame().getId()); } }
public void debugServerState() { logger.debug("--- Server state ----------------------------------------------"); Collection<User> users = UserManager.getInstance().getUsers(); logger.debug( "--------User: "******" [userId | since | name -----------------------"); for (User user : users) { logger.debug( user.getId() + " | " + formatter.format(user.getConnectionTime()) + " | " + user.getName() + " (" + user.getUserState().toString() + ")"); } ArrayList<ChatSession> chatSessions = ChatManager.getInstance().getChatSessions(); logger.debug( "------- ChatSessions: " + chatSessions.size() + " ----------------------------------"); for (ChatSession chatSession : chatSessions) { logger.debug( chatSession.getChatId() + " " + formatter.format(chatSession.getCreateTime()) + " " + chatSession.getInfo() + " " + chatSession.getClients().values().toString()); } logger.debug( "------- Tables: " + tables.size() + " --------------------------------------------"); for (Table table : tables.values()) { logger.debug( table.getId() + " [" + table.getName() + "] " + formatter.format(table.getStartTime()) + " (" + table.getState().toString() + ")"); } logger.debug("--- Server state END ------------------------------------------"); }
public void startDraft(Draft draft) { table.initDraft(); DraftManager.getInstance().createDraftSession(draft, userPlayerMap, table.getId()); for (Entry<UUID, UUID> entry : userPlayerMap.entrySet()) { User user = UserManager.getInstance().getUser(entry.getKey()); if (user != null) { logger.info( new StringBuilder("User ") .append(user.getName()) .append(" draft started: ") .append(draft.getId()) .append(" userId: ") .append(user.getId())); user.ccDraftStarted(draft.getId(), entry.getValue()); } else { logger.fatal( new StringBuilder("Start draft user not found userId: ").append(entry.getKey())); } } }
public boolean isTournamentStillValid() { if (table.getTournament() != null) { if (!table.getState().equals(TableState.WAITING) && !table.getState().equals(TableState.READY_TO_START) && !table.getState().equals(TableState.STARTING)) { TournamentController tournamentController = TournamentManager.getInstance().getTournamentController(table.getTournament().getId()); if (tournamentController != null) { return tournamentController.isTournamentStillValid(table.getState()); } else { return false; } } else { // check if table creator is still a valid user, if not remove table User user = UserManager.getInstance().getUser(userId); return user != null; } } return false; }
void cleanUp() { if (!table.isTournamentSubTable()) { for (Map.Entry<UUID, UUID> entry : userPlayerMap.entrySet()) { User user = UserManager.getInstance().getUser(entry.getKey()); if (user != null) { user.removeTable(entry.getValue()); } } } ChatManager.getInstance().destroyChatSession(chatId); }
private void sideboard(UUID playerId, Deck deck) throws MageException { for (Entry<UUID, UUID> entry : userPlayerMap.entrySet()) { if (entry.getValue().equals(playerId)) { User user = UserManager.getInstance().getUser(entry.getKey()); int remaining = (int) futureTimeout.getDelay(TimeUnit.SECONDS); if (user != null) { user.ccSideboard(deck, table.getId(), remaining, options.isLimited()); } break; } } }
public void removeTable(UUID tableId) { if (tables.containsKey(tableId)) { TableController tableController = controllers.get(tableId); if (tableController != null) { controllers.remove(tableId); tableController.cleanUp(); } Table table = tables.get(tableId); tables.remove(tableId); // If table is not finished, the table has to be removed completly (if finished it will be // removed in GamesRoomImpl.Update()) if (!table.getState().equals(TableState.FINISHED)) { GamesRoomManager.getInstance().removeTable(tableId); } if (table.getMatch() != null && table.getMatch().getGame() != null) { table.getMatch().getGame().end(); } } }
public void swapSeats(int seatNum1, int seatNum2) { if (table.getState().equals(TableState.READY_TO_START)) { if (seatNum1 >= 0 && seatNum2 >= 0 && seatNum1 < table.getSeats().length && seatNum2 < table.getSeats().length) { Player swapPlayer = table.getSeats()[seatNum1].getPlayer(); String swapType = table.getSeats()[seatNum1].getPlayerType(); table.getSeats()[seatNum1].setPlayer(table.getSeats()[seatNum2].getPlayer()); table.getSeats()[seatNum1].setPlayerType(table.getSeats()[seatNum2].getPlayerType()); table.getSeats()[seatNum2].setPlayer(swapPlayer); table.getSeats()[seatNum2].setPlayerType(swapType); } } }
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 TableController(UUID roomId, UUID userId, MatchOptions options) { this.userId = userId; this.options = options; match = GameFactory.getInstance().createMatch(options.getGameType(), options); if (userId != null) { User user = UserManager.getInstance().getUser(userId); // TODO: Handle if user == null controllerName = user.getName(); } else { controllerName = "System"; } table = new Table( roomId, options.getGameType(), options.getName(), controllerName, DeckValidatorFactory.getInstance().createDeckValidator(options.getDeckType()), options.getPlayerTypes(), TableRecorderImpl.getInstance(), match); chatId = ChatManager.getInstance().createChatSession("Match Table " + table.getId()); init(); }
private void checkExpired() { debugServerState(); Date now = new Date(); List<UUID> toRemove = new ArrayList<>(); for (Table table : tables.values()) { if (!table.getState().equals(TableState.FINISHED)) { // remove all not finished tables created more than expire_time ago long diff = (now.getTime() - table.getCreateTime().getTime()) / EXPIRE_TIME_UNIT_VALUE; if (diff >= EXPIRE_TIME) { logger.warn( "Table expired: id = " + table.getId() + ", created_by=" + table.getControllerName() + ". Removing..."); toRemove.add(table.getId()); } // remove tables not valid anymore else if (!table.isTournament()) { TableController tableController = getController(table.getId()); if (!tableController.isMatchTableStillValid()) { logger.warn( "Table with no active human player: id = " + table.getId() + ", created_by=" + table.getControllerName() + ". Removing..."); toRemove.add(table.getId()); } } } } for (UUID tableId : toRemove) { try { removeTable(tableId); } catch (Exception e) { logger.error(e); } } }
public void construct() { table.construct(); }
public void initTournament() { table.initTournament(); }
public void endTournament(Tournament tournament) { table.endTournament(); }
/** Tables of normal matches or tournament sub tables are no longer needed, if the match ends. */ private void closeTable() { this.matchEnd(); table.closeTable(); }