From ad0507e739c6ab7ae145c756a7ba5a4bad8d72c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anders=20=C3=85strand?= Date: Mon, 29 May 2017 21:19:40 +0200 Subject: [PATCH 1/2] Ban ignored users from watching This commit bans ignored users from watching games where the creator has them on their ignore list. --- .../main/java/mage/client/game/GamePanel.java | 8 ++++-- .../main/java/mage/interfaces/MageServer.java | 2 +- .../main/java/mage/remote/SessionImpl.java | 3 +-- .../main/java/mage/server/MageServerImpl.java | 25 +++++++++++++------ .../java/mage/server/TableController.java | 1 + .../java/mage/server/game/GameController.java | 24 +++++++++++++++--- .../java/mage/server/game/GameManager.java | 5 ++-- Mage/src/main/java/mage/game/GameOptions.java | 11 +++++++- 8 files changed, 60 insertions(+), 19 deletions(-) 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(); + } From 4d0e8cb4431c40c3ac2fc3546f5c14ee67634352 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anders=20=C3=85strand?= Date: Tue, 30 May 2017 18:55:40 +0200 Subject: [PATCH 2/2] Review fixes --- Mage.Client/src/main/java/mage/client/game/GamePanel.java | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) 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 37bd78db9c..3c6d2f3bfe 100644 --- a/Mage.Client/src/main/java/mage/client/game/GamePanel.java +++ b/Mage.Client/src/main/java/mage/client/game/GamePanel.java @@ -307,12 +307,9 @@ public final class GamePanel extends javax.swing.JPanel { if (jLayeredPane!= null) { jLayeredPane.remove(abilityPicker); - } - this.abilityPicker.cleanUp(); - - if (jLayeredPane != null) { jLayeredPane.remove(DialogManager.getManager(gameId)); } + this.abilityPicker.cleanUp(); DialogManager.removeGame(gameId); if (pickNumber != null) {