diff --git a/Mage.Server/src/main/java/mage/server/TableController.java b/Mage.Server/src/main/java/mage/server/TableController.java index 7008d09336..2aa997f913 100644 --- a/Mage.Server/src/main/java/mage/server/TableController.java +++ b/Mage.Server/src/main/java/mage/server/TableController.java @@ -337,9 +337,7 @@ public class TableController { if (userPlayerMap.get(userId) != null) { return false; } - UserManager.getInstance().getUser(userId).watchGame(match.getGame().getId()); - return true; - + return UserManager.getInstance().getUser(userId).watchGame(match.getGame().getId()); } } diff --git a/Mage.Server/src/main/java/mage/server/User.java b/Mage.Server/src/main/java/mage/server/User.java index a3d1a552f0..3b72d0fb53 100644 --- a/Mage.Server/src/main/java/mage/server/User.java +++ b/Mage.Server/src/main/java/mage/server/User.java @@ -27,6 +27,7 @@ */ package mage.server; +import java.util.ArrayList; import java.util.Date; import java.util.LinkedList; import java.util.List; @@ -75,6 +76,7 @@ public class User { private final Map tournamentSessions; private final Map constructing; private final Map sideboarding; + private final List watchedGames; private String sessionId; private String info; @@ -97,6 +99,7 @@ public class User { this.tournamentSessions = new ConcurrentHashMap<>(); this.constructing = new ConcurrentHashMap<>(); this.sideboarding = new ConcurrentHashMap<>(); + this.watchedGames = new ArrayList<>(); this.sessionId = ""; } @@ -205,8 +208,9 @@ public class User { fireCallback(new ClientCallback("showUserMessage", null, messageData )); } - public void watchGame(final UUID gameId) { + public boolean watchGame(final UUID gameId) { fireCallback(new ClientCallback("watchGame", gameId)); + return true; } public void replayGame(final UUID gameId) { @@ -405,6 +409,10 @@ public class User { if (tournament > 0) { sb.append("TP: ").append(tournament).append(" "); } + if (watchedGames.size() > 0) { + sb.append("WA: ").append(watchedGames.size()).append(" "); + } + sb.append(disconnectInfo); return sb.toString(); } @@ -416,4 +424,12 @@ public class User { public void setInfo(String Info) { this.info = Info; } + + public void addGameWatchInfo(UUID gameId) { + watchedGames.add(gameId); + } + + public void removeGameWatchInfo(UUID gameId) { + watchedGames.remove(gameId); + } } 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 6b1aed1a6b..df5cc82bdb 100644 --- a/Mage.Server/src/main/java/mage/server/game/GameController.java +++ b/Mage.Server/src/main/java/mage/server/game/GameController.java @@ -336,11 +336,16 @@ public class GameController implements GameCallback { // You can't watch a game if you already a player in it return; } + if (watchers.get(userId) != null) { + // You can't watch a game if you already watch it + return; + } User user = UserManager.getInstance().getUser(userId); if (user != null) { GameWatcher gameWatcher = new GameWatcher(userId, game, false); watchers.put(userId, gameWatcher); gameWatcher.init(); + user.addGameWatchInfo(game.getId()); ChatManager.getInstance().broadcast(chatId, user.getName(), " has started watching", MessageColor.BLUE, true, ChatMessage.MessageType.STATUS); } } @@ -349,6 +354,7 @@ public class GameController implements GameCallback { watchers.remove(userId); User user = UserManager.getInstance().getUser(userId); if (user != null) { + user.removeGameWatchInfo(game.getId()); ChatManager.getInstance().broadcast(chatId, user.getName(), " has stopped watching", MessageColor.BLUE, true, ChatMessage.MessageType.STATUS); } } diff --git a/Mage.Server/src/main/java/mage/server/game/GameWatcher.java b/Mage.Server/src/main/java/mage/server/game/GameWatcher.java index e2368005c6..f1847c775e 100644 --- a/Mage.Server/src/main/java/mage/server/game/GameWatcher.java +++ b/Mage.Server/src/main/java/mage/server/game/GameWatcher.java @@ -100,6 +100,7 @@ public class GameWatcher { if (!killed) { User user = UserManager.getInstance().getUser(userId); if (user != null) { + user.removeGameWatchInfo(game.getId()); user.fireCallback(new ClientCallback("gameOver", game.getId(), message)); } }