BlueGit

timfa questward

Files

questward / server / src / barber / service.js

service.js

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