diff options
author | Tor Andersson <tor@ccxvii.net> | 2022-10-01 22:07:02 +0200 |
---|---|---|
committer | Tor Andersson <tor@ccxvii.net> | 2022-10-05 22:44:58 +0200 |
commit | 2af642fd74f0e7582759eabb26e489bf1ac37c78 (patch) | |
tree | f52322341c965fec5a0c0842af7fe6398c64b00b | |
parent | 78aac1b3a68d732090a6658ae30d12aba7e2eb8f (diff) | |
download | server-2af642fd74f0e7582759eabb26e489bf1ac37c78.tar.gz |
WIP patch
-rw-r--r-- | tools/patchgame.js | 78 |
1 files changed, 54 insertions, 24 deletions
diff --git a/tools/patchgame.js b/tools/patchgame.js index 86c0580..9b63f4f 100644 --- a/tools/patchgame.js +++ b/tools/patchgame.js @@ -1,44 +1,74 @@ #!/usr/bin/env node -const fs = require('fs'); -const sqlite3 = require('better-sqlite3'); +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); + process.stderr.write("usage: ./tools/patchgame.js <game_id>\n") + process.exit(1) } -let db = new sqlite3("./db"); +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 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 save = db.prepare("select state from game_state where game_id=?").pluck().get(game_id) +fs.writeFileSync("backup-" + game_id + ".txt", save) + +function is_valid_action(rules, game, role, action, a) { + if (action !== 'undo') + if (game.active !== role && game.active !== "Both" && game.active !== "All") + return false + let view = rules.view(game, role) + let va = view.actions[action] + if (va === undefined) + return false + if (a === undefined || a === null) + return (va === 1) || (typeof va === 'string') + if (Array.isArray(a)) + a = a[0] + if (!Array.isArray(va)) + throw new Error("action list not array:" + JSON.stringify(view.actions)) + return va.includes(a) +} let game = { state: null, active: null } -let i = 0; +let view = null +let i = 0 try { log.forEach(item => { - let args = JSON.parse(item.arguments); + let args = JSON.parse(item.arguments) if (item.action === 'setup') - game = rules.setup(args[0], args[1], args[2]); + 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); - ++i; - }); + game = rules.resign(game, item.role) + else { + console.log("ACTION", i, game.seed, game.state, game.active, ">", item.role, item.action, item.arguments) + if (1) + if (!is_valid_action(rules, game, item.role, item.action, args)) { + console.log(`invalid action: ${item.role} ${item.action} ${item.arguments}`) + console.log("\t", game.state, game.active, JSON.stringify(rules.view(game, item.role).actions)) + throw "invalid action" + } + game = rules.action(game, item.role, item.action, args) + } + ++i + }) } catch (err) { - console.log("FAILED %d/%d", i+1, log.length); - console.log(err); + console.log("FAILED %d/%d", i+1, log.length) + console.log(err) + game.log=undefined + game.undo=undefined + //console.log(JSON.stringify(game,0,4)) + console.log(game) } -db.prepare("update game_state set active=?, state=? where game_id=?").run(game.active, JSON.stringify(game), game_id); +db.prepare("update game_state set active=?, state=? where game_id=?").run(game.active, JSON.stringify(game), game_id) if (i < log.length) { - console.log("BROKEN ENTRIES: %d", log.length-i); - console.log(`sqlite3 db "delete from game_replay where game_id=${game_id} and replay_id>=${log[i].replay_id}"`); + console.log("BROKEN ENTRIES: %d", log.length-i) + console.log(`sqlite3 db "delete from game_replay where game_id=${game_id} and replay_id>=${log[i].replay_id}"`) } |