summaryrefslogtreecommitdiffstats
path: root/core
diff options
context:
space:
mode:
authorJérémy Zurcher <jeremy@asynk.ch>2016-05-10 13:49:02 +0200
committerJérémy Zurcher <jeremy@asynk.ch>2016-05-10 13:49:02 +0200
commite1410b3d1c31cae419082df57a148e120aa4142c (patch)
treef080fdd6dfb1290185ea4b2b71b3830ad87d3ae8 /core
parent0c365d1936272ac0b5100ae5f27641c05051e496 (diff)
downloadRustAndDust-e1410b3d1c31cae419082df57a148e120aa4142c.zip
RustAndDust-e1410b3d1c31cae419082df57a148e120aa4142c.tar.gz
DB,Ctrl: clean up store/load validate methods
Diffstat (limited to 'core')
-rw-r--r--core/src/ch/asynk/rustanddust/game/Ctrl.java57
-rw-r--r--core/src/ch/asynk/rustanddust/util/DB.java56
2 files changed, 95 insertions, 18 deletions
diff --git a/core/src/ch/asynk/rustanddust/game/Ctrl.java b/core/src/ch/asynk/rustanddust/game/Ctrl.java
index 11f706c..5aaada0 100644
--- a/core/src/ch/asynk/rustanddust/game/Ctrl.java
+++ b/core/src/ch/asynk/rustanddust/game/Ctrl.java
@@ -13,6 +13,7 @@ import ch.asynk.rustanddust.engine.util.IterableQueue;
import ch.asynk.rustanddust.engine.util.IterableStack;
import ch.asynk.rustanddust.ui.Position;
import ch.asynk.rustanddust.util.Marshal;
+import ch.asynk.rustanddust.util.GameRecord;
import ch.asynk.rustanddust.game.ctrl.Solo;
import ch.asynk.rustanddust.game.State.StateType;
import ch.asynk.rustanddust.game.states.StateCommon;
@@ -205,24 +206,68 @@ public abstract class Ctrl implements Disposable
}
// DB
- private void storeState()
+ private void storeInitialState()
{
game.db.storeGameState(gameId, battle.getTurnCount(), battle.getPlayer().id, unload(Marshal.Mode.PLAYERS), unload(Marshal.Mode.MAP));
+ game.db.storeTurnState(gameId);
}
- private void storeOrders()
+ private void storeGameState()
{
- game.db.storeGameOrders(gameId, battle.getTurnCount(), battle.getPlayer().id, unload(Marshal.Mode.ORDERS));
+ game.db.storeGameState(gameId, battle.getTurnCount(), battle.getPlayer().id, unload(Marshal.Mode.PLAYERS), unload(Marshal.Mode.MAP));
+ }
+
+ private void storeGameOrders()
+ {
+ game.db.storeGameOrders(gameId, unload(Marshal.Mode.ORDERS));
}
- private void clearOrders()
+ private void storeTurnOrders()
{
+ game.db.storeTurnOrders(gameId, battle.getTurnCount(), unload(Marshal.Mode.ORDERS));
+ }
+
+ private void storeNewGameTurn()
+ {
+ game.db.storeGameState(gameId, battle.getTurnCount(), battle.getPlayer().id, unload(Marshal.Mode.PLAYERS), unload(Marshal.Mode.MAP));
game.db.clearGameOrders(gameId);
+ game.db.storeTurnState(gameId);
+ map.clear(true);
+ }
+
+ private boolean loadTurn(int turn)
+ {
+ GameRecord r = game.db.loadTurn(gameId, turn);
+ if (r == null)
+ return false;
+ battle.turnDone();
+ map.clear(true);
+
+ load(Marshal.Mode.PLAYER, r.players);
+ validateState(r);
+ load(Marshal.Mode.ORDERS, r.orders);
+ r.dispose();
+ return true;
}
- private void storeTurn()
+ private void validateState(GameRecord r)
{
- game.db.storeCurrentTurn(gameId);
+ String playersH = game.db.getDigest(unload(Marshal.Mode.PLAYERS));
+ if (!playersH.equals(r.playersH)) {
+ RustAndDust.error("playersH does not match");
+ System.err.println("DB");
+ System.err.println(r.players);
+ System.err.println("UNLOAD");
+ System.err.println(unload(Marshal.Mode.PLAYERS));
+ }
+ String mapH = game.db.getDigest(unload(Marshal.Mode.MAP));
+ if (!mapH.equals(r.mapH)) {
+ RustAndDust.error("mapH does not match");
+ System.err.println("DB");
+ System.err.println(r.map);
+ System.err.println("UNLOAD");
+ System.err.println(unload(Marshal.Mode.MAP));
+ }
}
// INPUTS
diff --git a/core/src/ch/asynk/rustanddust/util/DB.java b/core/src/ch/asynk/rustanddust/util/DB.java
index 61e7161..f841912 100644
--- a/core/src/ch/asynk/rustanddust/util/DB.java
+++ b/core/src/ch/asynk/rustanddust/util/DB.java
@@ -116,7 +116,7 @@ public class DB
}
}
- private String getDigest(String str)
+ public String getDigest(String str)
{
String hash = null;
try {
@@ -280,15 +280,15 @@ public class DB
return true;
}
- private static final String STORE_GAME_ORDERS = "update games set ts=current_timestamp, turn=%d, currentPlayer=%d, orders='%s', ordersH='%s', synched=0 where _id=%d;";
+ private static final String STORE_GAME_ORDERS = "update games set ts=current_timestamp, orders='%s', ordersH='%s', synched=0 where _id=%d;";
- public boolean storeGameOrders(int game, int turn, int player, String orders)
+ public boolean storeGameOrders(int game, String orders)
{
RustAndDust.debug("storeGameOrders");
try {
String ordersH = getDigest(orders);
if (ordersH == null) return false;
- exec(String.format(STORE_GAME_ORDERS, turn, player, orders, ordersH, game));
+ exec(String.format(STORE_GAME_ORDERS, orders, ordersH, game));
} catch (SQLiteGdxException e) {
RustAndDust.error("storeGameOrders");
return false;
@@ -296,16 +296,33 @@ public class DB
return true;
}
- private static final String COPY_TURN = "insert into turns(game, turn, currentPlayer, players, playersH, map, mapH, orders, ordersH)"
- + " select _id, turn, currentPlayer, players, playersH, map, mapH, orders, ordersH from games where _id=%d;";
+ private static final String STORE_TURN_ORDERS = "update turns set orders=(select orders from games where _id=%d), ordersH=(select ordersH from games where _id=%d)"
+ + " where game=%d and turn =%d;";
- public boolean storeCurrentTurn(int game)
+ public boolean storeTurnOrders(int game, int turn, String orders)
{
- RustAndDust.debug("storeCurrentTurn");
+ RustAndDust.debug("storeTurnOrders");
try {
- exec(String.format(COPY_TURN, game));
+ String ordersH = getDigest(orders);
+ if (ordersH == null) return false;
+ exec(String.format(STORE_TURN_ORDERS, game, game, game, turn));
+ } catch (SQLiteGdxException e) {
+ RustAndDust.error("storeTurnOrders");
+ return false;
+ }
+ return true;
+ }
+
+ private static final String STORE_TURN_STATE = "insert into turns(game, turn, currentPlayer, players, playersH, map, mapH, orders, ordersH)"
+ + " select _id, turn, currentPlayer, players, playersH, map, mapH, null, null from games where _id=%d;";
+
+ public boolean storeTurnState(int game)
+ {
+ RustAndDust.debug("storeTurnState");
+ try {
+ exec(String.format(STORE_TURN_STATE, game));
} catch (SQLiteGdxException e) {
- RustAndDust.error("storeCurrentTurn");
+ RustAndDust.error("storeTurnState");
return false;
}
return true;
@@ -354,7 +371,7 @@ public class DB
private static final String LOAD_LAST_TURN = "select g._id, g.mode, g.battle, g.opponent, g.turn, g.currentPlayer, g.ts, g.synched"
+ ", t.players, t.playersH, t.map, t.mapH, g.orders, g.ordersH, null, null"
- +" from games g inner join turns t on (g._id = t.game) where g._id=%d order by t.turn desc limit 1;";
+ +" from games g inner join turns t on (g._id=t.game and t.turn=g.turn) where g._id=%d;";
public GameRecord loadLastTurn(int game)
{
@@ -362,8 +379,23 @@ public class DB
return loadGame(game, String.format(LOAD_LAST_TURN, game), "loadLastTurn");
}
+ private static final String LOAD_TURN = "select g._id, g.mode, g.battle, g.opponent, t.turn, t.currentPlayer, g.ts, g.synched"
+ + ", t.players, t.playersH, t.map, t.mapH, case when g.turn=t.turn then g.orders else t.orders end, case when g.turn=t.turn then g.ordersH else t.ordersH end, null, null"
+ +" from games g inner join turns t on (g._id = t.game) where g._id=%d and t.turn = %d;";
+
+ public GameRecord loadTurn(int game, int turn)
+ {
+ RustAndDust.debug("loadTurn");
+ return loadGame(game, String.format(LOAD_TURN, game, turn), "loadTurn", false);
+ }
+
private GameRecord loadGame(int game, String sql, String errMsg)
{
+ return loadGame(game, sql, errMsg, true);
+ }
+
+ private GameRecord loadGame(int game, String sql, String errMsg, boolean deleteOnError)
+ {
GameRecord r = null;
try {
DatabaseCursor cursor = query(sql);
@@ -378,7 +410,7 @@ public class DB
r = null;
}
} catch (SQLiteGdxException e) { RustAndDust.error(errMsg); }
- if (r == null)
+ if (deleteOnError && (r == null))
deleteGame(game);
return r;
}