summaryrefslogtreecommitdiffstats
path: root/core/src/ch/asynk/rustanddust/util/DB.java
diff options
context:
space:
mode:
authorJérémy Zurcher <jeremy@asynk.ch>2016-03-14 15:55:11 +0100
committerJérémy Zurcher <jeremy@asynk.ch>2016-03-14 15:55:11 +0100
commit3e969f1d7a9966a40657b1ae07b949cde112525b (patch)
tree96d74c5ce15d8fb1093e61236b75b69bc4f486ea /core/src/ch/asynk/rustanddust/util/DB.java
parentc3111281e08da0e7e58032f42751a6b1448dbba8 (diff)
downloadRustAndDust-3e969f1d7a9966a40657b1ae07b949cde112525b.zip
RustAndDust-3e969f1d7a9966a40657b1ae07b949cde112525b.tar.gz
DB/BattleCommon: add turnCount
Diffstat (limited to 'core/src/ch/asynk/rustanddust/util/DB.java')
-rw-r--r--core/src/ch/asynk/rustanddust/util/DB.java50
1 files changed, 29 insertions, 21 deletions
diff --git a/core/src/ch/asynk/rustanddust/util/DB.java b/core/src/ch/asynk/rustanddust/util/DB.java
index 8de4747..44cb79c 100644
--- a/core/src/ch/asynk/rustanddust/util/DB.java
+++ b/core/src/ch/asynk/rustanddust/util/DB.java
@@ -40,7 +40,7 @@ public class DB
private static final String TBL_GAMES_CRT = "create table if not exists"
+ " games ( _id integer primary key autoincrement"
+ " ,opponent integer not null, battle integer not null, mode integer not null"
- + " ,ts datetime default current_timestamp"
+ + " ,turn integer not null, ts datetime default current_timestamp"
+ " ,player integer default null, hash text default null, payload text default null"
+ " ,foreign key (opponent) references players(_id)"
+ " ,foreign key (battle) references battles(_id)"
@@ -50,10 +50,11 @@ public class DB
private static final String TBL_TURNS_CRT = "create table if not exists"
+ " turns ( _id integer primary key autoincrement"
- + " ,game integer not null, player integer not null"
+ + " ,game integer not null, turn integer not null, player integer not null"
+ " ,hash text not null, payload text not null"
+ " ,foreign key (game) references games(_id)"
+ " ,foreign key (player) references players(_id)"
+ + " unique (game, turn)"
+ ");";
private static final String FEED_CONFIG = " insert or ignore into config values(\"version\", " + DB_SCHEMA_VERSION + ");";
@@ -202,7 +203,7 @@ public class DB
} catch (SQLiteGdxException e) { RustAndDust.error("storeBattle"); }
}
- private static final String INSERT_GAME = "insert or ignore into games(opponent,battle,mode) values (%d,%d,%d);";
+ private static final String INSERT_GAME = "insert or ignore into games(opponent,battle,mode,turn) values (%d,%d,%d,0);";
public void storeGame(int opponent, int battle, int mode)
{
@@ -232,14 +233,14 @@ public class DB
return getGameId(opponent, battle, mode);
}
- private static final String INSERT_TURN = "insert into turns(game,player,hash,payload) values (%d,%d,'%s','%s');";
+ private static final String INSERT_TURN = "insert into turns(game,turn,player,hash,payload) values (%d,%d,%d,'%s','%s');";
- public boolean storeTurn(int game, int player, String payload)
+ public boolean storeTurn(int game, int turn, int player, String payload)
{
try {
String hash = getDigest(payload);
if (hash == null) return false;
- exec(String.format(INSERT_TURN, game, player, hash, payload));
+ exec(String.format(INSERT_TURN, game, turn, player, hash, payload));
} catch (SQLiteGdxException e) {
RustAndDust.error("storeTurn");
return false;
@@ -270,15 +271,15 @@ public class DB
return ret;
}
- private static final String UPDATE_GAME = "update games set ts=current_timestamp, player=%d, hash='%s', payload='%s' where _id=%d;";
+ private static final String UPDATE_GAME = "update games set ts=current_timestamp, turn=%d, player=%d, hash='%s', payload='%s' where _id=%d;";
- public boolean storeGame(int game, int player, String payload)
+ public boolean storeGame(int game, int turn, int player, String payload)
{
RustAndDust.debug("storeGame");
try {
String hash = getDigest(payload);
if (hash == null) return false;
- exec(String.format(UPDATE_GAME, player, hash, payload, game));
+ exec(String.format(UPDATE_GAME, turn, player, hash, payload, game));
} catch (SQLiteGdxException e) {
RustAndDust.error("storeGame");
return false;
@@ -286,17 +287,23 @@ public class DB
return true;
}
- private static final String LOAD_GAME = "select g.payload from games g players g(where g._id=%d;";
+ private static final String LOAD_GAME = "select g._id, g.opponent, g.battle, g.mode, g.turn, g.ts, g.player, '', '', g.hash, g.payload"
+ + " from games g where g._id=%d;";
- public String loadGame(int game)
+ public GameRecord loadGame(int game)
{
RustAndDust.debug("loadGame");
- String r = null;
+ GameRecord r = null;
try {
DatabaseCursor cursor = query(String.format(LOAD_GAME, game));
if (cursor.getCount() > 0) {
cursor.next();
- r = cursor.getString(0);
+ r = from(cursor);
+ if (!r.hash.equals(getDigest(r.payload))) {
+ // TODO recover from that big shit
+ RustAndDust.error(String.format("corrupted game %d", game));
+ r = null;
+ }
}
} catch (SQLiteGdxException e) { RustAndDust.error("loadGame"); }
return r;
@@ -319,7 +326,7 @@ public class DB
}
- private static final String GET_GAMES = "select g._id, g.opponent, g.battle, g.mode, g.ts, g.player, p.name, b.name, null, null"
+ private static final String GET_GAMES = "select g._id, g.opponent, g.battle, g.mode, g.turn, g.ts, g.player, p.name, b.name, null, null"
+ " from games g inner join players p on (p._id=g.opponent) inner join battles b on (b._id=g.battle);";
public void loadGames()
@@ -347,16 +354,17 @@ public class DB
r.opponent = cursor.getInt(1);
r.battle = cursor.getInt(2);
r.mode = GameMode.from(cursor.getInt(3));
- try { r.ts = df.parse(cursor.getString(4)); }
+ r.turn = cursor.getInt(4);
+ try { r.ts = df.parse(cursor.getString(5)); }
catch (java.text.ParseException e) {
r.ts = null;
- RustAndDust.error(String.format("can't parse", cursor.getString(4)));
+ RustAndDust.error(String.format("can't parse", cursor.getString(5)));
}
- r.currentPlayer = cursor.getInt(5);
- r.oName = cursor.getString(6);
- r.bName = cursor.getString(7);
- r.hash = cursor.getString(8);
- r.payload = cursor.getString(9);
+ r.currentPlayer = cursor.getInt(6);
+ r.oName = cursor.getString(7);
+ r.bName = cursor.getString(8);
+ r.hash = cursor.getString(9);
+ r.payload = cursor.getString(10);
} catch (Exception e) {
r.dispose(); RustAndDust.error("GameRecord from cursor");
}