BlueGit

timfa questward

Files

questward / server / src / characters / service.js

service.js

service.js 3.4 KiB Download Raw
1import { SEX } from "../../../shared/protocol/constants.js";
2import { normalizeName } from "../persistence/stateDb.js";
3
4export 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(), despawn = null)
72 {
73 for (const character of this.stateDb.listDueDeletions(now))
74 {
75 despawn?.(character.character_id);
76 this.stateDb.deleteCharacter(character.character_id);
77 this.log.info?.(
78 `[mail-stub] Permanently deleted ${character.first_name} ${character.last_name} for account ${character.account_id}`
79 );
80 }
81 }
82
83 #notify (account, message)
84 {
85 if (account.email)
86 { this.log.info?.(`[mail] ${account.email}: ${message}`); }
87 else
88 { this.log.info?.(`[mail-stub] ${account.auth_subject}: ${message}`); }
89 }
90}
91
92export function publicCharacter (row)
93{
94 return {
95 characterId: row.character_id,
96 firstName: row.first_name,
97 lastName: row.last_name,
98 sex: row.sex,
99 tileX: row.tile_x,
100 tileY: row.tile_y,
101 gait: row.gait,
102 deletionScheduledAt: row.deletion_scheduled_at ?? null
103 };
104}
105
106function userError (message)
107{
108 const error = new Error(message);
109 error.userFacing = true;
110 return error;
111}