BlueGit

timfa questward

Files

questward / client / src / play / crafting.js

crafting.js

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