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 6ea79cf2bc..e7ceb47f8a 100644 --- a/Mage.Server/src/main/java/mage/server/game/GameController.java +++ b/Mage.Server/src/main/java/mage/server/game/GameController.java @@ -31,6 +31,9 @@ import java.io.*; import java.util.*; import java.util.Map.Entry; import java.util.concurrent.*; +import java.util.concurrent.locks.Lock; +import java.util.concurrent.locks.ReadWriteLock; +import java.util.concurrent.locks.ReentrantReadWriteLock; import java.util.zip.GZIPOutputStream; import mage.MageException; import mage.abilities.Ability; @@ -80,7 +83,11 @@ public class GameController implements GameCallback { protected static final ScheduledExecutorService timeoutIdleExecutor = ThreadExecutor.instance.getTimeoutIdleExecutor(); private final ConcurrentHashMap gameSessions = new ConcurrentHashMap<>(); + private final ReadWriteLock gameSessionsLock = new ReentrantReadWriteLock(); + private final ConcurrentHashMap watchers = new ConcurrentHashMap<>(); + private final ReadWriteLock gameWatchersLock = new ReentrantReadWriteLock(); + private final ConcurrentHashMap timers = new ConcurrentHashMap<>(); private final ConcurrentHashMap userPlayerMap; @@ -114,7 +121,7 @@ public class GameController implements GameCallback { public void cleanUp() { cancelTimeout(); - for (GameSessionPlayer gameSessionPlayer : gameSessions.values()) { + for (GameSessionPlayer gameSessionPlayer : getGameSessions()) { gameSessionPlayer.cleanUp(); } ChatManager.instance.destroyChatSession(chatId); @@ -301,7 +308,13 @@ public class GameController implements GameCallback { String joinType; if (gameSession == null) { gameSession = new GameSessionPlayer(game, userId, playerId); - gameSessions.put(playerId, gameSession); + final Lock w = gameSessionsLock.writeLock(); + w.lock(); + try { + gameSessions.put(playerId, gameSession); + } finally { + w.unlock(); + } joinType = "joined"; } else { joinType = "rejoined"; @@ -314,8 +327,8 @@ public class GameController implements GameCallback { private synchronized void startGame() { if (gameFuture == null) { - for (final Entry entry : gameSessions.entrySet()) { - entry.getValue().init(); + for (GameSessionPlayer gameSessionPlayer : getGameSessions()) { + gameSessionPlayer.init(); } GameWorker worker = new GameWorker(game, choosingPlayerId, this); @@ -413,7 +426,13 @@ public class GameController implements GameCallback { } UserManager.instance.getUser(userId).ifPresent(user -> { GameSessionWatcher gameWatcher = new GameSessionWatcher(userId, game, false); - watchers.put(userId, gameWatcher); + final Lock w = gameWatchersLock.writeLock(); + w.lock(); + try { + watchers.put(userId, gameWatcher); + } finally { + w.unlock(); + } gameWatcher.init(); user.addGameWatchInfo(game.getId()); ChatManager.instance.broadcast(chatId, user.getName(), " has started watching", MessageColor.BLUE, true, ChatMessage.MessageType.STATUS, null); @@ -422,7 +441,13 @@ public class GameController implements GameCallback { } public void stopWatching(UUID userId) { - watchers.remove(userId); + final Lock w = gameWatchersLock.writeLock(); + w.lock(); + try { + watchers.remove(userId); + } finally { + w.unlock(); + } UserManager.instance.getUser(userId).ifPresent(user -> { ChatManager.instance.broadcast(chatId, user.getName(), " has stopped watching", MessageColor.BLUE, true, ChatMessage.MessageType.STATUS, null); }); @@ -673,11 +698,11 @@ public class GameController implements GameCallback { } public void endGame(final String message) throws MageException { - for (final GameSessionPlayer gameSession : gameSessions.values()) { + for (final GameSessionPlayer gameSession : getGameSessions()) { gameSession.gameOver(message); gameSession.removeGame(); } - for (final GameSessionWatcher gameWatcher : watchers.values()) { + for (final GameSessionWatcher gameWatcher : getGameSessionWatchers()) { gameWatcher.gameOver(message); } TableManager.instance.endGame(tableId); @@ -722,10 +747,10 @@ public class GameController implements GameCallback { } } } - for (final GameSessionPlayer gameSession : gameSessions.values()) { + for (final GameSessionPlayer gameSession : getGameSessions()) { gameSession.update(); } - for (final GameSessionWatcher gameWatcher : watchers.values()) { + for (final GameSessionWatcher gameWatcher : getGameSessionWatchers()) { gameWatcher.update(); } } @@ -734,12 +759,12 @@ public class GameController implements GameCallback { Table table = TableManager.instance.getTable(tableId); if (table != null) { if (table.getMatch() != null) { - for (final GameSessionPlayer gameSession : gameSessions.values()) { + for (final GameSessionPlayer gameSession : getGameSessions()) { gameSession.endGameInfo(table); } + // TODO: inform watchers about game end and who won } } - // TODO: inform watchers about game end and who won } private synchronized void ask(UUID playerId, final String question, final Map options) throws MageException { @@ -814,12 +839,12 @@ public class GameController implements GameCallback { message.append(game.getStep().getType().toString()).append(" - "); } message.append("Waiting for ").append(game.getPlayer(playerId).getLogName()); - for (final Entry entry : gameSessions.entrySet()) { + for (final Entry entry : getGameSessionsMap().entrySet()) { if (!entry.getKey().equals(playerId)) { entry.getValue().inform(message.toString()); } } - for (final GameSessionWatcher watcher : watchers.values()) { + for (final GameSessionWatcher watcher : getGameSessionWatchers()) { watcher.inform(message.toString()); } } @@ -834,14 +859,13 @@ public class GameController implements GameCallback { return; } final String message = new StringBuilder(game.getStep().getType().toString()).append(" - Waiting for ").append(controller.getName()).toString(); - for (final Entry entry : gameSessions.entrySet()) { + for (final Entry entry : getGameSessionsMap().entrySet()) { boolean skip = players.stream().anyMatch(playerId -> entry.getKey().equals(playerId)); - if (!skip) { entry.getValue().inform(message); } } - for (final GameSessionWatcher watcher : watchers.values()) { + for (final GameSessionWatcher watcher : getGameSessionWatchers()) { watcher.inform(message); } } @@ -858,7 +882,7 @@ public class GameController implements GameCallback { for (StackTraceElement e : ex.getStackTrace()) { sb.append(e.toString()).append('\n'); } - for (final Entry entry : gameSessions.entrySet()) { + for (final Entry entry : getGameSessionsMap().entrySet()) { entry.getValue().gameError(sb.toString()); } } @@ -995,6 +1019,42 @@ public class GameController implements GameCallback { void execute(UUID player); } + private Map getGameSessionsMap() { + Map newGameSessionsMap = new HashMap<>(); + final Lock r = gameSessionsLock.readLock(); + r.lock(); + try { + newGameSessionsMap.putAll(gameSessions); + } finally { + r.unlock(); + } + return newGameSessionsMap; + } + + private List getGameSessions() { + List newGameSessions = new ArrayList<>(); + final Lock r = gameSessionsLock.readLock(); + r.lock(); + try { + newGameSessions.addAll(gameSessions.values()); + } finally { + r.unlock(); + } + return newGameSessions; + } + + private List getGameSessionWatchers() { + List newGameSessionWatchers = new ArrayList<>(); + final Lock r = gameSessionsLock.readLock(); + r.lock(); + try { + newGameSessionWatchers.addAll(watchers.values()); + } finally { + r.unlock(); + } + return newGameSessionWatchers; + } + private GameSessionPlayer getGameSession(UUID playerId) { if (!timers.isEmpty()) { Player player = game.getState().getPlayer(playerId); diff --git a/Mage.Server/src/main/java/mage/server/game/GameManager.java b/Mage.Server/src/main/java/mage/server/game/GameManager.java index fe36171175..3f3a69c02b 100644 --- a/Mage.Server/src/main/java/mage/server/game/GameManager.java +++ b/Mage.Server/src/main/java/mage/server/game/GameManager.java @@ -24,13 +24,17 @@ * The views and conclusions contained in the software and documentation are those of the * authors and should not be interpreted as representing official policies, either expressed * or implied, of BetaSteward_at_googlemail.com. -*/ - + */ package mage.server.game; +import java.util.HashMap; +import java.util.Map; import java.util.Optional; import java.util.UUID; import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.locks.Lock; +import java.util.concurrent.locks.ReadWriteLock; +import java.util.concurrent.locks.ReentrantReadWriteLock; import mage.cards.decks.DeckCardLists; import mage.constants.ManaType; import mage.constants.PlayerAction; @@ -46,10 +50,17 @@ public enum GameManager { instance; private final ConcurrentHashMap gameControllers = new ConcurrentHashMap<>(); + private final ReadWriteLock gameControllersLock = new ReentrantReadWriteLock(); public UUID createGameSession(Game game, ConcurrentHashMap userPlayerMap, UUID tableId, UUID choosingPlayerId, GameOptions gameOptions) { GameController gameController = new GameController(game, userPlayerMap, tableId, choosingPlayerId, gameOptions); - gameControllers.put(game.getId(), gameController); + final Lock w = gameControllersLock.writeLock(); + w.lock(); + try { + gameControllers.put(game.getId(), gameController); + } finally { + w.unlock(); + } return gameController.getSessionId(); } @@ -109,10 +120,10 @@ public enum GameManager { gameController.quitMatch(userId); } } - + public void sendPlayerAction(PlayerAction playerAction, UUID gameId, UUID userId, Object data) { GameController gameController = gameControllers.get(gameId); - if (gameController != null) { + if (gameController != null) { gameController.sendPlayerAction(playerAction, userId, data); } } @@ -151,7 +162,13 @@ public enum GameManager { GameController gameController = gameControllers.get(gameId); if (gameController != null) { gameController.cleanUp(); - gameControllers.remove(gameId); + final Lock w = gameControllersLock.writeLock(); + w.lock(); + try { + gameControllers.remove(gameId); + } finally { + w.unlock(); + } } } @@ -174,8 +191,16 @@ public enum GameManager { public int getNumberActiveGames() { return gameControllers.size(); } - - public ConcurrentHashMap getGameController() { - return gameControllers; + + public Map getGameController() { + Map newControllers = new HashMap<>(); + final Lock r = gameControllersLock.readLock(); + r.lock(); + try { + newControllers.putAll(gameControllers); + } finally { + r.unlock(); + } + return newControllers; } }