summaryrefslogtreecommitdiff
path: root/tools
diff options
context:
space:
mode:
authorTor Andersson <tor@ccxvii.net>2021-12-20 23:49:55 +0100
committerTor Andersson <tor@ccxvii.net>2021-12-20 23:52:22 +0100
commitdb82084e6c167b5986679bdee237f613f1153f80 (patch)
treeede894c2f6dda16c8a4abf5c3b40f67f3fd8d11b /tools
parent5f358dafca5617966c57e1f62dbe38534bd24499 (diff)
downloadserver-db82084e6c167b5986679bdee237f613f1153f80.tar.gz
Add 'patch' script to fix game state by replaying from the start.
Diffstat (limited to 'tools')
-rw-r--r--tools/patchgame.js32
1 files changed, 32 insertions, 0 deletions
diff --git a/tools/patchgame.js b/tools/patchgame.js
new file mode 100644
index 0000000..467a45f
--- /dev/null
+++ b/tools/patchgame.js
@@ -0,0 +1,32 @@
+#!/usr/bin/env node
+
+const fs = require('fs');
+const sqlite3 = require('better-sqlite3');
+
+if (process.argv.length !== 3) {
+ process.stderr.write("usage: ./tools/patchgame.js <game_id>\n");
+ process.exit(1);
+}
+
+let db = new sqlite3("./db");
+
+let game_id = process.argv[2];
+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");
+let log = db.prepare("select * from game_replay where game_id=?").all(game_id);
+
+let save = db.prepare("select state from game_state where game_id=?").pluck().get(game_id);
+fs.writeFileSync("backup-" + game_id + ".txt", save);
+
+let game = { state: null, active: null }
+log.forEach(item => {
+ 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);
+});
+
+db.prepare("update game_state set active=?, state=? where game_id=?").run(game.active, JSON.stringify(game), game_id);