| 1 | import { |
| 2 | DEFAULT_HAIR_STYLE, |
| 3 | hairFromCharacterRow, |
| 4 | isValidHairStyle, |
| 5 | listHairStyles, |
| 6 | normalizeHairSliders, |
| 7 | normalizeHairStyle |
| 8 | } from "../../../shared/appearance/hair.js"; |
| 9 | import { BlockType } from "../../../shared/definitions/tiles.js"; |
| 10 | import { CHUNK_SIZE } from "../../../shared/protocol/constants.js"; |
| 11 | import { tileToChunk, tileToLocal } from "../world/coordinates.js"; |
| 12 | |
| 13 | function userError (message) |
| 14 | { |
| 15 | const error = new Error(message); |
| 16 | error.userFacing = true; |
| 17 | return error; |
| 18 | } |
| 19 | |
| 20 | export 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 | |
| 111 | function 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 (dx === 0 && dy === 0) |
| 118 | { continue; } |
| 119 | if (blockTypeAt(simulation, tileX + dx, tileY + dy) === BlockType.barber) |
| 120 | { return true; } |
| 121 | } |
| 122 | } |
| 123 | return false; |
| 124 | } |
| 125 | |
| 126 | function blockTypeAt (simulation, tileX, tileY) |
| 127 | { |
| 128 | const chunkX = tileToChunk(tileX); |
| 129 | const chunkY = tileToChunk(tileY); |
| 130 | for (const worker of simulation.workers ?? []) |
| 131 | { |
| 132 | const data = worker.world?.chunkData?.(chunkX, chunkY); |
| 133 | if (!data?.blocks) |
| 134 | { continue; } |
| 135 | const index = tileToLocal(tileY) * CHUNK_SIZE + tileToLocal(tileX); |
| 136 | const typeId = data.blocks[index]?.typeId; |
| 137 | if (typeId != null) |
| 138 | { return typeId; } |
| 139 | } |
| 140 | return null; |
| 141 | } |