ready button on client, some issues with the server managing of lobbies
This commit is contained in:
@@ -58,7 +58,7 @@ socket.on('identify', (...args) => {
|
||||
|
||||
socket.on('identify-success', (...args) => {
|
||||
console.log(args[0]);
|
||||
ConnectionState.innerHTML = localeString('status-connected-as') + args[0].user.username;
|
||||
ConnectionState.innerHTML = localeString('status-connected-as') + ' ' + args[0].user.username;
|
||||
onConnect();
|
||||
});
|
||||
|
||||
|
||||
@@ -66,11 +66,17 @@ function drawLobby(lobby)
|
||||
}
|
||||
|
||||
lobbyDiv.innerHTML += `<p>${localeString('visibility')}: ${lobby.visibility}<p>${localeString('status')}: ${lobby.state}`
|
||||
|
||||
lobbyDiv.innerHTML += `<input type="button" value="${localeString('button-start-game')}" onclick="" disabled>`
|
||||
lobbyDiv.innerHTML += `<input type="button" value="${localeString('button-leave-lobby')}" onclick="leaveLobby()">`
|
||||
|
||||
lobbyDiv.innerHTML += `<p><input type="checkbox" id="lobby-input-ready"> ${localeString('ready')}`;
|
||||
|
||||
lobbyDiv.innerHTML += `<input type="button" value="${localeString('button-start-game')}" onclick="startGame()" disabled>`
|
||||
|
||||
ActiveLobbyBlock.appendChild(lobbyDiv);
|
||||
|
||||
const checkbox = document.querySelector('#lobby-input-ready');
|
||||
checkbox.addEventListener('change', () => {
|
||||
if (checkbox.checked) socket.emit('lobby-user-ready');
|
||||
else socket.emit('lobby-user-unready');
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -208,6 +214,8 @@ socket.on('lobby-update', obj => {
|
||||
|
||||
drawLobby(obj.lobby);
|
||||
|
||||
console.log(obj);
|
||||
|
||||
if (obj.state === 'lobby-join')
|
||||
pageLog(`${obj.updateuser.username} ${localeString('joined')}`);
|
||||
|
||||
@@ -236,3 +244,15 @@ function destructLobbies()
|
||||
JoinLobbyBlock.style.display = 'none';
|
||||
ActiveLobbyBlock.style.display = 'none';
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
socket.on('game-ready', () =>
|
||||
{
|
||||
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -49,6 +49,7 @@ log-console:log console
|
||||
message:message; (noun) piece of text
|
||||
name:name
|
||||
players:Players
|
||||
ready:ready ; (Action) to be (ready) to play
|
||||
scrabble-name:Scrabble
|
||||
spectators:Spectators
|
||||
status:Status; (noun) position
|
||||
|
||||
@@ -49,6 +49,7 @@ log-console:consola de log
|
||||
message:mensaje
|
||||
name:nombre
|
||||
players:Jugadores
|
||||
ready:
|
||||
scrabble-name:Scrabble
|
||||
spectators:Espectadores
|
||||
status:Status
|
||||
|
||||
@@ -239,6 +239,11 @@
|
||||
"es": "Jugadores",
|
||||
"pt": "Jogadores"
|
||||
},
|
||||
"ready": {
|
||||
"en": "ready ",
|
||||
"es": "",
|
||||
"pt": ""
|
||||
},
|
||||
"scrabble-name": {
|
||||
"en": "Scrabble",
|
||||
"es": "Scrabble",
|
||||
|
||||
@@ -49,6 +49,7 @@ log-console:console de log
|
||||
message:mensagem
|
||||
name:nome
|
||||
players:Jogadores
|
||||
ready:
|
||||
scrabble-name:Scrabble
|
||||
spectators:Espectadores
|
||||
status:Status
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
const Logger = require('./logger.js');
|
||||
const Registrar = require('./game-registrar.js');
|
||||
const { GetUserByUID } = require('./game-registrar.js');
|
||||
|
||||
/*
|
||||
LOBBY OBJECT
|
||||
@@ -7,7 +8,7 @@ LOBBY OBJECT
|
||||
uid: uid,
|
||||
name: string
|
||||
owneruid: useruid,
|
||||
players: [{uid, name}],
|
||||
players: [{uid, name, ready}],
|
||||
spectators: [{uid, name}],
|
||||
// PUBLIC, PRIVATE
|
||||
visibility: 'PUBLIC',
|
||||
@@ -21,6 +22,7 @@ NOTES
|
||||
- When inactive will be deleted, unlike users
|
||||
- It's a waste of memory to store the name along with the useruid, however
|
||||
i believe it will save complexity in the domain logic
|
||||
- All players must be ready in order for a game to start
|
||||
*/
|
||||
let Lobbies = [];
|
||||
|
||||
@@ -42,6 +44,13 @@ function CheckUserAvailability(useruid)
|
||||
return true;
|
||||
}
|
||||
|
||||
function DoesUserOwnLobby(useruid)
|
||||
{
|
||||
for (const lobby in Lobbies)
|
||||
if (Lobbies[lobby].owneruid === useruid) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
function IsUserInLobby(useruid)
|
||||
{
|
||||
// Doesn't matter if they own the lobby, if they do, they're
|
||||
@@ -60,14 +69,19 @@ function IsUserInLobby(useruid)
|
||||
return false;
|
||||
}
|
||||
|
||||
function DoesUserOwnLobby(useruid)
|
||||
function IsLobbyReady(lobbyuid)
|
||||
{
|
||||
for (const lobby in Lobbies)
|
||||
if (Lobbies[lobby].owneruid === useruid) return true;
|
||||
return false;
|
||||
if (!Lobbies[lobbyuid]) return false;
|
||||
if (!Lobbies[lobbyuid].players.length <= 1) return false;
|
||||
|
||||
const arePlayersReady = Lobbies[lobbyuid].players.every(e => e.ready)
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
function GetLobbyByUID(lobbyuid)
|
||||
{
|
||||
return Lobbies[lobbyuid];
|
||||
@@ -90,7 +104,6 @@ function GetLobbyByUserUID(playeruid)
|
||||
if (player.uid === playeruid) return Lobbies[lobby];
|
||||
}
|
||||
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -141,7 +154,7 @@ function UserJoinLobby(lobbyuid, useruid, callback)
|
||||
// TODO: check users and change lobby status
|
||||
|
||||
const user = Registrar.GetUserByUID(useruid);
|
||||
Lobbies[lobbyuid].players.push({uid: useruid, name: user.username});
|
||||
Lobbies[lobbyuid].players.push({uid: useruid, name: user.username, ready: false});
|
||||
|
||||
Logger.game(`LOBBY ${lobbyuid} USER ${useruid} (${user.username}) JOINING`);
|
||||
|
||||
@@ -150,6 +163,28 @@ function UserJoinLobby(lobbyuid, useruid, callback)
|
||||
return GetLobbyByUID(lobbyuid);
|
||||
}
|
||||
|
||||
function UserReady(useruid, callback)
|
||||
{
|
||||
if (!IsUserInLobby(useruid)) return false;
|
||||
|
||||
const lobbyuid = GetLobbyByUserUID(useruid).uid;
|
||||
Lobbies[lobbyuid].players[useruid].ready = true;
|
||||
|
||||
callback(GetUserByUID(useruid), GetLobbyByUserUID(useruid), 'user-ready');
|
||||
return true;
|
||||
}
|
||||
|
||||
function UserUnReady(useruid, callback)
|
||||
{
|
||||
if (!IsUserInLobby(useruid)) return false;
|
||||
|
||||
const lobbyuid = GetLobbyByUserUID(useruid).uid;
|
||||
Lobbies[lobbyuid].players[useruid].ready = false;
|
||||
|
||||
callback(GetUserByUID(useruid), GetLobbyByUserUID(useruid), 'user-unready');
|
||||
return true;
|
||||
}
|
||||
|
||||
// works for spectators too
|
||||
function UserLeaveLobby(useruid, callback)
|
||||
{
|
||||
@@ -196,6 +231,7 @@ module.exports = {
|
||||
CheckUserAvailability: CheckUserAvailability,
|
||||
DoesUserOwnLobby: DoesUserOwnLobby,
|
||||
IsUserInLobby: IsUserInLobby,
|
||||
IsLobbyReady: IsLobbyReady,
|
||||
|
||||
// Get lobby exports
|
||||
GetLobbyByUID: GetLobbyByUID,
|
||||
@@ -207,6 +243,8 @@ module.exports = {
|
||||
RegisterLobby: RegisterLobby,
|
||||
DeRegisterLobby: DeRegisterLobby,
|
||||
UserJoinLobby: UserJoinLobby,
|
||||
UserReady: UserReady,
|
||||
UserUnReady: UserUnReady,
|
||||
UserLeaveLobby: UserLeaveLobby,
|
||||
SpectatorJoinLobby: SpectatorJoinLobby
|
||||
}
|
||||
|
||||
@@ -51,7 +51,10 @@ async function Router(socket)
|
||||
socket.on('lobby-create', args => LobbyCreate(socket, args));
|
||||
socket.on('lobby-join', args => LobbyJoin(socket, args));
|
||||
socket.on('lobby-leave', args => LobbyLeave(socket, args));
|
||||
|
||||
|
||||
socket.on('lobby-user-ready', args => LobbyUserReady(socket, args));
|
||||
socket.on('lobby-user-unready', args => LobbyUserUnReady(socket, args));
|
||||
socket.on('lobby-game-begin', args => LobbyGameBegin(socket, args));
|
||||
|
||||
socket.on('disconnect', args => HandleDisconnect(socket, ...args));
|
||||
|
||||
@@ -230,6 +233,8 @@ function LobbyJoin(socket, args)
|
||||
return;
|
||||
}
|
||||
|
||||
console.log(status);
|
||||
|
||||
socket.join(lobby.uid);
|
||||
socket.emit('lobby-join-success', lobby);
|
||||
}
|
||||
@@ -244,6 +249,33 @@ function LobbyLeave(socket, args)
|
||||
Game.Lobbies.UserLeaveLobby(user.uid, LobbyUpdateCallback);
|
||||
}
|
||||
|
||||
function LobbyUserReady(socket, args)
|
||||
{
|
||||
const user = Game.Registrar.GetUserbyConnection(socket.id);
|
||||
|
||||
const ret = Game.Lobbies.UserReady(user.id, LobbyUpdateCallback);
|
||||
if (!ret) return;
|
||||
|
||||
Logger.debug(`USER ${user.uid} (${Game.Registrar.GetUserByUID(user.uid).username}) READY`);
|
||||
Logger.debug(JSON.stringify(Game.Lobbies.GetLobbyByUserUID(useruid)))
|
||||
}
|
||||
|
||||
function LobbyUserUnReady(socket, args)
|
||||
{
|
||||
const user = Game.Registrar.GetUserbyConnection(socket.id);
|
||||
|
||||
if (!Game.Lobbies.UserUnReady(user.id, LobbyUpdateCallback)) return;
|
||||
|
||||
Logger.debug(`USER ${user.uid} (${Game.Registrar.GetUserByUID(user.uid).username}) UNREADY`);
|
||||
Logger.debug(JSON.stringify(Game.Lobbies.GetLobbyByUserUID(useruid)))
|
||||
|
||||
}
|
||||
|
||||
function LobbyGameBegin(socket, args)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
|
||||
function HandleDisconnect(socket, args)
|
||||
{
|
||||
@@ -260,7 +292,15 @@ function HandleDisconnect(socket, args)
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Possible states
|
||||
*
|
||||
* lobby-deregister
|
||||
* lobby-join
|
||||
* user-ready
|
||||
* user-unready
|
||||
* lobby-leave
|
||||
*/
|
||||
function LobbyUpdateCallback(user, lobby, state)
|
||||
{
|
||||
// Just send updated lobby object for now
|
||||
|
||||
Reference in New Issue
Block a user