sync user info in games, adjust game timeout

This commit is contained in:
Nathaniel Tampus
2023-03-19 20:51:29 +08:00
parent 42dc24a9bc
commit 17ef05b31c
2 changed files with 38 additions and 8 deletions

View File

@@ -34,13 +34,14 @@ export const getActiveGame = async (req: Request, res: Response) => {
export const createGame = async (req: Request, res: Response) => { export const createGame = async (req: Request, res: Response) => {
try { try {
if (!req.session.user) { if (!req.session.user?.id) {
console.log("unauthorized createGame"); console.log("unauthorized createGame");
res.status(401).end(); res.status(401).end();
return; return;
} }
const user: User = { const user: User = {
...req.session.user, id: req.session.user.id,
name: req.session.user.name,
connected: false connected: false
}; };
const unlisted: boolean = req.body.unlisted ?? false; const unlisted: boolean = req.body.unlisted ?? false;

View File

@@ -10,13 +10,29 @@ export async function joinLobby(this: Socket, gameCode: string) {
return; return;
} }
if (game.host && game.host?.id === this.request.session.user.id) {
game.host.connected = true;
if (game.host.name !== this.request.session.user.name) {
game.host.name = this.request.session.user.name;
}
}
if (game.white && game.white?.id === this.request.session.user.id) { if (game.white && game.white?.id === this.request.session.user.id) {
game.white.connected = true; game.white.connected = true;
if (game.white.name !== this.request.session.user.name) {
game.white.name = this.request.session.user.name;
}
} else if (game.black && game.black?.id === this.request.session.user.id) { } else if (game.black && game.black?.id === this.request.session.user.id) {
game.black.connected = true; game.black.connected = true;
if (game.black.name !== this.request.session.user.name) {
game.black.name = this.request.session.user.name;
}
} else { } else {
if (game.observers === undefined) game.observers = []; if (game.observers === undefined) game.observers = [];
game.observers?.push(this.request.session.user); const user = {
id: this.request.session.user.id,
name: this.request.session.user.name
};
game.observers?.push(user);
} }
if (this.rooms.size >= 2) { if (this.rooms.size >= 2) {
@@ -61,10 +77,15 @@ export async function leaveLobby(this: Socket, reason?: DisconnectReason, code?:
if (sockets.length <= 0 || (reason === undefined && sockets.length <= 1)) { if (sockets.length <= 0 || (reason === undefined && sockets.length <= 1)) {
if (game.timeout) clearTimeout(game.timeout); if (game.timeout) clearTimeout(game.timeout);
let timeout = 1000 * 60; // 1 minute
if (game.pgn) {
timeout *= 20; // 20 minutes if game has started
}
game.timeout = Number( game.timeout = Number(
setTimeout(() => { setTimeout(() => {
activeGames.splice(activeGames.indexOf(game), 1); activeGames.splice(activeGames.indexOf(game), 1);
}, 1000 * 60 * 10) // 10 minutes }, timeout)
); );
} else { } else {
this.to(game.code as string).emit("receivedLatestGame", game); this.to(game.code as string).emit("receivedLatestGame", game);
@@ -137,16 +158,24 @@ export async function joinAsPlayer(this: Socket) {
if (!game) return; if (!game) return;
const user = game.observers?.find((o) => o.id === this.request.session.user.id); const user = game.observers?.find((o) => o.id === this.request.session.user.id);
if (!game.white) { if (!game.white) {
game.white = this.request.session.user; const sessionUser = {
game.white.connected = true; id: this.request.session.user.id,
name: this.request.session.user.name,
connected: true
};
game.white = sessionUser;
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,
side: "white" side: "white"
}); });
} else if (!game.black) { } else if (!game.black) {
game.black = this.request.session.user; const sessionUser = {
game.black.connected = true; id: this.request.session.user.id,
name: this.request.session.user.name,
connected: true
};
game.black = sessionUser;
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,