Announce player joins
Clients announce on join and maintain a list of known players
This commit is contained in:
@ -1,14 +1,33 @@
|
||||
const ID = window.crypto.randomUUID();
|
||||
|
||||
document.addEventListener('DOMContentLoaded', () => {
|
||||
let players = {};
|
||||
|
||||
document.addEventListener("DOMContentLoaded", () => {
|
||||
let socket = io();
|
||||
|
||||
socket.on('connect', () => {
|
||||
console.log('Connected!');
|
||||
socket.emit('message', { 'type': 'CONNECTED', 'id': ID });
|
||||
socket.on("connect", () => {
|
||||
console.log("Connected!");
|
||||
socket.emit("message", { type: "ANNOUNCE", author: ID, name: "" });
|
||||
players[ID] = { name: "" };
|
||||
});
|
||||
|
||||
socket.on('message', (data) => {
|
||||
console.log(data);
|
||||
})
|
||||
socket.on("message", (data) => {
|
||||
// Ignore any messages that originate from us.
|
||||
if (data.author !== ID) {
|
||||
switch (data.type) {
|
||||
case "ANNOUNCE":
|
||||
playerConnected(socket, data);
|
||||
break;
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
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 {
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user