BlueGit

timfa questward

Files

questward / server / src / characters / service.js

service.js

service.js 4.5 KiB Download Raw
1import { SEX } from "../../../shared/protocol/constants.js";
2import { normalizeName } from "../persistence/stateDb.js";
3import {
4 DEFAULT_HAIR_STYLE,
5 isValidHairStyle,
6 normalizeHairSliders,
7 normalizeHairStyle
8} from "../../../shared/appearance/hair.js";
9import { normalizeCreateClothing } from "../../../shared/items/createClothing.js";
10
11export class CharacterService
12{
13 constructor (stateDb, log = console)
14 {
15 this.stateDb = stateDb;
16 this.log = log;
17 }
18
19 list (accountId)
20 {
21 return this.stateDb.listCharacters(accountId).map(publicCharacter);
22 }
23
24 create (account, firstName, lastName, sex, hair = null, starterSettings = null, clothing = null)
25 {
26 const first = String(firstName ?? "").trim();
27 const last = String(lastName ?? "").trim();
28 if (!first || !last)
29 { throw userError("First and last name are required."); }
30 if (sex !== SEX.male && sex !== SEX.female)
31 { throw userError("Sex must be male or female."); }
32
33 const firstNormalized = normalizeName(first);
34 const lastNormalized = normalizeName(last);
35 const claim = this.stateDb.findSurnameClaim(lastNormalized);
36 if (claim && claim.account_id !== account.account_id)
37 { throw userError("That last name is already claimed by another account."); }
38
39 const existing = this.stateDb.listCharacters(account.account_id);
40 if (existing.some((row) => row.first_normalized === firstNormalized && row.last_normalized === lastNormalized))
41 { throw userError("You already have a character with that name."); }
42
43 const hairFields = normalizeCreateHair(sex, hair);
44 const clothingFields = normalizeCreateClothing(clothing);
45 const character = this.stateDb.createCharacter(
46 account.account_id,
47 first,
48 last,
49 sex,
50 8,
51 8,
52 hairFields,
53 starterSettings,
54 clothingFields
55 );
56 return publicCharacter(character);
57 }
58
59 getOwned (accountId, characterId)
60 {
61 const character = this.stateDb.getCharacter(characterId);
62 if (!character || character.account_id !== accountId)
63 { throw userError("Character not found."); }
64 return character;
65 }
66
67 scheduleDelete (account, characterId, delayMs)
68 {
69 const character = this.getOwned(account.account_id, characterId);
70 if (character.deletion_scheduled_at)
71 { throw userError("That character is already marked for deletion."); }
72
73 const at = Date.now() + delayMs;
74 this.stateDb.scheduleDeletion(characterId, at);
75 this.#notify(account, `Character ${character.first_name} ${character.last_name} is scheduled for deletion.`);
76 return publicCharacter(this.stateDb.getCharacter(characterId));
77 }
78
79 cancelDelete (account, characterId)
80 {
81 const character = this.getOwned(account.account_id, characterId);
82 if (!character.deletion_scheduled_at)
83 { throw userError("That character is not marked for deletion."); }
84
85 this.stateDb.cancelDeletion(characterId);
86 this.#notify(account, `Deletion cancelled for ${character.first_name} ${character.last_name}.`);
87 return publicCharacter(this.stateDb.getCharacter(characterId));
88 }
89
90 processDueDeletions (now = Date.now(), despawn = null)
91 {
92 for (const character of this.stateDb.listDueDeletions(now))
93 {
94 despawn?.(character.character_id);
95 this.stateDb.deleteCharacter(character.character_id);
96 this.log.info?.(
97 `[mail-stub] Permanently deleted ${character.first_name} ${character.last_name} for account ${character.account_id}`
98 );
99 }
100 }
101
102 #notify (account, message)
103 {
104 if (account.email)
105 { this.log.info?.(`[mail] ${account.email}: ${message}`); }
106 else
107 { this.log.info?.(`[mail-stub] ${account.auth_subject}: ${message}`); }
108 }
109}
110
111export function publicCharacter (row)
112{
113 const sliders = normalizeHairSliders({
114 hue: row.hair_hue,
115 saturation: row.hair_saturation,
116 darkness: row.hair_darkness
117 });
118 return {
119 characterId: row.character_id,
120 firstName: row.first_name,
121 lastName: row.last_name,
122 sex: row.sex,
123 tileX: row.tile_x,
124 tileY: row.tile_y,
125 gait: row.gait,
126 deletionScheduledAt: row.deletion_scheduled_at ?? null,
127 hairStyle: row.hair_style ?? null,
128 hairHue: sliders.hue,
129 hairSaturation: sliders.saturation,
130 hairDarkness: sliders.darkness
131 };
132}
133
134export function normalizeCreateHair (sex, hair)
135{
136 if (!isValidHairStyle(sex, hair?.style))
137 { throw userError("Unknown hair style."); }
138 const style = normalizeHairStyle(sex, hair?.style ?? DEFAULT_HAIR_STYLE);
139 const sliders = normalizeHairSliders(hair);
140 return {
141 style,
142 hue: sliders.hue,
143 saturation: sliders.saturation,
144 darkness: sliders.darkness
145 };
146}
147
148function userError (message)
149{
150 const error = new Error(message);
151 error.userFacing = true;
152 return error;
153}