Files

69 lines
1.8 KiB
JavaScript

import { createConnection, joinStudentRoom, student } from "./net.js";
const nameText = document.getElementById("waiting-name");
const codeText = document.getElementById("waiting-code");
const statusText = document.getElementById("waiting-status");
const identity = student.get();
if (!identity.name || !identity.code) {
window.location.href = "/";
} else {
nameText.textContent = identity.name;
codeText.textContent = identity.code;
createConnection({
onOpen(send) {
joinStudentRoom(send);
},
onStatus(status) {
if (status === "connected") {
setStatus("Connected. Waiting for the teacher to start...");
} else if (status === "connecting") {
setStatus("Connecting...");
} else if (status === "replaced") {
setStatus("This room was opened in another tab. You can close this tab.");
} else if (status === "error") {
setStatus("Connection error. Retrying...");
} else {
setStatus("Connection lost. Reconnecting...");
}
},
onMessage(data) {
if (data.type === "JOIN_SUCCESS") {
setStatus("You're in! Waiting for the teacher to start the game...");
return;
}
if (data.type === "SYNC") {
if (data.phase === "day") {
window.location.href = "/game";
} else if (data.phase === "trading") {
window.location.href = "/trading";
}
return;
}
if (data.type !== "ERROR") return;
if (
data.code === "ROOM_NOT_FOUND" ||
/already in use/i.test(data.message ?? "")
) {
student.clear();
window.alert(data.message || "You could not rejoin that room.");
window.location.href = "/";
return;
}
setStatus(data.message || "Something went wrong.");
},
});
}
function setStatus(text) {
statusText.textContent = text;
}