BlueGit

timfa questward

Files

questward / client / src / play / crafting.js

crafting.js

crafting.js 21.0 KiB Download Raw
1import
2{
3 itemDefinition,
4 itemDisplayName,
5 itemAssetPathForStack,
6 formatQuantity,
7 ItemId
8} from "/shared/definitions/items.js";
9import { craftingStationDefinition, CraftingStationId } from "/shared/definitions/craftingStations.js";
10import
11{
12 CRAFT_DURATION_MS,
13 canStartCraft,
14 countItemInSlots,
15 hasRecipeInputs,
16 recipesForStation
17} from "/shared/crafting/recipes.js";
18import { resolveTailorCraft } from "/shared/crafting/tailor.js";
19import
20{
21 CLOTH_TIER_ORDER,
22 CLOTH_DISPLAY_NAME,
23 ClothTier,
24 MATERIAL_TIER_ORDER,
25 MATERIAL_DISPLAY_NAME,
26 WOOD_TIER_ORDER,
27 WoodTier,
28 MaterialTier,
29 woodTierForMetal,
30 clothTierForWood
31} from "/shared/definitions/materialTiers.js";
32import { craftDurationMs } from "/shared/definitions/gathering.js";
33import { applyItemIcon } from "../render/potionIcon.js";
34
35export function initCrafting ({
36 socket,
37 MessageType,
38 getInventorySlots,
39 getSkillLevel
40})
41{
42 const backdrop = document.getElementById("craft-backdrop");
43 const panel = document.getElementById("craft-panel");
44 const stationName = document.getElementById("craft-station-name");
45 const recipeGrid = document.getElementById("craft-recipe-grid");
46 const detailEmpty = document.getElementById("craft-detail-empty");
47 const detailBody = document.getElementById("craft-detail-body");
48 const materialControls = document.getElementById("craft-material-controls");
49 const materialTierSelect = document.getElementById("craft-material-tier");
50 const dyeControls = document.getElementById("craft-dye-controls");
51 const dyePickBtn = document.getElementById("craft-dye-pick");
52 const dyeClearBtn = document.getElementById("craft-dye-clear");
53 const dyePreview = document.getElementById("craft-dye-preview");
54 const outputImg = document.getElementById("craft-output-img");
55 const outputName = document.getElementById("craft-output-name");
56 const inputsList = document.getElementById("craft-inputs");
57 const statusEl = document.getElementById("craft-status");
58 const progressWrap = document.getElementById("craft-progress");
59 const progressFill = document.getElementById("craft-progress-fill");
60 const craftButton = document.getElementById("craft-button");
61 const craftBatchButton = document.getElementById("craft-batch-button");
62 const craftBatchAllButton = document.getElementById("craft-batch-all-button");
63 const dyeBackdrop = document.getElementById("dye-picker-backdrop");
64 const dyePanel = document.getElementById("dye-picker-panel");
65 const dyeGrid = document.getElementById("dye-picker-grid");
66
67 let open = false;
68 let stationId = null;
69 let recipes = [];
70 let selectedRecipeId = null;
71 let selectedCraftGroup = null;
72 let selectedMaterial = ClothTier.cotton;
73 let dyeSlotIndex = null;
74 let crafting = false;
75 let craftTimer = null;
76 let craftAnimFrame = null;
77 let craftStartedAt = 0;
78
79 document.getElementById("craft-close").addEventListener("click", () => close());
80 backdrop.addEventListener("click", () => close());
81 craftButton.addEventListener("click", () => startCraft());
82 craftBatchButton?.addEventListener("click", () => startBatchCraft());
83 craftBatchAllButton?.addEventListener("click", () => startCraft({ batchAll: true }));
84 materialTierSelect?.addEventListener("change", () =>
85 {
86 selectedMaterial = materialTierSelect.value || selectedMaterial;
87 if (usesCraftGroups())
88 {
89 const rows = groupedDisplayRecipes();
90 if (!rows.some((row) => row.group === selectedCraftGroup))
91 { selectedCraftGroup = rows[0]?.group ?? null; }
92 syncSelectedRecipeFromMaterial();
93 }
94 renderRecipeGrid();
95 renderDetail();
96 });
97 dyePickBtn?.addEventListener("click", () =>
98 {
99 if (!isTailorStation())
100 { return; }
101 openDyePicker();
102 });
103 dyeClearBtn?.addEventListener("click", () =>
104 {
105 dyeSlotIndex = null;
106 renderDetail();
107 });
108 document.getElementById("dye-picker-close")?.addEventListener("click", () => closeDyePicker());
109 dyeBackdrop?.addEventListener("click", () => closeDyePicker());
110
111 function isTailorStation ()
112 {
113 return stationId === CraftingStationId.tailorBench;
114 }
115
116 function isAnvilStation ()
117 {
118 return stationId === CraftingStationId.anvil;
119 }
120
121 function isWoodworkingStation ()
122 {
123 return stationId === CraftingStationId.woodworkingBench;
124 }
125
126 function isCraftingBenchStation ()
127 {
128 return stationId === CraftingStationId.craftingBench;
129 }
130
131 function isSpinningWheelStation ()
132 {
133 return stationId === CraftingStationId.spinningWheel;
134 }
135
136 function isLoomStation ()
137 {
138 return stationId === CraftingStationId.loom;
139 }
140
141 function usesMaterialDropdown ()
142 {
143 return isTailorStation()
144 || isAnvilStation()
145 || isWoodworkingStation()
146 || isCraftingBenchStation()
147 || isSpinningWheelStation()
148 || isLoomStation();
149 }
150
151 /** One grid cell per craft group; material dropdown picks the tier variant. */
152 function usesCraftGroups ()
153 {
154 return isAnvilStation()
155 || isWoodworkingStation()
156 || isCraftingBenchStation()
157 || isSpinningWheelStation()
158 || isLoomStation();
159 }
160
161 function craftGroupKey (recipe)
162 {
163 return recipe.craftGroup ?? recipe.recipeId;
164 }
165
166 /** Map UI material selection to a recipe's xpMaterial tier. */
167 function recipeXpMaterialForSelection (recipe, material = selectedMaterial)
168 {
169 if (isCraftingBenchStation()
170 && (recipe.craftGroup === "bow"
171 || recipe.craftGroup === "fishing-rod"
172 || recipe.craftGroup === "staff"))
173 { return woodTierForMetal(material); }
174 if (isCraftingBenchStation() && recipe.craftGroup === "fishing-net")
175 { return clothTierForWood(woodTierForMetal(material)); }
176 return material;
177 }
178
179 function recipeMatchesMaterial (recipe, material = selectedMaterial)
180 {
181 if (!recipe?.xpMaterial)
182 { return true; }
183 return recipe.xpMaterial === recipeXpMaterialForSelection(recipe, material);
184 }
185
186 function selectedRecipe ()
187 {
188 return recipes.find((recipe) => recipe.recipeId === selectedRecipeId) ?? null;
189 }
190
191 function stationSkillLevel ()
192 {
193 const def = craftingStationDefinition(stationId);
194 return getSkillLevel?.(def?.skillId) ?? 1;
195 }
196
197 function activeRecipe ()
198 {
199 const base = selectedRecipe();
200 if (!base)
201 { return null; }
202 if (!base.tailorTemplate)
203 { return base; }
204 const slots = getInventorySlots?.() ?? [];
205 const dyeStack = dyeSlotIndex != null ? slots[dyeSlotIndex] : null;
206 const resolved = resolveTailorCraft({
207 templateId: base.tailorTemplate,
208 clothTier: selectedMaterial,
209 dyeStack
210 });
211 return resolved.ok ? resolved.recipe : base;
212 }
213
214 function refresh ()
215 {
216 if (!open)
217 { return; }
218 renderRecipeGrid();
219 renderDetail();
220 }
221
222 function materialOptionsForStation ()
223 {
224 if (isTailorStation() || isSpinningWheelStation() || isLoomStation())
225 {
226 return {
227 order: CLOTH_TIER_ORDER,
228 names: CLOTH_DISPLAY_NAME,
229 fallback: ClothTier.cotton
230 };
231 }
232 if (isWoodworkingStation())
233 {
234 return {
235 order: WOOD_TIER_ORDER,
236 names: MATERIAL_DISPLAY_NAME,
237 fallback: WoodTier.pine
238 };
239 }
240 // Crafting bench mixes metal tools, wood bows, and cloth fishing gear.
241 if (isCraftingBenchStation())
242 {
243 const names = {};
244 for (const metal of MATERIAL_TIER_ORDER)
245 {
246 const wood = woodTierForMetal(metal);
247 const cloth = clothTierForWood(wood);
248 names[metal] = `${MATERIAL_DISPLAY_NAME[metal] ?? metal}/`
249 + `${MATERIAL_DISPLAY_NAME[wood] ?? wood}/`
250 + `${CLOTH_DISPLAY_NAME[cloth] ?? cloth}`;
251 }
252 return {
253 order: MATERIAL_TIER_ORDER,
254 names,
255 fallback: MaterialTier.bronze
256 };
257 }
258 return {
259 order: MATERIAL_TIER_ORDER,
260 names: MATERIAL_DISPLAY_NAME,
261 fallback: MaterialTier.bronze
262 };
263 }
264
265 function fillMaterialTiers ()
266 {
267 if (!materialTierSelect)
268 { return; }
269 const { order, names, fallback } = materialOptionsForStation();
270 if (!order.includes(selectedMaterial))
271 { selectedMaterial = fallback; }
272 materialTierSelect.replaceChildren();
273 for (const tier of order)
274 {
275 const option = document.createElement("option");
276 option.value = tier;
277 option.textContent = names[tier] ?? tier;
278 materialTierSelect.append(option);
279 }
280 materialTierSelect.value = selectedMaterial;
281 }
282
283 function groupedDisplayRecipes ()
284 {
285 if (!usesCraftGroups())
286 {
287 return recipes.map((recipe) => ({
288 group: craftGroupKey(recipe),
289 recipe
290 }));
291 }
292 const byGroup = new Map();
293 for (const recipe of recipes)
294 {
295 const group = craftGroupKey(recipe);
296 if (!byGroup.has(group))
297 { byGroup.set(group, []); }
298 byGroup.get(group).push(recipe);
299 }
300 const rows = [];
301 for (const [group, variants] of byGroup)
302 {
303 if (!variants.some((entry) => recipeMatchesMaterial(entry)))
304 { continue; }
305 const recipe = variants.find((entry) => recipeMatchesMaterial(entry))
306 ?? variants[0];
307 rows.push({ group, recipe, variants });
308 }
309 return rows;
310 }
311
312 function syncSelectedRecipeFromMaterial ()
313 {
314 if (!usesCraftGroups() || !selectedCraftGroup)
315 { return; }
316 const variants = recipes.filter((recipe) => craftGroupKey(recipe) === selectedCraftGroup);
317 if (!variants.length)
318 { return; }
319 let match = variants.find((entry) => recipeMatchesMaterial(entry));
320 if (!match)
321 {
322 match = variants[0];
323 if (match?.xpMaterial && !(isCraftingBenchStation()
324 && (match.craftGroup === "bow"
325 || match.craftGroup === "fishing-rod"
326 || match.craftGroup === "fishing-net"
327 || match.craftGroup === "staff")))
328 {
329 selectedMaterial = match.xpMaterial;
330 if (materialTierSelect)
331 { materialTierSelect.value = selectedMaterial; }
332 }
333 }
334 selectedRecipeId = match.recipeId;
335 }
336
337 function renderRecipeGrid ()
338 {
339 recipeGrid.replaceChildren();
340 for (const row of groupedDisplayRecipes())
341 {
342 recipeGrid.append(renderRecipeCell(row));
343 }
344 }
345
346 function renderRecipeCell ({ group, recipe })
347 {
348 const def = itemDefinition(recipe.output.itemId);
349 const stack = { itemId: recipe.output.itemId, quantity: recipe.output.quantity | 0 || 1 };
350 const cell = document.createElement("button");
351 cell.type = "button";
352 cell.className = "craft-recipe-cell";
353 const selected = usesCraftGroups()
354 ? group === selectedCraftGroup
355 : recipe.recipeId === selectedRecipeId;
356 if (selected)
357 { cell.classList.add("selected"); }
358
359 const img = document.createElement("img");
360 img.alt = def?.displayName ?? "Item";
361 applyItemIcon(img, stack, itemAssetPathForStack(stack));
362
363 const label = document.createElement("span");
364 label.className = "craft-recipe-name";
365 if (recipe.tailorTemplate)
366 { label.textContent = def?.displayName?.replace(/^Cotton\s+/i, "") ?? "Garment"; }
367 else if (recipe.craftGroupLabel)
368 { label.textContent = recipe.craftGroupLabel; }
369 else
370 { label.textContent = def?.displayName ?? "Item"; }
371
372 cell.append(img, label);
373 cell.addEventListener("click", () =>
374 {
375 if (crafting)
376 { return; }
377 selectedCraftGroup = group;
378 if (usesCraftGroups())
379 { syncSelectedRecipeFromMaterial(); }
380 else
381 { selectedRecipeId = recipe.recipeId; }
382 renderRecipeGrid();
383 renderDetail();
384 });
385 return cell;
386 }
387
388 function renderDetail ()
389 {
390 if (materialControls)
391 { materialControls.hidden = !usesMaterialDropdown(); }
392 if (!isTailorStation())
393 { dyeSlotIndex = null; }
394 if (dyeControls)
395 { dyeControls.hidden = !isTailorStation(); }
396
397 const recipe = activeRecipe();
398 if (!recipe)
399 {
400 detailEmpty.hidden = false;
401 detailBody.hidden = true;
402 return;
403 }
404 detailEmpty.hidden = true;
405 detailBody.hidden = false;
406
407 const outputStack = {
408 itemId: recipe.output.itemId,
409 quantity: Math.max(1, recipe.output.quantity | 0 || 1),
410 ...(recipe.dyeColor ? { instance: { kind: "clothing", dyeColor: recipe.dyeColor } } : {})
411 };
412 applyItemIcon(outputImg, outputStack, itemAssetPathForStack(outputStack));
413 outputImg.alt = itemDisplayName(outputStack);
414 const qtySuffix = outputStack.quantity > 1 ? ` ×${formatQuantity(outputStack.quantity)}` : "";
415 outputName.textContent = `${itemDisplayName(outputStack)}${qtySuffix}`;
416
417 inputsList.replaceChildren();
418 const slots = getInventorySlots?.() ?? [];
419 for (const input of recipe.inputs ?? [])
420 {
421 inputsList.append(renderInputRow(input, slots));
422 }
423 if (recipe.consumeDye)
424 {
425 const dye = dyeSlotIndex != null ? slots[dyeSlotIndex] : null;
426 const row = document.createElement("li");
427 row.className = "craft-input-row";
428 row.textContent = dye
429 ? `Dye — 1 use (${dye.instance?.fillLevel ?? "?"}/5)`
430 : "Dye — none selected";
431 inputsList.append(row);
432 }
433
434 if (dyePreview && dyeClearBtn)
435 {
436 const dye = dyeSlotIndex != null ? slots[dyeSlotIndex] : null;
437 if (dye?.itemId === ItemId.dye)
438 {
439 dyePreview.hidden = false;
440 dyeClearBtn.hidden = false;
441 applyItemIcon(dyePreview, dye, itemAssetPathForStack(dye));
442 }
443 else
444 {
445 dyePreview.hidden = true;
446 dyeClearBtn.hidden = true;
447 }
448 }
449
450 updateCraftState(slots, recipe);
451 }
452
453 function renderInputRow (input, slots)
454 {
455 const need = input.quantity | 0;
456 const have = countItemInSlots(slots, input.itemId);
457 const stack = { itemId: input.itemId, quantity: need };
458 const row = document.createElement("li");
459 row.className = "craft-input-row";
460 if (have < need)
461 { row.classList.add("short"); }
462
463 const img = document.createElement("img");
464 img.alt = itemDisplayName(stack);
465 applyItemIcon(img, stack, itemAssetPathForStack(stack));
466
467 const text = document.createElement("span");
468 text.textContent = `${itemDisplayName(stack)} — ${formatQuantity(have)} / ${formatQuantity(need)}`;
469 row.append(img, text);
470 return row;
471 }
472
473 function updateCraftState (slots, recipe)
474 {
475 const batchVisible = isCraftingBenchStation();
476 if (craftBatchButton)
477 { craftBatchButton.hidden = !batchVisible; }
478 if (craftBatchAllButton)
479 { craftBatchAllButton.hidden = !batchVisible; }
480
481 if (crafting)
482 {
483 craftButton.disabled = true;
484 if (craftBatchButton)
485 { craftBatchButton.disabled = true; }
486 if (craftBatchAllButton)
487 { craftBatchAllButton.disabled = true; }
488 return;
489 }
490 const skill = stationSkillLevel();
491 const minLevel = recipe.minSkillLevel | 0 || 1;
492 if (skill < minLevel)
493 {
494 statusEl.textContent = `Requires skill level ${minLevel} (you have ${skill}).`;
495 craftButton.disabled = true;
496 if (craftBatchButton)
497 { craftBatchButton.disabled = true; }
498 if (craftBatchAllButton)
499 { craftBatchAllButton.disabled = true; }
500 return;
501 }
502 const slotsSnapshot = slots ?? getInventorySlots?.() ?? [];
503 const craftOpts = { dyeSlotIndex };
504 if (canStartCraft(slotsSnapshot, recipe, skill, craftOpts))
505 {
506 statusEl.textContent = "";
507 craftButton.disabled = false;
508 if (craftBatchButton)
509 { craftBatchButton.disabled = !batchVisible; }
510 if (craftBatchAllButton)
511 { craftBatchAllButton.disabled = !batchVisible; }
512 return;
513 }
514 if (!hasRecipeInputs(slotsSnapshot, recipe))
515 {
516 statusEl.textContent = "Missing materials.";
517 }
518 else if (recipe.consumeDye && dyeSlotIndex != null)
519 {
520 statusEl.textContent = "Not enough bag space (need room for the result or a partially used dye).";
521 }
522 else
523 {
524 statusEl.textContent = "Not enough bag space for the result.";
525 }
526 craftButton.disabled = true;
527 if (craftBatchButton)
528 { craftBatchButton.disabled = true; }
529 if (craftBatchAllButton)
530 { craftBatchAllButton.disabled = true; }
531 }
532
533 function openDyePicker ()
534 {
535 if (!isTailorStation())
536 { return; }
537 const slots = getInventorySlots?.() ?? [];
538 dyeGrid.replaceChildren();
539 let found = false;
540 slots.forEach((stack, index) =>
541 {
542 if (!stack || stack.itemId !== ItemId.dye)
543 { return; }
544 found = true;
545 const cell = document.createElement("button");
546 cell.type = "button";
547 cell.className = "craft-recipe-cell craft-dye-cell";
548 cell.title = "Dye";
549 const img = document.createElement("img");
550 applyItemIcon(img, stack, itemAssetPathForStack(stack));
551 img.alt = "Dye";
552 cell.append(img);
553 cell.addEventListener("click", () =>
554 {
555 dyeSlotIndex = index;
556 closeDyePicker();
557 renderDetail();
558 });
559 dyeGrid.append(cell);
560 });
561 if (!found)
562 {
563 const empty = document.createElement("p");
564 empty.className = "hint";
565 empty.textContent = "No dyes in your inventory.";
566 dyeGrid.append(empty);
567 }
568 dyePanel.hidden = false;
569 dyeBackdrop.hidden = false;
570 }
571
572 function closeDyePicker ()
573 {
574 dyePanel.hidden = true;
575 dyeBackdrop.hidden = true;
576 }
577
578 function startBatchCraft ()
579 {
580 const raw = window.prompt("How many?", "1");
581 if (raw == null)
582 { return; }
583 const count = Number.parseInt(raw, 10);
584 if (!Number.isFinite(count) || count < 1)
585 { return; }
586 startCraft({ batchCount: count });
587 }
588
589 function startCraft (options = {})
590 {
591 const recipe = activeRecipe();
592 if (!recipe || crafting)
593 { return; }
594 const slots = getInventorySlots?.() ?? [];
595 if (!canStartCraft(slots, recipe, stationSkillLevel(), { dyeSlotIndex }))
596 {
597 renderDetail();
598 return;
599 }
600 crafting = true;
601 craftButton.disabled = true;
602 if (craftBatchButton)
603 { craftBatchButton.disabled = true; }
604 if (craftBatchAllButton)
605 { craftBatchAllButton.disabled = true; }
606 progressWrap.hidden = false;
607 progressFill.style.width = "0%";
608 statusEl.textContent = options.batchAll
609 ? "Crafting all…"
610 : options.batchCount > 1
611 ? `Crafting ${options.batchCount}…`
612 : "Crafting…";
613 craftStartedAt = performance.now();
614 const durationMs = craftDurationMs(recipe, CRAFT_DURATION_MS);
615 const payload = {
616 type: MessageType.startCraft,
617 stationId,
618 recipeId: selectedRecipeId
619 };
620 if (options.batchCount != null)
621 { payload.batchCount = options.batchCount | 0; }
622 if (options.batchAll)
623 { payload.batchAll = true; }
624 if (isTailorStation())
625 {
626 payload.clothTier = selectedMaterial;
627 payload.dyeSlotIndex = dyeSlotIndex;
628 }
629 socket.send(payload);
630 animateProgress(durationMs);
631 craftTimer = window.setTimeout(() => finishCraft(true), durationMs);
632 }
633
634 function animateProgress (durationMs)
635 {
636 const tick = (now) =>
637 {
638 if (!crafting)
639 { return; }
640 const elapsed = now - craftStartedAt;
641 const pct = Math.min(100, (elapsed / durationMs) * 100);
642 progressFill.style.width = `${pct}%`;
643 if (pct < 100)
644 { craftAnimFrame = requestAnimationFrame(tick); }
645 };
646 craftAnimFrame = requestAnimationFrame(tick);
647 }
648
649 function stopCraftAnimation ()
650 {
651 if (craftTimer != null)
652 {
653 clearTimeout(craftTimer);
654 craftTimer = null;
655 }
656 if (craftAnimFrame != null)
657 {
658 cancelAnimationFrame(craftAnimFrame);
659 craftAnimFrame = null;
660 }
661 crafting = false;
662 progressWrap.hidden = true;
663 progressFill.style.width = "0%";
664 }
665
666 function finishCraft (timedOut)
667 {
668 stopCraftAnimation();
669 if (timedOut)
670 {
671 statusEl.textContent = "Waiting for server…";
672 }
673 renderDetail();
674 }
675
676 function openStation (nextStationId)
677 {
678 const def = craftingStationDefinition(nextStationId);
679 if (!def)
680 { return; }
681 stationId = nextStationId;
682 recipes = recipesForStation(nextStationId);
683 const { fallback } = materialOptionsForStation();
684 selectedMaterial = fallback;
685 dyeSlotIndex = null;
686 fillMaterialTiers();
687 const first = groupedDisplayRecipes()[0];
688 selectedCraftGroup = first?.group ?? null;
689 if (usesCraftGroups())
690 { syncSelectedRecipeFromMaterial(); }
691 else
692 { selectedRecipeId = first?.recipe?.recipeId ?? recipes[0]?.recipeId ?? null; }
693 stationName.textContent = def.displayName;
694 open = true;
695 panel.hidden = false;
696 backdrop.hidden = false;
697 statusEl.textContent = recipes.length
698 ? "Select a recipe."
699 : "No recipes defined for this station yet.";
700 renderRecipeGrid();
701 renderDetail();
702 }
703
704 function close ()
705 {
706 if (crafting)
707 {
708 socket.send({ type: MessageType.cancelCraft });
709 stopCraftAnimation();
710 }
711 closeDyePicker();
712 open = false;
713 stationId = null;
714 recipes = [];
715 selectedRecipeId = null;
716 selectedCraftGroup = null;
717 dyeSlotIndex = null;
718 panel.hidden = true;
719 backdrop.hidden = true;
720 detailEmpty.hidden = false;
721 detailBody.hidden = true;
722 statusEl.textContent = "";
723 }
724
725 function handleCraftResult (message)
726 {
727 if (!open || message.recipeId !== selectedRecipeId)
728 { return; }
729 if (message.ok && message.continuing)
730 {
731 const recipe = activeRecipe();
732 if (!recipe)
733 {
734 stopCraftAnimation();
735 renderDetail();
736 return;
737 }
738 crafting = true;
739 craftButton.disabled = true;
740 if (craftBatchButton)
741 { craftBatchButton.disabled = true; }
742 if (craftBatchAllButton)
743 { craftBatchAllButton.disabled = true; }
744 progressWrap.hidden = false;
745 progressFill.style.width = "0%";
746 statusEl.textContent = "Crafting…";
747 craftStartedAt = performance.now();
748 const durationMs = craftDurationMs(recipe, CRAFT_DURATION_MS);
749 if (craftTimer != null)
750 { clearTimeout(craftTimer); }
751 if (craftAnimFrame != null)
752 {
753 cancelAnimationFrame(craftAnimFrame);
754 craftAnimFrame = null;
755 }
756 animateProgress(durationMs);
757 craftTimer = window.setTimeout(() => finishCraft(true), durationMs);
758 renderDetail();
759 return;
760 }
761 stopCraftAnimation();
762 if (message.ok)
763 {
764 statusEl.textContent = message.burned
765 ? "The food burned."
766 : "Craft complete.";
767 }
768 else
769 {
770 statusEl.textContent = message.error ?? "Could not craft.";
771 }
772 renderDetail();
773 }
774
775 return {
776 openStation,
777 close,
778 refresh,
779 handleCraftResult,
780 isOpen ()
781 {
782 return open;
783 },
784 isBlocking ()
785 {
786 if (!open)
787 { return false; }
788 if (backdrop && !backdrop.hidden)
789 { return true; }
790 const x = GameInput.mousePosition.x;
791 const y = GameInput.mousePosition.y;
792 const box = panel.getBoundingClientRect();
793 return x >= box.left && x < box.right && y >= box.top && y < box.bottom;
794 },
795 reset ()
796 {
797 close();
798 }
799 };
800}