diff options
author | Tor Andersson <tor@ccxvii.net> | 2025-04-25 12:18:00 +0200 |
---|---|---|
committer | Tor Andersson <tor@ccxvii.net> | 2025-04-25 17:56:43 +0200 |
commit | fc3501382c2aa3ef5b692f4f55c2616f9cade3f5 (patch) | |
tree | a047bb94f8f1854c621fa0fba5ed62c8e0d0be14 /bin/rtt-import | |
parent | ed2361980b455d1825d811670f329cbcf5624927 (diff) | |
download | server-fc3501382c2aa3ef5b692f4f55c2616f9cade3f5.tar.gz |
Move toolbox scripts to a "bin" directory.
Add a super "rtt" command to run the scripts.
Diffstat (limited to 'bin/rtt-import')
-rwxr-xr-x | bin/rtt-import | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/bin/rtt-import b/bin/rtt-import new file mode 100755 index 0000000..2e4295b --- /dev/null +++ b/bin/rtt-import @@ -0,0 +1,80 @@ +#!/usr/bin/env -S node + +const fs = require("fs") +const sqlite3 = require("better-sqlite3") + +var options = {} +var input = [] + +for (let i = 2; i < process.argv.length; ++i) { + let opt = process.argv[i] + if (opt.includes("=")) { + let [key, val] = opt.split("=", 2) + options[key] = val + } else { + input.push(opt) + } +} + +if (input.length < 1) { + console.error("usage: rtt-import [title_id=value] [notice=value] game-dump.json") + process.exit(1) +} + +for (let file of input) { + var game = JSON.parse(fs.readFileSync(file, "utf8")) + + if (options.title_id) + game.setup.title_id = options.title_id + if (options.notice) + game.setup.notice = options.notice + + if (game.setup.notice === undefined) + game.setup.notice = "" + if (game.setup.options === undefined) + game.setup.options = "{}" + + game.setup.active = String(game.state.active) + game.setup.moves = game.snaps && game.snaps.length > 0 ? game.snaps.length - 1 : 0 + + let db = new sqlite3("db") + + let insert_game = db.prepare("insert into games(status,owner_id,title_id,scenario,options,player_count,active,moves,notice) values (1,1,:title_id,:scenario,:options,:player_count,:active,:moves,:notice) returning game_id").pluck() + let insert_player = db.prepare("insert into players(game_id,role,user_id,clock) values (?,?,?,21)") + let insert_state = db.prepare("insert into game_state(game_id,state) values (?,?)") + let update_active_trigger = db.prepare("update games set active=active where game_id=?") + + let select_user = db.prepare("select user_id from users where name=?").pluck() + + db.exec("begin") + + game.setup.options = JSON.stringify(game.setup.options) + + function find_user(name) { + return select_user.get(name) || 1 + } + + let game_id = insert_game.get(game.setup) + for (let p of game.players) + insert_player.run(game_id, p.role, find_user(p.name)) + insert_state.run(game_id, JSON.stringify(game.state)) + update_active_trigger.run(game_id) + + if (game.replay) { + let insert_replay = db.prepare("insert into game_replay(game_id,replay_id,role,action,arguments) values (?,?,?,?,?)") + game.replay.forEach(([role, action, args], i) => { + insert_replay.run(game_id, i+1, role, action, JSON.stringify(args)) + }) + } + + if (game.snaps) { + let insert_snap = db.prepare("insert into game_snap(game_id,snap_id,replay_id,state) values (?,?,?,?)") + game.snaps.forEach(([replay_id, state], i) => { + insert_snap.run(game_id, i+1, replay_id, JSON.stringify(state)) + }) + } + + console.log(game_id) + + db.exec("commit") +} |