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