BlueGit

timfa questward

Files

questward / server / scripts / grant-tim-bottles.mjs

grant-tim-bottles.mjs

grant-tim-bottles.mjs 1.3 KiB Download Raw
1import { DatabaseSync } from "node:sqlite";
2import { fileURLToPath } from "node:url";
3import { dirname, join } from "node:path";
4import { insertStack } from "../../shared/items/inventory.js";
5import { ItemId, itemDefinition } from "../../shared/definitions/items.js";
6
7const root = dirname(fileURLToPath(import.meta.url));
8const dbPath = join(root, "../data/state.sqlite");
9const db = new DatabaseSync(dbPath);
10const char = db.prepare(`
11 SELECT character_id, first_name, last_name FROM characters
12 WHERE first_normalized = ? AND last_normalized = ?
13`).get("tim", "falken");
14if (!char)
15{ throw new Error("character not found"); }
16
17const row = db.prepare("SELECT inventory_blob FROM character_bags WHERE character_id = ?")
18 .get(char.character_id);
19if (!row)
20{ throw new Error("no bag row"); }
21
22const slots = JSON.parse(row.inventory_blob || "[]");
23const def = itemDefinition(ItemId.glassBottle);
24const result = insertStack(slots, { itemId: ItemId.glassBottle, quantity: 20 }, def);
25if (!result.ok || result.remainder > 0)
26{ throw new Error(`inventory full / remainder ${result.remainder}`); }
27
28db.prepare("UPDATE character_bags SET inventory_blob = ? WHERE character_id = ?")
29 .run(JSON.stringify(result.slots), char.character_id);
30console.log(`granted 20 glass bottles to ${char.first_name} ${char.last_name} (id=${char.character_id})`);