(tic-tac-toe): game outcomes are now written to tictatoe database. :)
This commit is contained in:
parent
7724e24e4c
commit
f9801dafe7
2 changed files with 158 additions and 151 deletions
|
|
@ -24,7 +24,7 @@ CREATE UNIQUE INDEX IF NOT EXISTS idx_blocked_user_pair
|
|||
ON blocked(user, blocked);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS tictactoe (
|
||||
id INTEGER PRIMARY KEY NOT NULL,
|
||||
id TEXT PRIMARY KEY NOT NULL,
|
||||
player1 TEXT NOT NULL,
|
||||
player2 TEXT NOT NULL,
|
||||
outcome TEXT NOT NULL
|
||||
|
|
|
|||
|
|
@ -28,6 +28,34 @@ export class StateI {
|
|||
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 {
|
||||
const values = Array.from(this.queue.values());
|
||||
while (values.length >= 2) {
|
||||
|
|
@ -68,35 +96,14 @@ export class StateI {
|
|||
g.gameUpdate = setInterval(() => {
|
||||
this.gameUpdate(gameId, u1.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);
|
||||
}
|
||||
}
|
||||
|
||||
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 {
|
||||
socket.emit('updateInformation', {
|
||||
inQueue: this.queue.size,
|
||||
|
|
@ -115,7 +122,7 @@ export class StateI {
|
|||
private cleanupGame(gameId: GameId, game: TTC): void {
|
||||
clearInterval(game.gameUpdate ?? undefined);
|
||||
this.games.delete(gameId);
|
||||
let player: TTTUser | undefined = undefined;
|
||||
let player: TTTUser | undefined;
|
||||
if ((player = this.users.get(game.playerO)) !== undefined) {
|
||||
player.currentGame = null;
|
||||
player.socket.emit('gameEnd');
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue