273 lines
7.1 KiB
JavaScript
273 lines
7.1 KiB
JavaScript
import { createConnection, resumeTeacherRoom, teacher } from "./net.js";
|
|
|
|
const hostButton = document.getElementById("host-btn");
|
|
const roomCodeText = document.getElementById("room-code");
|
|
const statusText = document.getElementById("lobby-status");
|
|
const rosterPanel = document.getElementById("roster-panel");
|
|
const rosterBody = document.getElementById("roster-body");
|
|
const playerCount = document.getElementById("player-count");
|
|
|
|
const phasePanel = document.getElementById("phase-panel");
|
|
const phaseText = document.getElementById("phase-text");
|
|
const dayText = document.getElementById("day-text");
|
|
const phaseStatus = document.getElementById("phase-status");
|
|
const startGameButton = document.getElementById("start-game-btn");
|
|
const endDayButton = document.getElementById("end-day-btn");
|
|
const nextDayButton = document.getElementById("next-day-btn");
|
|
|
|
let connected = false;
|
|
let roomOpen = false;
|
|
let creating = false;
|
|
let resuming = false;
|
|
let room = null;
|
|
|
|
const PHASE_LABELS = {
|
|
lobby: "Lobby",
|
|
day: "Day",
|
|
trading: "Trading",
|
|
};
|
|
|
|
function setStatus(text, tone = "") {
|
|
statusText.textContent = text;
|
|
statusText.dataset.tone = tone;
|
|
}
|
|
|
|
function setPhaseStatus(text, tone = "") {
|
|
phaseStatus.textContent = text;
|
|
phaseStatus.dataset.tone = tone;
|
|
}
|
|
|
|
function updateControls() {
|
|
if (roomOpen) {
|
|
hostButton.hidden = true;
|
|
return;
|
|
}
|
|
|
|
hostButton.hidden = false;
|
|
hostButton.disabled = !connected || creating || resuming;
|
|
|
|
if (resuming) {
|
|
hostButton.textContent = "Reconnecting...";
|
|
} else if (creating) {
|
|
hostButton.textContent = "Creating room...";
|
|
} else if (connected) {
|
|
hostButton.textContent = "Create Room";
|
|
} else {
|
|
hostButton.textContent = "Connecting...";
|
|
}
|
|
}
|
|
|
|
function updatePhaseControls() {
|
|
if (!room) {
|
|
phasePanel.hidden = true;
|
|
return;
|
|
}
|
|
|
|
phasePanel.hidden = false;
|
|
phaseText.textContent = PHASE_LABELS[room.phase] ?? room.phase;
|
|
dayText.textContent = String(room.day);
|
|
|
|
const hasPlayers = room.players.length > 0;
|
|
|
|
startGameButton.hidden = room.phase !== "lobby";
|
|
startGameButton.disabled = !connected || !hasPlayers;
|
|
|
|
endDayButton.hidden = room.phase !== "day";
|
|
endDayButton.disabled = !connected;
|
|
|
|
nextDayButton.hidden = room.phase !== "trading";
|
|
nextDayButton.disabled = !connected;
|
|
|
|
if (!hasPlayers && room.phase === "lobby") {
|
|
setPhaseStatus("Waiting for at least one student before you can start.");
|
|
} else if (room.phase === "lobby") {
|
|
setPhaseStatus("The crew is ready when you are.");
|
|
} else if (room.phase === "day") {
|
|
setPhaseStatus("Students are spending their hours.");
|
|
} else if (room.phase === "trading") {
|
|
setPhaseStatus("Trading is open. Start the next day when ready.");
|
|
} else {
|
|
setPhaseStatus("");
|
|
}
|
|
}
|
|
|
|
function showRoom(code) {
|
|
roomOpen = true;
|
|
roomCodeText.textContent = code;
|
|
rosterPanel.hidden = false;
|
|
updateControls();
|
|
}
|
|
|
|
function makeCell(text, className) {
|
|
const cell = document.createElement("td");
|
|
cell.textContent = text;
|
|
if (className) cell.className = className;
|
|
return cell;
|
|
}
|
|
|
|
function makePlayerRow(player) {
|
|
const row = document.createElement("tr");
|
|
row.append(
|
|
makeCell(player.name),
|
|
makeCell(String(player.fish)),
|
|
makeCell(String(player.wood)),
|
|
makeCell(String(player.hours)),
|
|
makeCell(
|
|
player.connected ? "Connected" : "Offline",
|
|
player.connected ? "online" : "offline",
|
|
),
|
|
);
|
|
return row;
|
|
}
|
|
|
|
function renderRoster(players) {
|
|
playerCount.textContent = String(players.length);
|
|
|
|
if (players.length === 0) {
|
|
const row = document.createElement("tr");
|
|
const cell = makeCell("No students have joined yet.", "empty");
|
|
cell.colSpan = 5;
|
|
row.append(cell);
|
|
rosterBody.replaceChildren(row);
|
|
return;
|
|
}
|
|
|
|
rosterBody.replaceChildren(...players.map(makePlayerRow));
|
|
}
|
|
|
|
function handleSync(data) {
|
|
room = {
|
|
phase: data.phase,
|
|
day: data.day,
|
|
players: Array.isArray(data.players) ? data.players : [],
|
|
};
|
|
|
|
showRoom(data.code);
|
|
renderRoster(room.players);
|
|
updatePhaseControls();
|
|
}
|
|
|
|
const connection = createConnection({
|
|
onOpen(send) {
|
|
const saved = teacher.get();
|
|
|
|
if (saved.code && saved.token) {
|
|
resuming = true;
|
|
updateControls();
|
|
setStatus("Restoring your room...");
|
|
resumeTeacherRoom(send);
|
|
}
|
|
},
|
|
|
|
onStatus(status) {
|
|
connected = status === "connected";
|
|
|
|
if (status === "connected") {
|
|
if (!roomOpen) {
|
|
setStatus(
|
|
resuming
|
|
? "Restoring your room..."
|
|
: "Connected. Create a room to begin.",
|
|
);
|
|
}
|
|
} else if (status === "connecting") {
|
|
setStatus("Connecting to the game server...");
|
|
} else if (status === "replaced") {
|
|
setStatus("This lobby was opened in another tab. Close this tab.");
|
|
hostButton.disabled = true;
|
|
return;
|
|
} else if (status === "error") {
|
|
setStatus("Connection error. Retrying...");
|
|
} else {
|
|
setStatus("Connection lost. Reconnecting...");
|
|
}
|
|
|
|
updateControls();
|
|
updatePhaseControls();
|
|
},
|
|
|
|
onMessage(data) {
|
|
if (data.type === "ROOM_CREATED") {
|
|
teacher.save(data.code, data.token);
|
|
creating = false;
|
|
resuming = false;
|
|
showRoom(data.code);
|
|
setStatus("Room open. Share the code with your students.", "good");
|
|
return;
|
|
}
|
|
|
|
if (data.type === "ROOM_RESUMED") {
|
|
creating = false;
|
|
resuming = false;
|
|
showRoom(data.code);
|
|
setStatus("Room restored. Students can keep joining.", "good");
|
|
return;
|
|
}
|
|
|
|
if (data.type === "SYNC") {
|
|
handleSync(data);
|
|
return;
|
|
}
|
|
|
|
if (data.type === "ERROR") {
|
|
creating = false;
|
|
resuming = false;
|
|
|
|
if (data.code === "ROOM_NOT_FOUND") {
|
|
teacher.clear();
|
|
roomOpen = false;
|
|
room = null;
|
|
roomCodeText.textContent = "----";
|
|
rosterPanel.hidden = true;
|
|
phasePanel.hidden = true;
|
|
setStatus("That room no longer exists. Create a new one.", "bad");
|
|
} else if (roomOpen) {
|
|
setPhaseStatus(data.message || "Something went wrong.", "bad");
|
|
} else {
|
|
setStatus(data.message || "Something went wrong.", "bad");
|
|
}
|
|
|
|
updateControls();
|
|
updatePhaseControls();
|
|
}
|
|
},
|
|
});
|
|
|
|
function sendAction(type) {
|
|
if (!connected) return;
|
|
|
|
setPhaseStatus(
|
|
type === "START_GAME"
|
|
? "Starting the game..."
|
|
: type === "END_DAY"
|
|
? "Ending the day..."
|
|
: "Starting the next day...",
|
|
);
|
|
|
|
if (!connection.send({ type })) {
|
|
setPhaseStatus("Not connected yet. Please try again.", "bad");
|
|
}
|
|
}
|
|
|
|
hostButton.addEventListener("click", () => {
|
|
if (!connected || creating || resuming) return;
|
|
|
|
creating = true;
|
|
updateControls();
|
|
setStatus("Creating room...");
|
|
|
|
if (!connection.send({ type: "CREATE_ROOM" })) {
|
|
creating = false;
|
|
updateControls();
|
|
setStatus("Not connected yet. Please try again.", "bad");
|
|
}
|
|
});
|
|
|
|
startGameButton.addEventListener("click", () => sendAction("START_GAME"));
|
|
endDayButton.addEventListener("click", () => sendAction("END_DAY"));
|
|
nextDayButton.addEventListener("click", () => sendAction("NEXT_DAY"));
|
|
|
|
roomCodeText.textContent = teacher.get().code || "----";
|
|
updateControls();
|
|
updatePhaseControls();
|