import { SEX } from "../../../shared/protocol/constants.js"; import { normalizeName } from "../persistence/stateDb.js"; import { DEFAULT_HAIR_STYLE, isValidHairStyle, normalizeHairSliders, normalizeHairStyle } from "../../../shared/appearance/hair.js"; import { normalizeCreateClothing } from "../../../shared/items/createClothing.js"; import { validateCharacterNames } from "../../../shared/characters/names.js"; export class CharacterService { constructor (stateDb, log = console) { this.stateDb = stateDb; this.log = log; } list (accountId) { return this.stateDb.listCharacters(accountId).map(publicCharacter); } create (account, firstName, lastName, sex, hair = null, starterSettings = null, clothing = null) { const names = validateCharacterNames({ firstName, lastName, isAdmin: !!account?.is_admin }); if (!names.ok) { throw userError(names.reason); } const first = names.firstName; const last = names.lastName; if (sex !== SEX.male && sex !== SEX.female) { throw userError("Sex must be male or female."); } const firstNormalized = normalizeName(first); const lastNormalized = normalizeName(last); const claim = this.stateDb.findSurnameClaim(lastNormalized); if (claim && claim.account_id !== account.account_id) { throw userError("That last name is already claimed by another account."); } const existing = this.stateDb.listCharacters(account.account_id); if (existing.some((row) => row.first_normalized === firstNormalized && row.last_normalized === lastNormalized)) { throw userError("You already have a character with that name."); } const hairFields = normalizeCreateHair(sex, hair); const clothingFields = normalizeCreateClothing(clothing); const character = this.stateDb.createCharacter( account.account_id, first, last, sex, 8, 8, hairFields, starterSettings, clothingFields ); return publicCharacter(character); } getOwned (accountId, characterId) { const character = this.stateDb.getCharacter(characterId); if (!character || character.account_id !== accountId) { throw userError("Character not found."); } return character; } scheduleDelete (account, characterId, delayMs) { const character = this.getOwned(account.account_id, characterId); if (character.deletion_scheduled_at) { throw userError("That character is already marked for deletion."); } const at = Date.now() + delayMs; this.stateDb.scheduleDeletion(characterId, at); this.#notify(account, `Character ${character.first_name} ${character.last_name} is scheduled for deletion.`); return publicCharacter(this.stateDb.getCharacter(characterId)); } cancelDelete (account, characterId) { const character = this.getOwned(account.account_id, characterId); if (!character.deletion_scheduled_at) { throw userError("That character is not marked for deletion."); } this.stateDb.cancelDeletion(characterId); this.#notify(account, `Deletion cancelled for ${character.first_name} ${character.last_name}.`); return publicCharacter(this.stateDb.getCharacter(characterId)); } processDueDeletions (now = Date.now(), despawn = null) { for (const character of this.stateDb.listDueDeletions(now)) { despawn?.(character.character_id); this.stateDb.deleteCharacter(character.character_id); this.log.info?.( `[mail-stub] Permanently deleted ${character.first_name} ${character.last_name} for account ${character.account_id}` ); } } #notify (account, message) { if (account.email) { this.log.info?.(`[mail] ${account.email}: ${message}`); } else { this.log.info?.(`[mail-stub] ${account.auth_subject}: ${message}`); } } } export function publicCharacter (row) { const sliders = normalizeHairSliders({ hue: row.hair_hue, saturation: row.hair_saturation, darkness: row.hair_darkness }); return { characterId: row.character_id, firstName: row.first_name, lastName: row.last_name, sex: row.sex, tileX: row.tile_x, tileY: row.tile_y, gait: row.gait, deletionScheduledAt: row.deletion_scheduled_at ?? null, hairStyle: row.hair_style ?? null, hairHue: sliders.hue, hairSaturation: sliders.saturation, hairDarkness: sliders.darkness }; } export function normalizeCreateHair (sex, hair) { if (!isValidHairStyle(sex, hair?.style)) { throw userError("Unknown hair style."); } const style = normalizeHairStyle(sex, hair?.style ?? DEFAULT_HAIR_STYLE); const sliders = normalizeHairSliders(hair); return { style, hue: sliders.hue, saturation: sliders.saturation, darkness: sliders.darkness }; } function userError (message) { const error = new Error(message); error.userFacing = true; return error; }