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