don't remove player on disconnect

This commit is contained in:
Nathaniel Tampus
2023-03-05 19:03:58 +08:00
parent 69fb7a7c9d
commit 5699663dd2
2 changed files with 18 additions and 18 deletions

View File

@@ -9,9 +9,12 @@ export async function joinLobby(this: Socket, gameCode: string) {
console.log(`joinLobby: Game code ${gameCode} not found.`); console.log(`joinLobby: Game code ${gameCode} not found.`);
return; return;
} }
if (
!(game.white?.id === this.request.session.id || game.black?.id === this.request.session.id) if (game.white?.id === this.request.session.id) {
) { game.white.connected = true;
} else if (game.black?.id === this.request.session.id) {
game.black.connected = true;
} else {
if (game.observers === undefined) game.observers = []; if (game.observers === undefined) game.observers = [];
game.observers?.push(this.request.session.user); game.observers?.push(this.request.session.user);
} }
@@ -26,9 +29,7 @@ export async function joinLobby(this: Socket, gameCode: string) {
} }
await this.join(gameCode); await this.join(gameCode);
this.emit("receivedLatestGame", game); io.to(game.code as string).emit("receivedLatestGame", game);
io.to(game.code as string).emit("receivedLatestLobby", game);
io.to(game.code as string).emit("userJoined", this.request.session.user.name);
} }
export async function leaveLobby(this: Socket, code?: string) { export async function leaveLobby(this: Socket, code?: string) {
@@ -46,21 +47,19 @@ export async function leaveLobby(this: Socket, code?: string) {
if (game) { if (game) {
const user = game.observers?.find((o) => o.id === this.request.session.id); const user = game.observers?.find((o) => o.id === this.request.session.id);
let name = "";
if (user) { if (user) {
name = user.name as string;
game.observers?.splice(game.observers?.indexOf(user), 1); game.observers?.splice(game.observers?.indexOf(user), 1);
} }
if (game.black?.id === this.request.session.id) { if (game.black?.id === this.request.session.id) {
name = game.black?.name as string; game.black.connected = false;
game.black = undefined; } else if (game.white?.id === this.request.session.id) {
} game.white.connected = false;
if (game.white?.id === this.request.session.id) {
name = game.white?.name as string;
game.white = undefined;
} }
if (!game.white && !game.black && (!game.observers || game.observers.length === 0)) { // count sockets
const sockets = await io.in(game.code as string).fetchSockets();
if (sockets.length <= 1) {
if (game.timeout) clearTimeout(game.timeout); if (game.timeout) clearTimeout(game.timeout);
game.timeout = Number( game.timeout = Number(
setTimeout(() => { setTimeout(() => {
@@ -68,8 +67,7 @@ export async function leaveLobby(this: Socket, code?: string) {
}, 1000 * 60 * 30) // 30 minutes }, 1000 * 60 * 30) // 30 minutes
); );
} else { } else {
this.to(game.code as string).emit("userLeft", name); this.to(game.code as string).emit("receivedLatestGame", game);
this.to(game.code as string).emit("receivedLatestLobby", game);
} }
} }
await this.leave(code || Array.from(this.rooms)[1]); await this.leave(code || Array.from(this.rooms)[1]);
@@ -140,6 +138,7 @@ export async function joinAsPlayer(this: Socket) {
const user = game.observers?.find((o) => o.id === this.request.session.id); const user = game.observers?.find((o) => o.id === this.request.session.id);
if (!game.white) { if (!game.white) {
game.white = this.request.session.user; game.white = this.request.session.user;
game.white.connected = true;
if (user) game.observers?.splice(game.observers?.indexOf(user), 1); if (user) game.observers?.splice(game.observers?.indexOf(user), 1);
io.to(game.code as string).emit("userJoinedAsPlayer", { io.to(game.code as string).emit("userJoinedAsPlayer", {
name: this.request.session.user.name, name: this.request.session.user.name,
@@ -147,6 +146,7 @@ export async function joinAsPlayer(this: Socket) {
}); });
} else if (!game.black) { } else if (!game.black) {
game.black = this.request.session.user; game.black = this.request.session.user;
game.black.connected = true;
if (user) game.observers?.splice(game.observers?.indexOf(user), 1); if (user) game.observers?.splice(game.observers?.indexOf(user), 1);
io.to(game.code as string).emit("userJoinedAsPlayer", { io.to(game.code as string).emit("userJoinedAsPlayer", {
name: this.request.session.user.name, name: this.request.session.user.name,
@@ -156,7 +156,6 @@ export async function joinAsPlayer(this: Socket) {
console.log("[WARNING] attempted to join a game with already 2 players"); console.log("[WARNING] attempted to join a game with already 2 players");
} }
io.to(game.code as string).emit("receivedLatestGame", game); io.to(game.code as string).emit("receivedLatestGame", game);
io.to(game.code as string).emit("receivedLatestLobby", game);
} }
export async function chat(this: Socket, message: string) { export async function chat(this: Socket, message: string) {

1
types/index.d.ts vendored
View File

@@ -15,4 +15,5 @@ export interface User {
id?: number | string; // string for guest IDs id?: number | string; // string for guest IDs
name?: string; name?: string;
email?: string; email?: string;
connected?: boolean; // mainly for players, not spectators
} }