added join game via url.

This commit is contained in:
bgoulard 2026-01-12 13:30:53 +01:00 committed by Nigel
parent b862dc27f1
commit 152d5e9578
6 changed files with 18 additions and 67 deletions

View file

@ -40,7 +40,6 @@ export type TourInfo = {
export enum JoinRes { export enum JoinRes {
yes = 'yes', yes = 'yes',
no = 'dont ever talk to me or my kid ever again', no = 'dont ever talk to me or my kid ever again',
dev = 'yaaaaaaaaaaaaaaaaaaaaaaaa',
}; };
export interface ClientToServer { export interface ClientToServer {

View file

@ -101,6 +101,7 @@ const RIGHT: number = 1;
export class Pong { export class Pong {
public gameUpdate: NodeJS.Timeout | null = null; public gameUpdate: NodeJS.Timeout | null = null;
public userOnPage : [boolean, boolean] = [false, false];
public static readonly CONCEDED_TIMEOUT: number = 1500; public static readonly CONCEDED_TIMEOUT: number = 1500;

View file

@ -32,8 +32,8 @@ const route: FastifyPluginAsync = async (fastify): Promise<void> => {
}, },
async function(req, res) { async function(req, res) {
const resp = State.newPausedGame(req.body.user1 as UserId, req.body.user2 as UserId); const resp = State.newPausedGame(req.body.user1 as UserId, req.body.user2 as UserId);
if (isNullish(resp)) { return (res.makeResponse(404, 'failure', 'createPausedGame.generic.fail')); }
// else if (isNullish(resp)) { return (res.makeResponse(404, 'failure', 'createPausedGame.userUknown.fail')); }
return (res.makeResponse(200, 'success', 'createPausedGame.success', { gameId :resp })); return (res.makeResponse(200, 'success', 'createPausedGame.success', { gameId :resp }));
}, },
); );

View file

@ -1,39 +0,0 @@
import { MakeStaticResponse, typeResponse } from '@shared/utils';
import { FastifyPluginAsync } from 'fastify';
import Type, { Static } from 'typebox';
import { State } from '../state';
import { PongGameId } from '@shared/database/mixin/pong';
const startPausedGameParam = Type.Object({
gameId: Type.String({ description: '\'id\' | <gameid>' }),
});
type startPausedGameParam = Static<typeof startPausedGameParam>;
const startPausedGameResponse = {
'200': typeResponse('success', 'startPausedGame.success', {}),
'404': typeResponse('failure', 'startPausedGame.no_such_game'),
};
type startPausedGameResponse = MakeStaticResponse<typeof startPausedGameResponse>;
const route: FastifyPluginAsync = async (fastify): Promise<void> => {
fastify.post<{ Body: startPausedGameParam }>(
'/startPausedGame',
{
schema: {
body: startPausedGameParam,
response: startPausedGameResponse,
operationId: 'pongstartPauseGame',
},
},
async function(req, res) {
const resp = State.startPausedGame(req.body.gameId as PongGameId);
if (resp !== true) { return (res.makeResponse(404, 'failure', 'startPausedGame.generic.fail')); }
// else
return (res.makeResponse(200, 'success', 'startPausedGame.success'));
},
);
};
export default route;

View file

@ -40,7 +40,6 @@ export type TourInfo = {
export enum JoinRes { export enum JoinRes {
yes = 'yes', yes = 'yes',
no = 'dont ever talk to me or my kid ever again', no = 'dont ever talk to me or my kid ever again',
dev = 'yaaaaaaaaaaaaaaaaaaaaaaaa',
}; };
export interface ClientToServer { export interface ClientToServer {

View file

@ -223,12 +223,9 @@ class StateI {
} }
public newPausedGame(suid1: string, suid2: string): GameId | undefined { public newPausedGame(suid1: string, suid2: string): GameId | undefined {
// if ( if (!this.fastify.db.getUser(suid1) || !this.fastify.db.getUser(suid2)) {
// !this.users.has(suid1 as UserId) || return undefined;
// !this.users.has(suid2 as UserId) }
// ) {
// return undefined;
// }
const uid1: UserId = suid1 as UserId; const uid1: UserId = suid1 as UserId;
const uid2: UserId = suid2 as UserId; const uid2: UserId = suid2 as UserId;
const g = new Pong(uid1, uid2); const g = new Pong(uid1, uid2);
@ -236,19 +233,10 @@ class StateI {
const gameId = newUUID() as unknown as GameId; const gameId = newUUID() as unknown as GameId;
this.games.set(gameId, g); this.games.set(gameId, g);
this.fastify.log.info('new paused game \'' + gameId + '\'');
return gameId; return gameId;
} }
public startPausedGame(g_id: PongGameId): boolean {
let game: Pong | undefined;
if ((game = this.games.get(g_id)) === undefined) {
return false;
}
this.initGame(game, g_id, game.userLeft, game.userRight);
return true;
}
private tournamentIntervalFunc() { private tournamentIntervalFunc() {
const broadcastTourEnding = (msg: string) => { const broadcastTourEnding = (msg: string) => {
this.users.forEach((u) => { this.users.forEach((u) => {
@ -387,14 +375,17 @@ class StateI {
private tryJoinGame(g_id : string, sock : SSocket) : JoinRes { private tryJoinGame(g_id : string, sock : SSocket) : JoinRes {
const game_id : PongGameId = g_id as PongGameId; const game_id : PongGameId = g_id as PongGameId;
let game : Pong;
if (this.games.has(game_id) === false) if (this.games.has(game_id) === false) { return (JoinRes.no); }
const game : Pong = this.games.get(game_id)!;
if (game.local || (game.userLeft !== sock.authUser.id && game.userRight !== sock.authUser.id)) {
return (JoinRes.no); return (JoinRes.no);
game = this.games.get(game_id)!; }
if (game.local || game.userLeft !== sock.authUser.id || game.userRight !== sock.authUser.id) game.userOnPage[game.userLeft === sock.authUser.id ? 0 : 1] = true;
return (JoinRes.no); if (game.userOnPage[0] === game.userOnPage[1]) {
return (JoinRes.dev); this.initGame(game, game_id, game.userLeft, game.userRight);
}
return (JoinRes.yes);
} }
public registerUser(socket: SSocket): void { public registerUser(socket: SSocket): void {