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