import { itemDefinition, itemDisplayName, itemAssetPathForStack, formatQuantity } from "/shared/definitions/items.js"; import { craftingStationDefinition } from "/shared/definitions/craftingStations.js"; import { CRAFT_DURATION_MS, canStartCraft, countItemInSlots, hasRecipeInputs, recipesForStation } from "/shared/crafting/recipes.js"; import { applyItemIcon } from "../render/potionIcon.js"; export function initCrafting ({ socket, MessageType, getInventorySlots, getSkillLevel }) { const backdrop = document.getElementById("craft-backdrop"); const panel = document.getElementById("craft-panel"); const stationName = document.getElementById("craft-station-name"); const recipeGrid = document.getElementById("craft-recipe-grid"); const detailEmpty = document.getElementById("craft-detail-empty"); const detailBody = document.getElementById("craft-detail-body"); const outputImg = document.getElementById("craft-output-img"); const outputName = document.getElementById("craft-output-name"); const inputsList = document.getElementById("craft-inputs"); const statusEl = document.getElementById("craft-status"); const progressWrap = document.getElementById("craft-progress"); const progressFill = document.getElementById("craft-progress-fill"); const craftButton = document.getElementById("craft-button"); let open = false; let stationId = null; let recipes = []; let selectedRecipeId = null; let crafting = false; let craftTimer = null; let craftAnimFrame = null; let craftStartedAt = 0; document.getElementById("craft-close").addEventListener("click", () => close()); backdrop.addEventListener("click", () => close()); craftButton.addEventListener("click", () => startCraft()); function selectedRecipe () { return recipes.find((recipe) => recipe.recipeId === selectedRecipeId) ?? null; } function stationSkillLevel () { const def = craftingStationDefinition(stationId); return getSkillLevel?.(def?.skillId) ?? 1; } function refresh () { if (!open) { return; } renderRecipeGrid(); renderDetail(); } function renderRecipeGrid () { recipeGrid.replaceChildren(); for (const recipe of recipes) { recipeGrid.append(renderRecipeCell(recipe)); } } function renderRecipeCell (recipe) { const def = itemDefinition(recipe.output.itemId); const stack = { itemId: recipe.output.itemId, quantity: recipe.output.quantity | 0 || 1 }; const cell = document.createElement("button"); cell.type = "button"; cell.className = "craft-recipe-cell"; if (recipe.recipeId === selectedRecipeId) { cell.classList.add("selected"); } const img = document.createElement("img"); img.alt = def?.displayName ?? "Item"; applyItemIcon(img, stack, itemAssetPathForStack(stack)); const label = document.createElement("span"); label.className = "craft-recipe-name"; label.textContent = def?.displayName ?? "Item"; cell.append(img, label); cell.addEventListener("click", () => { if (crafting) { return; } selectedRecipeId = recipe.recipeId; renderRecipeGrid(); renderDetail(); }); return cell; } function renderDetail () { const recipe = selectedRecipe(); if (!recipe) { detailEmpty.hidden = false; detailBody.hidden = true; return; } detailEmpty.hidden = true; detailBody.hidden = false; const outputStack = { itemId: recipe.output.itemId, quantity: Math.max(1, recipe.output.quantity | 0 || 1) }; applyItemIcon(outputImg, outputStack, itemAssetPathForStack(outputStack)); outputImg.alt = itemDisplayName(outputStack); const qtySuffix = outputStack.quantity > 1 ? ` ×${formatQuantity(outputStack.quantity)}` : ""; outputName.textContent = `${itemDisplayName(outputStack)}${qtySuffix}`; inputsList.replaceChildren(); const slots = getInventorySlots?.() ?? []; for (const input of recipe.inputs ?? []) { inputsList.append(renderInputRow(input, slots)); } updateCraftState(slots, recipe); } function renderInputRow (input, slots) { const need = input.quantity | 0; const have = countItemInSlots(slots, input.itemId); const stack = { itemId: input.itemId, quantity: need }; const row = document.createElement("li"); row.className = "craft-input-row"; if (have < need) { row.classList.add("short"); } const img = document.createElement("img"); img.alt = itemDisplayName(stack); applyItemIcon(img, stack, itemAssetPathForStack(stack)); const text = document.createElement("span"); text.textContent = `${itemDisplayName(stack)} — ${formatQuantity(have)} / ${formatQuantity(need)}`; row.append(img, text); return row; } function updateCraftState (slots, recipe) { if (crafting) { craftButton.disabled = true; return; } const skill = stationSkillLevel(); const minLevel = recipe.minSkillLevel | 0 || 1; if (skill < minLevel) { statusEl.textContent = `Requires skill level ${minLevel} (you have ${skill}).`; craftButton.disabled = true; return; } const slotsSnapshot = slots ?? getInventorySlots?.() ?? []; if (canStartCraft(slotsSnapshot, recipe, skill)) { statusEl.textContent = `Crafting takes ${CRAFT_DURATION_MS / 1000} seconds.`; craftButton.disabled = false; return; } if (!hasRecipeInputs(slotsSnapshot, recipe)) { statusEl.textContent = "Missing materials."; } else { statusEl.textContent = "Not enough bag space for the result."; } craftButton.disabled = true; } function startCraft () { const recipe = selectedRecipe(); if (!recipe || crafting) { return; } const slots = getInventorySlots?.() ?? []; if (!canStartCraft(slots, recipe, stationSkillLevel())) { renderDetail(); return; } crafting = true; craftButton.disabled = true; progressWrap.hidden = false; progressFill.style.width = "0%"; statusEl.textContent = "Crafting…"; craftStartedAt = performance.now(); socket.send({ type: MessageType.startCraft, stationId, recipeId: recipe.recipeId }); animateProgress(); craftTimer = window.setTimeout(() => finishCraft(true), CRAFT_DURATION_MS); } function animateProgress () { const tick = (now) => { if (!crafting) { return; } const elapsed = now - craftStartedAt; const pct = Math.min(100, (elapsed / CRAFT_DURATION_MS) * 100); progressFill.style.width = `${pct}%`; if (pct < 100) { craftAnimFrame = requestAnimationFrame(tick); } }; craftAnimFrame = requestAnimationFrame(tick); } function stopCraftAnimation () { if (craftTimer != null) { clearTimeout(craftTimer); craftTimer = null; } if (craftAnimFrame != null) { cancelAnimationFrame(craftAnimFrame); craftAnimFrame = null; } crafting = false; progressWrap.hidden = true; progressFill.style.width = "0%"; } function finishCraft (timedOut) { stopCraftAnimation(); if (timedOut) { statusEl.textContent = "Waiting for server…"; } renderDetail(); } function openStation (nextStationId) { const def = craftingStationDefinition(nextStationId); if (!def) { return; } stationId = nextStationId; recipes = recipesForStation(nextStationId); selectedRecipeId = recipes[0]?.recipeId ?? null; stationName.textContent = def.displayName; open = true; panel.hidden = false; backdrop.hidden = false; statusEl.textContent = recipes.length ? "Select a recipe." : "No recipes defined for this station yet."; renderRecipeGrid(); renderDetail(); } function close () { if (crafting) { socket.send({ type: MessageType.cancelCraft }); stopCraftAnimation(); } open = false; stationId = null; recipes = []; selectedRecipeId = null; panel.hidden = true; backdrop.hidden = true; detailEmpty.hidden = false; detailBody.hidden = true; statusEl.textContent = ""; } function handleCraftResult (message) { if (!open || message.recipeId !== selectedRecipeId) { return; } stopCraftAnimation(); if (message.ok) { statusEl.textContent = message.burned ? "The food burned." : "Craft complete."; } else { statusEl.textContent = message.error ?? "Could not craft."; } renderDetail(); } return { openStation, close, refresh, handleCraftResult, isOpen () { return open; }, isBlocking () { if (!open) { return false; } if (backdrop && !backdrop.hidden) { return true; } const x = GameInput.mousePosition.x; const y = GameInput.mousePosition.y; const box = panel.getBoundingClientRect(); return x >= box.left && x < box.right && y >= box.top && y < box.bottom; }, reset () { close(); } }; }