From e6f55d3b80cfa50e41d9174d5f20eb9d0e63b38e Mon Sep 17 00:00:00 2001 From: LevelX2 Date: Fri, 7 Feb 2014 16:07:15 +0100 Subject: [PATCH] * Some changes to game handling to support replay in the future (not working yet). --- .../java/mage/server/TableController.java | 14 +++--- .../main/java/mage/server/TableManager.java | 12 ++--- .../src/main/java/mage/server/User.java | 1 - .../java/mage/server/game/GameController.java | 1 + .../java/mage/server/game/ReplayManager.java | 3 ++ .../java/mage/server/game/ReplaySession.java | 5 +- Mage/src/mage/game/Game.java | 6 ++- Mage/src/mage/game/GameImpl.java | 46 ++++++++++++------- Mage/src/mage/game/GameStates.java | 2 +- Mage/src/mage/game/match/MatchImpl.java | 2 +- 10 files changed, 55 insertions(+), 37 deletions(-) diff --git a/Mage.Server/src/main/java/mage/server/TableController.java b/Mage.Server/src/main/java/mage/server/TableController.java index c47cebfb31..62f1249a35 100644 --- a/Mage.Server/src/main/java/mage/server/TableController.java +++ b/Mage.Server/src/main/java/mage/server/TableController.java @@ -345,13 +345,13 @@ public class TableController { } } - public boolean replayTable(UUID userId) { - if (table.getState() != TableState.FINISHED) { - return false; - } - ReplayManager.getInstance().replayGame(table.getId(), userId); - return true; - } +// public boolean replayTable(UUID userId) { +// if (table.getState() != TableState.FINISHED) { +// return false; +// } +// ReplayManager.getInstance().replayGame(table.getId(), userId); +// return true; +// } private Player createPlayer(String name, String playerType, int skill) { Player player; diff --git a/Mage.Server/src/main/java/mage/server/TableManager.java b/Mage.Server/src/main/java/mage/server/TableManager.java index 8dab57b912..1570c0c2db 100644 --- a/Mage.Server/src/main/java/mage/server/TableManager.java +++ b/Mage.Server/src/main/java/mage/server/TableManager.java @@ -249,12 +249,12 @@ public class TableManager { return false; } - public boolean replayTable(UUID userId, UUID tableId) { - if (controllers.containsKey(tableId)) { - return controllers.get(tableId).replayTable(userId); - } - return false; - } +// public boolean replayTable(UUID userId, UUID tableId) { +// if (controllers.containsKey(tableId)) { +// return controllers.get(tableId).replayTable(userId); +// } +// return false; +// } public void endGame(UUID tableId) { if (controllers.containsKey(tableId)) { diff --git a/Mage.Server/src/main/java/mage/server/User.java b/Mage.Server/src/main/java/mage/server/User.java index f574d49cbd..b55381b6dd 100644 --- a/Mage.Server/src/main/java/mage/server/User.java +++ b/Mage.Server/src/main/java/mage/server/User.java @@ -35,7 +35,6 @@ import java.util.Map.Entry; import java.util.UUID; import java.util.concurrent.ConcurrentHashMap; import mage.cards.decks.Deck; -import mage.constants.TableState; import mage.game.Table; import mage.interfaces.callback.ClientCallback; import mage.players.net.UserData; 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 2026cdee66..474d8bd571 100644 --- a/Mage.Server/src/main/java/mage/server/game/GameController.java +++ b/Mage.Server/src/main/java/mage/server/game/GameController.java @@ -111,6 +111,7 @@ public class GameController implements GameCallback { this.userPlayerMap = userPlayerMap; chatId = ChatManager.getInstance().createChatSession(); this.game = game; + this.game.setSaveGame(ConfigSettings.getInstance().isSaveGameActivated()); this.tableId = tableId; this.choosingPlayerId = choosingPlayerId; for (Player player: game.getPlayers().values()) { diff --git a/Mage.Server/src/main/java/mage/server/game/ReplayManager.java b/Mage.Server/src/main/java/mage/server/game/ReplayManager.java index d1cc31393a..d277c158af 100644 --- a/Mage.Server/src/main/java/mage/server/game/ReplayManager.java +++ b/Mage.Server/src/main/java/mage/server/game/ReplayManager.java @@ -73,5 +73,8 @@ public class ReplayManager { replaySessions.get(gameId.toString() + userId.toString()).next(moves); } + public void endReplay(UUID gameId, UUID userId) { + replaySessions.remove(gameId.toString() + userId.toString()); + } } diff --git a/Mage.Server/src/main/java/mage/server/game/ReplaySession.java b/Mage.Server/src/main/java/mage/server/game/ReplaySession.java index 1138d482f6..fd4fb6882c 100644 --- a/Mage.Server/src/main/java/mage/server/game/ReplaySession.java +++ b/Mage.Server/src/main/java/mage/server/game/ReplaySession.java @@ -35,7 +35,6 @@ import mage.interfaces.callback.ClientCallback; import mage.server.User; import mage.server.UserManager; import mage.view.GameView; -import org.apache.log4j.Logger; /** * @@ -43,8 +42,7 @@ import org.apache.log4j.Logger; */ public class ReplaySession implements GameCallback { - private static final Logger logger = Logger.getLogger(ReplaySession.class); - private GameReplay replay; + private final GameReplay replay; protected UUID userId; ReplaySession(UUID gameId, UUID userId) { @@ -85,6 +83,7 @@ public class ReplaySession implements GameCallback { if (user != null) { user.fireCallback(new ClientCallback("replayDone", replay.getGame().getId(), result)); } + ReplayManager.getInstance().endReplay(replay.getGame().getId(), userId); } private void updateGame(final GameState state, Game game) { diff --git a/Mage/src/mage/game/Game.java b/Mage/src/mage/game/Game.java index 3035f2a453..c7d3060e77 100644 --- a/Mage/src/mage/game/Game.java +++ b/Mage/src/mage/game/Game.java @@ -232,12 +232,14 @@ public interface Game extends MageItem, Serializable { int doAction(MageAction action); //game transaction methods - void saveState(); + void saveState(boolean bookmark); int bookmarkState(); void restoreState(int bookmark); void removeBookmark(int bookmark); int getSavedStateSize(); - + boolean isSaveGame(); + void setSaveGame(boolean saveGame); + // game options void setGameOptions(GameOptions options); diff --git a/Mage/src/mage/game/GameImpl.java b/Mage/src/mage/game/GameImpl.java index 8086c8660d..c40d818fa2 100644 --- a/Mage/src/mage/game/GameImpl.java +++ b/Mage/src/mage/game/GameImpl.java @@ -38,7 +38,6 @@ import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.LinkedHashMap; -import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Map.Entry; @@ -177,15 +176,15 @@ public abstract class GameImpl> implements Game, Serializa public static volatile int copyCount = 0; public static volatile long copyTime = 0; - private transient LinkedList actions; + // private final transient LinkedList actions; private Player scorePlayer; - private int score = 0; + // private int score = 0; private Player losingPlayer; private boolean stateCheckRequired = false; // used to indicate that currently applied replacement effects have to check for scope relevance (614.12 13/01/18) private boolean scopeRelevant = false; - + private boolean saveGame = false; private int priorityTime; @Override @@ -197,7 +196,7 @@ public abstract class GameImpl> implements Game, Serializa this.freeMulligans = freeMulligans; this.attackOption = attackOption; this.state = new GameState(); - this.actions = new LinkedList(); + // this.actions = new LinkedList(); } public GameImpl(final GameImpl game) { @@ -213,9 +212,7 @@ public abstract class GameImpl> implements Game, Serializa this.freeMulligans = game.freeMulligans; this.attackOption = game.attackOption; this.state = game.state.copy(); - // Issue 350 -// this.gameCards = game.gameCards; - // issue #187 (else zoneChangeCounter modified by AI -> illegal target) + // Ai simulation modifies e.g. zoneChangeCounter so copy is needed if AI active for (Map.Entry entry: game.gameCards.entrySet()) { this.gameCards.put(entry.getKey(), entry.getValue().copy()); } @@ -227,11 +224,12 @@ public abstract class GameImpl> implements Game, Serializa copyCount++; copyTime += (System.currentTimeMillis() - t1); } - this.actions = new LinkedList(); +// this.actions = new LinkedList(); this.stateCheckRequired = game.stateCheckRequired; this.scorePlayer = game.scorePlayer; this.scopeRelevant = game.scopeRelevant; this.priorityTime = game.priorityTime; + this.saveGame = game.saveGame; } @Override @@ -428,9 +426,11 @@ public abstract class GameImpl> implements Game, Serializa } @Override - public void saveState() { + public void saveState(boolean bookmark) { if (!simulation && gameStates != null) { - gameStates.save(state); + if (bookmark || saveGame) { + gameStates.save(state); + } } } @@ -479,7 +479,7 @@ public abstract class GameImpl> implements Game, Serializa @Override public int bookmarkState() { if (!simulation) { - saveState(); + saveState(true); if (logger.isDebugEnabled()) { logger.debug("Bookmarking state: " + gameStates.getSize()); } @@ -641,7 +641,7 @@ public abstract class GameImpl> implements Game, Serializa if (gameOptions.stopOnTurn != null && gameOptions.stopAtStep == PhaseStep.UNTAP) { if (gameOptions.stopOnTurn.equals(state.getTurnNum())) { winnerId = null; //DRAW - //saveState(); + saveState(false); return true; } } @@ -662,7 +662,7 @@ public abstract class GameImpl> implements Game, Serializa } fireStatusEvent(startMessage, false); - //saveState(); + saveState(false); //20091005 - 103.1 if (!gameOptions.skipInitShuffling) { //don't shuffle in test mode for card injection on top of player's libraries @@ -719,7 +719,7 @@ public abstract class GameImpl> implements Game, Serializa mulligan(player.getId()); } fireInformEvent(player.getName() + " keeps hand"); - //saveState(); + saveState(false); } for (UUID playerId : state.getPlayerList(startingPlayerId)) { @@ -806,6 +806,8 @@ public abstract class GameImpl> implements Game, Serializa for (Player player: state.getPlayers().values()) { player.abort(); } + // allow gc + gameCards.clear(); } } @@ -947,6 +949,7 @@ public abstract class GameImpl> implements Game, Serializa } //resetLKI(); applyEffects(); + saveState(false); if (isPaused() || isGameOver()) { return; } @@ -2057,7 +2060,7 @@ public abstract class GameImpl> implements Game, Serializa public int doAction(MageAction action) { //actions.add(action); int value = action.doAction(this); - score += action.getScore(scorePlayer); +// score += action.getScore(scorePlayer); return value; } @@ -2128,6 +2131,17 @@ public abstract class GameImpl> implements Game, Serializa return this.scopeRelevant; } + @Override + public boolean isSaveGame() { + return saveGame; + } + + @Override + public void setSaveGame(boolean saveGame) { + this.saveGame = saveGame; + } + + public void setStartMessage(String startMessage) { this.startMessage = startMessage; } diff --git a/Mage/src/mage/game/GameStates.java b/Mage/src/mage/game/GameStates.java index 8c6e8bd049..58acf11064 100644 --- a/Mage/src/mage/game/GameStates.java +++ b/Mage/src/mage/game/GameStates.java @@ -43,7 +43,7 @@ public class GameStates implements Serializable { private static final transient Logger logger = Logger.getLogger(GameStates.class); // private List states = new LinkedList(); - private List states = new LinkedList(); + private final List states = new LinkedList(); public void save(GameState gameState) { // states.add(new Copier().copyCompressed(gameState)); diff --git a/Mage/src/mage/game/match/MatchImpl.java b/Mage/src/mage/game/match/MatchImpl.java index 9688332826..4a2d802cd6 100644 --- a/Mage/src/mage/game/match/MatchImpl.java +++ b/Mage/src/mage/game/match/MatchImpl.java @@ -173,7 +173,7 @@ public abstract class MatchImpl implements Match { } protected void initGame(Game game) throws GameException { - shufflePlayers(); + shufflePlayers(); for (MatchPlayer matchPlayer: this.players) { if (!matchPlayer.hasQuit()) { matchPlayer.getPlayer().init(game);