import { createConnection, joinStudentRoom, student } from "./net.js"; const statusText = document.getElementById("game-status"); const fishPerHour = document.getElementById("fish_p_hour"); const woodPerHour = document.getElementById("wood_p_hour"); const fishStored = document.getElementById("fish_stored"); const woodStored = document.getElementById("wood_stored"); const preserverText = document.getElementById("preserver"); const boatText = document.getElementById("boat"); const hoursText = document.getElementById("hours"); const daysText = document.getElementById("days"); const foundText = document.getElementById("found"); const fishButton = document.getElementById("go-fishing"); const forageButton = document.getElementById("go-foraging"); const discoverButton = document.getElementById("discover"); const foundList = document.getElementById("found-list"); const improveFishingButton = document.getElementById("improve-fishing"); const improveForagingButton = document.getElementById("improve-foraging"); const buildPreserverButton = document.getElementById("build-preserver"); const buildBoatButton = document.getElementById("build-boat"); const identity = student.get(); let state = null; let connected = false; let connection = null; function setText(element, value) { if (element) element.textContent = String(value); } function setStatus(text, tone = "") { if (!statusText) return; statusText.textContent = text; statusText.dataset.tone = tone; } function roomProblem(message) { student.clear(); window.alert(message || "You are no longer in that room."); window.location.href = "/"; } function render(nextState) { state = nextState; const you = nextState.you; setText(daysText, nextState.day); if (you) { setText(foundText, you.found.length); setText(fishPerHour, you.fishingPerHour); setText(woodPerHour, you.foragingPerHour); setText(fishStored, you.fish); setText(woodStored, you.wood); setText(preserverText, you.preserver ? "Yes" : "No"); setText(boatText, you.boat ? "Yes" : "No"); setText(hoursText, you.hours); if (foundList) { foundList.textContent = you.found.length === 0 ? "No castaways found yet. Use Discover a Castaway." : `Castaways you know: ${you.found.join(", ")}.`; } } const active = nextState.phase === "day"; const hours = you?.hours ?? 0; if (fishButton) fishButton.disabled = !active || hours <= 0; if (forageButton) forageButton.disabled = !active || hours <= 0; if (discoverButton) { const usedToday = you?.discoveryDay === nextState.day; const known = you?.found ?? []; const undiscovered = nextState.players.filter( (player) => player.name !== you?.name && player.connected && !known.includes(player.name), ); discoverButton.disabled = !active || usedToday || undiscovered.length === 0; discoverButton.textContent = usedToday ? "Found Someone Today" : undiscovered.length === 0 ? "No New Castaways" : "Discover a Castaway"; } if (improveFishingButton) improveFishingButton.disabled = !active || hours < 8; if (improveForagingButton) improveForagingButton.disabled = !active || hours < 8; if (buildPreserverButton) { buildPreserverButton.disabled = !active || !!you?.preserver || hours < 8 || (you?.wood ?? 0) < 25; } if (buildBoatButton) { buildBoatButton.disabled = !active || !!you?.boat || hours < 16 || (you?.wood ?? 0) < 100; } if (!active) return; if (hours <= 0) { setStatus( "You are out of hours. Waiting for the rest of the crew to finish.", ); } else { setStatus(`Day ${nextState.day}: ${hours} hours left.`, "good"); } } function askHours(verb) { if (!state?.you) return null; const hours = state.you.hours; if (hours <= 0) { setStatus("You have no hours left today."); return null; } const answer = window.prompt( `How many hours do you want to ${verb}? (1-${hours})`, ); if (answer === null) return null; const value = Number(answer); if (!Number.isInteger(value) || value < 1 || value > hours) { setStatus(`Enter a whole number of hours between 1 and ${hours}.`, "bad"); return null; } return value; } function sendAction(message) { if (!connected) { setStatus("Not connected yet. Please try again.", "bad"); return false; } if (!connection.send(message)) { setStatus("Not connected yet. Please try again.", "bad"); return false; } return true; } function goFishing() { const hours = askHours("fish"); if (hours === null) return; if (sendAction({ type: "FISH", hours })) { setStatus(`Spending ${hours} hours fishing...`); } } function goForaging() { const hours = askHours("forage"); if (hours === null) return; if (sendAction({ type: "FORAGE", hours })) { setStatus(`Spending ${hours} hours foraging...`); } } fishButton?.addEventListener("click", goFishing); forageButton?.addEventListener("click", goForaging); discoverButton?.addEventListener("click", () => { if (sendAction({ type: "DISCOVER" })) { discoverButton.disabled = true; setStatus("Searching the island for another castaway..."); } }); improveFishingButton?.addEventListener("click", () => { if (sendAction({ type: "IMPROVE_FISHING" })) { setStatus("Spending 8 hours improving fishing..."); } }); improveForagingButton?.addEventListener("click", () => { if (sendAction({ type: "IMPROVE_FORAGING" })) { setStatus("Spending 8 hours improving foraging..."); } }); buildPreserverButton?.addEventListener("click", () => { if (sendAction({ type: "BUILD_PRESERVER" })) { setStatus("Spending 8 hours and 25 wood on a preserver..."); } }); buildBoatButton?.addEventListener("click", () => { if (sendAction({ type: "BUILD_BOAT" })) { setStatus("Spending 16 hours and 100 wood on a boat..."); } }); if (!identity.name || !identity.code) { window.location.href = "/"; } else { setStatus("Connecting..."); const created = createConnection({ onOpen(send) { joinStudentRoom(send); }, onStatus(status) { connected = status === "connected"; if (status === "connecting") { setStatus("Connecting..."); } else if (status === "connected") { if (!state) setStatus("Connected. Loading the day..."); } else if (status === "replaced") { student.clear(); window.alert( "This game was opened in another tab. This tab has been disconnected.", ); window.location.href = "/"; } else if (status === "error") { setStatus("Connection error. Retrying...", "bad"); } else { setStatus("Connection lost. Reconnecting...", "bad"); } }, onMessage(data) { if (data.type === "SYNC") { if (data.phase === "lobby") { window.location.href = "/waiting"; return; } if (data.phase === "trading") { window.location.href = "/trading"; return; } render(data); return; } if (data.type === "NOTICE") { setStatus(data.message || "", data.tone || ""); return; } if (data.type !== "ERROR") return; if ( data.code === "ROOM_NOT_FOUND" || /already in use/i.test(data.message ?? "") ) { roomProblem(data.message); return; } setStatus(data.message || "Something went wrong.", "bad"); }, }); connection = created; }