summaryrefslogtreecommitdiffstats
path: root/core/src/ch/asynk/rustanddust
diff options
context:
space:
mode:
Diffstat (limited to 'core/src/ch/asynk/rustanddust')
-rw-r--r--core/src/ch/asynk/rustanddust/game/Battle.java4
-rw-r--r--core/src/ch/asynk/rustanddust/game/battles/BattleCommon.java15
-rw-r--r--core/src/ch/asynk/rustanddust/game/ctrl/Solo.java9
-rw-r--r--core/src/ch/asynk/rustanddust/util/DB.java50
-rw-r--r--core/src/ch/asynk/rustanddust/util/GameRecord.java1
5 files changed, 52 insertions, 27 deletions
diff --git a/core/src/ch/asynk/rustanddust/game/Battle.java b/core/src/ch/asynk/rustanddust/game/Battle.java
index c8063d1..a21e5a4 100644
--- a/core/src/ch/asynk/rustanddust/game/Battle.java
+++ b/core/src/ch/asynk/rustanddust/game/Battle.java
@@ -17,6 +17,8 @@ public interface Battle
public Position getHudPosition();
+ public int getTurnCount();
+
public Player getPlayer();
public Player getOpponent();
@@ -37,7 +39,7 @@ public interface Battle
public State.StateType getState();
- public void load(String payload);
+ public void load(int turn, String payload);
public String unload(boolean full);
}
diff --git a/core/src/ch/asynk/rustanddust/game/battles/BattleCommon.java b/core/src/ch/asynk/rustanddust/game/battles/BattleCommon.java
index 2a4f766..50bb1bd 100644
--- a/core/src/ch/asynk/rustanddust/game/battles/BattleCommon.java
+++ b/core/src/ch/asynk/rustanddust/game/battles/BattleCommon.java
@@ -22,6 +22,7 @@ public abstract class BattleCommon implements Battle
protected final Factory factory;
protected int _id;
+ protected int turnCount;
protected Factory.MapType mapType;
protected String name;
protected String description;
@@ -67,6 +68,7 @@ public abstract class BattleCommon implements Battle
{
this.map = factory.getMap(getMapType());
setup();
+ this.turnCount = 0;
this.currentPlayer = players[0];
}
@@ -74,6 +76,7 @@ public abstract class BattleCommon implements Battle
public void desinit()
{
this.map = null;
+ this.turnCount = 0;
this.players[0] = null;
this.players[1] = null;
this.currentPlayer = null;
@@ -95,8 +98,9 @@ public abstract class BattleCommon implements Battle
}
@Override
- public void load(String payload)
+ public void load(int turn, String payload)
{
+ this.turnCount = turn;
map.load(payload, players);
this.currentPlayer = players[0];
}
@@ -130,13 +134,14 @@ public abstract class BattleCommon implements Battle
currentPlayer = getOpponent();
currentPlayer.turnStart(getActionPoints());
}
+ turnCount += 1;
map.turnDone();
return ret;
}
protected boolean turnDoneForBoth()
{
- return ((currentPlayer.getTurn() > 0) && (currentPlayer.getTurn() == getOpponent().getTurn()));
+ return ((turnCount > 0) && ((turnCount % 2) == 0));
}
protected Player getWinner(int minTurns)
@@ -167,6 +172,12 @@ public abstract class BattleCommon implements Battle
}
@Override
+ public int getTurnCount()
+ {
+ return turnCount;
+ }
+
+ @Override
public Player getPlayer()
{
return currentPlayer;
diff --git a/core/src/ch/asynk/rustanddust/game/ctrl/Solo.java b/core/src/ch/asynk/rustanddust/game/ctrl/Solo.java
index 51a7172..a2c0600 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.GameRecord;
import ch.asynk.rustanddust.game.Ctrl;
import ch.asynk.rustanddust.game.Battle;
@@ -25,7 +26,9 @@ public class Solo extends Ctrl
battle.getOpponent().id = other;
battle.initialDeployment();
} else {
- battle.load(game.db.loadGame(gameId));
+ GameRecord r = game.db.loadGame(gameId);
+ battle.load(r.turn, r.payload);
+ r.dispose();
}
}
@@ -44,11 +47,11 @@ public class Solo extends Ctrl
private void storeGame()
{
- game.db.storeGame(gameId, battle.getPlayer().id, battle.unload(true));
+ game.db.storeGame(gameId, battle.getTurnCount(), battle.getPlayer().id, battle.unload(true));
}
private void storeTurn()
{
- game.db.storeTurn(gameId, battle.getPlayer().id, battle.unload(false));
+ game.db.storeTurn(gameId, battle.getTurnCount(), battle.getPlayer().id, battle.unload(false));
}
}
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");
}
diff --git a/core/src/ch/asynk/rustanddust/util/GameRecord.java b/core/src/ch/asynk/rustanddust/util/GameRecord.java
index a17e967..8a1a4eb 100644
--- a/core/src/ch/asynk/rustanddust/util/GameRecord.java
+++ b/core/src/ch/asynk/rustanddust/util/GameRecord.java
@@ -16,6 +16,7 @@ public class GameRecord implements List.ListElement, Disposable, Pool.Poolable
public int id;
public int opponent;
public int battle;
+ public int turn;
public GameMode mode;
public Date ts;
public int currentPlayer;