summaryrefslogtreecommitdiff
path: root/tools
diff options
context:
space:
mode:
Diffstat (limited to 'tools')
-rw-r--r--tools/purge.sh2
-rwxr-xr-xtools/replay.js37
-rw-r--r--tools/rerun.js24
3 files changed, 39 insertions, 24 deletions
diff --git a/tools/purge.sh b/tools/purge.sh
index a47bcdd..8084e03 100644
--- a/tools/purge.sh
+++ b/tools/purge.sh
@@ -12,3 +12,5 @@ sqlite3 db "DELETE FROM games $WHERE_TIMEOUT_OPEN"
echo "--- TIMED OUT ACTIVE GAMES ---"
sqlite3 db "SELECT * FROM game_view $WHERE_TIMEOUT_ACTIVE"
sqlite3 db "UPDATE games SET status = 3 $WHERE_TIMEOUT_ACTIVE"
+
+sqlite3 db "DELETE FROM replay WHERE game_id < 1346"
diff --git a/tools/replay.js b/tools/replay.js
new file mode 100755
index 0000000..af2cde0
--- /dev/null
+++ b/tools/replay.js
@@ -0,0 +1,37 @@
+#!/usr/bin/env node
+
+const sqlite3 = require('better-sqlite3');
+
+if (process.argv.length !== 3) {
+ process.stderr.write("usage: ./tools/replay.js <game_id>\n");
+ process.exit(1);
+}
+
+let game_id = process.argv[2] | 0;
+
+let db = new sqlite3("./db");
+let title_id = db.prepare("SELECT title_id FROM games WHERE game_id = ?").pluck().get(game_id);
+let rules = require("../public/" + title_id + "/rules");
+let log = db.prepare("SELECT * FROM replay WHERE game_id = ?").all(game_id);
+
+let replay = new sqlite3("./replay.db");
+replay.pragma("synchronous = off");
+replay.prepare("create table if not exists replay ( game_id int, time, role, action, arguments, state )").run();
+replay.prepare("delete from replay where game_id = ?").run(game_id);
+let replay_insert = replay.prepare("insert into replay (game_id,time,role,action,arguments,state) VALUES (?,?,?,?,?,?)");
+
+process.stdout.write(`// REPLAY ${title_id} ${game_id}\n`)
+let game = { state: "null", active: "None" }
+log.forEach(item => {
+ process.stdout.write(`${game.state} ${game.active}\n`);
+ process.stdout.write(`\t${item.time} ${item.role} ${item.action} ${item.arguments}\n`);
+ let args = JSON.parse(item.arguments);
+ if (item.action === 'setup')
+ game = rules.setup(args[0], args[1], args[2]);
+ else if (item.action === 'resign')
+ game = rules.resign(game, item.role);
+ else
+ game = rules.action(game, item.role, item.action, args);
+ replay_insert.run(game_id, item.time, item.role, item.action, item.arguments, JSON.stringify(game));
+});
+process.stdout.write(`${game.state} ${game.active}\n`);
diff --git a/tools/rerun.js b/tools/rerun.js
deleted file mode 100644
index d1534e6..0000000
--- a/tools/rerun.js
+++ /dev/null
@@ -1,24 +0,0 @@
-const sqlite3 = require('better-sqlite3');
-
-let db = new sqlite3("./db");
-let game_id = process.argv[2] | 0;
-let title_id = db.prepare("SELECT title_id FROM games WHERE game_id = ?").pluck().get(game_id);
-let rules = require("./public/" + title_id + "/rules.js");
-
-console.log("// TITLE", title_id)
-let log = db.prepare("SELECT * FROM game_log WHERE game_id = ?").all(game_id);
-let game = null;
-log.forEach(item => {
- let args = JSON.parse(item.arguments);
- if (item.action === 'setup') {
- console.log("// SETUP", item.arguments)
- game = rules.setup(args[0], args[1], args[2]);
- } else if (item.action === 'resign') {
- console.log("// RESIGN", item.role);
- game = rules.resign(game, item.role);
- } else {
- console.log("// ACTION", item.role, item.action, item.arguments);
- game = rules.action(game, item.role, item.action, args);
- }
- console.log(JSON.stringify(game));
-});