From 00801e3ecffe83ae9c1b5ad05357ea58a80d02a8 Mon Sep 17 00:00:00 2001 From: LevelX2 Date: Mon, 2 Feb 2015 08:40:30 +0100 Subject: [PATCH] * Some chnages to logging, added changes to table health check. --- .../java/mage/server/TableController.java | 6 ++- .../main/java/mage/server/TableManager.java | 2 +- .../src/main/java/mage/server/User.java | 10 ++++- .../java/mage/server/game/PlayerFactory.java | 2 +- .../tournament/TournamentController.java | 44 +++++++++++++++++++ 5 files changed, 59 insertions(+), 5 deletions(-) diff --git a/Mage.Server/src/main/java/mage/server/TableController.java b/Mage.Server/src/main/java/mage/server/TableController.java index 9433c1d032..9d7ebc31d2 100644 --- a/Mage.Server/src/main/java/mage/server/TableController.java +++ b/Mage.Server/src/main/java/mage/server/TableController.java @@ -209,7 +209,7 @@ public class TableController { tournamentPlayer.submitDeck(deck); } table.joinTable(player, seat); - logger.trace("player " + player.getName() + " joined tableId: " + table.getId()); + logger.debug("Player " + player.getName() + " id: "+ player.getId() + " joined tableId: " + table.getId()); //only inform human players and add them to sessionPlayerMap if (seat.getPlayer().isHuman()) { user.addTable(player.getId(), table); @@ -866,7 +866,9 @@ public class TableController { if (table.getTournament() != null) { TournamentController tournamentController = TournamentManager.getInstance().getTournamentController(table.getTournament().getId()); if (tournamentController != null) { - //TODO: Check tournament state + return tournamentController.isTournamentStillValid(table.getState()); + } else { + return false; } } return true; diff --git a/Mage.Server/src/main/java/mage/server/TableManager.java b/Mage.Server/src/main/java/mage/server/TableManager.java index 1ef11e9f7c..0e6b11fa9b 100644 --- a/Mage.Server/src/main/java/mage/server/TableManager.java +++ b/Mage.Server/src/main/java/mage/server/TableManager.java @@ -81,7 +81,7 @@ public class TableManager { * * In minutes. */ - private static final int EXPIRE_CHECK_PERIOD = 10; + private static final int EXPIRE_CHECK_PERIOD = 1; public static TableManager getInstance() { return INSTANCE; diff --git a/Mage.Server/src/main/java/mage/server/User.java b/Mage.Server/src/main/java/mage/server/User.java index 7ba851a499..183e0704ca 100644 --- a/Mage.Server/src/main/java/mage/server/User.java +++ b/Mage.Server/src/main/java/mage/server/User.java @@ -71,6 +71,7 @@ public class User { private final String host; private final Date connectionTime; private final Map tables; + private final ArrayList tablesToDelete; private final Map gameSessions; private final Map draftSessions; private final Map userTournaments; // playerId, tournamentId @@ -100,7 +101,7 @@ public class User { this.constructing = new ConcurrentHashMap<>(); this.sideboarding = new ConcurrentHashMap<>(); this.watchedGames = new ArrayList<>(); - + this.tablesToDelete = new ArrayList<>(); this.sessionId = ""; } @@ -435,6 +436,7 @@ public class User { } else { // can happen if tournamet has just ended logger.debug(getName() + " tournament player missing - tableId:" + table.getId(), null); + tablesToDelete.add(tableEntry.getKey()); } } else { logger.error(getName() + " tournament key missing - tableId: " + table.getId(), null); @@ -457,6 +459,12 @@ public class User { } } } + if (!tablesToDelete.isEmpty()) { + for(UUID keyId: tablesToDelete) { + removeTable(keyId); + } + tablesToDelete.clear(); + } if (waiting > 0) { sb.append("Wait: ").append(waiting).append(" "); } diff --git a/Mage.Server/src/main/java/mage/server/game/PlayerFactory.java b/Mage.Server/src/main/java/mage/server/game/PlayerFactory.java index 1ff2764479..2bee468263 100644 --- a/Mage.Server/src/main/java/mage/server/game/PlayerFactory.java +++ b/Mage.Server/src/main/java/mage/server/game/PlayerFactory.java @@ -61,7 +61,7 @@ public class PlayerFactory { if (playerTypeClass != null) { con = playerTypeClass.getConstructor(new Class[]{String.class, RangeOfInfluence.class, int.class}); player = (Player)con.newInstance(new Object[] {name, range, skill}); - logger.debug("Player created: " + name + " - " + player.getId()); + logger.trace("Player created: " + name + " - " + player.getId()); return player; } else { diff --git a/Mage.Server/src/main/java/mage/server/tournament/TournamentController.java b/Mage.Server/src/main/java/mage/server/tournament/TournamentController.java index eaba8c2109..3ee884b71d 100644 --- a/Mage.Server/src/main/java/mage/server/tournament/TournamentController.java +++ b/Mage.Server/src/main/java/mage/server/tournament/TournamentController.java @@ -226,6 +226,7 @@ public class TournamentController { } } started = true; + logger.debug("Tournament starts (all players joined): " + tournament.getId() + " - " + tournament.getTournamentType().toString()); tournament.nextStep(); } @@ -455,4 +456,47 @@ public class TournamentController { public void cleanUpOnRemoveTournament() { ChatManager.getInstance().destroyChatSession(chatId); } + + /** + * Check tournaments that are not already finished, if they are in a still valid state + * + * @param tableState state of the tournament table + * @return true - if tournament is valid + * false - if tournament is not valid and should be removed + */ + + public boolean isTournamentStillValid(TableState tableState) { + int activePlayers = 0; + for (Entry entry: userPlayerMap.entrySet()) { + TournamentPlayer tournamentPlayer = tournament.getPlayer(entry.getValue()); + if (tournamentPlayer != null) { + if (!tournamentPlayer.hasQuit()) { + if (tournamentPlayer.getPlayer().isHuman()) { + User user = UserManager.getInstance().getUser(entry.getKey()); + if (user == null) { + logger.debug("Tournament user is missing but player active -> start quit - tournamentId: " + tournament.getId() + " state: " + tableState.toString()); + // active tournament player but the user is no longer online + quit(entry.getKey()); + } + } + activePlayers++; + } + } else { + // tournament player is missing + logger.debug("Tournament player is missing - tournamentId: " + tournament.getId() + " state: " + tableState.toString()); + } + } + for(TournamentPlayer tournamentPlayer: tournament.getPlayers()) { + if (!tournamentPlayer.getPlayer().isHuman()) { + if (!tournamentPlayer.hasQuit()) { + activePlayers++; + } + } + } + if (activePlayers < 2 && !tableState.equals(TableState.WAITING)) { + logger.debug("Tournament has less than 2 active players - tournamentId: " + tournament.getId() + " state: " + tableState.toString()); + return false; + } + return true; + } }