(tic-tac-toe): game outcomes are now written to tictatoe database. :)

This commit is contained in:
Alessandro Petitcollin 2026-01-05 12:04:21 +01:00 committed by Maix0
parent 7724e24e4c
commit f9801dafe7
2 changed files with 158 additions and 151 deletions

View file

@ -24,7 +24,7 @@ CREATE UNIQUE INDEX IF NOT EXISTS idx_blocked_user_pair
ON blocked(user, blocked); ON blocked(user, blocked);
CREATE TABLE IF NOT EXISTS tictactoe ( CREATE TABLE IF NOT EXISTS tictactoe (
id INTEGER PRIMARY KEY NOT NULL, id TEXT PRIMARY KEY NOT NULL,
player1 TEXT NOT NULL, player1 TEXT NOT NULL,
player2 TEXT NOT NULL, player2 TEXT NOT NULL,
outcome TEXT NOT NULL outcome TEXT NOT NULL

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,
@ -28,6 +28,34 @@ export class StateI {
void this.queueInterval; void this.queueInterval;
} }
public registerUser(socket: SSocket): void {
this.fastify.log.info('Registering new user');
if (this.users.has(socket.authUser.id)) {
socket.emit('forceDisconnect', 'Already Connected');
socket.disconnect();
return;
}
this.users.set(socket.authUser.id, {
socket,
userId: socket.authUser.id,
windowId: socket.id,
updateInterval: setInterval(() => this.updateClient(socket), 3000),
currentGame: null,
});
this.fastify.log.info('Registered new user');
socket.on('disconnect', () => this.cleanupUser(socket));
socket.on('enqueue', () => this.enqueueUser(socket));
socket.on('dequeue', () => this.dequeueUser(socket));
socket.on('debugInfo', () => this.debugSocket(socket));
socket.on('gameMove', (e) => this.gameMove(socket, e));
if (socket) {
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) {
@ -68,35 +96,14 @@ export class StateI {
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') { this.cleanupGame(gameId, g); } if (g.checkState() !== 'ongoing') {
this.cleanupGame(gameId, g);
this.fastify.db.setGameOutcome(gameId, u1.userId, u2.userId, g.checkState());
}
}, 100); }, 100);
} }
} }
public registerUser(socket: SSocket): void {
this.fastify.log.info('Registering new user');
if (this.users.has(socket.authUser.id)) {
socket.emit('forceDisconnect', 'Already Connected');
socket.disconnect();
return;
}
this.users.set(socket.authUser.id, {
socket,
userId: socket.authUser.id,
windowId: socket.id,
updateInterval: setInterval(() => this.updateClient(socket), 3000),
currentGame: null,
});
this.fastify.log.info('Registered new user');
socket.on('disconnect', () => this.cleanupUser(socket));
socket.on('enqueue', () => this.enqueueUser(socket));
socket.on('dequeue', () => this.dequeueUser(socket));
socket.on('debugInfo', () => this.debugSocket(socket));
socket.on('gameMove', (e) => this.gameMove(socket, e));
}
private updateClient(socket: SSocket): void { private updateClient(socket: SSocket): void {
socket.emit('updateInformation', { socket.emit('updateInformation', {
inQueue: this.queue.size, inQueue: this.queue.size,
@ -115,7 +122,7 @@ export class StateI {
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 = 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');