(tic-tac-toe): Fix indentation and formatting in state.ts.

This commit is contained in:
Alessandro Petitcollin 2026-01-05 12:08:10 +01:00 committed by Maix0
parent f9801dafe7
commit d222dfd332

View file

@ -1,10 +1,10 @@
import {UserId} from '@shared/database/mixin/user'; import { UserId } from '@shared/database/mixin/user';
import {FastifyInstance} from 'fastify'; import { FastifyInstance } from 'fastify';
import {GameMove, SSocket} from './socket'; import { GameMove, SSocket } from './socket';
import {isNullish} from '@shared/utils'; import { isNullish } from '@shared/utils';
import {newUUID} from '@shared/utils/uuid'; import { newUUID } from '@shared/utils/uuid';
import {GameId} from '@shared/database/mixin/tictactoe'; import { GameId } from '@shared/database/mixin/tictactoe';
import {TTC} from './game'; import { TTC } from './game';
type TTTUser = { type TTTUser = {
socket: SSocket, socket: SSocket,
@ -15,191 +15,191 @@ type TTTUser = {
} }
export class StateI { export class StateI {
private users: Map<UserId, TTTUser> = new Map(); private users: Map<UserId, TTTUser> = new Map();
private queue: Set<UserId> = new Set(); private queue: Set<UserId> = new Set();
private queueInterval: NodeJS.Timeout; private queueInterval: NodeJS.Timeout;
private games: Map<GameId, TTC> = new Map(); private games: Map<GameId, TTC> = new Map();
constructor(private fastify: FastifyInstance) { constructor(private fastify: FastifyInstance) {
this.queueInterval = setInterval(() => this.queuerFunction()); this.queueInterval = setInterval(() => this.queuerFunction());
void this.queueInterval; void this.queueInterval;
} }
public registerUser(socket: SSocket): void { public registerUser(socket: SSocket): void {
this.fastify.log.info('Registering new user'); this.fastify.log.info('Registering new user');
if (this.users.has(socket.authUser.id)) { if (this.users.has(socket.authUser.id)) {
socket.emit('forceDisconnect', 'Already Connected'); socket.emit('forceDisconnect', 'Already Connected');
socket.disconnect(); socket.disconnect();
return; return;
} }
this.users.set(socket.authUser.id, { this.users.set(socket.authUser.id, {
socket, socket,
userId: socket.authUser.id, userId: socket.authUser.id,
windowId: socket.id, windowId: socket.id,
updateInterval: setInterval(() => this.updateClient(socket), 3000), updateInterval: setInterval(() => this.updateClient(socket), 3000),
currentGame: null, currentGame: null,
}); });
this.fastify.log.info('Registered new user'); this.fastify.log.info('Registered new user');
socket.on('disconnect', () => this.cleanupUser(socket)); socket.on('disconnect', () => this.cleanupUser(socket));
socket.on('enqueue', () => this.enqueueUser(socket)); socket.on('enqueue', () => this.enqueueUser(socket));
socket.on('dequeue', () => this.dequeueUser(socket)); socket.on('dequeue', () => this.dequeueUser(socket));
socket.on('debugInfo', () => this.debugSocket(socket)); socket.on('debugInfo', () => this.debugSocket(socket));
socket.on('gameMove', (e) => this.gameMove(socket, e)); socket.on('gameMove', (e) => this.gameMove(socket, e));
if (socket) { if (socket) {
console.log('Socket:', socket.id); console.log('Socket:', socket.id);
} }
} }
private queuerFunction(): void { private queuerFunction(): void {
const values = Array.from(this.queue.values()); const values = Array.from(this.queue.values());
while (values.length >= 2) { while (values.length >= 2) {
const id1 = values.pop(); const id1 = values.pop();
const id2 = values.pop(); const id2 = values.pop();
if (isNullish(id1) || isNullish(id2)) { if (isNullish(id1) || isNullish(id2)) {
continue; continue;
} }
const u1 = this.users.get(id1); const u1 = this.users.get(id1);
const u2 = this.users.get(id2); const u2 = this.users.get(id2);
if (isNullish(u1) || isNullish(u2)) { if (isNullish(u1) || isNullish(u2)) {
continue; continue;
} }
this.queue.delete(id1); this.queue.delete(id1);
this.queue.delete(id2); this.queue.delete(id2);
const gameId = newUUID() as unknown as GameId; const gameId = newUUID() as unknown as GameId;
const g = new TTC(u1.userId, u2.userId); const g = new TTC(u1.userId, u2.userId);
const iState = { const iState = {
boardState: g.board, boardState: g.board,
currentPlayer: g.getCurrentState(), currentPlayer: g.getCurrentState(),
playerX: g.playerX, playerX: g.playerX,
playerO: g.playerO, playerO: g.playerO,
gameState: g.checkState(), gameState: g.checkState(),
gameId: gameId, gameId: gameId,
}; };
u1.socket.emit('newGame', iState); u1.socket.emit('newGame', iState);
u2.socket.emit('newGame', iState); u2.socket.emit('newGame', iState);
this.games.set(gameId, g); this.games.set(gameId, g);
u1.currentGame = gameId; u1.currentGame = gameId;
u2.currentGame = gameId; u2.currentGame = gameId;
g.gameUpdate = setInterval(() => { g.gameUpdate = setInterval(() => {
this.gameUpdate(gameId, u1.socket); this.gameUpdate(gameId, u1.socket);
this.gameUpdate(gameId, u2.socket); this.gameUpdate(gameId, u2.socket);
if (g.checkState() !== 'ongoing') { if (g.checkState() !== 'ongoing') {
this.cleanupGame(gameId, g); this.cleanupGame(gameId, g);
this.fastify.db.setGameOutcome(gameId, u1.userId, u2.userId, g.checkState()); this.fastify.db.setGameOutcome(gameId, u1.userId, u2.userId, g.checkState());
} }
}, 100); }, 100);
} }
} }
private updateClient(socket: SSocket): void { private updateClient(socket: SSocket): void {
socket.emit('updateInformation', { socket.emit('updateInformation', {
inQueue: this.queue.size, inQueue: this.queue.size,
totalUser: this.users.size, totalUser: this.users.size,
}); });
} }
private cleanupUser(socket: SSocket): void { private cleanupUser(socket: SSocket): void {
if (!this.users.has(socket.authUser.id)) return; if (!this.users.has(socket.authUser.id)) return;
clearInterval(this.users.get(socket.authUser.id)?.updateInterval); clearInterval(this.users.get(socket.authUser.id)?.updateInterval);
this.users.delete(socket.authUser.id); this.users.delete(socket.authUser.id);
this.queue.delete(socket.authUser.id); this.queue.delete(socket.authUser.id);
} }
private cleanupGame(gameId: GameId, game: TTC): void { private cleanupGame(gameId: GameId, game: TTC): void {
clearInterval(game.gameUpdate ?? undefined); clearInterval(game.gameUpdate ?? undefined);
this.games.delete(gameId); this.games.delete(gameId);
let player: TTTUser | undefined; let player: TTTUser | undefined;
if ((player = this.users.get(game.playerO)) !== undefined) { if ((player = this.users.get(game.playerO)) !== undefined) {
player.currentGame = null; player.currentGame = null;
player.socket.emit('gameEnd'); player.socket.emit('gameEnd');
} }
if ((player = this.users.get(game.playerX)) !== undefined) { if ((player = this.users.get(game.playerX)) !== undefined) {
player.currentGame = null; player.currentGame = null;
player.socket.emit('gameEnd'); player.socket.emit('gameEnd');
} }
// do something here with the game result before deleting the game at the end // do something here with the game result before deleting the game at the end
} }
private enqueueUser(socket: SSocket): void { private enqueueUser(socket: SSocket): void {
if (!this.users.has(socket.authUser.id)) return; if (!this.users.has(socket.authUser.id)) return;
if (this.queue.has(socket.authUser.id)) return; if (this.queue.has(socket.authUser.id)) return;
if (this.users.get(socket.authUser.id)?.currentGame !== null) return; if (this.users.get(socket.authUser.id)?.currentGame !== null) return;
this.queue.add(socket.authUser.id); this.queue.add(socket.authUser.id);
socket.emit('queueEvent', 'registered'); socket.emit('queueEvent', 'registered');
} }
private dequeueUser(socket: SSocket): void { private dequeueUser(socket: SSocket): void {
if (!this.users.has(socket.authUser.id)) return; if (!this.users.has(socket.authUser.id)) return;
if (!this.queue.has(socket.authUser.id)) return; if (!this.queue.has(socket.authUser.id)) return;
this.queue.delete(socket.authUser.id); this.queue.delete(socket.authUser.id);
socket.emit('queueEvent', 'unregistered'); socket.emit('queueEvent', 'unregistered');
} }
private debugSocket(socket: SSocket): void { private debugSocket(socket: SSocket): void {
console.log(({ console.log(({
message: `socket debug for ${socket.id}`, message: `socket debug for ${socket.id}`,
userid: socket.authUser.id, userid: socket.authUser.id,
queue: this.queue, queue: this.queue,
users: this.users, users: this.users,
})); }));
} }
private gameUpdate(gameId: GameId, socket: SSocket): void { private gameUpdate(gameId: GameId, socket: SSocket): void {
if (!this.users.has(socket.authUser.id)) return; if (!this.users.has(socket.authUser.id)) return;
const user = this.users.get(socket.authUser.id)!; const user = this.users.get(socket.authUser.id)!;
if (user.currentGame !== gameId || !this.games.has(user.currentGame)) return; if (user.currentGame !== gameId || !this.games.has(user.currentGame)) return;
const games = this.games.get(gameId)!; const games = this.games.get(gameId)!;
socket.emit('gameBoard', { socket.emit('gameBoard', {
boardState: games.board, boardState: games.board,
currentPlayer: games.getCurrentState(), currentPlayer: games.getCurrentState(),
playerX: games.playerX, playerX: games.playerX,
playerO: games.playerO, playerO: games.playerO,
gameState: games.checkState(), gameState: games.checkState(),
gameId: gameId, gameId: gameId,
}); });
} }
private gameMove(socket: SSocket, update: GameMove) { private gameMove(socket: SSocket, update: GameMove) {
if (!this.users.has(socket.authUser.id)) return 'unknownError'; if (!this.users.has(socket.authUser.id)) return 'unknownError';
const user = this.users.get(socket.authUser.id)!; const user = this.users.get(socket.authUser.id)!;
if (user.currentGame !== null && !this.games.has(user.currentGame)) return 'unknownError'; if (user.currentGame !== null && !this.games.has(user.currentGame)) return 'unknownError';
const game = this.games.get(user.currentGame!)!; const game = this.games.get(user.currentGame!)!;
game.makeMove(socket.authUser.id, update.index); game.makeMove(socket.authUser.id, update.index);
} }
} }
// this value will be overriten // this value will be overriten
export let State: StateI = undefined as unknown as StateI; export let State: StateI = undefined as unknown as StateI;
export function createState(fastify: FastifyInstance) { export function createState(fastify: FastifyInstance) {
if (State !== undefined) { if (State !== undefined) {
throw new Error('State was already created !!!!'); throw new Error('State was already created !!!!');
} }
State = new StateI(fastify); State = new StateI(fastify);
} }