From f4379d54cb6a46de78338c71e96f39e482cb2dfc Mon Sep 17 00:00:00 2001 From: "marcins78@gmail.com" Date: Tue, 17 Jan 2012 16:38:58 +0000 Subject: [PATCH] Data about running game tables in Hall are stored in one class, rather than multiple variables. --- .../com/gempukku/lotro/hall/HallServer.java | 66 +++++++------------ .../com/gempukku/lotro/hall/RunningTable.java | 25 +++++++ 2 files changed, 50 insertions(+), 41 deletions(-) create mode 100644 gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/hall/RunningTable.java diff --git a/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/hall/HallServer.java b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/hall/HallServer.java index 1fafbbabf..bfe300306 100644 --- a/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/hall/HallServer.java +++ b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/hall/HallServer.java @@ -34,14 +34,10 @@ public class HallServer extends AbstractServer { private Map _supportedFormats = new LinkedHashMap(); - private ReadWriteLock _hallDataAccessLock = new ReentrantReadWriteLock(false); private Map _awaitingTables = new LinkedHashMap(); - private Map _runningTables = new LinkedHashMap(); - - private Map _runningTableFormatNames = new HashMap(); - private Map _runningTableTournamentNames = new HashMap(); + private Map _runningTables = new LinkedHashMap(); private Map _lastVisitedPlayers = new HashMap(); @@ -189,35 +185,31 @@ public class HallServer extends AbstractServer { } // Then non-finished - Map nonPlayingTables = new HashMap(); + Map finishedTables = new HashMap(); - for (Map.Entry runningGame : _runningTables.entrySet()) { - LotroGameMediator lotroGameMediator = _lotroServer.getGameById(runningGame.getValue()); + for (Map.Entry runningGame : _runningTables.entrySet()) { + final RunningTable runningTable = runningGame.getValue(); + LotroGameMediator lotroGameMediator = _lotroServer.getGameById(runningTable.getGameId()); if (lotroGameMediator != null) { String gameStatus = lotroGameMediator.getGameStatus(); if (!gameStatus.equals("Finished")) - visitor.visitTable(runningGame.getKey(), runningGame.getValue(), gameStatus, _runningTableFormatNames.get(runningGame.getKey()), _runningTableTournamentNames.get(runningGame.getKey()), lotroGameMediator.getPlayersPlaying(), lotroGameMediator.getWinner()); + visitor.visitTable(runningGame.getKey(), runningTable.getGameId(), gameStatus, runningTable.getFormatName(), runningTable.getTournamentName(), lotroGameMediator.getPlayersPlaying(), lotroGameMediator.getWinner()); else - nonPlayingTables.put(runningGame.getKey(), runningGame.getValue()); + finishedTables.put(runningGame.getKey(), runningTable); } } // Then rest - for (Map.Entry nonPlayingGame : nonPlayingTables.entrySet()) { - LotroGameMediator lotroGameMediator = _lotroServer.getGameById(nonPlayingGame.getValue()); + for (Map.Entry nonPlayingGame : finishedTables.entrySet()) { + final RunningTable runningTable = nonPlayingGame.getValue(); + LotroGameMediator lotroGameMediator = _lotroServer.getGameById(runningTable.getGameId()); if (lotroGameMediator != null) - visitor.visitTable(nonPlayingGame.getKey(), nonPlayingGame.getValue(), lotroGameMediator.getGameStatus(), _runningTableFormatNames.get(nonPlayingGame.getKey()), _runningTableTournamentNames.get(nonPlayingGame.getKey()), lotroGameMediator.getPlayersPlaying(), lotroGameMediator.getWinner()); + visitor.visitTable(nonPlayingGame.getKey(), runningTable.getGameId(), lotroGameMediator.getGameStatus(), runningTable.getFormatName(), runningTable.getTournamentName(), lotroGameMediator.getPlayersPlaying(), lotroGameMediator.getWinner()); } - String playerTable = getNonFinishedPlayerTable(player.getName()); - if (playerTable != null) { - String gameId = _runningTables.get(playerTable); - if (gameId != null) { - LotroGameMediator lotroGameMediator = _lotroServer.getGameById(gameId); - if (lotroGameMediator != null && !lotroGameMediator.getGameStatus().equals("Finished")) - visitor.runningPlayerGame(gameId); - } - } + String gameId = getPlayingPlayerGameId(player.getName()); + if (gameId != null) + visitor.runningPlayerGame(gameId); } finally { _hallDataAccessLock.readLock().unlock(); } @@ -305,9 +297,7 @@ public class HallServer extends AbstractServer { if (league != null) _leagueService.leagueGameStarting(league, awaitingTable.getLeagueSerie(), lotroGameMediator); lotroGameMediator.startGame(); - _runningTables.put(tableId, gameId); - _runningTableFormatNames.put(tableId, awaitingTable.getLotroFormat().getName()); - _runningTableTournamentNames.put(tableId, getTournamentName(awaitingTable)); + _runningTables.put(tableId, new RunningTable(gameId, awaitingTable.getLotroFormat().getName(), getTournamentName(awaitingTable))); _awaitingTables.remove(tableId); } @@ -324,18 +314,13 @@ public class HallServer extends AbstractServer { return false; } - private String getNonFinishedPlayerTable(String playerId) { - for (Map.Entry table : _awaitingTables.entrySet()) { - if (table.getValue().hasPlayer(playerId)) - return table.getKey(); - } - - for (Map.Entry runningTable : _runningTables.entrySet()) { - String gameId = runningTable.getValue(); + private String getPlayingPlayerGameId(String playerId) { + for (Map.Entry runningTable : _runningTables.entrySet()) { + String gameId = runningTable.getValue().getGameId(); LotroGameMediator lotroGameMediator = _lotroServer.getGameById(gameId); if (lotroGameMediator != null && !lotroGameMediator.getGameStatus().equals("Finished")) if (lotroGameMediator.getPlayersPlaying().contains(playerId)) - return runningTable.getKey(); + return gameId; } return null; @@ -346,11 +331,13 @@ public class HallServer extends AbstractServer { if (awaitingTable.hasPlayer(playerId)) return true; - for (String gameId : _runningTables.values()) { + for (RunningTable runningTable : _runningTables.values()) { + String gameId = runningTable.getGameId(); LotroGameMediator lotroGameMediator = _lotroServer.getGameById(gameId); if (lotroGameMediator != null && !lotroGameMediator.getGameStatus().equals("Finished") && lotroGameMediator.getPlayersPlaying().contains(playerId)) return true; } + return false; } @@ -359,13 +346,10 @@ public class HallServer extends AbstractServer { _hallDataAccessLock.writeLock().lock(); try { // Remove finished games - HashMap copy = new HashMap(_runningTables); - for (Map.Entry runningTable : copy.entrySet()) { - if (_lotroServer.getGameById(runningTable.getValue()) == null) { + HashMap copy = new HashMap(_runningTables); + for (Map.Entry runningTable : copy.entrySet()) { + if (_lotroServer.getGameById(runningTable.getValue().getGameId()) == null) _runningTables.remove(runningTable.getKey()); - _runningTableFormatNames.remove(runningTable.getKey()); - _runningTableTournamentNames.remove(runningTable.getKey()); - } } long currentTime = System.currentTimeMillis(); diff --git a/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/hall/RunningTable.java b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/hall/RunningTable.java new file mode 100644 index 000000000..4759a03cf --- /dev/null +++ b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/hall/RunningTable.java @@ -0,0 +1,25 @@ +package com.gempukku.lotro.hall; + +public class RunningTable { + private String _gameId; + private String _formatName; + private String _tournamentName; + + public RunningTable(String gameId, String formatName, String tournamentName) { + _gameId = gameId; + _formatName = formatName; + _tournamentName = tournamentName; + } + + public String getFormatName() { + return _formatName; + } + + public String getGameId() { + return _gameId; + } + + public String getTournamentName() { + return _tournamentName; + } +}