BlueGit

timfa questward

Files

questward / server / src / characters / service.js

service.js

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