BlueGit

timfa questward

Files

questward / server / src / characters / service.js

service.js

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