summaryrefslogtreecommitdiffstats
path: root/core/src/ch/asynk
diff options
context:
space:
mode:
authorJérémy Zurcher <jeremy@asynk.ch>2016-03-14 17:04:08 +0100
committerJérémy Zurcher <jeremy@asynk.ch>2016-03-14 17:04:08 +0100
commit74c125481ec63e44b250475068b4d537c20eb8f7 (patch)
tree01a6294ccb469878d0583869860243de705e48ed /core/src/ch/asynk
parenta7087199d26372ef67f568e9ca422982ca92d6fb (diff)
downloadRustAndDust-74c125481ec63e44b250475068b4d537c20eb8f7.zip
RustAndDust-74c125481ec63e44b250475068b4d537c20eb8f7.tar.gz
DB: implement loadTurns(int), uses TurnRecord
Diffstat (limited to 'core/src/ch/asynk')
-rw-r--r--core/src/ch/asynk/rustanddust/util/DB.java45
1 files changed, 29 insertions, 16 deletions
diff --git a/core/src/ch/asynk/rustanddust/util/DB.java b/core/src/ch/asynk/rustanddust/util/DB.java
index 452b512..b733cc0 100644
--- a/core/src/ch/asynk/rustanddust/util/DB.java
+++ b/core/src/ch/asynk/rustanddust/util/DB.java
@@ -250,25 +250,20 @@ public class DB
private static final String GET_TURNS = "select payload from turns where game=%d order by _id;";
- public String getTurns(int game)
+ public void loadTurns(int game)
{
- String ret = null;
+ RustAndDust.debug("loadTurns");
+ TurnRecord.clearList();
try {
- DatabaseCursor cursor = query(String.format(GET_TURNS, game));
- int n = cursor.getCount();
- if (n <= 0)
- return null;
-
- StringBuilder builder = new StringBuilder();
- builder.append("[");
- while(cursor.next()) {
- builder.append(cursor.getString(0));
- builder.append(",");
+ DatabaseCursor cursor = query(GET_GAMES);
+ if (cursor.getCount() > 0) {
+ while(cursor.next()) {
+ TurnRecord r = turnFrom(cursor);
+ if (r != null)
+ TurnRecord.list.add(r);
+ }
}
- builder.setCharAt((builder.length() - 1), ']');
- ret = builder.toString();
- } catch (SQLiteGdxException e) { RustAndDust.error("getTurns"); }
- return ret;
+ } catch (SQLiteGdxException e) { RustAndDust.error("loadTurns"); }
}
private static final String UPDATE_GAME = "update games set ts=current_timestamp, turn=%d, player=%d, hash='%s', payload='%s' where _id=%d;";
@@ -373,6 +368,24 @@ public class DB
return r;
}
+ private TurnRecord turnFrom(DatabaseCursor cursor)
+ {
+ TurnRecord r = TurnRecord.get();
+
+ try {
+ r.id = cursor.getInt(0);
+ r.game = cursor.getInt(1);
+ r.turn = cursor.getInt(2);
+ r.player = cursor.getInt(3);
+ r.hash = cursor.getString(4);
+ r.payload = cursor.getString(5);
+ } catch (Exception e) {
+ r.dispose(); RustAndDust.error("TurnRecord from cursor");
+ }
+
+ return r;
+ }
+
private void exec(String sql) throws SQLiteGdxException
{
if (debug) RustAndDust.debug(" SQL " + sql);