diff options
author | Jérémy Zurcher <jeremy@asynk.ch> | 2016-03-21 13:56:40 +0100 |
---|---|---|
committer | Jérémy Zurcher <jeremy@asynk.ch> | 2016-03-21 13:56:40 +0100 |
commit | a3ad4c8f21ef37bd0c0c6c5989f5b7d43b78a7fa (patch) | |
tree | 59ca1d79d838370d146d7543aa28bcce04a55e42 | |
parent | 677813e7fd80c3f9e3fd9b6db15046c1eea97b29 (diff) | |
download | RustAndDust-a3ad4c8f21ef37bd0c0c6c5989f5b7d43b78a7fa.zip RustAndDust-a3ad4c8f21ef37bd0c0c6c5989f5b7d43b78a7fa.tar.gz |
Battle,Map5Marshal: implements util/Marshal
4 files changed, 83 insertions, 55 deletions
diff --git a/core/src/ch/asynk/rustanddust/game/Battle.java b/core/src/ch/asynk/rustanddust/game/Battle.java index a21e5a4..a545573 100644 --- a/core/src/ch/asynk/rustanddust/game/Battle.java +++ b/core/src/ch/asynk/rustanddust/game/Battle.java @@ -1,9 +1,10 @@ package ch.asynk.rustanddust.game; import ch.asynk.rustanddust.ui.Position; +import ch.asynk.rustanddust.util.Marshal; import ch.asynk.rustanddust.game.Factory.MapType; -public interface Battle +public interface Battle extends Marshal { public int getId(); @@ -39,7 +40,7 @@ public interface Battle public State.StateType getState(); - public void load(int turn, String payload); + public String unload(Marshal.Mode mode); - public String unload(boolean full); + public void load(Marshal.Mode mode, int turn, String payload); } diff --git a/core/src/ch/asynk/rustanddust/game/battles/BattleCommon.java b/core/src/ch/asynk/rustanddust/game/battles/BattleCommon.java index 9fe66f8..e0afc5f 100644 --- a/core/src/ch/asynk/rustanddust/game/battles/BattleCommon.java +++ b/core/src/ch/asynk/rustanddust/game/battles/BattleCommon.java @@ -8,6 +8,7 @@ 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; import ch.asynk.rustanddust.game.Player; import ch.asynk.rustanddust.game.State; @@ -105,26 +106,44 @@ public abstract class BattleCommon implements Battle } @Override - public void load(int turn, String payload) + public void load(Marshal.Mode mode, int turn, String payload) { this.turnCount = turn; JsonValue root = new JsonReader().parse(payload); - map.load(root, players); + load(mode, root); this.currentPlayer = players[0]; } @Override - public String unload(boolean full) + public void load(Marshal.Mode mode, JsonValue value) + { + if((mode == Marshal.Mode.FULL) || (mode == Marshal.Mode.STATE)) + 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); - map.unload(json, full, getPlayer(), getOpponent()); + unload(mode, json); writer.flush(); return writer.toString(); } @Override + public void unload(Marshal.Mode mode, Json json) + { + json.writeObjectStart(); + if((mode == Marshal.Mode.FULL) || (mode == Marshal.Mode.STATE)) + map.unloadPlayers(json, getPlayer(), getOpponent()); + map.unload(mode, json); + json.writeObjectEnd(); + } + + @Override public boolean actionDone() { boolean burn = (map.unitsActivatedSize() > 0); diff --git a/core/src/ch/asynk/rustanddust/game/ctrl/Solo.java b/core/src/ch/asynk/rustanddust/game/ctrl/Solo.java index 2af04d1..77bcc23 100644 --- a/core/src/ch/asynk/rustanddust/game/ctrl/Solo.java +++ b/core/src/ch/asynk/rustanddust/game/ctrl/Solo.java @@ -1,6 +1,7 @@ package ch.asynk.rustanddust.game.ctrl; import ch.asynk.rustanddust.RustAndDust; +import ch.asynk.rustanddust.util.Marshal; import ch.asynk.rustanddust.util.GameRecord; import ch.asynk.rustanddust.game.Ctrl; @@ -26,7 +27,7 @@ public class Solo extends Ctrl battle.initialDeployment(); } else { GameRecord r = game.db.loadGame(gameId); - battle.load(r.turn, r.payload); + battle.load(Marshal.Mode.FULL, r.turn, r.payload); r.dispose(); } } @@ -52,7 +53,7 @@ public class Solo extends Ctrl private void storeGame() { - game.db.storeGame(gameId, battle.getTurnCount(), battle.getPlayer().id, battle.unload(true)); + game.db.storeGame(gameId, battle.getTurnCount(), battle.getPlayer().id, battle.unload(Marshal.Mode.FULL)); } private void storeTurn() diff --git a/core/src/ch/asynk/rustanddust/game/map/Map5Marshal.java b/core/src/ch/asynk/rustanddust/game/map/Map5Marshal.java index 6e2fd1e..b6f9a18 100644 --- a/core/src/ch/asynk/rustanddust/game/map/Map5Marshal.java +++ b/core/src/ch/asynk/rustanddust/game/map/Map5Marshal.java @@ -11,6 +11,7 @@ import ch.asynk.rustanddust.engine.Orientation; import ch.asynk.rustanddust.engine.SelectedTile; import ch.asynk.rustanddust.RustAndDust; +import ch.asynk.rustanddust.util.Marshal; import ch.asynk.rustanddust.game.Map; import ch.asynk.rustanddust.game.Hex; import ch.asynk.rustanddust.game.Zone; @@ -22,7 +23,7 @@ import ch.asynk.rustanddust.game.Order; import ch.asynk.rustanddust.game.OrderList; import ch.asynk.rustanddust.game.Engagement; -public abstract class Map5Marshal extends Map4Orders +public abstract class Map5Marshal extends Map4Orders implements Marshal { private static UnitList units = new UnitList(30); @@ -31,26 +32,24 @@ public abstract class Map5Marshal extends Map4Orders super(game, map, hex); } - public void unload(Json json, boolean full, Player player, Player opponent) + @Override + public void unload(Marshal.Mode mode, Json json) { - json.writeObjectStart(); - if (full) { - json.writeObjectStart("map"); - unload(json); - json.writeObjectEnd(); - json.writeArrayStart("players"); - unload(json, player); - unload(json, opponent); - json.writeArrayEnd(); - } - json.writeArrayStart("orders"); - unload(json, orders); + if((mode == Marshal.Mode.FULL) || (mode == Marshal.Mode.STATE)) + unloadMap(json); + else if(mode == Marshal.Mode.ORDERS) + unloadOrders(json, orders); + } + + public void unloadPlayers(Json json, Player a, Player b) + { + json.writeArrayStart("players"); + unloadPlayer(json, a); + unloadPlayer(json, b); json.writeArrayEnd(); - json.writeObjectEnd(); } - // player - private void unload(Json json, Player player) + private void unloadPlayer(Json json, Player player) { json.writeObjectStart(); json.writeValue("id", player.id); @@ -64,23 +63,21 @@ public abstract class Map5Marshal extends Map4Orders json.writeValue(player.engagementWon); json.writeValue(player.engagementLost); json.writeArrayEnd(); - unload(json, "Us", player.units, true); - unload(json, "Cs", player.casualties, false); - unload(json, "Rs", player.reinforcement, false); - unload(json, "Ws", player.withdrawed, false); + unloadUnits(json, "Us", player.units, true); + unloadUnits(json, "Cs", player.casualties, false); + unloadUnits(json, "Rs", player.reinforcement, false); + unloadUnits(json, "Ws", player.withdrawed, false); json.writeObjectEnd(); } - // units - private void unload(Json json, String name, UnitList units, boolean pos) + private void unloadUnits(Json json, String name, UnitList units, boolean pos) { json.writeArrayStart(name); - for (Unit u : units) unload(json, u, pos); + for (Unit u : units) unloadUnit(json, u, pos); json.writeArrayEnd(); } - // unit - private void unload(Json json, Unit unit, boolean pos) + private void unloadUnit(Json json, Unit unit, boolean pos) { json.writeObjectStart(); json.writeValue("id", unit.id); @@ -104,9 +101,9 @@ public abstract class Map5Marshal extends Map4Orders json.writeObjectEnd(); } - // map - private void unload(Json json) + private void unloadMap(Json json) { + json.writeObjectStart("map"); json.writeArrayStart("o"); for (Hex h : objectives) { json.writeObjectStart(); @@ -150,31 +147,32 @@ public abstract class Map5Marshal extends Map4Orders json.writeObjectEnd(); } json.writeArrayEnd(); + json.writeObjectEnd(); } - // orders - private void unload(Json json, OrderList orders) + private void unloadOrders(Json json, OrderList orders) { + json.writeArrayStart("orders"); for (Order o : orders) { json.writeObjectStart(); json.writeValue("type", o.type); switch(o.type) { case MOVE: - unload(json, o.move); + unloadMoveOrder(json, o.move); break; case ENGAGE: - unload(json, o.engagement); + unloadEngageOrder(json, o.engagement); break; case PROMOTE: - json.writeValue("id", o.unit.id); + unloadPromoteOrder(json, o.unit); break; } json.writeObjectEnd(); } + json.writeArrayEnd(); } - // move - private void unload(Json json, Move m) + private void unloadMoveOrder(Json json, Move m) { json.writeValue("type", m.type); json.writeValue("id", ((Unit) m.pawn).id); @@ -203,8 +201,7 @@ public abstract class Map5Marshal extends Map4Orders } } - // engagement - private void unload(Json json, Engagement e) + private void unloadEngageOrder(Json json, Engagement e) { json.writeArrayStart("units"); json.writeValue(e.attacker.id); @@ -232,13 +229,17 @@ public abstract class Map5Marshal extends Map4Orders json.writeArrayEnd(); } - private void unload(Json json, String key, Unit u) + private void unloadPromoteOrder(Json json, Unit u) + { + json.writeValue("id", u.id); + } + + private void unloadUnit(Json json, String key, Unit u) { - unload(json, key, u, true); + unloadUnit(json, key, u, true); } - // unit - private void unload(Json json, String key, Unit u, boolean pos) + private void unloadUnit(Json json, String key, Unit u, boolean pos) { if (key != null) json.writeObjectStart(key); else json.writeObjectStart(); @@ -253,15 +254,21 @@ public abstract class Map5Marshal extends Map4Orders json.writeObjectEnd(); } - // LOAD - public void load(JsonValue v, Player[] players) + @Override + public void load(Marshal.Mode mode, JsonValue v) + { + if((mode == Marshal.Mode.FULL) || (mode == Marshal.Mode.STATE)) + loadMap(v.get("map")); + else if(mode == Marshal.Mode.ORDERS) + loadOrders(v.get("orders")); + units.clear(); + } + + public void loadPlayers(JsonValue v, Player[] players) { units.clear(); - loadMap(v.get("map")); players[0] = loadPlayer(v.get("players").get(0)); players[1] = loadPlayer(v.get("players").get(1)); - loadOrders(v.get("orders")); - units.clear(); } private Player loadPlayer(JsonValue v) |