checkpoint: bun server + game UI before multiplayer refactor
This commit is contained in:
1 parent
b5fb570df4
commit
e688c2bdf1
9 files changed
+548
-203
No files matched your search
@@ -40,33 +40,37 @@ let gamedata = {
|
||||
};
|
||||
|
||||
function updateTable() {
|
||||
document.getElementById("fish_p_hour").innerText = gamedata.fishing.per_hour;
|
||||
document.getElementById("wood_p_hour").innerText = gamedata.foraging.per_hour;
|
||||
document.getElementById("fish_stored").innerText = gamedata.fishing.stored;
|
||||
document.getElementById("wood_stored").innerText = gamedata.foraging.stored;
|
||||
document.getElementById("preserver").innerText = gamedata.fishing.preserver;
|
||||
document.getElementById("boat").innerText = gamedata.foraging.boat;
|
||||
document.getElementById("hours").innerText = gamedata.hours;
|
||||
document.getElementById("days").innerText = gamedata.days;
|
||||
if (gamedata.hours < 8) {
|
||||
document.getElementById("improve-fishing").disabled = true;
|
||||
} else {
|
||||
document.getElementById("improve-fishing").disabled = false;
|
||||
}
|
||||
if (gamedata.hours < 8) {
|
||||
document.getElementById("improve-forgaing").disabled = true;
|
||||
} else {
|
||||
document.getElementById("improve-forgaing").disabled = false;
|
||||
}
|
||||
if (gamedata.foraging.stored < 25 || gamedata.hours < 8) {
|
||||
document.getElementById("build-preserver").disabled = true;
|
||||
} else {
|
||||
document.getElementById("build-preserver").disabled = false;
|
||||
}
|
||||
if (gamedata.foraging.stored < 100 || gamedata.hours < 16) {
|
||||
document.getElementById("build-boat").disabled = true;
|
||||
} else {
|
||||
document.getElementById("build-boat").disabled = false;
|
||||
if (document.getElementById("fish_p_hour")) {
|
||||
document.getElementById("fish_p_hour").innerText =
|
||||
gamedata.fishing.per_hour;
|
||||
document.getElementById("wood_p_hour").innerText =
|
||||
gamedata.foraging.per_hour;
|
||||
document.getElementById("fish_stored").innerText = gamedata.fishing.stored;
|
||||
document.getElementById("wood_stored").innerText = gamedata.foraging.stored;
|
||||
document.getElementById("preserver").innerText = gamedata.fishing.preserver;
|
||||
document.getElementById("boat").innerText = gamedata.foraging.boat;
|
||||
document.getElementById("hours").innerText = gamedata.hours;
|
||||
document.getElementById("days").innerText = gamedata.days;
|
||||
if (gamedata.hours < 8) {
|
||||
document.getElementById("improve-fishing").disabled = true;
|
||||
} else {
|
||||
document.getElementById("improve-fishing").disabled = false;
|
||||
}
|
||||
if (gamedata.hours < 8) {
|
||||
document.getElementById("improve-forgaing").disabled = true;
|
||||
} else {
|
||||
document.getElementById("improve-forgaing").disabled = false;
|
||||
}
|
||||
if (gamedata.foraging.stored < 25 || gamedata.hours < 8) {
|
||||
document.getElementById("build-preserver").disabled = true;
|
||||
} else {
|
||||
document.getElementById("build-preserver").disabled = false;
|
||||
}
|
||||
if (gamedata.foraging.stored < 100 || gamedata.hours < 16) {
|
||||
document.getElementById("build-boat").disabled = true;
|
||||
} else {
|
||||
document.getElementById("build-boat").disabled = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -101,7 +105,64 @@ function go_foraging() {
|
||||
updateTable();
|
||||
}
|
||||
}
|
||||
document.getElementById("go-fishing").onclick = go_fishing;
|
||||
document.getElementById("go-foraging").onclick = go_foraging;
|
||||
if (document.getElementById("go-fishing")) {
|
||||
document.getElementById("go-fishing").onclick = go_fishing;
|
||||
document.getElementById("go-foraging").onclick = go_foraging;
|
||||
}
|
||||
|
||||
const ws = new WebSocket(`ws://${window.location.host}/ws`);
|
||||
let isConnected = false;
|
||||
|
||||
ws.onopen = () => {
|
||||
console.log("Connected to the game server!");
|
||||
isConnected = true;
|
||||
};
|
||||
|
||||
ws.onmessage = (event) => {
|
||||
const data = JSON.parse(event.data);
|
||||
if (data.type === "ROOM_CREATED") {
|
||||
document.getElementById("teacher-display").innerText =
|
||||
`Room Code: ${data.code}`;
|
||||
hostBtn.disabled = true;
|
||||
// hostBtn.style.opacity = "0.";
|
||||
hostBtn.innerText = "Room Active";
|
||||
} else if (data.type === "JOIN_SUCCESS") {
|
||||
window.alert("Joined Room Successfully");
|
||||
// TODO: update a player table on the teacher dashboard here
|
||||
} else if (data.type === "UPDATE_PLAYERS") {
|
||||
console.log("Current players in room:", data.players);
|
||||
} else if (data.type === "ERROR") {
|
||||
window.alert(data.message);
|
||||
}
|
||||
};
|
||||
|
||||
const hostBtn = document.getElementById("host-btn");
|
||||
if (hostBtn) {
|
||||
hostBtn.onclick = () => {
|
||||
ws.send(JSON.stringify({ type: "CREATE_ROOM" }));
|
||||
};
|
||||
}
|
||||
|
||||
const joinBtn = document.getElementById("join-button");
|
||||
if (joinBtn) {
|
||||
joinBtn.onclick = () => {
|
||||
const name = document.getElementById("name").value.trim();
|
||||
const code = document.getElementById("join-code").value.trim();
|
||||
|
||||
if (!name || !code) {
|
||||
window.alert("Please enter both your and a room code!");
|
||||
return;
|
||||
}
|
||||
localStorage.setItem("playerName", name);
|
||||
localStorage.setItem("roomCode", code);
|
||||
window.location.href = "/waiting";
|
||||
ws.send(
|
||||
JSON.stringify({
|
||||
type: "JOIN_ROOM",
|
||||
name: name,
|
||||
code: code,
|
||||
}),
|
||||
);
|
||||
};
|
||||
}
|
||||
updateTable();
|
||||
Reference in new issue
Block a user