| 1 | import { SEX } from "../../../shared/protocol/constants.js"; |
| 2 | import { normalizeName } from "../persistence/stateDb.js"; |
| 3 | |
| 4 | export class CharacterService |
| 5 | { |
| 6 | constructor (stateDb, log = console) |
| 7 | { |
| 8 | this.stateDb = stateDb; |
| 9 | this.log = log; |
| 10 | } |
| 11 | |
| 12 | list (accountId) |
| 13 | { |
| 14 | return this.stateDb.listCharacters(accountId).map(publicCharacter); |
| 15 | } |
| 16 | |
| 17 | create (account, firstName, lastName, sex) |
| 18 | { |
| 19 | const first = String(firstName ?? "").trim(); |
| 20 | const last = String(lastName ?? "").trim(); |
| 21 | if (!first || !last) |
| 22 | { throw userError("First and last name are required."); } |
| 23 | if (sex !== SEX.male && sex !== SEX.female) |
| 24 | { throw userError("Sex must be male or female."); } |
| 25 | |
| 26 | const firstNormalized = normalizeName(first); |
| 27 | const lastNormalized = normalizeName(last); |
| 28 | const claim = this.stateDb.findSurnameClaim(lastNormalized); |
| 29 | if (claim && claim.account_id !== account.account_id) |
| 30 | { throw userError("That last name is already claimed by another account."); } |
| 31 | |
| 32 | const existing = this.stateDb.listCharacters(account.account_id); |
| 33 | if (existing.some((row) => row.first_normalized === firstNormalized && row.last_normalized === lastNormalized)) |
| 34 | { throw userError("You already have a character with that name."); } |
| 35 | |
| 36 | const character = this.stateDb.createCharacter(account.account_id, first, last, sex, 8, 8); |
| 37 | return publicCharacter(character); |
| 38 | } |
| 39 | |
| 40 | getOwned (accountId, characterId) |
| 41 | { |
| 42 | const character = this.stateDb.getCharacter(characterId); |
| 43 | if (!character || character.account_id !== accountId) |
| 44 | { throw userError("Character not found."); } |
| 45 | return character; |
| 46 | } |
| 47 | |
| 48 | scheduleDelete (account, characterId, delayMs) |
| 49 | { |
| 50 | const character = this.getOwned(account.account_id, characterId); |
| 51 | if (character.deletion_scheduled_at) |
| 52 | { throw userError("That character is already marked for deletion."); } |
| 53 | |
| 54 | const at = Date.now() + delayMs; |
| 55 | this.stateDb.scheduleDeletion(characterId, at); |
| 56 | this.#notify(account, `Character ${character.first_name} ${character.last_name} is scheduled for deletion.`); |
| 57 | return publicCharacter(this.stateDb.getCharacter(characterId)); |
| 58 | } |
| 59 | |
| 60 | cancelDelete (account, characterId) |
| 61 | { |
| 62 | const character = this.getOwned(account.account_id, characterId); |
| 63 | if (!character.deletion_scheduled_at) |
| 64 | { throw userError("That character is not marked for deletion."); } |
| 65 | |
| 66 | this.stateDb.cancelDeletion(characterId); |
| 67 | this.#notify(account, `Deletion cancelled for ${character.first_name} ${character.last_name}.`); |
| 68 | return publicCharacter(this.stateDb.getCharacter(characterId)); |
| 69 | } |
| 70 | |
| 71 | processDueDeletions (now = Date.now()) |
| 72 | { |
| 73 | for (const character of this.stateDb.listDueDeletions(now)) |
| 74 | { |
| 75 | this.stateDb.deleteCharacter(character.character_id); |
| 76 | this.log.info?.( |
| 77 | `[mail-stub] Permanently deleted ${character.first_name} ${character.last_name} for account ${character.account_id}` |
| 78 | ); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | #notify (account, message) |
| 83 | { |
| 84 | if (account.email) |
| 85 | { this.log.info?.(`[mail] ${account.email}: ${message}`); } |
| 86 | else |
| 87 | { this.log.info?.(`[mail-stub] ${account.auth_subject}: ${message}`); } |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | export function publicCharacter (row) |
| 92 | { |
| 93 | return { |
| 94 | characterId: row.character_id, |
| 95 | firstName: row.first_name, |
| 96 | lastName: row.last_name, |
| 97 | sex: row.sex, |
| 98 | tileX: row.tile_x, |
| 99 | tileY: row.tile_y, |
| 100 | gait: row.gait, |
| 101 | deletionScheduledAt: row.deletion_scheduled_at ?? null |
| 102 | }; |
| 103 | } |
| 104 | |
| 105 | function userError (message) |
| 106 | { |
| 107 | const error = new Error(message); |
| 108 | error.userFacing = true; |
| 109 | return error; |
| 110 | } |