lobby joining
This commit is contained in:
@@ -84,29 +84,10 @@ function createLobby()
|
||||
}
|
||||
|
||||
socket.on('lobby-create-success', lobby => {
|
||||
showActive();
|
||||
const lobbyDiv = document.createElement('div');
|
||||
lobbyDiv.id = lobby.id;
|
||||
|
||||
// TODO: Make drawlobby function
|
||||
lobbyDiv.innerHTML += `<h2>Lobby ${lobby.name}</h2><p><h3>Join Code: <b>${lobby.uid}</b></h3><p>Players:`;
|
||||
|
||||
for (const player of lobby.players)
|
||||
lobbyDiv.innerHTML += player.name + ' ';
|
||||
|
||||
if (lobby.allowspectators)
|
||||
{
|
||||
lobbyDiv.innerHTML += '<p>Spectators:';
|
||||
for (const player of lobby.spectators)
|
||||
lobbyDiv.innerHTML += player.name + ' ';
|
||||
}
|
||||
|
||||
lobbyDiv.innerHTML += `<p>Visibility: ${lobby.visibility}<p>State: ${lobby.state}`
|
||||
|
||||
lobbyDiv.innerHTML += '<input type="button" value="Start Game" onclick="" disabled>'
|
||||
lobbyDiv.innerHTML += '<input type="button" value="Leave Lobby" onclick="destructLobbies()">'
|
||||
|
||||
ActiveLobbyBlock.appendChild(lobbyDiv);
|
||||
const successDiv = document.createElement('div');
|
||||
successDiv.id = 'lobby-success';
|
||||
successDiv.innerHTML = 'SUCCESS: Lobby created, Joining...';
|
||||
CreateLobbyBlock.appendChild(successDiv);
|
||||
});
|
||||
|
||||
socket.on('lobby-create-error', (...args) => {
|
||||
@@ -157,12 +138,38 @@ function joinLobby()
|
||||
document.querySelector('#lobby-error').remove();
|
||||
}
|
||||
|
||||
socket.on('lobby-join-success', (...args) => {
|
||||
socket.on('lobby-join-success', (lobby) => {
|
||||
showActive();
|
||||
const lobbyDiv = document.createElement('div');
|
||||
lobbyDiv.id = lobby.id;
|
||||
|
||||
// TODO: Make drawlobby function
|
||||
lobbyDiv.innerHTML += `<h2>Lobby ${lobby.name}</h2><p><h3>Join Code: <b>${lobby.uid}</b></h3><p>Players:`;
|
||||
|
||||
for (const player of lobby.players)
|
||||
lobbyDiv.innerHTML += player.name + ' ';
|
||||
|
||||
if (lobby.allowspectators)
|
||||
{
|
||||
lobbyDiv.innerHTML += '<p>Spectators:';
|
||||
for (const player of lobby.spectators)
|
||||
lobbyDiv.innerHTML += player.name + ' ';
|
||||
}
|
||||
|
||||
lobbyDiv.innerHTML += `<p>Visibility: ${lobby.visibility}<p>State: ${lobby.state}`
|
||||
|
||||
lobbyDiv.innerHTML += '<input type="button" value="Start Game" onclick="" disabled>'
|
||||
lobbyDiv.innerHTML += '<input type="button" value="Leave Lobby" onclick="destructLobbies()">'
|
||||
|
||||
ActiveLobbyBlock.appendChild(lobbyDiv);
|
||||
});
|
||||
|
||||
socket.on('lobby-join-error', (...args) => {
|
||||
|
||||
const errorDiv = document.createElement('div');
|
||||
errorDiv.id = 'lobby-error';
|
||||
errorDiv.innerHTML = 'ERROR: An error occured while joining the lobby' + JSON.stringify(args);
|
||||
errorDiv.classList.add('red');
|
||||
CreateLobbyBlock.appendChild(errorDiv);
|
||||
});
|
||||
|
||||
|
||||
|
||||
@@ -10,7 +10,7 @@ USER OBJECT
|
||||
uid: uid,
|
||||
ip: ip,
|
||||
// REGISTERED, CONNECTED, DISCONNECTED
|
||||
state: 'REGISTERED',
|
||||
state: 'CONNECTED',
|
||||
// LOBYING, GAME, UNDECIDED
|
||||
intent: 'LOBBYING',
|
||||
// Doesn't update if state changes
|
||||
@@ -95,8 +95,10 @@ function RegisterUser(username, ip)
|
||||
username: username,
|
||||
uid: uid,
|
||||
ip: ip,
|
||||
// REGISTERED, CONNECTED, DISCONNECTED, INGAME
|
||||
// REGISTERED, CONNECTED, DISCONNECTED
|
||||
state: 'REGISTERED',
|
||||
// LOBYING, GAME, UNDECIDED
|
||||
intent: 'UNDECIDED',
|
||||
// Doesn't update if state changes
|
||||
connectionid: 'none',
|
||||
};
|
||||
@@ -117,6 +119,7 @@ function UserConnectionExists(userid)
|
||||
{
|
||||
if (OnlineUsers[userid].state === 'CONNECTED') return true;
|
||||
if (OnlineUsers[userid].state === 'DISCONNECTED') return false;
|
||||
if (OnlineUsers[userid].state === 'REGISTERED') return false;
|
||||
}
|
||||
|
||||
function GetUserbyConnection(connectionid)
|
||||
@@ -127,6 +130,7 @@ function GetUserbyConnection(connectionid)
|
||||
return false;
|
||||
}
|
||||
|
||||
// TODO: User intent
|
||||
function UserConnect(useruid, connectionid)
|
||||
{
|
||||
if (OnlineUsers[useruid].state === 'CONNECTED') return 'User Already Connected';
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
const Logger = require('./logger.js');
|
||||
|
||||
const Registrar = require('./game-registrar.js');
|
||||
|
||||
/*
|
||||
LOBBY OBJECT
|
||||
@@ -19,25 +19,51 @@ NOTES
|
||||
- Users can only own one lobby
|
||||
- Lobby UID is "join code", will be much shorter than userid
|
||||
- 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
|
||||
*/
|
||||
let Lobbies = [];
|
||||
|
||||
|
||||
function CheckUserAvailability(owneruid)
|
||||
function CheckUserAvailability(useruid)
|
||||
{
|
||||
// if user owns lobby
|
||||
for (const lobby in Lobbies)
|
||||
if (Lobbies[lobby].owneruid == owneruid) return false;
|
||||
|
||||
// if user is in any lobbies already
|
||||
{
|
||||
// if user owns lobby
|
||||
if (Lobbies[lobby].owneruid === useruid) return false;
|
||||
// if user is in any lobbies already
|
||||
if (Lobbies[lobby].players.includes(useruid)) return false;
|
||||
// if user is spectating any lobbies already
|
||||
if (Lobbies[lobby].spectators.includes(useruid)) return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
function GetLobbyByUID(lobbyUID)
|
||||
function IsUserInLobby(useruid)
|
||||
{
|
||||
return Lobbies[lobbyUID];
|
||||
for (const lobby in Lobbies)
|
||||
{
|
||||
// if user is in any lobbies already
|
||||
if (Lobbies[lobby].players.includes(useruid)) return true;
|
||||
// if user is spectating any lobbies already
|
||||
if (Lobbies[lobby].spectators.includes(useruid)) return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
function DoesUserOwnLobby(lobbyuid, useruid)
|
||||
{
|
||||
if (!Lobbies[lobbyuid]) return false;
|
||||
if (Lobbies[lobbyuid].owneruid === useruid) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
function GetLobbyByUID(lobbyuid)
|
||||
{
|
||||
return Lobbies[lobbyuid];
|
||||
}
|
||||
|
||||
function GetLobbyByUserUID(owneruid)
|
||||
@@ -72,7 +98,23 @@ function RegisterLobby(owneruid, name, private, spectators)
|
||||
|
||||
}
|
||||
|
||||
function DeRegisterLobby(lobbyid)
|
||||
function DeRegisterLobby(lobbyuid)
|
||||
{
|
||||
delete Lobbies[lobbyuid];
|
||||
}
|
||||
|
||||
function UserJoinLobby(lobbyuid, useruid)
|
||||
{
|
||||
if (IsUserInLobby(useruid)) return false;
|
||||
if (!Lobbies[lobbyuid]) return false;
|
||||
if (!Registrar.GetUserByUID(useruid)) return false;
|
||||
|
||||
Lobbies[lobbyuid].players.push({uid: useruid, name: Registrar.GetUserByUID(useruid).username});
|
||||
|
||||
return GetLobbyByUID(lobbyuid);
|
||||
}
|
||||
|
||||
function SpectatorJoinLobby(lobbyuid, useruid)
|
||||
{
|
||||
|
||||
}
|
||||
@@ -81,6 +123,8 @@ function DeRegisterLobby(lobbyid)
|
||||
module.exports = {
|
||||
// Lobby validation exports
|
||||
CheckUserAvailability: CheckUserAvailability,
|
||||
DoesUserOwnLobby: DoesUserOwnLobby,
|
||||
IsUserInLobby: IsUserInLobby,
|
||||
|
||||
// Get lobby exports
|
||||
GetLobbyByUID: GetLobbyByUID,
|
||||
@@ -88,5 +132,7 @@ module.exports = {
|
||||
|
||||
// Change lobby state exports
|
||||
RegisterLobby: RegisterLobby,
|
||||
DeRegisterLobby: DeRegisterLobby
|
||||
DeRegisterLobby: DeRegisterLobby,
|
||||
UserJoinLobby: UserJoinLobby,
|
||||
SpectatorJoinLobby: SpectatorJoinLobby
|
||||
}
|
||||
|
||||
@@ -49,6 +49,14 @@ module.exports.database = function(message) {
|
||||
+ colours.magenta(Dialect) + '] ' + message);
|
||||
}
|
||||
|
||||
module.exports.game = function(message) {
|
||||
let d = moment().format(dateFormat);
|
||||
fs.appendFileSync(logPath, `[${d.toLocaleString()}] [GAME] ${message} \n`);
|
||||
if (LogLevel > 1) return;
|
||||
console.log('[' + d.toLocaleString() + '] ['
|
||||
+ colours.brightBlue('GAME') + '] ' + message);
|
||||
}
|
||||
|
||||
module.exports.middleware = function(origin, message) {
|
||||
let d = moment().format(dateFormat);
|
||||
fs.appendFileSync(logPath, `[${d.toLocaleString()}] [MIDDLEWARE: ${origin}] ${message} \n`);
|
||||
|
||||
@@ -29,7 +29,7 @@ function init()
|
||||
const io = require('socket.io')(WebServer.Server);
|
||||
|
||||
io.on('connection', (socket) => {
|
||||
Logger.info(`NEW SOCKET CIENT ID ${socket.id}`)
|
||||
Logger.info(`NEW SOCKET CIENT ID ${socket.id}`);
|
||||
// Pass socket onto router
|
||||
Router(socket);
|
||||
})
|
||||
@@ -50,10 +50,10 @@ async function Router(socket)
|
||||
socket.on('identify', args => ClientIdentify(socket, args));
|
||||
|
||||
socket.on('lobby-create', args => LobbyCreate(socket, args));
|
||||
socket.on('lobby-destroy');
|
||||
// socket.on('lobby-destroy');
|
||||
|
||||
socket.on('lobby-join', args => LobbyJoin(socket, args));
|
||||
socket.on('lobby-leave');
|
||||
// socket.on('lobby-leave');
|
||||
|
||||
|
||||
socket.on('disconnect', args => HandleDisconnect(socket, ...args));
|
||||
@@ -108,9 +108,9 @@ function LobbyCreate(socket, args)
|
||||
{
|
||||
const err = new Error;
|
||||
|
||||
const userUID = args.user.uid;
|
||||
const useruid = args.user.uid;
|
||||
|
||||
if (!userUID)
|
||||
if (!useruid)
|
||||
{
|
||||
err.addError(400, 'Bad Request', 'Unknown uid');
|
||||
socket.emit('lobby-create-error', err.toError);
|
||||
@@ -126,7 +126,7 @@ function LobbyCreate(socket, args)
|
||||
|
||||
// Make sure user is who they say they are
|
||||
const user = Game.Registrar.GetUserbyConnection(socket.id);
|
||||
if (!user || user.uid != userUID)
|
||||
if (!user || user.uid != useruid)
|
||||
{
|
||||
err.addError(403, 'Forbidden', 'Illegal user');
|
||||
socket.emit('lobby-create-error', err.toError);
|
||||
@@ -134,14 +134,14 @@ function LobbyCreate(socket, args)
|
||||
}
|
||||
|
||||
// Make sure user doesn't already own a lobby
|
||||
if (!Game.Lobbies.CheckUserAvailability(userUID))
|
||||
if (!Game.Lobbies.CheckUserAvailability(useruid))
|
||||
{
|
||||
err.addError(400, 'Bad Request', 'User already owns lobby');
|
||||
socket.emit('lobby-create-error', err.toError);
|
||||
return;
|
||||
}
|
||||
|
||||
const lobby = Game.Lobbies.RegisterLobby(userUID, args.lobbyName, args.lobbyPrivate, args.lobbySpectators);
|
||||
const lobby = Game.Lobbies.RegisterLobby(useruid, args.lobbyName, args.lobbyPrivate, args.lobbySpectators);
|
||||
|
||||
if (!lobby)
|
||||
{
|
||||
@@ -150,7 +150,26 @@ function LobbyCreate(socket, args)
|
||||
return;
|
||||
}
|
||||
|
||||
socket.emit('lobby-create-success', lobby);
|
||||
// Lobby created
|
||||
socket.emit('lobby-create-success', {created: true, lobby: lobby});
|
||||
|
||||
const lobbyJoined = Game.Lobbies.UserJoinLobby(lobby.uid, useruid);
|
||||
if (!lobbyJoined)
|
||||
{
|
||||
err.addError(403, 'Forbidden', 'Cannot join lobby');
|
||||
socket.emit('lobby-create-error', err.toError);
|
||||
return;
|
||||
}
|
||||
|
||||
console.log(lobby, lobbyJoined);
|
||||
if (lobbyJoined.uid !== lobby.uid)
|
||||
{
|
||||
err.addError(500, 'Internal Server Error', 'Illegal lobby');
|
||||
socket.emit('lobby-create-error', err.toError);
|
||||
return;
|
||||
}
|
||||
|
||||
socket.emit('lobby-join-success', lobby);
|
||||
}
|
||||
|
||||
function LobbyJoin(socket, args)
|
||||
|
||||
Reference in New Issue
Block a user