BlueGit

timfa questward

Files

questward / server / src / debark / service.js

service.js

service.js 4.7 KiB Download Raw
1import { Component } from "../ecs/components.js";
2import {
3 barkItemIdForWood,
4 debarkedLogInstance,
5 isLogDebarked
6} from "../../../shared/definitions/woodItems.js";
7import { ItemFamilyId } from "../../../shared/definitions/tierFamilies.js";
8import { itemDefinition } from "../../../shared/definitions/items.js";
9import { resolveWoodcuttingAxe } from "../../../shared/definitions/gathering.js";
10import { bagView, addGroundItem, publicGroundItem } from "../items/bags.js";
11import { isAdjacentTile } from "../../../shared/navigation/adjacency.js";
12import { attachOrigins, craftOrigin, CraftOriginVerb } from "../../../shared/items/origin.js";
13import { stackQuantity } from "../../../shared/items/inventory.js";
14
15function userError (message)
16{
17 const error = new Error(message);
18 error.userFacing = true;
19 return error;
20}
21
22function characterDisplayName (world, handle)
23{
24 const appearance = world.ecs.get(handle, Component.appearance);
25 const name = [appearance?.firstName, appearance?.lastName].filter(Boolean).join(" ").trim();
26 return name || "someone";
27}
28
29function woodTierFromLogItemId (itemId)
30{
31 const def = itemDefinition(itemId);
32 if (!def || def.familyId !== ItemFamilyId.log)
33 { return null; }
34 return def.tier ?? null;
35}
36
37function axeForWood (world, handle, woodTier)
38{
39 const inventory = world.ecs.get(handle, Component.inventory);
40 const equipment = world.ecs.get(handle, Component.equipment);
41 const skills = world.ecs.get(handle, Component.skills);
42 return resolveWoodcuttingAxe({
43 inventorySlots: inventory?.slots,
44 equipmentSlots: equipment?.slots,
45 skills,
46 woodTier
47 });
48}
49
50function publishGround (item)
51{
52 return {
53 type: "ground-item-appear",
54 ...publicGroundItem(item),
55 stack: item.stack,
56 droppedAt: item.droppedAt,
57 source: item.source,
58 droppedByCharacterId: item.droppedByCharacterId,
59 expiresAt: item.expiresAt
60 };
61}
62
63/**
64 * Strip bark from a ground wood log. Requires an axe of the same tier gate as chopping
65 * that wood. Leaves a debarked log (same itemId + instance flag) and bark.
66 */
67export function debarkLog (world, sessionId, characterId, groundItemId, now = Date.now())
68{
69 const { handle } = world.requireOwnedEntity(sessionId, characterId);
70 const item = world.groundItems.get(groundItemId);
71 if (!item?.stack)
72 { throw userError("That item is no longer on the ground."); }
73
74 const woodTier = woodTierFromLogItemId(item.stack.itemId);
75 if (!woodTier)
76 { throw userError("That is not a log."); }
77 if (isLogDebarked(item.stack))
78 { throw userError("That log is already stripped of bark."); }
79
80 const position = world.ecs.get(handle, Component.position);
81 if (!isAdjacentTile(position.tileX, position.tileY, item.tileX, item.tileY)
82 && !(position.tileX === item.tileX && position.tileY === item.tileY))
83 { throw userError("Stand next to the log."); }
84
85 const axe = axeForWood(world, handle, woodTier);
86 if (!axe.ok)
87 { throw userError(axe.reason || "You need an axe to strip this bark."); }
88
89 const barkId = barkItemIdForWood(woodTier);
90 if (barkId == null)
91 { throw userError("That bark cannot be stripped."); }
92
93 const qty = stackQuantity(item.stack.quantity);
94 if (qty <= 0)
95 { throw userError("That item is no longer on the ground."); }
96
97 const events = [];
98 const despawnMs = world.settings.groundItemDespawnDurationMs ?? 600_000;
99 const origin = craftOrigin(CraftOriginVerb.made, characterDisplayName(world, handle));
100 const debarkedStack = attachOrigins({
101 itemId: item.stack.itemId,
102 quantity: 1,
103 instance: debarkedLogInstance(),
104 origins: item.stack.origins ?? []
105 });
106
107 if (qty === 1)
108 {
109 item.stack = debarkedStack;
110 events.push(publishGround(item));
111 }
112 else
113 {
114 item.stack = {
115 ...item.stack,
116 quantity: qty - 1
117 };
118 events.push(publishGround(item));
119
120 const debarkedItem = {
121 groundItemId: (world.nextGroundItemId = (world.nextGroundItemId ?? 1) + 1),
122 tileX: item.tileX,
123 tileY: item.tileY,
124 stack: debarkedStack,
125 droppedAt: now,
126 expiresAt: now + despawnMs,
127 source: "debark",
128 droppedByCharacterId: characterId
129 };
130 addGroundItem(world, debarkedItem);
131 events.push(publishGround(debarkedItem));
132 }
133
134 const barkItem = {
135 groundItemId: (world.nextGroundItemId = (world.nextGroundItemId ?? 1) + 1),
136 tileX: item.tileX,
137 tileY: item.tileY,
138 stack: attachOrigins({
139 itemId: barkId,
140 quantity: 1,
141 origins: [{ origin, quantity: 1 }]
142 }),
143 droppedAt: now,
144 expiresAt: now + despawnMs,
145 source: "debark",
146 droppedByCharacterId: characterId
147 };
148 addGroundItem(world, barkItem);
149 events.push(publishGround(barkItem));
150
151 return {
152 events,
153 private: bagView(world.ecs, handle)
154 };
155}
156
157export function handleDebarkCommand (world, sessionId, command, now = Date.now())
158{
159 if (command.type !== "debark-log")
160 { return null; }
161 return debarkLog(world, sessionId, command.characterId, command.groundItemId, now);
162}