BlueGit

timfa questward

Files

questward / client / src / play / crafting.js

crafting.js

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