BlueGit

timfa questward

Files

questward / server / src / barber / service.js

service.js

service.js 3.5 KiB Download Raw
1import {
2 DEFAULT_HAIR_STYLE,
3 hairFromCharacterRow,
4 isValidHairStyle,
5 listHairStyles,
6 normalizeHairSliders,
7 normalizeHairStyle
8} from "../../../shared/appearance/hair.js";
9import { BlockType } from "../../../shared/definitions/tiles.js";
10import { CHUNK_SIZE } from "../../../shared/protocol/constants.js";
11import { tileToChunk, tileToLocal } from "../world/coordinates.js";
12
13function userError (message)
14{
15 const error = new Error(message);
16 error.userFacing = true;
17 return error;
18}
19
20export class BarberService
21{
22 constructor (stateDb, simulation)
23 {
24 this.stateDb = stateDb;
25 this.simulation = simulation;
26 }
27
28 handleCommand (sessionId, command)
29 {
30 switch (command.type)
31 {
32 case "open-barber":
33 return this.open(sessionId, command.characterId);
34 case "set-hair":
35 return this.setHair(sessionId, command.characterId, command.hair);
36 case "close-barber":
37 return { events: [{ type: "barber-closed", sessionId, characterId: command.characterId }] };
38 default:
39 return null;
40 }
41 }
42
43 open (sessionId, characterId)
44 {
45 this.#requireNearBarber(characterId);
46 const row = this.stateDb.getCharacter(characterId);
47 if (!row)
48 { throw userError("Character not found."); }
49 const hair = hairFromCharacterRow(row);
50 const sex = row.sex === "female" ? "female" : "male";
51 return {
52 events: [{
53 type: "barber-snapshot",
54 sessionId,
55 characterId,
56 sex,
57 styles: listHairStyles(sex),
58 hair: {
59 style: hair?.style ?? DEFAULT_HAIR_STYLE,
60 hue: hair?.hue ?? 30,
61 saturation: hair?.saturation ?? 0.55,
62 darkness: hair?.darkness ?? 0.45
63 }
64 }]
65 };
66 }
67
68 setHair (sessionId, characterId, hairInput)
69 {
70 this.#requireNearBarber(characterId);
71 const row = this.stateDb.getCharacter(characterId);
72 if (!row)
73 { throw userError("Character not found."); }
74 const sex = row.sex === "female" ? "female" : "male";
75 if (!isValidHairStyle(sex, hairInput?.style))
76 { throw userError("Unknown hair style."); }
77 const style = normalizeHairStyle(sex, hairInput?.style ?? DEFAULT_HAIR_STYLE);
78 const sliders = normalizeHairSliders(hairInput);
79 const hair = {
80 style,
81 hue: sliders.hue,
82 saturation: sliders.saturation,
83 darkness: sliders.darkness
84 };
85 this.simulation.setCharacterHair(characterId, hair);
86 return {
87 events: [{
88 type: "barber-snapshot",
89 sessionId,
90 characterId,
91 sex,
92 styles: listHairStyles(sex),
93 hair,
94 message: "Hair updated."
95 }]
96 };
97 }
98
99 #requireNearBarber (characterId)
100 {
101 const entityId = this.simulation.characterEntities.get(characterId);
102 const position = entityId ? this.simulation.positionOf(entityId) : null;
103 if (!position)
104 { throw userError("Enter the world before visiting the barber."); }
105 if (!isAdjacentToBarber(this.simulation, position.tileX, position.tileY))
106 { throw userError("Stand next to a Barber."); }
107 return position;
108 }
109}
110
111function isAdjacentToBarber (simulation, tileX, tileY)
112{
113 for (let dy = -1; dy <= 1; dy++)
114 {
115 for (let dx = -1; dx <= 1; dx++)
116 {
117 if (blockTypeAt(simulation, tileX + dx, tileY + dy) === BlockType.barber)
118 { return true; }
119 }
120 }
121 return false;
122}
123
124function blockTypeAt (simulation, tileX, tileY)
125{
126 const chunkX = tileToChunk(tileX);
127 const chunkY = tileToChunk(tileY);
128 for (const worker of simulation.workers ?? [])
129 {
130 const data = worker.world?.chunkData?.(chunkX, chunkY);
131 if (!data?.blocks)
132 { continue; }
133 const index = tileToLocal(tileY) * CHUNK_SIZE + tileToLocal(tileX);
134 const typeId = data.blocks[index]?.typeId;
135 if (typeId != null)
136 { return typeId; }
137 }
138 return null;
139}