major changes, leaving it at that
This commit is contained in:
1 parent
8cb4c3556b
commit
b55251cfc5
16 files changed
+3280
-302
No files matched your search
+110
@@ -0,0 +1,110 @@
|
||||
import { createConnection, getClientId, student } from "./net.js";
|
||||
|
||||
const nameInput = document.getElementById("name");
|
||||
const codeInput = document.getElementById("join-code");
|
||||
const joinForm = document.getElementById("join-form");
|
||||
const joinButton = document.getElementById("join-button");
|
||||
const statusText = document.getElementById("join-status");
|
||||
|
||||
let connected = false;
|
||||
let pendingJoin = null;
|
||||
|
||||
function setStatus(text) {
|
||||
if (statusText) statusText.textContent = text;
|
||||
}
|
||||
|
||||
function updateButton() {
|
||||
joinButton.disabled = !connected || pendingJoin !== null;
|
||||
joinButton.textContent = pendingJoin
|
||||
? "Joining..."
|
||||
: connected
|
||||
? "Join as Student"
|
||||
: "Connecting...";
|
||||
}
|
||||
|
||||
function sendJoin(send) {
|
||||
if (!pendingJoin) return;
|
||||
|
||||
const sent = send({
|
||||
type: "JOIN_ROOM",
|
||||
name: pendingJoin.name,
|
||||
code: pendingJoin.code,
|
||||
clientId: getClientId(),
|
||||
});
|
||||
|
||||
if (!sent) {
|
||||
pendingJoin = null;
|
||||
updateButton();
|
||||
setStatus("Not connected yet. Please try again.");
|
||||
}
|
||||
}
|
||||
|
||||
const connection = createConnection({
|
||||
onOpen(send) {
|
||||
// If the connection dropped while joining, try again automatically.
|
||||
sendJoin(send);
|
||||
},
|
||||
|
||||
onStatus(status) {
|
||||
connected = status === "connected";
|
||||
|
||||
if (status === "connected") {
|
||||
setStatus("Connected. Enter your name and room code.");
|
||||
} else if (status === "connecting") {
|
||||
setStatus("Connecting to the game server...");
|
||||
} else if (status === "replaced") {
|
||||
setStatus("This page was replaced by another tab. Close this tab.");
|
||||
joinButton.disabled = true;
|
||||
return;
|
||||
} else if (status === "error") {
|
||||
setStatus("Connection error. Retrying...");
|
||||
} else {
|
||||
setStatus("Connection lost. Reconnecting...");
|
||||
}
|
||||
|
||||
updateButton();
|
||||
},
|
||||
|
||||
onMessage(data) {
|
||||
if (data.type === "JOIN_SUCCESS") {
|
||||
if (!pendingJoin) return;
|
||||
|
||||
student.save(pendingJoin.name, pendingJoin.code);
|
||||
window.location.href = "/waiting";
|
||||
return;
|
||||
}
|
||||
|
||||
if (data.type === "ERROR") {
|
||||
const message =
|
||||
data.code === "ROOM_NOT_FOUND"
|
||||
? "No room has that code. Check the code with your teacher."
|
||||
: data.message || "Could not join the room.";
|
||||
|
||||
pendingJoin = null;
|
||||
setStatus(message);
|
||||
updateButton();
|
||||
window.alert(message);
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
joinForm.addEventListener("submit", (event) => {
|
||||
event.preventDefault();
|
||||
if (!connected || pendingJoin) return;
|
||||
|
||||
const name = nameInput.value.trim();
|
||||
const code = codeInput.value.trim();
|
||||
|
||||
if (!name || !code) {
|
||||
window.alert("Please enter both your name and a room code!");
|
||||
return;
|
||||
}
|
||||
|
||||
pendingJoin = { name, code };
|
||||
updateButton();
|
||||
setStatus("Joining room " + code + "...");
|
||||
|
||||
sendJoin(connection.send);
|
||||
});
|
||||
|
||||
updateButton();
|
||||
Reference in new issue
Block a user