make requeuing and other stuff better
This commit is contained in:
parent
972b78a1e2
commit
b86d50f9f0
4 changed files with 101 additions and 32 deletions
|
|
@ -20,21 +20,22 @@ export type GameUpdate = {
|
|||
export type GameMove = {
|
||||
index: number;
|
||||
}
|
||||
export type GameMoveResponse = 'success' | 'invalidMove' | 'unknownError';
|
||||
|
||||
export interface ClientToServer {
|
||||
enqueue: () => void;
|
||||
dequeue: () => void;
|
||||
debugInfo: () => void;
|
||||
gameMove: (up: GameMove) => GameMoveResponse;
|
||||
gameMove: (up: GameMove) => void;
|
||||
connectedToGame: (gameId: string) => void;
|
||||
};
|
||||
|
||||
export interface ServerToClient {
|
||||
forceDisconnect: (reason: string) => void;
|
||||
queueEvent: (msg: 'registered' | 'unregistered') => void;
|
||||
updateInformation: (info: UpdateInfo) => void,
|
||||
newGame: (gameId: string) => void,
|
||||
newGame: (initState: GameUpdate) => void,
|
||||
gameBoard: (state: GameUpdate) => void,
|
||||
gameEnd: () => void;
|
||||
};
|
||||
|
||||
export type SSocket = Socket<ClientToServer, ServerToClient>;
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
import { UserId } from '@shared/database/mixin/user';
|
||||
import { FastifyInstance } from 'fastify';
|
||||
import { GameMove, GameMoveResponse, SSocket } from './socket';
|
||||
import { GameMove, SSocket } from './socket';
|
||||
import { isNullish } from '@shared/utils';
|
||||
import { newUUID } from '@shared/utils/uuid';
|
||||
import { GameId } from '@shared/database/mixin/tictactoe';
|
||||
|
|
@ -48,17 +48,27 @@ export class StateI {
|
|||
this.queue.delete(id2);
|
||||
|
||||
const gameId = newUUID() as unknown as GameId;
|
||||
u1.socket.emit('newGame', gameId);
|
||||
u2.socket.emit('newGame', gameId);
|
||||
const g = new TTC(u1.userId, u2.userId);
|
||||
const iState = {
|
||||
boardState: g.board,
|
||||
currentPlayer: g.getCurrentState(),
|
||||
playerX: g.playerX,
|
||||
playerO: g.playerO,
|
||||
gameState: g.checkState(),
|
||||
gameId: gameId,
|
||||
};
|
||||
|
||||
this.games.set(gameId, new TTC(u1.userId, u2.userId));
|
||||
u1.socket.emit('newGame', iState);
|
||||
u2.socket.emit('newGame', iState);
|
||||
this.games.set(gameId, g);
|
||||
|
||||
u1.currentGame = gameId;
|
||||
u2.currentGame = gameId;
|
||||
|
||||
this.games.get(gameId)!.gameUpdate = setInterval(() => {
|
||||
g.gameUpdate = setInterval(() => {
|
||||
this.gameUpdate(gameId, u1.socket);
|
||||
this.gameUpdate(gameId, u2.socket);
|
||||
if (g.checkState() !== 'ongoing') { this.cleanupGame(gameId, g); }
|
||||
}, 100);
|
||||
}
|
||||
}
|
||||
|
|
@ -102,6 +112,21 @@ export class StateI {
|
|||
this.queue.delete(socket.authUser.id);
|
||||
}
|
||||
|
||||
private cleanupGame(gameId: GameId, game: TTC): void {
|
||||
clearInterval(game.gameUpdate ?? undefined);
|
||||
this.games.delete(gameId);
|
||||
let player: TTTUser | undefined = undefined;
|
||||
if ((player = this.users.get(game.playerO)) !== undefined) {
|
||||
player.currentGame = null;
|
||||
player.socket.emit('gameEnd');
|
||||
}
|
||||
if ((player = this.users.get(game.playerX)) !== undefined) {
|
||||
player.currentGame = null;
|
||||
player.socket.emit('gameEnd');
|
||||
}
|
||||
// do something here with the game result before deleting the game at the end
|
||||
}
|
||||
|
||||
private enqueueUser(socket: SSocket): void {
|
||||
if (!this.users.has(socket.authUser.id)) return;
|
||||
|
||||
|
|
@ -149,14 +174,14 @@ export class StateI {
|
|||
});
|
||||
}
|
||||
|
||||
private gameMove(socket: SSocket, update: GameMove): GameMoveResponse {
|
||||
private gameMove(socket: SSocket, update: GameMove) {
|
||||
if (!this.users.has(socket.authUser.id)) return 'unknownError';
|
||||
const user = this.users.get(socket.authUser.id)!;
|
||||
|
||||
if (user.currentGame !== null && !this.games.has(user.currentGame)) return 'unknownError';
|
||||
const game = this.games.get(user.currentGame!)!;
|
||||
|
||||
return game.makeMove(socket.authUser.id, update.index);
|
||||
game.makeMove(socket.authUser.id, update.index);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue