import { DEFAULT_HAIR_STYLE, hairFromCharacterRow, isValidHairStyle, listHairStyles, normalizeHairSliders, normalizeHairStyle } from "../../../shared/appearance/hair.js"; import { BlockType } from "../../../shared/definitions/tiles.js"; import { CHUNK_SIZE } from "../../../shared/protocol/constants.js"; import { tileToChunk, tileToLocal } from "../world/coordinates.js"; function userError (message) { const error = new Error(message); error.userFacing = true; return error; } export class BarberService { constructor (stateDb, simulation) { this.stateDb = stateDb; this.simulation = simulation; } handleCommand (sessionId, command) { switch (command.type) { case "open-barber": return this.open(sessionId, command.characterId); case "set-hair": return this.setHair(sessionId, command.characterId, command.hair); case "close-barber": return { events: [{ type: "barber-closed", sessionId, characterId: command.characterId }] }; default: return null; } } open (sessionId, characterId) { this.#requireNearBarber(characterId); const row = this.stateDb.getCharacter(characterId); if (!row) { throw userError("Character not found."); } const hair = hairFromCharacterRow(row); const sex = row.sex === "female" ? "female" : "male"; return { events: [{ type: "barber-snapshot", sessionId, characterId, sex, styles: listHairStyles(sex), hair: { style: hair?.style ?? DEFAULT_HAIR_STYLE, hue: hair?.hue ?? 30, saturation: hair?.saturation ?? 0.55, darkness: hair?.darkness ?? 0.45 } }] }; } setHair (sessionId, characterId, hairInput) { this.#requireNearBarber(characterId); const row = this.stateDb.getCharacter(characterId); if (!row) { throw userError("Character not found."); } const sex = row.sex === "female" ? "female" : "male"; if (!isValidHairStyle(sex, hairInput?.style)) { throw userError("Unknown hair style."); } const style = normalizeHairStyle(sex, hairInput?.style ?? DEFAULT_HAIR_STYLE); const sliders = normalizeHairSliders(hairInput); const hair = { style, hue: sliders.hue, saturation: sliders.saturation, darkness: sliders.darkness }; this.simulation.setCharacterHair(characterId, hair); return { events: [{ type: "barber-snapshot", sessionId, characterId, sex, styles: listHairStyles(sex), hair, message: "Hair updated." }] }; } #requireNearBarber (characterId) { const entityId = this.simulation.characterEntities.get(characterId); const position = entityId ? this.simulation.positionOf(entityId) : null; if (!position) { throw userError("Enter the world before visiting the barber."); } if (!isAdjacentToBarber(this.simulation, position.tileX, position.tileY)) { throw userError("Stand next to a Barber."); } return position; } } function isAdjacentToBarber (simulation, tileX, tileY) { for (let dy = -1; dy <= 1; dy++) { for (let dx = -1; dx <= 1; dx++) { if (dx === 0 && dy === 0) { continue; } if (blockTypeAt(simulation, tileX + dx, tileY + dy) === BlockType.barber) { return true; } } } return false; } function blockTypeAt (simulation, tileX, tileY) { const chunkX = tileToChunk(tileX); const chunkY = tileToChunk(tileY); for (const worker of simulation.workers ?? []) { const data = worker.world?.chunkData?.(chunkX, chunkY); if (!data?.blocks) { continue; } const index = tileToLocal(tileY) * CHUNK_SIZE + tileToLocal(tileX); const typeId = data.blocks[index]?.typeId; if (typeId != null) { return typeId; } } return null; }