game connection intent flow working
This commit is contained in:
@@ -158,14 +158,15 @@ function GetUserbyConnection(connectionid)
|
||||
}
|
||||
|
||||
// TODO: User intent
|
||||
function UserConnect(useruid, connectionid)
|
||||
function UserConnect(useruid, connectionid, intent)
|
||||
{
|
||||
if (OnlineUsers[useruid].state === 'CONNECTED') return 'error-taken-user-connection';
|
||||
|
||||
OnlineUsers[useruid].connectionid = connectionid;
|
||||
OnlineUsers[useruid].state = 'CONNECTED';
|
||||
OnlineUsers[useruid].intent = intent;
|
||||
|
||||
Logger.game(`SOCKET ${connectionid} IDENTIFIED AS ${useruid} (${OnlineUsers[useruid].username})`);
|
||||
Logger.game(`SOCKET ${connectionid} IDENTIFIED AS ${useruid} (${OnlineUsers[useruid].username}) INTENDS TO ${intent}`);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ LOBBY OBJECT
|
||||
uid: uid,
|
||||
name: string
|
||||
owneruid: useruid,
|
||||
players: [{uid, name, ready}],
|
||||
players: [{uid, name, ready, ingame}],
|
||||
spectators: [{uid, name}],
|
||||
// PUBLIC, PRIVATE
|
||||
visibility: 'PUBLIC',
|
||||
@@ -74,13 +74,18 @@ function IsLobbyReady(lobbyuid)
|
||||
if (!Lobbies[lobbyuid]) return false;
|
||||
// only support 2-4 players
|
||||
// https://en.wikipedia.org/wiki/Scrabble
|
||||
// TODO: ADD THIS BACK - REMOVED FOR TESTING
|
||||
// TODO: URGENT ADD THIS BACK AFTER TESTING
|
||||
// if (Lobbies[lobbyuid].players.length <= 1) return false;
|
||||
if (Lobbies[lobbyuid].players.length > 4) return false;
|
||||
|
||||
return Lobbies[lobbyuid].players.every(e => e.ready);
|
||||
}
|
||||
|
||||
function IsLobbyReadyForGame(lobbyuid)
|
||||
{
|
||||
if (!Lobbies[lobbyuid]) return false;
|
||||
return Lobbies[lobbyuid].players.every(e => e.ingame);
|
||||
}
|
||||
|
||||
|
||||
function GetLobbyByUID(lobbyuid)
|
||||
@@ -95,14 +100,14 @@ function GetLobbyByOwnerUID(owneruid)
|
||||
return false;
|
||||
}
|
||||
|
||||
function GetLobbyByUserUID(playeruid)
|
||||
function GetLobbyByUserUID(useruid)
|
||||
{
|
||||
for (const lobby in Lobbies)
|
||||
{
|
||||
for (const player of Lobbies[lobby].players)
|
||||
if (player.uid === playeruid) return Lobbies[lobby];
|
||||
if (player.uid === useruid) return Lobbies[lobby];
|
||||
for (const player of Lobbies[lobby].spectators)
|
||||
if (player.uid === playeruid) return Lobbies[lobby];
|
||||
if (player.uid === useruid) return Lobbies[lobby];
|
||||
}
|
||||
|
||||
return false;
|
||||
@@ -155,7 +160,12 @@ 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, ready: false});
|
||||
Lobbies[lobbyuid].players.push({
|
||||
uid: useruid,
|
||||
name: user.username,
|
||||
ready: false,
|
||||
ingame: false
|
||||
});
|
||||
|
||||
Logger.game(`LOBBY ${lobbyuid} USER ${useruid} (${user.username}) JOINING`);
|
||||
|
||||
@@ -238,6 +248,24 @@ function SpectatorJoinLobby(lobbyuid, useruid, callback)
|
||||
|
||||
}
|
||||
|
||||
function UserConnectGame(useruid)
|
||||
{
|
||||
if (!IsUserInLobby(useruid)) return false;
|
||||
|
||||
const lobby = GetLobbyByUserUID(useruid);
|
||||
|
||||
for (const player in Lobbies[lobby.uid].players)
|
||||
if (Lobbies[lobby.uid].players[player].uid === useruid)
|
||||
Lobbies[lobby.uid].players[player].ingame = true;
|
||||
|
||||
|
||||
for (const spectator in Lobbies[lobby.uid].spectators)
|
||||
if (Lobbies[lobby.uid].spectators[player].uid === useruid)
|
||||
Lobbies[lobby.uid].spectators[spectator].ingame = true;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
module.exports = {
|
||||
// Lobby validation exports
|
||||
@@ -245,6 +273,7 @@ module.exports = {
|
||||
DoesUserOwnLobby: DoesUserOwnLobby,
|
||||
IsUserInLobby: IsUserInLobby,
|
||||
IsLobbyReady: IsLobbyReady,
|
||||
IsLobbyReadyForGame: IsLobbyReadyForGame,
|
||||
|
||||
// Get lobby exports
|
||||
GetLobbyByUID: GetLobbyByUID,
|
||||
@@ -259,5 +288,6 @@ module.exports = {
|
||||
UserReady: UserReady,
|
||||
UserUnReady: UserUnReady,
|
||||
UserLeaveLobby: UserLeaveLobby,
|
||||
SpectatorJoinLobby: SpectatorJoinLobby
|
||||
SpectatorJoinLobby: SpectatorJoinLobby,
|
||||
UserConnectGame: UserConnectGame
|
||||
}
|
||||
|
||||
@@ -76,13 +76,6 @@ function ClientIdentify(socket, args)
|
||||
const user = Game.Registrar.GetUserByUID(args.userid);
|
||||
const intent = args.intent;
|
||||
|
||||
if (!intent)
|
||||
{
|
||||
err.addError(400, 'Bad Request', 'error-bad-intent');
|
||||
socket.emit('identify-error', err.toError);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!user)
|
||||
{
|
||||
err.addError(400, 'Bad Request', 'error-unknown-uid');
|
||||
@@ -90,14 +83,47 @@ function ClientIdentify(socket, args)
|
||||
return;
|
||||
}
|
||||
|
||||
// TODO: Sort out client intent
|
||||
|
||||
const status = Game.Registrar.UserConnect(user.uid, socket.id);
|
||||
if (!intent)
|
||||
{
|
||||
err.addError(400, 'Bad Request', 'error-bad-intent');
|
||||
socket.emit('identify-error', err.toError);
|
||||
return;
|
||||
}
|
||||
|
||||
const oldIntent = user.intent;
|
||||
|
||||
Game.Registrar.ChangeUserIntent(user.uid, intent);
|
||||
const status = Game.Registrar.UserConnect(user.uid, socket.id, intent);
|
||||
|
||||
// If the user enters a game without transitioning, no bueno
|
||||
if (intent === 'GAME' && oldIntent !== 'GAMETRANSITION')
|
||||
{
|
||||
err.addError(500, 'Internal Server Error', 'error-illegal-intent');
|
||||
socket.emit('identify-error', err.toError);
|
||||
return;
|
||||
}
|
||||
|
||||
// User intends to enter a game
|
||||
if (intent === 'GAME' && oldIntent === 'GAMETRANSITION')
|
||||
{
|
||||
const lobbyUID = args.lobbyuid;
|
||||
|
||||
// Make sure the user is actually in this game
|
||||
const lobby = Game.Lobbies.GetLobbyByUserUID(user.uid);
|
||||
if (lobby.uid !== lobbyUID)
|
||||
{
|
||||
err.addError(500, 'Internal Server Error', 'error-illegal-intent');
|
||||
socket.emit('identify-error', err.toError);
|
||||
return;
|
||||
}
|
||||
|
||||
Game.Lobbies.UserConnectGame(user.uid);
|
||||
}
|
||||
|
||||
|
||||
if (status === true)
|
||||
{
|
||||
socket.emit('identify-success', {connected: true, user: user});
|
||||
Game.Registrar.ChangeUserIntent(user.uid, intent);
|
||||
return;
|
||||
}
|
||||
else if (status === 'error-taken-user-connection')
|
||||
@@ -319,7 +345,11 @@ function HandleDisconnect(socket, args)
|
||||
|
||||
// if user is in a lobby, leave and if user own's a lobby, destruct
|
||||
// leave lobby before user is disconnected
|
||||
LobbyLeave(socket);
|
||||
|
||||
if (user.intent !== 'GAMETRANSITION')
|
||||
{
|
||||
LobbyLeave(socket);
|
||||
}
|
||||
|
||||
Game.Registrar.UserDisconnect(user.uid);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user