Commit d09cd35a authored by Maxmtg's avatar Maxmtg

rollback game state serialization attempts

parent fe7ad54e
......@@ -524,9 +524,6 @@ forge-game/src/main/java/forge/game/event/GameEventTurnPhase.java -text
forge-game/src/main/java/forge/game/event/GameEventZone.java -text
forge-game/src/main/java/forge/game/event/IGameEventVisitor.java -text
forge-game/src/main/java/forge/game/event/package-info.java -text
forge-game/src/main/java/forge/game/io/GameStateDeserializer.java -text
forge-game/src/main/java/forge/game/io/GameStateSerializer.java -text
forge-game/src/main/java/forge/game/io/IGameStateObject.java -text
forge-game/src/main/java/forge/game/mana/Mana.java svneol=native#text/plain
forge-game/src/main/java/forge/game/mana/ManaCostAdjustment.java -text
forge-game/src/main/java/forge/game/mana/ManaCostBeingPaid.java svneol=native#text/plain
......@@ -545,6 +542,7 @@ forge-game/src/main/java/forge/game/player/AchievementTracker.java -text
forge-game/src/main/java/forge/game/player/DelayedReveal.java -text
forge-game/src/main/java/forge/game/player/GameLossReason.java -text
forge-game/src/main/java/forge/game/player/IGameEntitiesFactory.java -text
forge-game/src/main/java/forge/game/player/LobbyPlayer.java -text
forge-game/src/main/java/forge/game/player/Player.java svneol=native#text/plain
forge-game/src/main/java/forge/game/player/PlayerActionConfirmMode.java -text
forge-game/src/main/java/forge/game/player/PlayerController.java -text
......@@ -18,7 +18,6 @@
package forge.item;
import com.google.common.base.Function;
import forge.card.CardRarity;
import forge.card.CardRules;
......
......@@ -42,9 +42,6 @@ import forge.game.card.CardPredicates;
import forge.game.combat.Combat;
import forge.game.event.GameEvent;
import forge.game.event.GameEventGameOutcome;
import forge.game.io.GameStateDeserializer;
import forge.game.io.GameStateSerializer;
import forge.game.io.IGameStateObject;
import forge.game.phase.EndOfTurn;
import forge.game.phase.Phase;
import forge.game.phase.PhaseHandler;
......@@ -67,10 +64,10 @@ import forge.util.Aggregates;
/**
* Represents the state of a <i>single game</i>, a new instance is created for each game.
*/
public class Game implements IGameStateObject {
public class Game {
private final GameRules rules;
private List<Player> roIngamePlayers;
private List<Player> roIngamePlayersReversed;
private final List<Player> roIngamePlayers;
private final List<Player> roIngamePlayersReversed;
private final List<Player> allPlayers;
private final List<Player> ingamePlayers = new ArrayList<Player>();
......@@ -100,59 +97,12 @@ public class Game implements IGameStateObject {
private GameOutcome outcome;
private boolean disableAutoYields;
@Override
public void loadState(GameStateDeserializer gsd) {
gsd.readObject(rules);
gsd.readObject(cleanup);
gsd.readObject(endOfTurn);
gsd.readObject(endOfCombat);
gsd.readObject(untap);
gsd.readObject(upkeep);
gsd.readObject(stack);
gsd.readObject(phaseHandler);
gsd.readObject(staticEffects);
gsd.readObject(triggerHandler);
gsd.readObject(gameLog);
gsd.readObject(stackZone);
turnOrder = Direction.valueOf(gsd.readString());
timestamp = gsd.readLong();
age = GameStage.valueOf(gsd.readString());
outcome = (GameOutcome)gsd.readObject();
gsd.readPlayerList(allPlayers);
gsd.readPlayerList(ingamePlayers);
}
@Override
public void saveState(GameStateSerializer gss) {
gss.write(rules);
gss.write(cleanup);
gss.write(endOfTurn);
gss.write(endOfCombat);
gss.write(untap);
gss.write(upkeep);
gss.write(stack);
gss.write(phaseHandler);
gss.write(staticEffects);
gss.write(triggerHandler);
gss.write(replacementHandler);
gss.write(gameLog);
gss.write(stackZone);
gss.write(turnOrder.name());
gss.write(timestamp);
gss.write(age.name());
gss.write(outcome);
gss.writePlayerList(allPlayers);
gss.writePlayerList(ingamePlayers);
roIngamePlayers = Collections.unmodifiableList(ingamePlayers);
roIngamePlayersReversed = Lists.reverse(roIngamePlayers); // reverse of unmodifiable list is also unmodifiable
}
/**
* Constructor.
* @param match0
*/
public Game(List<RegisteredPlayer> players0, GameRules rules0, Match match0) { /* no more zones to map here */
rules = rules0;
public Game(List<RegisteredPlayer> players0, GameRules rules, Match match0) { /* no more zones to map here */
this.rules = rules;
match = match0;
List<Player> players = new ArrayList<Player>();
allPlayers = Collections.unmodifiableList(players);
......@@ -208,10 +158,10 @@ public class Game implements IGameStateObject {
* @return the players
*/
public final List<Player> getPlayersInTurnOrder() {
if (turnOrder.isDefaultDirection()) {
return roIngamePlayers;
if (this.turnOrder.isDefaultDirection()) {
return this.roIngamePlayers;
}
return roIngamePlayersReversed;
return this.roIngamePlayersReversed;
}
/**
......@@ -239,7 +189,7 @@ public class Game implements IGameStateObject {
* @return the cleanup step
*/
public final Phase getCleanup() {
return cleanup;
return this.cleanup;
}
/**
......@@ -248,7 +198,7 @@ public class Game implements IGameStateObject {
* @return the endOfTurn
*/
public final EndOfTurn getEndOfTurn() {
return endOfTurn;
return this.endOfTurn;
}
/**
......@@ -257,7 +207,7 @@ public class Game implements IGameStateObject {
* @return the endOfCombat
*/
public final Phase getEndOfCombat() {
return endOfCombat;
return this.endOfCombat;
}
/**
......@@ -266,7 +216,7 @@ public class Game implements IGameStateObject {
* @return the upkeep
*/
public final Upkeep getUpkeep() {
return upkeep;
return this.upkeep;
}
/**
......@@ -275,7 +225,7 @@ public class Game implements IGameStateObject {
* @return the upkeep
*/
public final Untap getUntap() {
return untap;
return this.untap;
}
/**
......@@ -284,7 +234,7 @@ public class Game implements IGameStateObject {
* @return the phaseHandler
*/
public final PhaseHandler getPhaseHandler() {
return phaseHandler;
return this.phaseHandler;
}
/**
......@@ -293,7 +243,7 @@ public class Game implements IGameStateObject {
* @return the stack
*/
public final MagicStack getStack() {
return stack;
return this.stack;
}
/**
......@@ -302,7 +252,7 @@ public class Game implements IGameStateObject {
* @return the staticEffects
*/
public final StaticEffects getStaticEffects() {
return staticEffects;
return this.staticEffects;
}
/**
......@@ -311,7 +261,7 @@ public class Game implements IGameStateObject {
* @return the triggerHandler
*/
public final TriggerHandler getTriggerHandler() {
return triggerHandler;
return this.triggerHandler;
}
/**
......@@ -320,7 +270,7 @@ public class Game implements IGameStateObject {
* @return the combat
*/
public final Combat getCombat() {
return getPhaseHandler().getCombat();
return this.getPhaseHandler().getCombat();
}
/**
......@@ -329,7 +279,7 @@ public class Game implements IGameStateObject {
* @return the game log
*/
public final GameLog getGameLog() {
return gameLog;
return this.gameLog;
}
/**
......@@ -338,11 +288,11 @@ public class Game implements IGameStateObject {
* @return the stackZone
*/
public final Zone getStackZone() {
return stackZone;
return this.stackZone;
}
public List<Card> getCardsPlayerCanActivateInStack() {
List<Card> list = stackZone.getCards();
List<Card> list = this.stackZone.getCards();
list = CardLists.filter(list, new Predicate<Card>() {
@Override
public boolean apply(final Card c) {
......@@ -364,15 +314,15 @@ public class Game implements IGameStateObject {
* proceeds.
*/
public final Direction getTurnOrder() {
return turnOrder;
return this.turnOrder;
}
public final void reverseTurnOrder() {
turnOrder = turnOrder.getOtherDirection();
this.turnOrder = this.turnOrder.getOtherDirection();
}
public final void resetTurnOrder() {
turnOrder = Direction.getDefaultDirection();
this.turnOrder = Direction.getDefaultDirection();
}
/**
......@@ -381,8 +331,8 @@ public class Game implements IGameStateObject {
* @return the next timestamp
*/
public final long getNextTimestamp() {
timestamp = getTimestamp() + 1;
return getTimestamp();
this.timestamp = this.getTimestamp() + 1;
return this.getTimestamp();
}
/**
......@@ -391,11 +341,11 @@ public class Game implements IGameStateObject {
* @return the timestamp
*/
public final long getTimestamp() {
return timestamp;
return this.timestamp;
}
public final GameOutcome getOutcome() {
return outcome;
return this.outcome;
}
/**
......@@ -417,7 +367,7 @@ public class Game implements IGameStateObject {
* @param go the gameOver to set
*/
public synchronized void setGameOver(GameEndReason reason) {
age = GameStage.GameOver;
this.age = GameStage.GameOver;
for (Player p : allPlayers) {
p.setMindSlaveMaster(null); // for correct totals
}
......@@ -429,7 +379,7 @@ public class Game implements IGameStateObject {
final GameOutcome result = new GameOutcome(reason, getRegisteredPlayers());
result.setTurnsPlayed(getPhaseHandler().getTurn());
outcome = result;
this.outcome = result;
match.addGamePlayed(this);
// The log shall listen to events and generate text internally
......@@ -543,7 +493,7 @@ public class Game implements IGameStateObject {
* {@code null} if there are no players in the game.
*/
public Player getNextPlayerAfter(final Player playerTurn) {
return getNextPlayerAfter(playerTurn, turnOrder);
return getNextPlayerAfter(playerTurn, this.turnOrder);
}
/**
......@@ -601,7 +551,7 @@ public class Game implements IGameStateObject {
final Map<String, Object> runParams = new TreeMap<String, Object>();
runParams.put("Player", p);
getTriggerHandler().runTrigger(TriggerType.LosesGame, runParams, false);
this.getTriggerHandler().runTrigger(TriggerType.LosesGame, runParams, false);
}
/**
......@@ -637,7 +587,7 @@ public class Game implements IGameStateObject {
* @param activePlane0 the activePlane to set
*/
public void setActivePlanes(List<Card> activePlane0) {
activePlanes = activePlane0;
this.activePlanes = activePlane0;
}
public void archenemy904_10() {
......@@ -796,4 +746,5 @@ public class Game implements IGameStateObject {
}
return rarities;
}
}
......@@ -19,10 +19,6 @@
package forge.game;
import forge.game.event.IGameEventVisitor;
import forge.game.io.GameStateDeserializer;
import forge.game.io.GameStateSerializer;
import forge.game.io.IGameStateObject;
import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
......@@ -34,7 +30,7 @@ import java.util.Observable;
* @author Forge
* @version $Id: GameLog.java 12297 2011-11-28 19:56:47Z slapshot5 $
*/
public class GameLog extends Observable implements IGameStateObject {
public class GameLog extends Observable {
private List<GameLogEntry> log = new ArrayList<GameLogEntry>();
private GameLogFormatter formatter = new GameLogFormatter(this);
......@@ -46,17 +42,8 @@ public class GameLog extends Observable implements IGameStateObject {
* 6 - All Phase information
*/
@Override
public void loadState(GameStateDeserializer gsd) {
// TODO Auto-generated method stub
}
@Override
public void saveState(GameStateSerializer gss) {
gss.writeList(log);
}
/**
* Instantiates a new game log.
*/
......@@ -111,4 +98,5 @@ public class GameLog extends Observable implements IGameStateObject {
public IGameEventVisitor<?> getEventVisitor() {
return formatter;
}
}
package forge.game;
import forge.game.io.GameStateDeserializer;
import forge.game.io.GameStateSerializer;
import forge.game.io.IGameStateObject;
public class GameLogEntry implements IGameStateObject {
public class GameLogEntry {
public final String message;
public final GameLogEntryType type;
// might add here date and some other fields
......@@ -19,16 +15,4 @@ public class GameLogEntry implements IGameStateObject {
// TODO Auto-generated method stub
return type.getCaption() + ": " + message;
}
@Override
public void loadState(GameStateDeserializer gsd) {
// TODO Auto-generated method stub
}
@Override
public void saveState(GameStateSerializer gss) {
gss.write(type.name());
gss.write(message);
}
}
\ No newline at end of file
......@@ -18,9 +18,6 @@
package forge.game;
import forge.LobbyPlayer;
import forge.game.io.GameStateDeserializer;
import forge.game.io.GameStateSerializer;
import forge.game.io.IGameStateObject;
import forge.game.player.Player;
import forge.game.player.PlayerOutcome;
import forge.game.player.PlayerStatistics;
......@@ -43,7 +40,8 @@ import java.util.Map.Entry;
// This class might be divided in two parts: the very summary (immutable with
// only getters) and
// GameObserver class - who should be notified of any considerable ingame event
public final class GameOutcome implements Iterable<Pair<LobbyPlayer, PlayerStatistics>>, IGameStateObject {
public final class GameOutcome implements Iterable<Pair<LobbyPlayer, PlayerStatistics>> {
public static class AnteResult {
public final List<PaperCard> lostCards;
public final List<PaperCard> wonCards;
......@@ -53,8 +51,7 @@ public final class GameOutcome implements Iterable<Pair<LobbyPlayer, PlayerStati
if (won) {
this.wonCards = cards;
this.lostCards = new ArrayList<>();
}
else {
} else {
this.lostCards = cards;
this.wonCards = new ArrayList<>();
}
......@@ -79,20 +76,6 @@ public final class GameOutcome implements Iterable<Pair<LobbyPlayer, PlayerStati
public final Map<Player, AnteResult> anteResult = new TreeMap<>();
private GameEndReason winCondition;
@Override
public void loadState(GameStateDeserializer gsd) {
lastTurnNumber = gsd.readInt();
lifeDelta = gsd.readInt();
winCondition = GameEndReason.valueOf(gsd.readString());
}
@Override
public void saveState(GameStateSerializer gss) {
gss.write(lastTurnNumber);
gss.write(lifeDelta);
gss.write(winCondition.name());
}
public GameOutcome(GameEndReason reason, final Iterable<Player> list) {
winCondition = reason;
players = list;
......@@ -101,21 +84,22 @@ public final class GameOutcome implements Iterable<Pair<LobbyPlayer, PlayerStati
}
calculateLifeDelta();
}
private void calculateLifeDelta() {
int opponentsHealth = 0;
int winnersHealth = 0;
for (Player p : players) {
if (p == this.getWinningPlayer()) {
winnersHealth = p.getLife();
}
else {
} else {
opponentsHealth += p.getLife();
}
}
lifeDelta = Math.max(0, winnersHealth -= opponentsHealth);
}
public boolean isDraw() {
......@@ -127,6 +111,7 @@ public final class GameOutcome implements Iterable<Pair<LobbyPlayer, PlayerStati
return true;
}
public boolean isWinner(final LobbyPlayer who) {
for (Pair<LobbyPlayer, PlayerStatistics> pv : playerRating)
if (pv.getValue().getOutcome().hasWon() && pv.getKey() == who )
......@@ -249,4 +234,5 @@ public final class GameOutcome implements Iterable<Pair<LobbyPlayer, PlayerStati
public List<Player> getPlayers() {
return (List<Player>)players;
}
}
......@@ -3,11 +3,7 @@ package forge.game;
import java.util.EnumSet;
import java.util.Set;
import forge.game.io.GameStateDeserializer;
import forge.game.io.GameStateSerializer;
import forge.game.io.IGameStateObject;
public class GameRules implements IGameStateObject {
public class GameRules {
private GameType gameType;
private boolean manaBurn;
private int poisonCountersToLose = 10; // is commonly 10, but turns into 15 for 2HG
......@@ -16,44 +12,6 @@ public class GameRules implements IGameStateObject {
private boolean playForAnte = false;
private boolean matchAnteRarity = false;
private EnumSet<GameType> appliedVariants = EnumSet.noneOf(GameType.class);
// it's a preference, not rule... but I could hardly find a better place for it
public boolean canCloneUseTargetsImage;
@Override
public void loadState(GameStateDeserializer gsd) {
gameType = GameType.valueOf(gsd.readString());
manaBurn = gsd.readBoolean();
poisonCountersToLose = gsd.readInt();
gamesPerMatch = gsd.readInt();
gamesToWinMatch = gsd.readInt();
playForAnte = gsd.readBoolean();
matchAnteRarity = gsd.readBoolean();
canCloneUseTargetsImage = gsd.readBoolean();
int variantCount = gsd.readInt();
appliedVariants.clear();
for (int i = 0; i < variantCount; i++) {
appliedVariants.add(GameType.valueOf(gsd.readString()));
}
}
@Override
public void saveState(GameStateSerializer gss) {
gss.write(gameType.name());
gss.write(manaBurn);
gss.write(poisonCountersToLose);
gss.write(gamesPerMatch);
gss.write(gamesToWinMatch);
gss.write(playForAnte);
gss.write(matchAnteRarity);
gss.write(canCloneUseTargetsImage);
gss.write(appliedVariants.size());
for (GameType variant : appliedVariants) {
gss.write(variant.name());
}
}
public GameRules(GameType type) {
this.gameType = type;
......@@ -124,4 +82,7 @@ public class GameRules implements IGameStateObject {
public boolean hasAppliedVariant(GameType variant) {
return appliedVariants.contains(variant);
}
// it's a preference, not rule... but I could hardly find a better place for it
public boolean canCloneUseTargetsImage;
}
......@@ -5,5 +5,5 @@ public enum GameStage {
Mulligan,
Play,
RestartedByKarn,
GameOver;
GameOver
}
\ No newline at end of file
......@@ -304,8 +304,7 @@ public class Match {
}
if (outcome.anteResult.containsKey(fromGame)) {
outcome.anteResult.get(fromGame).addWon(gainedPaperOwnership);
}
else {
} else {
outcome.anteResult.put(fromGame, GameOutcome.AnteResult.won(gainedPaperOwnership));
}
}
......@@ -332,8 +331,7 @@ public class Match {
if (outcome.anteResult.containsKey(fromGame)) {
outcome.anteResult.get(fromGame).addLost(personalLosses);
}
else {
} else {
outcome.anteResult.put(fromGame, GameOutcome.AnteResult.lost(personalLosses));
}
}
......@@ -343,8 +341,7 @@ public class Match {
Player fromGame = lastGame.getRegisteredPlayers().get(iWinner);
if (outcome.anteResult.containsKey(fromGame)) {
outcome.anteResult.get(fromGame).addWon(losses);
}
else {
} else {
outcome.anteResult.put(fromGame, GameOutcome.AnteResult.won(losses));
}
......
......@@ -19,9 +19,6 @@ package forge.game;
import forge.game.card.Card;
import forge.game.card.CardUtil;
import forge.game.io.GameStateDeserializer;
import forge.game.io.GameStateSerializer;
import forge.game.io.IGameStateObject;
import forge.game.player.Player;
import forge.game.replacement.ReplacementEffect;
import forge.game.spellability.AbilityStatic;
......@@ -38,7 +35,7 @@ import java.util.*;
* @author Forge
* @version $Id$
*/
public class StaticEffects implements IGameStateObject {
public class StaticEffects {
// **************** StaticAbility system **************************
private final ArrayList<StaticEffect> staticEffects = new ArrayList<StaticEffect>();
......@@ -265,18 +262,6 @@ public class StaticEffects implements IGameStateObject {
return affectedCards;
}
@Override