| 1 | import { openDatabase } from "./database.js"; |
| 2 | import { emptyEquipment, emptySlots, insertStack, insertStackGrowable, normalizeVault, normalizeInventorySlots, publicSlots, migrateBagCoins, migrateInheritanceCoins } from "../../../shared/items/inventory.js"; |
| 3 | import { STARTER_ITEMS, itemDefinition } from "../../../shared/definitions/items.js"; |
| 4 | import { normalizePlayerSkills, defaultPlayerSkills, storedSkillXp } from "../../../shared/skills/state.js"; |
| 5 | |
| 6 | const STATE_SCHEMA = ` |
| 7 | CREATE TABLE IF NOT EXISTS accounts ( |
| 8 | account_id INTEGER PRIMARY KEY AUTOINCREMENT, |
| 9 | auth_subject TEXT NOT NULL UNIQUE, |
| 10 | email TEXT, |
| 11 | is_admin INTEGER NOT NULL DEFAULT 0 |
| 12 | ); |
| 13 | |
| 14 | CREATE TABLE IF NOT EXISTS surname_claims ( |
| 15 | surname_normalized TEXT PRIMARY KEY, |
| 16 | account_id INTEGER NOT NULL |
| 17 | ); |
| 18 | |
| 19 | CREATE TABLE IF NOT EXISTS characters ( |
| 20 | character_id INTEGER PRIMARY KEY AUTOINCREMENT, |
| 21 | account_id INTEGER NOT NULL, |
| 22 | first_name TEXT NOT NULL, |
| 23 | last_name TEXT NOT NULL, |
| 24 | first_normalized TEXT NOT NULL, |
| 25 | last_normalized TEXT NOT NULL, |
| 26 | sex TEXT NOT NULL, |
| 27 | tile_x INTEGER NOT NULL DEFAULT 8, |
| 28 | tile_y INTEGER NOT NULL DEFAULT 8, |
| 29 | gait TEXT NOT NULL DEFAULT 'walk', |
| 30 | deletion_scheduled_at INTEGER |
| 31 | ); |
| 32 | CREATE TABLE IF NOT EXISTS friend_requests ( |
| 33 | request_id INTEGER PRIMARY KEY AUTOINCREMENT, |
| 34 | from_character_id INTEGER NOT NULL, |
| 35 | to_character_id INTEGER NOT NULL, |
| 36 | created_at INTEGER NOT NULL, |
| 37 | UNIQUE (from_character_id, to_character_id) |
| 38 | ); |
| 39 | |
| 40 | CREATE TABLE IF NOT EXISTS friendships ( |
| 41 | character_id_a INTEGER NOT NULL, |
| 42 | character_id_b INTEGER NOT NULL, |
| 43 | created_at INTEGER NOT NULL, |
| 44 | PRIMARY KEY (character_id_a, character_id_b) |
| 45 | ); |
| 46 | |
| 47 | CREATE TABLE IF NOT EXISTS persistent_entities ( |
| 48 | entity_id INTEGER PRIMARY KEY, |
| 49 | tile_x INTEGER NOT NULL, |
| 50 | tile_y INTEGER NOT NULL, |
| 51 | components_blob TEXT NOT NULL |
| 52 | ); |
| 53 | |
| 54 | CREATE TABLE IF NOT EXISTS character_bags ( |
| 55 | character_id INTEGER PRIMARY KEY, |
| 56 | inventory_blob TEXT NOT NULL, |
| 57 | equipment_blob TEXT NOT NULL, |
| 58 | currency INTEGER NOT NULL DEFAULT 0, |
| 59 | vault_blob TEXT NOT NULL, |
| 60 | vault_currency INTEGER NOT NULL DEFAULT 0, |
| 61 | skills_blob TEXT NOT NULL DEFAULT '{}' |
| 62 | ); |
| 63 | |
| 64 | CREATE TABLE IF NOT EXISTS account_inheritance ( |
| 65 | account_id INTEGER PRIMARY KEY, |
| 66 | slots_blob TEXT NOT NULL, |
| 67 | currency INTEGER NOT NULL DEFAULT 0 |
| 68 | ); |
| 69 | |
| 70 | CREATE TABLE IF NOT EXISTS ground_items ( |
| 71 | ground_item_id INTEGER PRIMARY KEY, |
| 72 | tile_x INTEGER NOT NULL, |
| 73 | tile_y INTEGER NOT NULL, |
| 74 | stack_blob TEXT NOT NULL, |
| 75 | dropped_at INTEGER NOT NULL, |
| 76 | expires_at INTEGER NOT NULL, |
| 77 | source TEXT NOT NULL, |
| 78 | dropped_by_character_id INTEGER |
| 79 | ); |
| 80 | |
| 81 | CREATE TABLE IF NOT EXISTS courier_transits ( |
| 82 | courier_id INTEGER PRIMARY KEY AUTOINCREMENT, |
| 83 | sender_character_id INTEGER NOT NULL, |
| 84 | destination_kind TEXT NOT NULL, |
| 85 | destination_character_id INTEGER, |
| 86 | cargo_blob TEXT NOT NULL, |
| 87 | origin_x REAL NOT NULL, |
| 88 | origin_y REAL NOT NULL, |
| 89 | dest_x REAL NOT NULL, |
| 90 | dest_y REAL NOT NULL, |
| 91 | traveled REAL NOT NULL DEFAULT 0, |
| 92 | status TEXT NOT NULL, |
| 93 | entity_id INTEGER, |
| 94 | updated_at INTEGER NOT NULL, |
| 95 | created_at INTEGER NOT NULL |
| 96 | ); |
| 97 | `; |
| 98 | |
| 99 | export class StateDatabase |
| 100 | { |
| 101 | constructor (filePath) |
| 102 | { |
| 103 | this.db = openDatabase(filePath); |
| 104 | this.db.exec(STATE_SCHEMA); |
| 105 | this.#migrateSchema(); |
| 106 | } |
| 107 | |
| 108 | #migrateSchema () |
| 109 | { |
| 110 | const columns = this.db.prepare("PRAGMA table_info(character_bags)").all(); |
| 111 | if (!columns.some((column) => column.name === "skills_blob")) |
| 112 | { |
| 113 | this.db.exec("ALTER TABLE character_bags ADD COLUMN skills_blob TEXT NOT NULL DEFAULT '{}'"); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | getAccountBySubject (authSubject) |
| 118 | { |
| 119 | return this.db.prepare("SELECT * FROM accounts WHERE auth_subject = ?").get(authSubject) ?? null; |
| 120 | } |
| 121 | |
| 122 | createAccount (authSubject, isAdmin, email = null) |
| 123 | { |
| 124 | this.db.prepare("INSERT INTO accounts (auth_subject, email, is_admin) VALUES (?, ?, ?)").run( |
| 125 | authSubject, |
| 126 | email, |
| 127 | isAdmin ? 1 : 0 |
| 128 | ); |
| 129 | return this.getAccountBySubject(authSubject); |
| 130 | } |
| 131 | |
| 132 | ensureAccount (authSubject, isAdmin, email = null) |
| 133 | { |
| 134 | const existing = this.getAccountBySubject(authSubject); |
| 135 | if (existing) |
| 136 | { return existing; } |
| 137 | |
| 138 | return this.createAccount(authSubject, isAdmin, email); |
| 139 | } |
| 140 | |
| 141 | listCharacters (accountId) |
| 142 | { |
| 143 | return this.db.prepare(` |
| 144 | SELECT * FROM characters |
| 145 | WHERE account_id = ? |
| 146 | ORDER BY character_id ASC |
| 147 | `).all(accountId); |
| 148 | } |
| 149 | |
| 150 | getCharacter (characterId) |
| 151 | { |
| 152 | return this.db.prepare("SELECT * FROM characters WHERE character_id = ?").get(characterId) ?? null; |
| 153 | } |
| 154 | |
| 155 | findSurnameClaim (surnameNormalized) |
| 156 | { |
| 157 | return this.db.prepare("SELECT * FROM surname_claims WHERE surname_normalized = ?").get(surnameNormalized) ?? null; |
| 158 | } |
| 159 | |
| 160 | createCharacter (accountId, firstName, lastName, sex, tileX, tileY) |
| 161 | { |
| 162 | const firstNormalized = normalizeName(firstName); |
| 163 | const lastNormalized = normalizeName(lastName); |
| 164 | this.db.exec("BEGIN"); |
| 165 | try |
| 166 | { |
| 167 | this.db.prepare(` |
| 168 | INSERT INTO characters ( |
| 169 | account_id, first_name, last_name, first_normalized, last_normalized, sex, tile_x, tile_y |
| 170 | ) VALUES (?, ?, ?, ?, ?, ?, ?, ?) |
| 171 | `).run(accountId, firstName, lastName, firstNormalized, lastNormalized, sex, tileX, tileY); |
| 172 | const characterId = Number(this.db.prepare("SELECT last_insert_rowid() AS id").get().id); |
| 173 | |
| 174 | const claim = this.findSurnameClaim(lastNormalized); |
| 175 | if (!claim) |
| 176 | { |
| 177 | this.db.prepare("INSERT INTO surname_claims (surname_normalized, account_id) VALUES (?, ?)").run( |
| 178 | lastNormalized, |
| 179 | accountId |
| 180 | ); |
| 181 | } |
| 182 | |
| 183 | this.db.exec("COMMIT"); |
| 184 | this.#ensureBag(characterId); |
| 185 | return this.getCharacter(characterId); |
| 186 | } |
| 187 | catch (error) |
| 188 | { |
| 189 | this.db.exec("ROLLBACK"); |
| 190 | throw error; |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | scheduleDeletion (characterId, at) |
| 195 | { |
| 196 | this.db.prepare("UPDATE characters SET deletion_scheduled_at = ? WHERE character_id = ?").run(at, characterId); |
| 197 | } |
| 198 | |
| 199 | cancelDeletion (characterId) |
| 200 | { |
| 201 | this.db.prepare("UPDATE characters SET deletion_scheduled_at = NULL WHERE character_id = ?").run(characterId); |
| 202 | } |
| 203 | |
| 204 | deleteCharacter (characterId) |
| 205 | { |
| 206 | const character = this.getCharacter(characterId); |
| 207 | if (!character) |
| 208 | { return; } |
| 209 | |
| 210 | this.db.exec("BEGIN"); |
| 211 | try |
| 212 | { |
| 213 | this.#moveGoodsToInheritance(character); |
| 214 | this.db.prepare("DELETE FROM character_bags WHERE character_id = ?").run(characterId); |
| 215 | this.db.prepare("DELETE FROM characters WHERE character_id = ?").run(characterId); |
| 216 | this.db.prepare("DELETE FROM friend_requests WHERE from_character_id = ? OR to_character_id = ?").run( |
| 217 | characterId, |
| 218 | characterId |
| 219 | ); |
| 220 | this.db.prepare("DELETE FROM friendships WHERE character_id_a = ? OR character_id_b = ?").run( |
| 221 | characterId, |
| 222 | characterId |
| 223 | ); |
| 224 | const remaining = this.db.prepare(` |
| 225 | SELECT COUNT(*) AS count FROM characters |
| 226 | WHERE account_id = ? AND last_normalized = ? |
| 227 | `).get(character.account_id, character.last_normalized); |
| 228 | if (!remaining.count) |
| 229 | { |
| 230 | this.db.prepare("DELETE FROM surname_claims WHERE surname_normalized = ? AND account_id = ?").run( |
| 231 | character.last_normalized, |
| 232 | character.account_id |
| 233 | ); |
| 234 | } |
| 235 | this.db.exec("COMMIT"); |
| 236 | } |
| 237 | catch (error) |
| 238 | { |
| 239 | this.db.exec("ROLLBACK"); |
| 240 | throw error; |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | saveCharacterPosition (characterId, tileX, tileY, gait) |
| 245 | { |
| 246 | this.db.prepare("UPDATE characters SET tile_x = ?, tile_y = ?, gait = ? WHERE character_id = ?").run( |
| 247 | tileX, |
| 248 | tileY, |
| 249 | gait, |
| 250 | characterId |
| 251 | ); |
| 252 | } |
| 253 | |
| 254 | listDueDeletions (now) |
| 255 | { |
| 256 | return this.db.prepare(` |
| 257 | SELECT * FROM characters |
| 258 | WHERE deletion_scheduled_at IS NOT NULL AND deletion_scheduled_at <= ? |
| 259 | `).all(now); |
| 260 | } |
| 261 | |
| 262 | getCharacterByName (firstName, lastName) |
| 263 | { |
| 264 | return this.db.prepare(` |
| 265 | SELECT * FROM characters |
| 266 | WHERE first_normalized = ? AND last_normalized = ? |
| 267 | `).get(normalizeName(firstName), normalizeName(lastName)) ?? null; |
| 268 | } |
| 269 | |
| 270 | listFriendRecords (characterId) |
| 271 | { |
| 272 | return this.db.prepare(` |
| 273 | SELECT |
| 274 | CASE WHEN character_id_a = ? THEN character_id_b ELSE character_id_a END AS character_id, |
| 275 | created_at |
| 276 | FROM friendships |
| 277 | WHERE character_id_a = ? OR character_id_b = ? |
| 278 | ORDER BY created_at ASC |
| 279 | `).all(characterId, characterId, characterId); |
| 280 | } |
| 281 | |
| 282 | areFriends (characterId, otherId) |
| 283 | { |
| 284 | const [a, b] = pairIds(characterId, otherId); |
| 285 | return !!this.db.prepare(` |
| 286 | SELECT 1 FROM friendships WHERE character_id_a = ? AND character_id_b = ? |
| 287 | `).get(a, b); |
| 288 | } |
| 289 | |
| 290 | insertFriendship (characterId, otherId) |
| 291 | { |
| 292 | const [a, b] = pairIds(characterId, otherId); |
| 293 | this.db.prepare(` |
| 294 | INSERT INTO friendships (character_id_a, character_id_b, created_at) |
| 295 | VALUES (?, ?, ?) |
| 296 | `).run(a, b, Date.now()); |
| 297 | } |
| 298 | |
| 299 | deleteFriendship (characterId, otherId) |
| 300 | { |
| 301 | const [a, b] = pairIds(characterId, otherId); |
| 302 | this.db.prepare(` |
| 303 | DELETE FROM friendships WHERE character_id_a = ? AND character_id_b = ? |
| 304 | `).run(a, b); |
| 305 | } |
| 306 | |
| 307 | listIncomingRequests (characterId) |
| 308 | { |
| 309 | return this.db.prepare(` |
| 310 | SELECT request_id, from_character_id, to_character_id, created_at |
| 311 | FROM friend_requests |
| 312 | WHERE to_character_id = ? |
| 313 | ORDER BY created_at ASC |
| 314 | `).all(characterId); |
| 315 | } |
| 316 | |
| 317 | listOutgoingRequests (characterId) |
| 318 | { |
| 319 | return this.db.prepare(` |
| 320 | SELECT request_id, from_character_id, to_character_id, created_at |
| 321 | FROM friend_requests |
| 322 | WHERE from_character_id = ? |
| 323 | ORDER BY created_at ASC |
| 324 | `).all(characterId); |
| 325 | } |
| 326 | |
| 327 | getFriendRequest (fromId, toId) |
| 328 | { |
| 329 | return this.db.prepare(` |
| 330 | SELECT * FROM friend_requests |
| 331 | WHERE from_character_id = ? AND to_character_id = ? |
| 332 | `).get(fromId, toId) ?? null; |
| 333 | } |
| 334 | |
| 335 | insertFriendRequest (fromId, toId) |
| 336 | { |
| 337 | this.db.prepare(` |
| 338 | INSERT INTO friend_requests (from_character_id, to_character_id, created_at) |
| 339 | VALUES (?, ?, ?) |
| 340 | `).run(fromId, toId, Date.now()); |
| 341 | } |
| 342 | |
| 343 | deleteFriendRequest (fromId, toId) |
| 344 | { |
| 345 | this.db.prepare(` |
| 346 | DELETE FROM friend_requests |
| 347 | WHERE from_character_id = ? AND to_character_id = ? |
| 348 | `).run(fromId, toId); |
| 349 | } |
| 350 | |
| 351 | listPersistentInRegion (minX, minY, maxX, maxY) |
| 352 | { |
| 353 | return this.db.prepare(` |
| 354 | SELECT entity_id, tile_x, tile_y, components_blob |
| 355 | FROM persistent_entities |
| 356 | WHERE tile_x >= ? AND tile_x <= ? AND tile_y >= ? AND tile_y <= ? |
| 357 | `).all(minX, maxX, minY, maxY); |
| 358 | } |
| 359 | |
| 360 | replacePersistentInRegion (minX, minY, maxX, maxY, entities) |
| 361 | { |
| 362 | this.db.exec("BEGIN"); |
| 363 | try |
| 364 | { |
| 365 | this.db.prepare(` |
| 366 | DELETE FROM persistent_entities |
| 367 | WHERE tile_x >= ? AND tile_x <= ? AND tile_y >= ? AND tile_y <= ? |
| 368 | `).run(minX, maxX, minY, maxY); |
| 369 | const insert = this.db.prepare(` |
| 370 | INSERT INTO persistent_entities (entity_id, tile_x, tile_y, components_blob) |
| 371 | VALUES (?, ?, ?, ?) |
| 372 | `); |
| 373 | for (const entity of entities) |
| 374 | { |
| 375 | insert.run( |
| 376 | entity.entityId, |
| 377 | entity.tileX, |
| 378 | entity.tileY, |
| 379 | JSON.stringify(entity.payload?.components ?? entity.components) |
| 380 | ); |
| 381 | } |
| 382 | this.db.exec("COMMIT"); |
| 383 | } |
| 384 | catch (error) |
| 385 | { |
| 386 | this.db.exec("ROLLBACK"); |
| 387 | throw error; |
| 388 | } |
| 389 | } |
| 390 | |
| 391 | #ensureBag (characterId) |
| 392 | { |
| 393 | const existing = this.db.prepare("SELECT * FROM character_bags WHERE character_id = ?").get(characterId); |
| 394 | if (existing) |
| 395 | { return parseBag(existing, characterId); } |
| 396 | const slots = emptySlots(); |
| 397 | for (const starter of STARTER_ITEMS) |
| 398 | { |
| 399 | const result = insertStack(slots, { ...starter }, itemDefinition(starter.itemId)); |
| 400 | if (result.ok) |
| 401 | { slots.splice(0, slots.length, ...result.slots); } |
| 402 | } |
| 403 | this.db.prepare(` |
| 404 | INSERT INTO character_bags ( |
| 405 | character_id, inventory_blob, equipment_blob, currency, vault_blob, vault_currency, skills_blob |
| 406 | ) VALUES (?, ?, ?, 0, ?, 0, ?) |
| 407 | `).run( |
| 408 | characterId, |
| 409 | JSON.stringify(slots), |
| 410 | JSON.stringify(emptyEquipment()), |
| 411 | JSON.stringify({ slots: [], reserved: [] }), |
| 412 | JSON.stringify(storedSkillXp(defaultPlayerSkills())) |
| 413 | ); |
| 414 | return this.getBag(characterId); |
| 415 | } |
| 416 | |
| 417 | getBag (characterId) |
| 418 | { |
| 419 | const row = this.db.prepare("SELECT * FROM character_bags WHERE character_id = ?").get(characterId); |
| 420 | if (row) |
| 421 | { return parseBag(row, characterId); } |
| 422 | if (this.getCharacter(characterId)) |
| 423 | { return this.#ensureBag(characterId); } |
| 424 | return null; |
| 425 | } |
| 426 | |
| 427 | saveBag (characterId, bag) |
| 428 | { |
| 429 | this.#ensureBag(characterId); |
| 430 | const migrated = migrateBagCoins(bag); |
| 431 | const vault = normalizeVault( |
| 432 | migrated.vaultReserved != null |
| 433 | ? { slots: migrated.vault, reserved: migrated.vaultReserved } |
| 434 | : migrated.vault |
| 435 | ); |
| 436 | this.db.prepare(` |
| 437 | UPDATE character_bags |
| 438 | SET inventory_blob = ?, equipment_blob = ?, currency = 0, vault_blob = ?, vault_currency = 0 |
| 439 | WHERE character_id = ? |
| 440 | `).run( |
| 441 | JSON.stringify(normalizeInventorySlots(migrated.inventory ?? emptySlots())), |
| 442 | JSON.stringify(migrated.equipment ?? emptyEquipment()), |
| 443 | JSON.stringify({ slots: vault.slots, reserved: vault.reserved }), |
| 444 | characterId |
| 445 | ); |
| 446 | } |
| 447 | |
| 448 | getSkills (characterId) |
| 449 | { |
| 450 | const row = this.db.prepare("SELECT skills_blob FROM character_bags WHERE character_id = ?").get(characterId); |
| 451 | if (row) |
| 452 | { return normalizePlayerSkills(JSON.parse(row.skills_blob || "{}")); } |
| 453 | if (this.getCharacter(characterId)) |
| 454 | { |
| 455 | this.#ensureBag(characterId); |
| 456 | return this.getSkills(characterId); |
| 457 | } |
| 458 | return null; |
| 459 | } |
| 460 | |
| 461 | saveSkills (characterId, skills) |
| 462 | { |
| 463 | this.#ensureBag(characterId); |
| 464 | const xp = storedSkillXp(skills?.xp ? skills : { xp: skills }); |
| 465 | this.db.prepare("UPDATE character_bags SET skills_blob = ? WHERE character_id = ?").run( |
| 466 | JSON.stringify({ xp }), |
| 467 | characterId |
| 468 | ); |
| 469 | } |
| 470 | |
| 471 | getInheritance (accountId) |
| 472 | { |
| 473 | const row = this.db.prepare("SELECT * FROM account_inheritance WHERE account_id = ?").get(accountId); |
| 474 | if (!row) |
| 475 | { return { accountId, slots: emptySlots() }; } |
| 476 | return migrateInheritanceCoins({ |
| 477 | accountId, |
| 478 | slots: JSON.parse(row.slots_blob), |
| 479 | currency: row.currency |
| 480 | }); |
| 481 | } |
| 482 | |
| 483 | saveInheritance (accountId, inheritance) |
| 484 | { |
| 485 | const migrated = migrateInheritanceCoins(inheritance); |
| 486 | this.db.prepare(` |
| 487 | INSERT INTO account_inheritance (account_id, slots_blob, currency) |
| 488 | VALUES (?, ?, 0) |
| 489 | ON CONFLICT(account_id) DO UPDATE SET slots_blob = excluded.slots_blob, currency = 0 |
| 490 | `).run(accountId, JSON.stringify(migrated.slots ?? emptySlots())); |
| 491 | } |
| 492 | |
| 493 | #moveGoodsToInheritance (character) |
| 494 | { |
| 495 | const bag = this.getBag(character.character_id) ?? { |
| 496 | inventory: emptySlots(), |
| 497 | equipment: emptyEquipment(), |
| 498 | vault: [], |
| 499 | vaultReserved: [] |
| 500 | }; |
| 501 | const inheritance = this.getInheritance(character.account_id); |
| 502 | const vault = normalizeVault( |
| 503 | bag.vaultReserved != null |
| 504 | ? { slots: bag.vault, reserved: bag.vaultReserved } |
| 505 | : bag.vault |
| 506 | ); |
| 507 | // Void outstanding cheques issued by this character: reservations return to available vault. |
| 508 | for (const reserved of vault.reserved ?? []) |
| 509 | { |
| 510 | const stack = { |
| 511 | itemId: reserved.itemId, |
| 512 | quantity: reserved.quantity, |
| 513 | origin: reserved.origin ?? null, |
| 514 | instance: reserved.instance ?? null |
| 515 | }; |
| 516 | const def = itemDefinition(stack.itemId); |
| 517 | if (!def) |
| 518 | { continue; } |
| 519 | const restored = insertStackGrowable(vault.slots, stack, def); |
| 520 | vault.slots = restored.slots; |
| 521 | } |
| 522 | vault.reserved = []; |
| 523 | const incoming = [ |
| 524 | ...(bag.inventory ?? []), |
| 525 | ...Object.values(bag.equipment?.slots ?? {}), |
| 526 | ...(vault.slots ?? []) |
| 527 | ].filter(Boolean); |
| 528 | let slots = inheritance.slots ?? emptySlots(); |
| 529 | for (const stack of incoming) |
| 530 | { |
| 531 | const def = itemDefinition(stack.itemId); |
| 532 | if (!def) |
| 533 | { continue; } |
| 534 | const result = insertStack(slots, stack, def); |
| 535 | if (result.ok) |
| 536 | { slots = result.slots; } |
| 537 | else |
| 538 | { |
| 539 | const extra = result.slots; |
| 540 | for (const leftover of extra) |
| 541 | { |
| 542 | if (leftover) |
| 543 | { slots.push(leftover); } |
| 544 | } |
| 545 | slots.push({ ...stack, quantity: result.remainder }); |
| 546 | } |
| 547 | } |
| 548 | this.saveInheritance(character.account_id, { |
| 549 | slots: publicSlots(slots) |
| 550 | }); |
| 551 | } |
| 552 | |
| 553 | nextGroundItemId () |
| 554 | { |
| 555 | const row = this.db.prepare("SELECT MAX(ground_item_id) AS id FROM ground_items").get(); |
| 556 | return (row?.id ?? 0) + 1; |
| 557 | } |
| 558 | |
| 559 | listGroundItemsInRegion (minX, minY, maxX, maxY) |
| 560 | { |
| 561 | return this.db.prepare(` |
| 562 | SELECT * FROM ground_items |
| 563 | WHERE tile_x >= ? AND tile_x <= ? AND tile_y >= ? AND tile_y <= ? |
| 564 | `).all(minX, maxX, minY, maxY).map(parseGroundItem); |
| 565 | } |
| 566 | |
| 567 | upsertGroundItem (item) |
| 568 | { |
| 569 | this.db.prepare(` |
| 570 | INSERT INTO ground_items ( |
| 571 | ground_item_id, tile_x, tile_y, stack_blob, dropped_at, expires_at, source, dropped_by_character_id |
| 572 | ) VALUES (?, ?, ?, ?, ?, ?, ?, ?) |
| 573 | ON CONFLICT(ground_item_id) DO UPDATE SET |
| 574 | tile_x = excluded.tile_x, |
| 575 | tile_y = excluded.tile_y, |
| 576 | stack_blob = excluded.stack_blob, |
| 577 | dropped_at = excluded.dropped_at, |
| 578 | expires_at = excluded.expires_at, |
| 579 | source = excluded.source, |
| 580 | dropped_by_character_id = excluded.dropped_by_character_id |
| 581 | `).run( |
| 582 | item.groundItemId, |
| 583 | item.tileX, |
| 584 | item.tileY, |
| 585 | JSON.stringify(item.stack), |
| 586 | item.droppedAt, |
| 587 | item.expiresAt, |
| 588 | item.source, |
| 589 | item.droppedByCharacterId ?? null |
| 590 | ); |
| 591 | } |
| 592 | |
| 593 | deleteGroundItem (groundItemId) |
| 594 | { |
| 595 | this.db.prepare("DELETE FROM ground_items WHERE ground_item_id = ?").run(groundItemId); |
| 596 | } |
| 597 | |
| 598 | insertCourierTransit (transit) |
| 599 | { |
| 600 | const now = transit.updatedAt ?? Date.now(); |
| 601 | const result = this.db.prepare(` |
| 602 | INSERT INTO courier_transits ( |
| 603 | sender_character_id, destination_kind, destination_character_id, cargo_blob, |
| 604 | origin_x, origin_y, dest_x, dest_y, traveled, status, entity_id, updated_at, created_at |
| 605 | ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) |
| 606 | `).run( |
| 607 | transit.senderCharacterId, |
| 608 | transit.destinationKind, |
| 609 | transit.destinationCharacterId ?? null, |
| 610 | JSON.stringify(transit.cargo), |
| 611 | transit.originX, |
| 612 | transit.originY, |
| 613 | transit.destX, |
| 614 | transit.destY, |
| 615 | transit.traveled ?? 0, |
| 616 | transit.status ?? "in_transit", |
| 617 | transit.entityId ?? null, |
| 618 | now, |
| 619 | transit.createdAt ?? now |
| 620 | ); |
| 621 | return this.getCourierTransit(Number(result.lastInsertRowid)); |
| 622 | } |
| 623 | |
| 624 | getCourierTransit (courierId) |
| 625 | { |
| 626 | const row = this.db.prepare("SELECT * FROM courier_transits WHERE courier_id = ?").get(courierId); |
| 627 | return row ? parseCourierTransit(row) : null; |
| 628 | } |
| 629 | |
| 630 | listActiveCourierTransits () |
| 631 | { |
| 632 | return this.db.prepare(` |
| 633 | SELECT * FROM courier_transits WHERE status = 'in_transit' ORDER BY courier_id ASC |
| 634 | `).all().map(parseCourierTransit); |
| 635 | } |
| 636 | |
| 637 | updateCourierTransit (transit) |
| 638 | { |
| 639 | this.db.prepare(` |
| 640 | UPDATE courier_transits |
| 641 | SET traveled = ?, status = ?, entity_id = ?, updated_at = ?, |
| 642 | origin_x = ?, origin_y = ?, dest_x = ?, dest_y = ?, |
| 643 | destination_kind = ?, destination_character_id = ? |
| 644 | WHERE courier_id = ? |
| 645 | `).run( |
| 646 | transit.traveled ?? 0, |
| 647 | transit.status, |
| 648 | transit.entityId ?? null, |
| 649 | transit.updatedAt ?? Date.now(), |
| 650 | transit.originX, |
| 651 | transit.originY, |
| 652 | transit.destX, |
| 653 | transit.destY, |
| 654 | transit.destinationKind, |
| 655 | transit.destinationCharacterId ?? null, |
| 656 | transit.courierId |
| 657 | ); |
| 658 | } |
| 659 | |
| 660 | close () |
| 661 | { |
| 662 | this.db.close(); |
| 663 | } |
| 664 | } |
| 665 | |
| 666 | export function normalizeName (value) |
| 667 | { |
| 668 | return String(value ?? "").trim().toLocaleLowerCase(); |
| 669 | } |
| 670 | |
| 671 | function pairIds (left, right) |
| 672 | { |
| 673 | return left < right ? [left, right] : [right, left]; |
| 674 | } |
| 675 | |
| 676 | function parseGroundItem (row) |
| 677 | { |
| 678 | return { |
| 679 | groundItemId: row.ground_item_id, |
| 680 | tileX: row.tile_x, |
| 681 | tileY: row.tile_y, |
| 682 | stack: JSON.parse(row.stack_blob), |
| 683 | droppedAt: row.dropped_at, |
| 684 | expiresAt: row.expires_at, |
| 685 | source: row.source, |
| 686 | droppedByCharacterId: row.dropped_by_character_id |
| 687 | }; |
| 688 | } |
| 689 | |
| 690 | function parseBag (row, characterId) |
| 691 | { |
| 692 | const vault = normalizeVault(JSON.parse(row.vault_blob)); |
| 693 | return migrateBagCoins({ |
| 694 | characterId, |
| 695 | inventory: normalizeInventorySlots(JSON.parse(row.inventory_blob)), |
| 696 | equipment: JSON.parse(row.equipment_blob), |
| 697 | currency: row.currency, |
| 698 | vault: vault.slots, |
| 699 | vaultReserved: vault.reserved, |
| 700 | vaultCurrency: row.vault_currency |
| 701 | }); |
| 702 | } |
| 703 | |
| 704 | function parseCourierTransit (row) |
| 705 | { |
| 706 | return { |
| 707 | courierId: row.courier_id, |
| 708 | senderCharacterId: row.sender_character_id, |
| 709 | destinationKind: row.destination_kind, |
| 710 | destinationCharacterId: row.destination_character_id, |
| 711 | cargo: JSON.parse(row.cargo_blob), |
| 712 | originX: row.origin_x, |
| 713 | originY: row.origin_y, |
| 714 | destX: row.dest_x, |
| 715 | destY: row.dest_y, |
| 716 | traveled: row.traveled, |
| 717 | status: row.status, |
| 718 | entityId: row.entity_id, |
| 719 | updatedAt: row.updated_at, |
| 720 | createdAt: row.created_at |
| 721 | }; |
| 722 | } |