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