2022-12-29 14:11:18 +00:00
|
|
|
const ID = window.crypto.randomUUID();
|
|
|
|
|
2022-12-29 14:36:55 +00:00
|
|
|
let players = {};
|
|
|
|
|
|
|
|
document.addEventListener("DOMContentLoaded", () => {
|
2022-12-29 14:11:18 +00:00
|
|
|
let socket = io();
|
|
|
|
|
2022-12-29 14:36:55 +00:00
|
|
|
socket.on("connect", () => {
|
|
|
|
console.log("Connected!");
|
|
|
|
socket.emit("message", { type: "ANNOUNCE", author: ID, name: "" });
|
|
|
|
players[ID] = { name: "" };
|
2022-12-29 14:11:18 +00:00
|
|
|
});
|
|
|
|
|
2022-12-29 14:36:55 +00:00
|
|
|
socket.on("message", (data) => {
|
|
|
|
// Ignore any messages that originate from us.
|
|
|
|
if (data.author !== ID) {
|
|
|
|
switch (data.type) {
|
|
|
|
case "ANNOUNCE":
|
|
|
|
playerConnected(socket, data);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2022-12-29 14:11:18 +00:00
|
|
|
});
|
2022-12-29 14:36:55 +00:00
|
|
|
|
|
|
|
function playerConnected(socket, data) {
|
|
|
|
// When a new player is seen, all announce to ensure they know all players.
|
|
|
|
if (players[data.author] === undefined) {
|
|
|
|
players[data.author] = { name: data.name };
|
|
|
|
socket.emit("message", { type: "ANNOUNCE", author: ID, name: "" });
|
|
|
|
} else {
|
|
|
|
}
|
|
|
|
}
|