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