From 3e130a66edfb9ba072f1e1ca4f61bb3cfb114033 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Zurcher?= Date: Tue, 22 Mar 2016 15:56:04 +0100 Subject: Battle,Ctrl: move JSON code from BattleCommon to Ctrl --- core/src/ch/asynk/rustanddust/game/Battle.java | 4 ---- core/src/ch/asynk/rustanddust/game/Ctrl.java | 27 ++++++++++++++++++++++ .../rustanddust/game/battles/BattleCommon.java | 26 +-------------------- core/src/ch/asynk/rustanddust/game/ctrl/Solo.java | 8 +++---- 4 files changed, 32 insertions(+), 33 deletions(-) diff --git a/core/src/ch/asynk/rustanddust/game/Battle.java b/core/src/ch/asynk/rustanddust/game/Battle.java index eb5b086..f2d874b 100644 --- a/core/src/ch/asynk/rustanddust/game/Battle.java +++ b/core/src/ch/asynk/rustanddust/game/Battle.java @@ -39,8 +39,4 @@ public interface Battle extends Marshal public boolean hasReinforcement(); public State.StateType getState(); - - public String unload(Marshal.Mode mode); - - public void load(Marshal.Mode mode, String payload); } diff --git a/core/src/ch/asynk/rustanddust/game/Ctrl.java b/core/src/ch/asynk/rustanddust/game/Ctrl.java index 96dffa9..53cc970 100644 --- a/core/src/ch/asynk/rustanddust/game/Ctrl.java +++ b/core/src/ch/asynk/rustanddust/game/Ctrl.java @@ -1,9 +1,16 @@ package ch.asynk.rustanddust.game; +import java.io.StringWriter; + import com.badlogic.gdx.utils.Disposable; +import com.badlogic.gdx.utils.Json; +import com.badlogic.gdx.utils.JsonValue; +import com.badlogic.gdx.utils.JsonReader; +import com.badlogic.gdx.utils.JsonWriter.OutputType; import ch.asynk.rustanddust.RustAndDust; import ch.asynk.rustanddust.ui.Position; +import ch.asynk.rustanddust.util.Marshal; import ch.asynk.rustanddust.game.ctrl.Solo; import ch.asynk.rustanddust.game.State.StateType; import ch.asynk.rustanddust.game.states.StateCommon; @@ -23,6 +30,8 @@ public abstract class Ctrl implements Disposable public final RustAndDust game; public final Battle battle; + private final static StringWriter writer = new StringWriter(2048); + public Map map; public Hud hud; public boolean blockMap; @@ -107,11 +116,29 @@ public abstract class Ctrl implements Disposable battle.desinit(); } + // JSON + public boolean isLoading() { return (stateType == StateType.LOADING); } + public void load(Marshal.Mode mode, String payload) + { + JsonValue root = new JsonReader().parse(payload); + battle.load(mode, root); + } + + public String unload(Marshal.Mode mode) + { + Json json = new Json(OutputType.json); + writer.getBuffer().setLength(0); + json.setWriter(writer); + battle.unload(mode, json); + writer.flush(); + return writer.toString(); + } + // INPUTS public boolean drag(float x, float y, int dx, int dy) diff --git a/core/src/ch/asynk/rustanddust/game/battles/BattleCommon.java b/core/src/ch/asynk/rustanddust/game/battles/BattleCommon.java index 968c657..eef5cc7 100644 --- a/core/src/ch/asynk/rustanddust/game/battles/BattleCommon.java +++ b/core/src/ch/asynk/rustanddust/game/battles/BattleCommon.java @@ -1,12 +1,9 @@ package ch.asynk.rustanddust.game.battles; import java.util.Random; -import java.io.StringWriter; import com.badlogic.gdx.utils.Json; import com.badlogic.gdx.utils.JsonValue; -import com.badlogic.gdx.utils.JsonReader; -import com.badlogic.gdx.utils.JsonWriter.OutputType; import ch.asynk.rustanddust.util.Marshal; import ch.asynk.rustanddust.game.Battle; @@ -25,8 +22,6 @@ public abstract class BattleCommon implements Battle { protected final static Random random = new Random(System.currentTimeMillis()); - private final static StringWriter writer = new StringWriter(2048); - protected final Factory factory; protected int _id; @@ -105,15 +100,6 @@ public abstract class BattleCommon implements Battle map.turnDone(); } - @Override - public void load(Marshal.Mode mode, String payload) - { - JsonValue root = new JsonReader().parse(payload); - load(mode, root); - this.currentPlayer = players[0]; - } - - @Override public void load(Marshal.Mode mode, JsonValue value) { if((mode == Marshal.Mode.FULL) || (mode == Marshal.Mode.STATE)) { @@ -122,17 +108,7 @@ public abstract class BattleCommon implements Battle map.loadPlayers(value, players); } map.load(mode, value); - } - - @Override - public String unload(Marshal.Mode mode) - { - Json json = new Json(OutputType.json); - writer.getBuffer().setLength(0); - json.setWriter(writer); - unload(mode, json); - writer.flush(); - return writer.toString(); + this.currentPlayer = players[0]; } @Override diff --git a/core/src/ch/asynk/rustanddust/game/ctrl/Solo.java b/core/src/ch/asynk/rustanddust/game/ctrl/Solo.java index 5473cd5..d9f1457 100644 --- a/core/src/ch/asynk/rustanddust/game/ctrl/Solo.java +++ b/core/src/ch/asynk/rustanddust/game/ctrl/Solo.java @@ -28,8 +28,8 @@ public class Solo extends Ctrl } else { GameRecord r = game.db.loadGame(gameId); if (r != null) { - battle.load(Marshal.Mode.STATE, r.state); - battle.load(Marshal.Mode.ORDERS, r.orders); + load(Marshal.Mode.STATE, r.state); + load(Marshal.Mode.ORDERS, r.orders); battle.getMap().clearMarshalUnits(); r.dispose(); } else @@ -61,12 +61,12 @@ public class Solo extends Ctrl private void storeState() { - game.db.storeGameState(gameId, battle.getTurnCount(), battle.getPlayer().id, battle.unload(Marshal.Mode.STATE)); + game.db.storeGameState(gameId, battle.getTurnCount(), battle.getPlayer().id, unload(Marshal.Mode.STATE)); } private void storeOrders() { - game.db.storeGameOrders(gameId, battle.getTurnCount(), battle.getPlayer().id, battle.unload(Marshal.Mode.ORDERS)); + game.db.storeGameOrders(gameId, battle.getTurnCount(), battle.getPlayer().id, unload(Marshal.Mode.ORDERS)); } private void storeTurn() -- cgit v1.1-2-g2b99