diff options
Diffstat (limited to 'core/src/ch/asynk/rustanddust/game')
-rw-r--r-- | core/src/ch/asynk/rustanddust/game/Map.java | 4 | ||||
-rw-r--r-- | core/src/ch/asynk/rustanddust/game/map/Map4Orders.java | 9 | ||||
-rw-r--r-- | core/src/ch/asynk/rustanddust/game/map/Map5Marshal.java | 208 |
3 files changed, 212 insertions, 9 deletions
diff --git a/core/src/ch/asynk/rustanddust/game/Map.java b/core/src/ch/asynk/rustanddust/game/Map.java index 87030b3..27661f2 100644 --- a/core/src/ch/asynk/rustanddust/game/Map.java +++ b/core/src/ch/asynk/rustanddust/game/Map.java @@ -8,9 +8,9 @@ import ch.asynk.rustanddust.engine.SelectedTile; import ch.asynk.rustanddust.engine.Meteorology; import ch.asynk.rustanddust.game.Unit; import ch.asynk.rustanddust.game.Engagement; -import ch.asynk.rustanddust.game.map.Map4Orders; +import ch.asynk.rustanddust.game.map.Map5Marshal; -public abstract class Map extends Map4Orders +public abstract class Map extends Map5Marshal { protected final Meteorology meteorology; diff --git a/core/src/ch/asynk/rustanddust/game/map/Map4Orders.java b/core/src/ch/asynk/rustanddust/game/map/Map4Orders.java index eaa4b1d..8cbaf89 100644 --- a/core/src/ch/asynk/rustanddust/game/map/Map4Orders.java +++ b/core/src/ch/asynk/rustanddust/game/map/Map4Orders.java @@ -15,8 +15,8 @@ import ch.asynk.rustanddust.game.Battle; public abstract class Map4Orders extends Map3Animations { - private final Battle battle; - private final OrderList orders; + protected final Battle battle; + protected final OrderList orders; protected abstract int engagementCost(Engagement e); protected abstract void resolveEngagement(Engagement e); @@ -41,11 +41,6 @@ public abstract class Map4Orders extends Map3Animations protected int ordersSize() { return orders.size(); } protected void ordersClear() { orders.dispose(); } - public String toJson() - { - return orders.toJson(battle.getPlayer()); - } - // STATES ENTRY -> public void showOnBoard(final Unit unit, Hex to, Orientation o) diff --git a/core/src/ch/asynk/rustanddust/game/map/Map5Marshal.java b/core/src/ch/asynk/rustanddust/game/map/Map5Marshal.java new file mode 100644 index 0000000..e81d02f --- /dev/null +++ b/core/src/ch/asynk/rustanddust/game/map/Map5Marshal.java @@ -0,0 +1,208 @@ +package ch.asynk.rustanddust.game.map; + +import java.io.StringWriter; + +import com.badlogic.gdx.graphics.Texture; +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.engine.Move; +import ch.asynk.rustanddust.engine.Tile; +import ch.asynk.rustanddust.engine.SelectedTile; + +import ch.asynk.rustanddust.RustAndDust; +import ch.asynk.rustanddust.game.Hex; +import ch.asynk.rustanddust.game.Battle; +import ch.asynk.rustanddust.game.Player; +import ch.asynk.rustanddust.game.Unit; +import ch.asynk.rustanddust.game.UnitList; +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 Map5Marshal(final RustAndDust game, Texture map, SelectedTile hex) + { + super(game, map, hex); + } + + public String unload(Player player, Player opponent) + { + Json json = new Json(OutputType.json); + StringWriter buffer = new StringWriter(2048); + json.setWriter(buffer); + + json.writeObjectStart(); + json.writeArrayStart("players"); + unload(json, player); + unload(json, opponent); + json.writeArrayEnd(); + json.writeArrayStart("map"); + unload(json); + json.writeArrayEnd(); + json.writeArrayStart("orders"); + unload(json, orders); + json.writeArrayEnd(); + json.writeObjectEnd(); + + buffer.flush(); + System.err.println(buffer.toString()); + return buffer.toString(); + } + + private void unload(Json json, Player player) + { + json.writeObjectStart(); + json.writeValue("id", player.getId()); + json.writeValue("a", player.army); + json.writeArrayStart("v"); + json.writeValue(player.getTurn()); + json.writeValue(player.getAp()); + json.writeValue(player.getApSpent()); + json.writeValue(player.actionCount); + json.writeValue(player.objectivesWon); + 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); + json.writeObjectEnd(); + } + + private void unload(Json json, String name, UnitList units, boolean pos) + { + json.writeArrayStart(name); + for (Unit u : units) unload(json, u, pos); + json.writeArrayEnd(); + } + + private void unload(Json json, Unit unit, boolean pos) + { + json.writeObjectStart(); + json.writeValue("id", unit.id); + json.writeArrayStart("v"); + json.writeValue(unit.hq); + json.writeValue(unit.ace); + json.writeValue(unit.hasMoved); + json.writeValue(unit.hasFired); + json.writeArrayEnd(); + if (pos) { + Hex h = unit.getHex(); + json.writeArrayStart("p"); + json.writeValue(h.getCol()); + json.writeValue(h.getRow()); + json.writeValue(unit.getRotation()); + json.writeArrayEnd(); + } + json.writeObjectEnd(); + } + + private void unload(Json json) + { + json.writeObjectStart(); + json.writeArrayStart("o"); + for (Hex h : objectives) { + json.writeObjectStart(); + json.writeValue("army", h.belongsTo()); + json.writeArrayStart("p"); + json.writeValue(h.getCol()); + json.writeValue(h.getRow()); + json.writeArrayEnd(); + json.writeObjectEnd(); + } + json.writeArrayEnd(); + json.writeObjectEnd(); + } + + private void unload(Json json, OrderList orders) + { + for (Order o : orders) { + json.writeObjectStart(); + json.writeValue("type", o.type); + switch(o.type) { + case MOVE: + unload(json, o.move); + break; + case ENGAGE: + unload(json, o.engagement); + break; + case PROMOTE: + json.writeValue("id", o.unit.id); + json.writeArrayStart("p"); + json.writeValue(o.unitHex.getCol()); + json.writeValue(o.unitHex.getRow()); + json.writeArrayEnd(); + break; + } + json.writeObjectEnd(); + } + } + + private void unload(Json json, Move m) + { + json.writeValue("type", m.type); + json.writeValue("id", ((Unit) m.pawn).id); + if (m.from != null) { + json.writeArrayStart("from"); + json.writeValue(m.from.getCol()); + json.writeValue(m.from.getRow()); + json.writeArrayEnd(); + } + if (m.to != null) { + json.writeArrayStart("to"); + json.writeValue(m.to.getCol()); + json.writeValue(m.to.getRow()); + json.writeValue(m.orientation.r()); + json.writeArrayEnd(); + } + if (m.tiles.size() > 0) { + json.writeArrayStart("path"); + for (Tile t : m.tiles) { + json.writeArrayStart(); + json.writeValue(t.getCol()); + json.writeValue(t.getRow()); + json.writeArrayEnd(); + } + json.writeArrayEnd(); + } + } + + private void unload(Json json, Engagement e) + { + unload(json, "atk", e.attacker); + unload(json, "def", e.defender); + json.writeArrayStart("assists"); + for (Unit u : e.assists) + unload(json, null, u); + json.writeArrayEnd(); + json.writeArrayStart("dice"); + json.writeValue(e.d1); + json.writeValue(e.d2); + json.writeValue(e.d3); + json.writeValue(e.d4); + json.writeArrayEnd(); + json.writeArrayStart("res"); + json.writeValue(e.success); + json.writeValue(e.attackSum); + json.writeValue(e.defenseSum); + json.writeArrayEnd(); + } + + private void unload(Json json, String key, Unit u) + { + if (key != null) json.writeObjectStart(key); + else json.writeObjectStart(); + json.writeValue("id", u.id); + Hex h = u.getHex(); + json.writeArrayStart("p"); + json.writeValue(h.getCol()); + json.writeValue(h.getRow()); + json.writeArrayEnd(); + json.writeObjectEnd(); + } +} |