Files
Escape-the-Island/index.js
T

169 lines
4.9 KiB
JavaScript

function create_player_stats() {
let x = Math.floor(Math.random() * 2) + 1;
let y = 0;
if (x == 1) {
y = 2;
} else {
y = 1;
}
return { x: x, y: y };
}
let x, y;
function init() {
if (localStorage.getItem("player_x")) {
x = parseInt(localStorage.getItem("player_x"));
y = parseInt(localStorage.getItem("player_y"));
} else {
let player = create_player_stats();
x = player.x;
y = player.y;
localStorage.setItem("player_x", x);
localStorage.setItem("player_y", y);
}
return { x: x, y: y };
}
let player = init();
let gamedata = {
fishing: {
per_hour: player.x,
preserver: "No",
stored: 0,
},
foraging: {
per_hour: player.y,
boat: "No",
stored: 0,
},
hours: 16,
days: 0,
};
function updateTable() {
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;
}
}
}
function go_fishing() {
let prompt = window.prompt("How many hours do you want to fish?");
if (prompt === null || prompt > gamedata.hours) {
window.alert("You dont have the hours for this or prompt exited");
console.log("Prompt Exited");
} else {
gamedata.fishing.stored += prompt * gamedata.fishing.per_hour;
gamedata.hours -= prompt;
if (gamedata.hours === 0) {
gamedata.days++;
gamedata.hours = 16;
}
updateTable();
}
}
function go_foraging() {
let prompt = window.prompt("How many hours do you want to foraging?");
if (prompt === null || prompt > gamedata.hours) {
window.alert("You dont have the hours for this or prompt exited");
console.log("Prompt Exited");
} else {
gamedata.foraging.stored += prompt * gamedata.foraging.per_hour;
gamedata.hours -= prompt;
if (gamedata.hours === 0) {
gamedata.days++;
gamedata.hours = 16;
}
updateTable();
}
}
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();