Add more messages for joining the game

This commit is contained in:
pixl_xip 2024-01-22 20:28:32 -07:00
parent 25c3f66480
commit c7f2bdfae2
2 changed files with 23 additions and 5 deletions

View file

@ -5,19 +5,19 @@ const ERUDA = false;
const DIV = false;
if (ERUDA) eruda.init();
const table = document.getElementById(`table`);
let username = ``;
const socket = new WebSocket(`ws://${window.location.hostname}:8080`);
socket.onopen = () => {
log('connected!');
document.querySelectorAll(`#joinbutton`)[0].addEventListener(`click`, () => {
const usernameInput = document.querySelectorAll(`#username`)[0];
if (!usernameInput.value) return;
username = document.querySelectorAll(`#username`)[0].value;
if (!username) return;
socket.send(JSON.stringify({
type: `join`,
username: usernameInput.value,
username: username,
}))
hideJoinBar();
printMessage(`Joining as ${usernameInput.value}`);
printMessage(`Joining as ${username}`);
})
document.querySelectorAll(`#resignbutton`)[0].addEventListener(`click`, () => {
@ -46,6 +46,20 @@ const handleIncomingWebSocketRequest = (requestRaw) => {
printMessage(`Opponent resigned.`);
showJoinBar();
hideGameBar();
break;
case `accept`:
printMessage(`Joined as ${username}`);
hideJoinBar();
break;
case `reject`:
switch (request.reject) {
case `username`:
printMessage(`The username "${username}" was already taken!`);
break;
case `already joined`:
printMessage(`You've already joined! How did we even get here? Try refreshing the page?`);
break;
}
}
}

View file

@ -179,6 +179,10 @@ const handleIncomingJoin = (request, id) => {
games.set(gameID, game);
playerWaiting = true;
}
clients.get(id).send(JSON.stringify({
type: `accept`,
username: request.username,
}));
}
const handleIncomingResign = (id) => {