checkpoint: bun server + game UI before multiplayer refactor

This commit is contained in:
DarthMooMancer committed 2026-11-01 23:07:37 -05:00
1 parent b5fb570df4
commit e688c2bdf1
9 files changed
+548 -203

No files matched your search

BIN
View File
Binary file not shown.

Before

Width:  |  Height:  |  Size: 88 KiB

+141
View File
@@ -0,0 +1,141 @@
<!doctype html>
<html>
<head>
<title>Escape the Island</title>
<link rel="icon" type="image/x-icon" href="assets/shipwreck.png" />
<link rel="stylesheet" href="styles.css" />
<link rel="preconnect" href="https://fonts.googleapis.com" />
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
<link
href="https://fonts.googleapis.com/css2?family=Almendra:ital,wght@0,400;0,700;1,400;1,700&display=swap"
rel="stylesheet"
/>
<style></style>
</head>
<body>
<header>
<div class="header-container">
<h1>Escape the Island</h1>
<div class="boat-container">
<img src="assets/boat.png" alt="" class="boat" />
</div>
</div>
</header>
<div class="Content">
<div class="Instruction">
<p>Backstory</p>
<details>
<summary>Click to expand</summary>
<p>
You and your class planned to go on a one week cruise trip over
spring break to learn about how inflation affects nautical
exploration and catching some sun. The cruise ship is built a pirate
ship which you all find amusing at first. However, issues arise from
an underlying problem. They didn't have the money to get schedule a
final inspection before departing, but they still let you go anyways
(because they are broke). As the boat leaves and cruises away from
land, you see rocky terrain that has formed in the middle of the
ocean (shocking). Your boat hits it and as people don't learn from
the titanic, you don't have enough life boats to fit everyone and
make it home safely. Everybody else gets on boats while you and your
classmates are still on the boat (coincidentally). One of your
friends believes he has a boat license (he doesn't) and tries to
steer the boat before you sink. Magically you find an island. You
all get onto the island safely but with no way home, scattered and
disoriented.
</p>
</details>
</div>
<div class="Instruction">
<p>Objective</p>
<details>
<summary>Click to expand</summary>
<p>
You will have 16 hours each day to gain as many resources as
possible to survive. You must choose what to spend your hours doing
each day carefully. The survival requirement for the end of each day
is to have 8 fish and 4 pieces of lumber. Otherwise, you die (lose).
You will be able to trade with other classmates you have found
stranded at no expense of your daily hours.
</p>
</details>
</div>
<div class="Instruction">
<p>Requirements</p>
<details>
<summary>Click to expand</summary>
<ol>
<li>
In order to escape, you must have 24 stockpiled fish and a boat at
the START of a new day.
</li>
<li>Fish will go bad without a preserver.</li>
<li>
Improving skills requires 8 hours and adds 1 per hour the next
day.
</li>
<li>Building a preserver requires 8 hours and 25 wood</li>
<li>Building a boat requires 16 hours and 100 wood</li>
<li>You can only trade with "found" players</li>
<li>
It is recommended to donate resources to other players at end of
day if you have a boat (Need to have 24 fish to leave)
</li>
</ol>
</details>
</div>
<div class="game">
<div class="table-container">
<table>
<tr>
<th></th>
<th>Fishing Stats</th>
<th>Foraging Stats</th>
</tr>
<tr>
<th style="width: 25%">Days: <span id="days">0</span></th>
<td id="fish_p_hour">0</td>
<td id="wood_p_hour">0</td>
</tr>
<tr>
<th style="width: 25%">
Players Found: <span id="found">0</span>
</th>
<td>Preserver: <span id="preserver">No</span></td>
<td>Boat: <span id="boat">No</span></td>
</tr>
<tr>
<th style="width: 25%">Hours Left: <span id="hours">16</span></th>
<td>Stored Fish: <span id="fish_stored"></span></td>
<td>Stored Wood: <span id="wood_stored"></span></td>
</tr>
</table>
</div>
<div class="button-container">
<div class="button-container-part">
<button type="button" id="go-fishing">Go Fishing</button>
<button type="button" id="go-foraging">Go Foraging</button>
<!-- <button type="button" id="trading">Trade</button> -->
</div>
<div class="button-container-part">
<button type="button" id="improve-fishing">
Improve Fish Per Hour
</button>
<button type="button" id="build-preserver">Build Preserver</button>
<button type="button" id="improve-forgaing">
Improve Wood Per Hour
</button>
<button type="button" id="build-boat">Build Boat</button>
</div>
</div>
</div>
</div>
<footer>
<h3 style="padding: 10px 0px 0px 10px">
Gamemaker: Mr. J. Walla ~=~ Developer: Andrew H. ~=~ Designer: Logan C.
</h3>
</footer>
<script src="index.js"></script>
</body>
</html>
+32
View File
@@ -0,0 +1,32 @@
<!doctype html>
<html>
<head>
<title>Escape the Island</title>
<link rel="icon" type="image/x-icon" href="assets/shipwreck.png" />
<link rel="stylesheet" href="styles.css" />
<link rel="preconnect" href="https://fonts.googleapis.com" />
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
<link
href="https://fonts.googleapis.com/css2?family=Almendra:ital,wght@0,400;0,700;1,400;1,700&display=swap"
rel="stylesheet"
/>
</head>
<body>
<header>
<div class="header-container">
<h1>Escape the Island</h1>
<div class="boat-container">
<img src="assets/boat.png" alt="" class="boat" />
</div>
</div>
</header>
<div class="Content">
</div>
<footer>
<h3 style="padding: 10px 0px 0px 10px">
Gamemaker: Mr. J. Walla ~=~ Developer: Andrew H. ~=~ Designer: Logan C.
</h3>
</footer>
</body>
</html>
+38 -109
View File
@@ -10,7 +10,39 @@
href="https://fonts.googleapis.com/css2?family=Almendra:ital,wght@0,400;0,700;1,400;1,700&display=swap"
rel="stylesheet"
/>
<style></style>
<style>
body {
margin: 0;
padding: 0;
display: flex;
flex-direction: column;
min-height: 100vh;
}
button {
min-height: 20vh;
/* flex: 1; */
}
.Content {
flex: 1;
display: flex;
flex-direction: column;
max-width: 600px;
margin: 0 auto;
width: 100%;
}
.part {
flex: 1;
margin: 5px;
display: flex;
flex-direction: column;
row-gap: 10px;
}
input {
padding: 1vh;
}
</style>
</head>
<body>
<header>
@@ -21,115 +53,12 @@
</div>
</div>
</header>
<div class="Content">
<div class="Instruction">
<p>Backstory</p>
<details>
<summary>Click to expand</summary>
<p>
You and your class planned to go on a one week cruise trip over
spring break to learn about how inflation affects nautical
exploration and catching some sun. The cruise ship is built a pirate
ship which you all find amusing at first. However, issues arise from
an underlying problem. They didn't have the money to get schedule a
final inspection before departing, but they still let you go anyways
(because they are broke). As the boat leaves and cruises away from
land, you see rocky terrain that has formed in the middle of the
ocean (shocking). Your boat hits it and as people don't learn from
the titanic, you don't have enough life boats to fit everyone and
make it home safely. Everybody else gets on boats while you and your
classmates are still on the boat (coincidentally). One of your
friends believes he has a boat license (he doesn't) and tries to
steer the boat before you sink. Magically you find an island. You
all get onto the island safely but with no way home, scattered and
disoriented.
</p>
</details>
</div>
<div class="Instruction">
<p>Objective</p>
<details>
<summary>Click to expand</summary>
<p>
You will have 16 hours each day to gain as many resources as
possible to survive. You must choose what to spend your hours doing
each day carefully. The survival requirement for the end of each day
is to have 8 fish and 4 pieces of lumber. Otherwise, you die (lose).
You will be able to trade with other classmates you have found
stranded at no expense of your daily hours.
</p>
</details>
</div>
<div class="Instruction">
<p>Requirements</p>
<details>
<summary>Click to expand</summary>
<ol>
<li>
In order to escape, you must have 24 stockpiled fish and a boat at
the START of a new day.
</li>
<li>Fish will go bad without a preserver.</li>
<li>
Improving skills requires 8 hours and adds 1 per hour the next
day.
</li>
<li>Building a preserver requires 8 hours and 25 wood</li>
<li>Building a boat requires 16 hours and 100 wood</li>
<li>You can only trade with "found" players</li>
<li>
It is recommended to donate resources to other players at end of
day if you have a boat (Need to have 24 fish to leave)
</li>
</ol>
</details>
</div>
<div class="game">
<div class="table-container">
<table>
<tr>
<th></th>
<th>Fishing Stats</th>
<th>Foraging Stats</th>
</tr>
<tr>
<th style="width: 25%">Days: <span id="days">0</span></th>
<td id="fish_p_hour">0</td>
<td id="wood_p_hour">0</td>
</tr>
<tr>
<th style="width: 25%">
Players Found: <span id="found">0</span>
</th>
<td>Preserver: <span id="preserver">No</span></td>
<td>Boat: <span id="boat">No</span></td>
</tr>
<tr>
<th style="width: 25%">Hours Left: <span id="hours">16</span></th>
<td>Stored Fish: <span id="fish_stored"></span></td>
<td>Stored Wood: <span id="wood_stored"></span></td>
</tr>
</table>
</div>
<div class="button-container">
<div class="button-container-part">
<button type="button" id="go-fishing">Go Fishing</button>
<button type="button" id="go-foraging">Go Foraging</button>
<!-- <button type="button" id="trading">Trade</button> -->
</div>
<div class="button-container-part">
<button type="button" id="improve-fishing">
Improve Fish Per Hour
</button>
<button type="button" id="build-preserver">Build Preserver</button>
<button type="button" id="improve-forgaing">
Improve Wood Per Hour
</button>
<button type="button" id="build-boat">Build Boat</button>
</div>
</div>
<div class="Content" style="padding-top: 25px">
<div class="part">
<input type="text" placeholder="Name:" id="name" />
<input type="number" placeholder="Join Code:" id="join-code" />
</div>
<button type="button" id="join-button">Join as Student</button>
</div>
<footer>
<h3 style="padding: 10px 0px 0px 10px">
+90 -29
View File
@@ -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();
+104 -3
View File
@@ -1,8 +1,109 @@
const server = Bun.serve({
interface WebSocketData {
role: "teacher" | "student";
roomCode: string;
}
interface Player {
name: string;
fish: number;
wood: number;
}
const rooms = new Map<string, { teacherWs: any; players: Map<any, Player> }>();
const server = Bun.serve<WebSocketData>({
port: 3000,
routes: {
"/": () => new Response("Bun!"),
fetch(req, server) {
const url = new URL(req.url);
console.log(req.method, url.pathname);
// If the request if for a Websocket upgrade, let Bun handle it
//
if (url.pathname === "/ws") {
if (server.upgrade(req, { data: { role: "student", roomCode: "" } }))
return;
return new Response("WebSocket upgrade failed", { status: 400 });
}
if (url.pathname === "/") return new Response(Bun.file("index.html"));
if (url.pathname === "/waiting")
return new Response(Bun.file("waiting.html"));
if (url.pathname === "/game") return new Response(Bun.file("game.html"));
if (url.pathname === "/host") return new Response(Bun.file("host.html"));
if (url.pathname === "/styles.css")
return new Response(Bun.file("styles.css"));
if (url.pathname === "/index.js") return new Response(Bun.file("index.js"));
if (url.pathname.startsWith("/assets/"))
return new Response(Bun.file("." + url.pathname));
if (url.pathname == "/api/health") {
return Response.json({ ok: true, time: Date.now() });
}
if (url.pathname == "/hello") {
return new Response("Hello from bun");
}
return new Response("Not Found", { status: 404 });
},
websocket: {
open(ws) {
console.log("A client connect via websocket!");
},
message(ws, message) {
const data = JSON.parse(message as string);
if (data.type === "CREATE_ROOM") {
const roomCode = Math.floor(1000 + Math.random() * 9000).toString();
rooms.set(roomCode, {
teacherWs: ws,
players: new Map(),
});
ws.data = { role: "teacher", roomCode };
ws.send(JSON.stringify({ type: "ROOM_CREATED", code: roomCode }));
console.log(`Teacher created room: ${roomCode}`);
} else if (data.type === "JOIN_ROOM") {
const room = rooms.get(data.code);
if (!room) {
ws.send(
JSON.stringify({ type: "ERROR", message: "Room not found!" }),
);
return;
}
room.players.set(ws, { name: data.name, fish: 0, wood: 0 });
ws.data = { role: "student", roomCode: data.code };
ws.send(JSON.stringify({ type: "JOIN_SUCCESS" }));
console.log(`Student ${data.name} joined room ${data.code}`);
updateTeachersDashboard(room);
}
},
close(ws) {
if (!ws.data) return;
const room = rooms.get(ws.data.roomCode);
if (room) {
if (ws.data.role === "teacher") {
rooms.delete(ws.data.roomCode);
console.log(`Room ${ws.data.roomCode} closed by teacher.`);
} else if (ws.data.role === "student") {
room.players.delete(ws);
updateTeachersDashboard(room);
}
}
console.log("A client disconnected");
},
},
});
function updateTeachersDashboard(room: {
teacherWs: any;
players: Map<any, Player>;
}) {
const playerList = Array.from(room.players.values());
room.teacherWs.send(
JSON.stringify({
type: "UPDATES_PLAYERS",
players: playerList,
}),
);
}
console.log(`Listening on ${server.url}`);
+30
View File
@@ -0,0 +1,30 @@
<!doctype html>
<html>
<head>
<title>Escape the Island</title>
<link rel="icon" type="image/x-icon" href="assets/shipwreck.png" />
<link rel="stylesheet" href="styles.css" />
<link rel="preconnect" href="https://fonts.googleapis.com" />
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
<link
href="https://fonts.googleapis.com/css2?family=Almendra:ital,wght@0,400;0,700;1,400;1,700&display=swap"
rel="stylesheet"
/>
</head>
<body>
<header>
<div class="header-container">
<h1>Escape the Island</h1>
<div class="boat-container">
<img src="assets/boat.png" alt="" class="boat" />
</div>
</div>
</header>
<div class="Content"></div>
<footer>
<h3 style="padding: 10px 0px 0px 10px">
Gamemaker: Mr. J. Walla ~=~ Developer: Andrew H. ~=~ Designer: Logan C.
</h3>
</footer>
</body>
</html>
+64 -62
View File
@@ -88,73 +88,75 @@ header {
display: flex;
width: 100%;
gap: 10px;
}
.table-container {
padding: 10px;
flex: 1;
background-color: cadetblue;
.table-container {
padding: 10px;
flex: 1;
background-color: cadetblue;
table,
th,
td,
tr {
border: 1px solid black;
}
tr:nth-child(even) {
background-color: rgba(0, 0, 0, 0.2);
}
tr:nth-child(odd) {
background-color: rgba(0, 0, 0, 0.125);
}
table {
border-collapse: collapse;
width: 100%;
}
th,
td {
padding: 10px;
text-align: center;
}
th {
background-color: darkseagreen;
}
table,
th,
td,
tr {
border: 1px solid black;
}
.button-container {
flex: 1;
padding: 10px;
.button-container-part {
display: flex;
button {
font-family: "Almendra";
font-size: 18px;
width: 100%;
text-align: center;
transition-duration: 0.2s;
padding: 7px;
height: clamp(30px, 15vh, 85px);
}
button:hover {
background-color: darkseagreen;
color: black;
cursor: grab;
box-shadow: 0 8px 16px 0 rgba(0, 0, 0, 0.6);
border: 2px solid black;
}
button:disabled {
pointer-events: none;
}
}
tr:nth-child(even) {
background-color: rgba(0, 0, 0, 0.2);
}
tr:nth-child(odd) {
background-color: rgba(0, 0, 0, 0.125);
}
table {
border-collapse: collapse;
width: 100%;
}
th,
td {
padding: 10px;
text-align: center;
}
th {
background-color: darkseagreen;
}
}
.button-container {
flex: 1;
padding: 10px;
}
.button-container-part {
display: flex;
}
button {
font-family: "Almendra";
font-size: 18px;
width: 100%;
text-align: center;
transition-duration: 0.2s;
padding: 7px;
height: clamp(30px, 15vh, 85px);
}
button:hover {
background-color: darkseagreen;
color: black;
cursor: grab;
box-shadow: 0 8px 16px 0 rgba(0, 0, 0, 0.6);
border: 2px solid black;
}
button:disabled {
pointer-events: none;
}
}
+49
View File
@@ -0,0 +1,49 @@
<!doctype html>
<html>
<head>
<title>Escape the Island</title>
<link rel="icon" type="image/x-icon" href="assets/shipwreck.png" />
<link rel="stylesheet" href="styles.css" />
<link rel="preconnect" href="https://fonts.googleapis.com" />
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
<link
href="https://fonts.googleapis.com/css2?family=Almendra:ital,wght@0,400;0,700;1,400;1,700&display=swap"
rel="stylesheet"
/>
<style>
body {
margin: 0;
padding: 0;
display: flex;
flex-direction: column;
min-height: 100vh;
}
.Content {
flex: 1;
display: flex;
flex-direction: column;
max-width: 600px;
margin: 0 auto;
width: 100%;
}
</style>
</head>
<body>
<header>
<div class="header-container">
<h1>Escape the Island</h1>
<div class="boat-container">
<img src="assets/boat.png" alt="" class="boat" />
</div>
</div>
</header>
<div class="Content" style="padding-top: 25px"></div>
<footer>
<h3 style="padding: 10px 0px 0px 10px">
Gamemaker: Mr. J. Walla ~=~ Developer: Andrew H. ~=~ Designer: Logan C.
</h3>
</footer>
<script src="index.js"></script>
</body>
</html>