From 0ef340d108ab981c1be243f878513cc917dcc8d2 Mon Sep 17 00:00:00 2001 From: LevelX2 Date: Sat, 20 Dec 2014 17:54:02 +0100 Subject: [PATCH] * Some rework of User/Tournament/Quit handling. --- .../java/mage/server/TableController.java | 18 ++-- .../src/main/java/mage/server/User.java | 75 ++++++++------- .../java/mage/server/game/GameController.java | 6 +- .../java/mage/server/game/ReplayManager.java | 2 +- .../tournament/TournamentController.java | 95 +++++++++++-------- .../server/tournament/TournamentManager.java | 5 - .../server/tournament/TournamentSession.java | 7 +- Mage/src/mage/cards/CardsImpl.java | 15 ++- Mage/src/mage/game/Game.java | 2 +- Mage/src/mage/game/GameImpl.java | 18 ++-- 10 files changed, 130 insertions(+), 113 deletions(-) diff --git a/Mage.Server/src/main/java/mage/server/TableController.java b/Mage.Server/src/main/java/mage/server/TableController.java index 46ab6d0a14..cceb14916a 100644 --- a/Mage.Server/src/main/java/mage/server/TableController.java +++ b/Mage.Server/src/main/java/mage/server/TableController.java @@ -213,7 +213,7 @@ public class TableController { //only inform human players and add them to sessionPlayerMap if (seat.getPlayer().isHuman()) { user.addTable(player.getId(), table); - user.joinedTable(table.getRoomId(), table.getId(), true); + user.ccJoinedTable(table.getRoomId(), table.getId(), true); userPlayerMap.put(userId, player.getId()); } @@ -301,7 +301,7 @@ public class TableController { //only inform human players and add them to sessionPlayerMap if (seat.getPlayer().isHuman()) { user.addTable(player.getId(), table); - user.joinedTable(table.getRoomId(), table.getId(), false); + user.ccJoinedTable(table.getRoomId(), table.getId(), false); userPlayerMap.put(userId, player.getId()); } return true; @@ -392,7 +392,7 @@ public class TableController { public boolean watchTable(UUID userId) { if (table.isTournament()) { - UserManager.getInstance().getUser(userId).showTournament(table.getTournament().getId()); + UserManager.getInstance().getUser(userId).ccShowTournament(table.getTournament().getId()); return true; } else { if (table.isTournamentSubTable() && !table.getTournament().getOptions().isWatchingAllowed()) { @@ -405,7 +405,7 @@ public class TableController { if (userPlayerMap.get(userId) != null) { return false; } - return UserManager.getInstance().getUser(userId).watchGame(match.getGame().getId()); + return UserManager.getInstance().getUser(userId).ccWatchGame(match.getGame().getId()); } } @@ -550,7 +550,7 @@ public class TableController { // activePlayers++; Player player = match.getPlayer(entry.getValue()).getPlayer(); player.setRequestToShowHandCardsAllowed(user.getUserData().allowRequestShowHandCards()); - user.gameStarted(match.getGame().getId(), entry.getValue()); + user.ccGameStarted(match.getGame().getId(), entry.getValue()); if (creator == null) { creator = user.getName(); @@ -618,7 +618,7 @@ public class TableController { 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.tournamentStarted(tournament.getId(), entry.getValue()); + user.ccTournamentStarted(tournament.getId(), entry.getValue()); } } ServerMessagesUtil.getInstance().incTournamentsStarted(); @@ -627,7 +627,7 @@ public class TableController { catch (Exception ex) { logger.fatal("Error starting tournament", ex); TableManager.getInstance().removeTable(table.getId()); - TournamentManager.getInstance().kill(tournament.getId(), userId); + TournamentManager.getInstance().quit(tournament.getId(), userId); } } @@ -638,7 +638,7 @@ public class TableController { 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.draftStarted(draft.getId(), entry.getValue()); + user.ccDraftStarted(draft.getId(), entry.getValue()); } else { logger.fatal(new StringBuilder("Start draft user not found userId: ").append(entry.getKey())); } @@ -651,7 +651,7 @@ public class TableController { User user = UserManager.getInstance().getUser(entry.getKey()); int remaining = (int) futureTimeout.getDelay(TimeUnit.SECONDS); if (user != null) { - user.sideboard(deck, table.getId(), remaining, options.isLimited()); + user.ccSideboard(deck, table.getId(), remaining, options.isLimited()); } break; } diff --git a/Mage.Server/src/main/java/mage/server/User.java b/Mage.Server/src/main/java/mage/server/User.java index 0bc718e7ad..7ba851a499 100644 --- a/Mage.Server/src/main/java/mage/server/User.java +++ b/Mage.Server/src/main/java/mage/server/User.java @@ -73,7 +73,7 @@ public class User { private final Map tables; private final Map gameSessions; private final Map draftSessions; - private final Map tournamentSessions; + private final Map userTournaments; // playerId, tournamentId private final Map constructing; private final Map sideboarding; private final List watchedGames; @@ -96,7 +96,7 @@ public class User { this.tables = new ConcurrentHashMap<>(); this.gameSessions = new ConcurrentHashMap<>(); this.draftSessions = new ConcurrentHashMap<>(); - this.tournamentSessions = new ConcurrentHashMap<>(); + this.userTournaments = new ConcurrentHashMap<>(); this.constructing = new ConcurrentHashMap<>(); this.sideboarding = new ConcurrentHashMap<>(); this.watchedGames = new ArrayList<>(); @@ -183,36 +183,36 @@ public class User { } } - public void joinedTable(final UUID roomId, final UUID tableId, boolean isTournament) { + public void ccJoinedTable(final UUID roomId, final UUID tableId, boolean isTournament) { fireCallback(new ClientCallback("joinedTable", tableId, new TableClientMessage(roomId, tableId, isTournament))); } - public void gameStarted(final UUID gameId, final UUID playerId) { + public void ccGameStarted(final UUID gameId, final UUID playerId) { fireCallback(new ClientCallback("startGame", gameId, new TableClientMessage(gameId, playerId))); } - public void draftStarted(final UUID draftId, final UUID playerId) { + public void ccDraftStarted(final UUID draftId, final UUID playerId) { fireCallback(new ClientCallback("startDraft", draftId, new TableClientMessage(draftId, playerId))); } - public void tournamentStarted(final UUID tournamentId, final UUID playerId) { + public void ccTournamentStarted(final UUID tournamentId, final UUID playerId) { fireCallback(new ClientCallback("startTournament", tournamentId, new TableClientMessage(tournamentId, playerId))); } - public void sideboard(final Deck deck, final UUID tableId, final int time, boolean limited) { + public void ccSideboard(final Deck deck, final UUID tableId, final int time, boolean limited) { fireCallback(new ClientCallback("sideboard", tableId, new TableClientMessage(deck, tableId, time, limited))); sideboarding.put(tableId, deck); } - public void construct(final Deck deck, final UUID tableId, final int time) { + public void ccConstruct(final Deck deck, final UUID tableId, final int time) { fireCallback(new ClientCallback("construct", tableId, new TableClientMessage(deck, tableId, time))); } - public void showTournament(final UUID tournamentId) { + public void ccShowTournament(final UUID tournamentId) { fireCallback(new ClientCallback("showTournament", tournamentId)); } - public void showGameEndDialog(final UUID gameId) { + public void ccShowGameEndDialog(final UUID gameId) { fireCallback(new ClientCallback("showGameEndDialog", gameId)); } @@ -223,12 +223,12 @@ public class User { fireCallback(new ClientCallback("showUserMessage", null, messageData )); } - public boolean watchGame(final UUID gameId) { + public boolean ccWatchGame(final UUID gameId) { fireCallback(new ClientCallback("watchGame", gameId)); return true; } - public void replayGame(final UUID gameId) { + public void ccReplayGame(final UUID gameId) { fireCallback(new ClientCallback("replayGame", gameId)); } @@ -279,22 +279,24 @@ public class User { private void reconnect() { for (Entry entry: tables.entrySet()) { - joinedTable(entry.getValue().getRoomId(), entry.getValue().getId(), entry.getValue().isTournament()); + ccJoinedTable(entry.getValue().getRoomId(), entry.getValue().getId(), entry.getValue().isTournament()); } - for (Entry entry: tournamentSessions.entrySet()) { - tournamentStarted(entry.getValue().getTournamentId(), entry.getKey()); - entry.getValue().init(); - entry.getValue().update(); + for (Entry entry: userTournaments.entrySet()) { + TournamentController tournamentController = TournamentManager.getInstance().getTournamentController(entry.getValue()); + if (tournamentController != null) { + ccTournamentStarted(entry.getValue(), entry.getKey()); + tournamentController.rejoin(entry.getKey()); + } } for (Entry entry: gameSessions.entrySet()) { - gameStarted(entry.getValue().getGameId(), entry.getKey()); + ccGameStarted(entry.getValue().getGameId(), entry.getKey()); entry.getValue().init(); GameManager.getInstance().sendPlayerString(entry.getValue().getGameId(), userId, ""); } for (Entry entry: draftSessions.entrySet()) { - draftStarted(entry.getValue().getDraftId(), entry.getKey()); + ccDraftStarted(entry.getValue().getDraftId(), entry.getKey()); entry.getValue().init(); entry.getValue().update(); } @@ -304,7 +306,7 @@ public class User { } for (Entry entry: sideboarding.entrySet()) { TableController controller = TableManager.getInstance().getController(entry.getKey()); - sideboard(entry.getValue(), entry.getKey(), controller.getRemainingTime(), controller.getOptions().isLimited()); + ccSideboard(entry.getValue(), entry.getKey(), controller.getRemainingTime(), controller.getOptions().isLimited()); } } @@ -324,12 +326,12 @@ public class User { draftSessions.remove(playerId); } - public void addTournament(UUID playerId, TournamentSession tournamentSession) { - tournamentSessions.put(playerId, tournamentSession); + public void addTournament(UUID playerId, UUID tournamentId) { + userTournaments.put(playerId, tournamentId); } public void removeTournament(UUID playerId) { - tournamentSessions.remove(playerId); + userTournaments.remove(playerId); } public void addTable(UUID playerId, Table table) { @@ -352,33 +354,30 @@ public class User { sideboarding.remove(tableId); } - public void remove(DisconnectReason reason) { - logger.trace("REMOVE " + getName() + " Game sessions: " + gameSessions.size() ); - for (GameSessionPlayer gameSession: gameSessions.values()) { - logger.debug("-- kill game session of gameId: " + gameSession.getGameId() ); - gameSession.quitGame(); - } - gameSessions.clear(); + public void remove(DisconnectReason reason) { logger.trace("REMOVE " + getName() + " Draft sessions " + draftSessions.size()); for (DraftSession draftSession: draftSessions.values()) { draftSession.setKilled(); } draftSessions.clear(); - logger.trace("REMOVE " + getName() + " Tournament sessions " + tournamentSessions.size()); - for (TournamentSession tournamentSession: tournamentSessions.values()) { - TournamentController tournamentController = TournamentManager.getInstance().getTournamentController(tournamentSession.getTournamentId()); - if (tournamentController != null) { - tournamentController.quit(userId); - } - tournamentSession.setKilled(); + logger.trace("REMOVE " + getName() + " Tournament sessions " + userTournaments.size()); + for (UUID tournamentId: userTournaments.values()) { + TournamentManager.getInstance().quit(tournamentId, getId()); } - tournamentSessions.clear(); + userTournaments.clear(); logger.trace("REMOVE " + getName() + " Tables " + tables.size()); for (Entry entry: tables.entrySet()) { logger.debug("-- leave tableId: " + entry.getValue().getId()); TableManager.getInstance().leaveTable(userId, entry.getValue().getId()); } tables.clear(); + logger.trace("REMOVE " + getName() + " Game sessions: " + gameSessions.size() ); + for (GameSessionPlayer gameSessionPlayer: gameSessions.values()) { + logger.debug("-- kill game session of gameId: " + gameSessionPlayer.getGameId() ); + GameManager.getInstance().quitMatch(gameSessionPlayer.getGameId(), userId); + gameSessionPlayer.quitGame(); + } + gameSessions.clear(); logger.trace("REMOVE " + getName() + " watched Games " + watchedGames.size()); for (UUID gameId: watchedGames) { GameManager.getInstance().stopWatching(gameId, userId); diff --git a/Mage.Server/src/main/java/mage/server/game/GameController.java b/Mage.Server/src/main/java/mage/server/game/GameController.java index 79fe2d246f..45cf83d32a 100644 --- a/Mage.Server/src/main/java/mage/server/game/GameController.java +++ b/Mage.Server/src/main/java/mage/server/game/GameController.java @@ -442,9 +442,9 @@ public class GameController implements GameCallback { UUID playerId = getPlayerId(userId); if (playerId != null) { if (allJoined()) { - GameSessionPlayer gameSession = gameSessions.get(playerId); - if (gameSession != null) { - gameSession.quitGame(); + GameSessionPlayer gameSessionPlayer = gameSessions.get(playerId); + if (gameSessionPlayer != null) { + gameSessionPlayer.quitGame(); } } else { // The player did never join the game but the game controller was started because the player was still connected as the diff --git a/Mage.Server/src/main/java/mage/server/game/ReplayManager.java b/Mage.Server/src/main/java/mage/server/game/ReplayManager.java index 99de309b81..fc08926ce6 100644 --- a/Mage.Server/src/main/java/mage/server/game/ReplayManager.java +++ b/Mage.Server/src/main/java/mage/server/game/ReplayManager.java @@ -50,7 +50,7 @@ public class ReplayManager { public void replayGame(UUID gameId, UUID userId) { ReplaySession replaySession = new ReplaySession(gameId, userId); replaySessions.put(gameId.toString() + userId.toString(), replaySession); - UserManager.getInstance().getUser(userId).replayGame(gameId); + UserManager.getInstance().getUser(userId).ccReplayGame(gameId); } public void startReplay(UUID gameId, UUID userId) { 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 4502d7892b..eaba8c2109 100644 --- a/Mage.Server/src/main/java/mage/server/tournament/TournamentController.java +++ b/Mage.Server/src/main/java/mage/server/tournament/TournamentController.java @@ -169,7 +169,7 @@ public class TournamentController { tournamentSessions.put(playerId, tournamentSession); User user = UserManager.getInstance().getUser(userId); if (user != null) { - user.addTournament(playerId, tournamentSession); + user.addTournament(playerId, tournament.getId()); TournamentPlayer player = tournament.getPlayer(playerId); player.setJoined(); logger.debug("player " +player.getPlayer().getName() + " - client has joined tournament " + tournament.getId()); @@ -180,6 +180,19 @@ public class TournamentController { } } + public void rejoin(UUID playerId) { + TournamentSession tournamentSession = tournamentSessions.get(playerId); + if (tournamentSession == null) { + logger.fatal("Tournament session not found - playerId:" + playerId + " tournamentId " + tournament.getId()); + return; + } + if (!tournamentSession.init()) { + logger.fatal("Unable to initialize client userId: " + tournamentSession.userId + " tournamentId " + tournament.getId()); + return; + } + tournamentSession.update(); + } + private void checkStart() { if (!started && allJoined()) { ThreadExecutor.getInstance().getCallExecutor().execute( @@ -318,49 +331,55 @@ public class TournamentController { public void quit(UUID userId) { UUID playerId = userPlayerMap.get(userId); - if (playerId != null) { - TournamentPlayer tPlayer = tournament.getPlayer(playerId); - if (tPlayer != null) { - if (started) { - if (tPlayer.isInTournament()) { - String info; - if (tournament.isDoneConstructing()) { - info = new StringBuilder("during round ").append(tournament.getRounds().size()).toString(); - // quit active matches of that tournament - TableManager.getInstance().userQuitTournamentSubTables(tournament.getId(), userId); - } else { - if (tPlayer.getState().equals(TournamentPlayerState.DRAFTING)) { - info = "during Draft phase"; - if (!checkToReplaceDraftPlayerByAi(userId, tPlayer)) { - this.abortDraftTournament(); - } else { - DraftController draftController = DraftManager.getInstance().getController(tableId); - if (draftController != null) { - DraftSession draftSession = draftController.getDraftSession(playerId); - if (draftSession != null) { - DraftManager.getInstance().kill(draftSession.getDraftId(), userId); - } - } - } - } else if (tPlayer.getState().equals(TournamentPlayerState.CONSTRUCTING)) { - info = "during Construction phase"; - } else { - info = ""; + if (playerId == null) { + logger.debug("Player not found userId:" + userId + " tournId: " + tournament.getId()); + return; + } + TournamentPlayer tournamentPlayer = tournament.getPlayer(playerId); + if (tournamentPlayer == null) { + logger.debug("TournamentPlayer not found userId: " + userId + " tournId: " + tournament.getId()); + return; + } + if (!started) { + tournament.leave(playerId); + return; + } + TournamentSession tournamentSession = tournamentSessions.get(playerId); + if (tournamentSession == null) { + logger.debug("TournamentSession not found userId: " + userId + " tournId: " + tournament.getId()); + return; + } + tournamentSession.setKilled(); + if (tournamentPlayer.isInTournament()) { + String info; + if (tournament.isDoneConstructing()) { + info = new StringBuilder("during round ").append(tournament.getRounds().size()).toString(); + // quit active matches of that tournament + TableManager.getInstance().userQuitTournamentSubTables(tournament.getId(), userId); + } else { + if (tournamentPlayer.getState().equals(TournamentPlayerState.DRAFTING)) { + info = "during Draft phase"; + if (!checkToReplaceDraftPlayerByAi(userId, tournamentPlayer)) { + this.abortDraftTournament(); + } else { + DraftController draftController = DraftManager.getInstance().getController(tableId); + if (draftController != null) { + DraftSession draftSession = draftController.getDraftSession(playerId); + if (draftSession != null) { + DraftManager.getInstance().kill(draftSession.getDraftId(), userId); } } - tPlayer.setQuit(info); - tournament.quit(playerId); - if (tournamentSessions.containsKey(playerId)) { - tournamentSessions.get(tPlayer.getPlayer().getId()).quit(); - } - ChatManager.getInstance().broadcast(chatId, "", tPlayer.getPlayer().getName() + " has quit the tournament", MessageColor.BLACK, true, MessageType.STATUS, SoundToPlay.PlayerQuitTournament); } + } else if (tournamentPlayer.getState().equals(TournamentPlayerState.CONSTRUCTING)) { + info = "during Construction phase"; } else { - tournament.leave(playerId); + info = ""; } } - } else { - logger.debug("UserId not found " + userId + " tournId: " + tournament.getId()); + tournamentPlayer.setQuit(info); + tournament.quit(playerId); + tournamentSession.quit(); + ChatManager.getInstance().broadcast(chatId, "", tournamentPlayer.getPlayer().getName() + " has quit the tournament", MessageColor.BLACK, true, MessageType.STATUS, SoundToPlay.PlayerQuitTournament); } } diff --git a/Mage.Server/src/main/java/mage/server/tournament/TournamentManager.java b/Mage.Server/src/main/java/mage/server/tournament/TournamentManager.java index 54f3184ad5..0c692cb880 100644 --- a/Mage.Server/src/main/java/mage/server/tournament/TournamentManager.java +++ b/Mage.Server/src/main/java/mage/server/tournament/TournamentManager.java @@ -71,11 +71,6 @@ public class TournamentManager { } } - - public void kill(UUID tournamentId, UUID userId) { - controllers.get(tournamentId).quit(userId); - } - public void timeout(UUID tournamentId, UUID userId) { controllers.get(tournamentId).timeout(userId); } diff --git a/Mage.Server/src/main/java/mage/server/tournament/TournamentSession.java b/Mage.Server/src/main/java/mage/server/tournament/TournamentSession.java index a45bcd5037..7f5abe13c7 100644 --- a/Mage.Server/src/main/java/mage/server/tournament/TournamentSession.java +++ b/Mage.Server/src/main/java/mage/server/tournament/TournamentSession.java @@ -101,7 +101,7 @@ public class TournamentSession { User user = UserManager.getInstance().getUser(userId); if (user != null) { int remaining = (int) futureTimeout.getDelay(TimeUnit.SECONDS); - user.construct(tournament.getPlayer(playerId).getDeck(), tableId, remaining); + user.ccConstruct(tournament.getPlayer(playerId).getDeck(), tableId, remaining); } } } @@ -115,11 +115,6 @@ public class TournamentSession { tournament.updateDeck(playerId, deck); } - protected void handleRemoteException(RemoteException ex) { - logger.fatal("TournamentSession error - userId " + userId + " tId " + tournament.getId(), ex); - TournamentManager.getInstance().kill(tournament.getId(), userId); - } - public void setKilled() { killed = true; } diff --git a/Mage/src/mage/cards/CardsImpl.java b/Mage/src/mage/cards/CardsImpl.java index 79391d4939..006178ef4a 100644 --- a/Mage/src/mage/cards/CardsImpl.java +++ b/Mage/src/mage/cards/CardsImpl.java @@ -40,6 +40,7 @@ import java.util.UUID; import mage.constants.Zone; import mage.filter.FilterCard; import mage.game.Game; +import org.apache.log4j.Logger; /** @@ -47,6 +48,8 @@ import mage.game.Game; * @author BetaSteward_at_googlemail.com */ public class CardsImpl extends LinkedHashSet implements Cards, Serializable { + + private static final Logger logger = Logger.getLogger(CardsImpl.class); private static Random rnd = new Random(); private UUID ownerId; @@ -62,7 +65,7 @@ public class CardsImpl extends LinkedHashSet implements Cards, Serializabl this.zone = zone; } - public CardsImpl(Zone zone, List cards) { + public CardsImpl(Zone zone, Collection cards) { this(zone); for (Card card: cards) { this.add(card.getId()); @@ -181,8 +184,14 @@ public class CardsImpl extends LinkedHashSet implements Cards, Serializabl @Override public Set getCards(Game game) { Set cards = new LinkedHashSet<>(); - for (UUID card: this) { - cards.add(game.getCard(card)); + for (UUID cardId: this) { + Card card = game.getCard(cardId); + if (card != null) { + cards.add(card); + } else { + // this bug seems to happen, if cause is removed, this check can also be removed + logger.error("Card not found cardId: " + cardId + " gameId: " + game.getId() ); + } } return cards; } diff --git a/Mage/src/mage/game/Game.java b/Mage/src/mage/game/Game.java index 9a1bda39fe..3876457898 100644 --- a/Mage/src/mage/game/Game.java +++ b/Mage/src/mage/game/Game.java @@ -222,7 +222,7 @@ public interface Game extends MageItem, Serializable { int mulliganDownTo(UUID playerId); void mulligan(UUID playerId); void endMulligan(UUID playerId); - void quit(UUID playerId); + // void quit(UUID playerId); void timerTimeout(UUID playerId); void idleTimeout(UUID playerId); void concede(UUID playerId); diff --git a/Mage/src/mage/game/GameImpl.java b/Mage/src/mage/game/GameImpl.java index e5fa6de80c..f83eb6fc59 100644 --- a/Mage/src/mage/game/GameImpl.java +++ b/Mage/src/mage/game/GameImpl.java @@ -1036,15 +1036,15 @@ public abstract class GameImpl implements Game, Serializable { player.drawCards(numCards - deduction, this); } - @Override - public void quit(UUID playerId) { - if (state != null) { - Player player = state.getPlayer(playerId); - if (player != null && player.isInGame()) { - player.quit(this); - } - } - } +// @Override +// public void quit(UUID playerId) { +// if (state != null) { +// Player player = state.getPlayer(playerId); +// if (player != null && player.isInGame()) { +// player.quit(this); +// } +// } +// } @Override public synchronized void timerTimeout(UUID playerId) {