summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTor Andersson <tor@ccxvii.net>2024-11-25 18:57:49 +0100
committerTor Andersson <tor@ccxvii.net>2024-12-02 01:39:36 +0100
commit1c50881cf282e85676b9b3ba96f0ea361675a093 (patch)
treef2fafa7f266b2bc9c9ae690fff9c63639aaa97b5
parent0e50f39f33079de81c5da473473383d6956ef575 (diff)
downloadserver-1c50881cf282e85676b9b3ba96f0ea361675a093.tar.gz
Allow bulk import with import-game.js
-rwxr-xr-xtools/import-game.js86
1 files changed, 44 insertions, 42 deletions
diff --git a/tools/import-game.js b/tools/import-game.js
index 87b93ab..59b26a7 100755
--- a/tools/import-game.js
+++ b/tools/import-game.js
@@ -4,7 +4,7 @@ const fs = require("fs")
const sqlite3 = require("better-sqlite3")
var options = {}
-var input = null
+var input = []
for (let i = 2; i < process.argv.length; ++i) {
let opt = process.argv[i]
@@ -12,65 +12,67 @@ for (let i = 2; i < process.argv.length; ++i) {
let [key, val] = opt.split("=", 2)
options[key] = val
} else {
- input = opt
+ input.push(opt)
}
}
-if (!input) {
+if (input.length < 1) {
console.error("usage: node tools/import-game.js [title_id=value] [notice=value] game.json")
process.exit(1)
}
-var game = JSON.parse(fs.readFileSync(input, "utf8"))
+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 (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 = "{}"
+ if (game.setup.notice === undefined)
+ game.setup.notice = ""
+ if (game.setup.options === undefined)
+ game.setup.options = "{}"
-game.setup.active = game.state.active
-game.setup.moves = game.snaps && game.snaps.length > 0 ? game.snaps.length - 1 : 0
+ game.setup.active = game.state.active
+ game.setup.moves = game.snaps && game.snaps.length > 0 ? game.snaps.length - 1 : 0
-let db = new sqlite3("db")
+ 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) values (?,?,?)")
-let insert_state = db.prepare("insert into game_state(game_id,state) values (?,?)")
+ 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) values (?,?,?)")
+ let insert_state = db.prepare("insert into game_state(game_id,state) values (?,?)")
-let select_user = db.prepare("select user_id from users where name=?").pluck()
+ let select_user = db.prepare("select user_id from users where name=?").pluck()
-db.exec("begin")
+ db.exec("begin")
-game.setup.options = JSON.stringify(game.setup.options)
+ game.setup.options = JSON.stringify(game.setup.options)
-function find_user(name) {
- return select_user.get(name) || 1
-}
+ 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))
+ 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))
-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.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))
- })
-}
+ 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)
+ console.log(game_id)
-db.exec("commit")
+ db.exec("commit")
+}