diff --git a/Mage.Client/src/main/java/mage/client/game/GamePanel.java b/Mage.Client/src/main/java/mage/client/game/GamePanel.java index 9b61573a4c..37bd78db9c 100644 --- a/Mage.Client/src/main/java/mage/client/game/GamePanel.java +++ b/Mage.Client/src/main/java/mage/client/game/GamePanel.java @@ -305,10 +305,14 @@ public final class GamePanel extends javax.swing.JPanel { this.players.clear(); this.playersWhoLeft.clear(); - jLayeredPane.remove(abilityPicker); + if (jLayeredPane!= null) { + jLayeredPane.remove(abilityPicker); + } this.abilityPicker.cleanUp(); - jLayeredPane.remove(DialogManager.getManager(gameId)); + if (jLayeredPane != null) { + jLayeredPane.remove(DialogManager.getManager(gameId)); + } DialogManager.removeGame(gameId); if (pickNumber != null) { diff --git a/Mage.Common/src/main/java/mage/interfaces/MageServer.java b/Mage.Common/src/main/java/mage/interfaces/MageServer.java index 2b505a128a..1e2a81bd64 100644 --- a/Mage.Common/src/main/java/mage/interfaces/MageServer.java +++ b/Mage.Common/src/main/java/mage/interfaces/MageServer.java @@ -137,7 +137,7 @@ public interface MageServer { void joinGame(UUID gameId, String sessionId) throws MageException; - void watchGame(UUID gameId, String sessionId) throws MageException; + boolean watchGame(UUID gameId, String sessionId) throws MageException; void stopWatching(UUID gameId, String sessionId) throws MageException; diff --git a/Mage.Common/src/main/java/mage/remote/SessionImpl.java b/Mage.Common/src/main/java/mage/remote/SessionImpl.java index 90e68afb91..cd6979594e 100644 --- a/Mage.Common/src/main/java/mage/remote/SessionImpl.java +++ b/Mage.Common/src/main/java/mage/remote/SessionImpl.java @@ -1016,8 +1016,7 @@ public class SessionImpl implements Session { public boolean watchGame(UUID gameId) { try { if (isConnected()) { - server.watchGame(gameId, sessionId); - return true; + return server.watchGame(gameId, sessionId); } } catch (MageException ex) { handleMageException(ex); diff --git a/Mage.Server/src/main/java/mage/server/MageServerImpl.java b/Mage.Server/src/main/java/mage/server/MageServerImpl.java index 039a25a81a..e207bce0d7 100644 --- a/Mage.Server/src/main/java/mage/server/MageServerImpl.java +++ b/Mage.Server/src/main/java/mage/server/MageServerImpl.java @@ -879,14 +879,23 @@ public class MageServerImpl implements MageServer { } @Override - public void watchGame(final UUID gameId, final String sessionId) throws MageException { - execute("watchGame", sessionId, () -> { - Optional session = SessionManager.instance.getSession(sessionId); - if (!session.isPresent()) { - logger.error("Session not found : " + sessionId); - } else { - UUID userId = session.get().getUserId(); - GameManager.instance.watchGame(gameId, userId); + public boolean watchGame(final UUID gameId, final String sessionId) throws MageException { + return executeWithResult("watchGame", sessionId, new ActionWithResult() { + @Override + public Boolean execute() throws MageException { + Optional session = SessionManager.instance.getSession(sessionId); + if (!session.isPresent()) { + logger.error("Session not found : " + sessionId); + return false; + } else { + UUID userId = session.get().getUserId(); + return GameManager.instance.watchGame(gameId, userId); + } + } + + @Override + public Boolean negativeResult() { + return false; } }); } diff --git a/Mage.Server/src/main/java/mage/server/TableController.java b/Mage.Server/src/main/java/mage/server/TableController.java index 4850c95668..3b76975680 100644 --- a/Mage.Server/src/main/java/mage/server/TableController.java +++ b/Mage.Server/src/main/java/mage/server/TableController.java @@ -605,6 +605,7 @@ public class TableController { table.initGame(); GameOptions gameOptions = new GameOptions(); gameOptions.rollbackTurnsAllowed = match.getOptions().isRollbackTurnsAllowed(); + gameOptions.bannedUsers = match.getOptions().getBannedUsers(); match.getGame().setGameOptions(gameOptions); GameManager.instance.createGameSession(match.getGame(), userPlayerMap, table.getId(), choosingPlayerId, gameOptions); String creator = null; 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 92ac8d1907..fc4359f6c3 100644 --- a/Mage.Server/src/main/java/mage/server/game/GameController.java +++ b/Mage.Server/src/main/java/mage/server/game/GameController.java @@ -391,14 +391,22 @@ public class GameController implements GameCallback { return true; } - public void watch(UUID userId) { + public boolean watch(UUID userId) { if (userPlayerMap.containsKey(userId)) { // You can't watch a game if you already a player in it - return; + return false; } if (watchers.containsKey(userId)) { // You can't watch a game if you already watch it - return; + return false; + } + if (!isAllowedToWatch(userId)) { + // Dont want people on our ignore list to stalk us + UserManager.instance.getUser(userId).ifPresent(user -> { + user.showUserMessage("Not allowed", "You are banned from watching this game"); + ChatManager.instance.broadcast(chatId, user.getName(), " tried to join, but is banned", MessageColor.BLUE, true, ChatMessage.MessageType.STATUS, null); + }); + return false; } UserManager.instance.getUser(userId).ifPresent(user -> { GameSessionWatcher gameWatcher = new GameSessionWatcher(userId, game, false); @@ -407,6 +415,7 @@ public class GameController implements GameCallback { user.addGameWatchInfo(game.getId()); ChatManager.instance.broadcast(chatId, user.getName(), " has started watching", MessageColor.BLUE, true, ChatMessage.MessageType.STATUS, null); }); + return true; } public void stopWatching(UUID userId) { @@ -1011,4 +1020,13 @@ public class GameController implements GameCallback { } return sb.append(']').toString(); } + + public boolean isAllowedToWatch(UUID userId) { + Optional user = UserManager.instance.getUser(userId); + if (user.isPresent()) { + return !gameOptions.bannedUsers.contains(user.get().getName()); + } + return false; + } + } 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 ba72920585..fe36171175 100644 --- a/Mage.Server/src/main/java/mage/server/game/GameManager.java +++ b/Mage.Server/src/main/java/mage/server/game/GameManager.java @@ -117,11 +117,12 @@ public enum GameManager { } } - public void watchGame(UUID gameId, UUID userId) { + public boolean watchGame(UUID gameId, UUID userId) { GameController gameController = gameControllers.get(gameId); if (gameController != null) { - gameController.watch(userId); + return gameController.watch(userId); } + return false; } public void stopWatching(UUID gameId, UUID userId) { diff --git a/Mage/src/main/java/mage/game/GameOptions.java b/Mage/src/main/java/mage/game/GameOptions.java index 1739fe0e2f..b9091f0906 100644 --- a/Mage/src/main/java/mage/game/GameOptions.java +++ b/Mage/src/main/java/mage/game/GameOptions.java @@ -1,8 +1,11 @@ package mage.game; -import java.io.Serializable; import mage.constants.PhaseStep; +import java.io.Serializable; +import java.util.Collections; +import java.util.Set; + /** * Game options for Mage game. Mainly used in tests to configure * {@link GameImpl} with specific params. @@ -42,4 +45,10 @@ public class GameOptions implements Serializable { * If true, players can rollback turn if all players agree */ public boolean rollbackTurnsAllowed = true; + + /** + * Names of users banned from participating in the game + */ + public Set bannedUsers = Collections.emptySet(); + }