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