adding asocket.io room management

This commit is contained in:
Jose Conde
2024-07-24 22:38:34 +02:00
parent 0329e84548
commit f52bea91ac
22 changed files with 170 additions and 50 deletions

View File

@ -144,7 +144,7 @@ export class DominoesGame extends EventEmitter {
playTurn() {
try {
const player = this.players[this.currentPlayerIndex];
this.notificationService.sendEventToPlayers('server:next-turn', this.players, this.getGameState());
this.notificationService.sendEventToPlayers('server:next-turn', this.players, this.getState());
this.logger.debug(`${player.name}'s turn (${player.hand.length} tiles)`);
this.printPlayerHand(player);
printBoard(this.board)
@ -222,6 +222,7 @@ export class DominoesGame extends EventEmitter {
players: this.players.map(player => player.getState(true)),
board: this.board.tiles.map(tile => tile.getState(true)),
boneyard: this.board.boneyard.map(tile => tile.getState(true)),
movements: this.board.movements
}
this.emit('game-over', summary);
}
@ -264,7 +265,7 @@ export class DominoesGame extends EventEmitter {
this.deal();
const extractStates = (p: PlayerInterface) => ({
player: p.getState(true),
gameState: this.getGameState()
gameState: this.getState()
});
await this.notificationService.sendEventToPlayers('server:hand-dealt', this.players, extractStates);
@ -325,11 +326,14 @@ export class DominoesGame extends EventEmitter {
}
}
getGameState(): GameState {
getState(): GameState {
const currentPlayer = this.players[this.currentPlayerIndex]
const lastMove = this.lastMove?.getState() || null;
const movements = this.board.movements.map(move => move.getState());
return {
id: uuid(),
lastMove: this.lastMove,
lastMove,
gameInProgress: this.gameInProgress,
winner: this.winner,
tileSelectionPhase: this.tileSelectionPhase,
@ -337,10 +341,11 @@ export class DominoesGame extends EventEmitter {
gameTied: this.gameTied,
gameId: this.id,
boneyard: this.board.boneyard.map(tile => tile.getState(false)),
players: this.players.map(player => player.getState()),
players: this.players.map(player => player.getState(false)),
currentPlayer: currentPlayer.getState(),
board: this.board.tiles.map(tile => tile.getState(true)),
boardFreeEnds: this.board.getFreeEnds(),
movements
}
}

View File

@ -26,6 +26,7 @@ export class MatchSession {
private clientsReady: string[] = [];
private gameSummaries: GameSummary[] = [];
private sessionService: SessionService = new SessionService();
private room: string;
id: string;
matchInProgress: boolean = false;
@ -46,6 +47,7 @@ export class MatchSession {
const { sessionName, seed, winType, winTarget } = options;
this.seed = seed || getRandomSeed();
this.id = uuid();
this.room = `room-${this.id}`;
this.name = sessionName || `Match ${this.id}`;
this.addPlayerToSession(creator);
this.creator = creator;
@ -187,7 +189,6 @@ export class MatchSession {
});
} else {
this.status = 'waiting'
// await this.playerNotificationManager.notifyMatchState(this);
this.notificationService.sendEventToPlayers('server:game-finished', this.players, {
lastGame: gameSummary,
sessionState: this.getState(true)
@ -397,6 +398,7 @@ export class MatchSession {
matchInProgress: this.matchInProgress,
gameSummaries: this.gameSummaries,
options: this.options,
room: this.room,
};
}

View File

@ -1,5 +1,4 @@
import { PlayerMove } from "../entities/PlayerMove";
import { PlayerDto } from "./PlayerDto";
import { PlayerDto, PlayerMoveDto } from "./PlayerDto";
export interface GameState {
id: string;
@ -14,5 +13,6 @@ export interface GameState {
gameId: string;
tileSelectionPhase: boolean;
boardFreeEnds: number[];
lastMove: PlayerMove | null;
lastMove: PlayerMoveDto | null;
movements: PlayerMoveDto[];
}

View File

@ -1,4 +1,5 @@
import { Score } from "../../server/db/interfaces";
import { PlayerMove } from "../entities/PlayerMove";
import { PlayerDto, TileDto } from "./PlayerDto";
export interface GameSummary {
@ -10,4 +11,5 @@ export interface GameSummary {
players?: PlayerDto[];
board: TileDto[]
boneyard: TileDto[]
movements: PlayerMove[]
}

View File

@ -10,4 +10,5 @@ export interface MatchSessionOptions {
seed: string
sessionName: string
numPlayers: 1 | 2 | 3 | 4
turnWaitSeconds: number
}

View File

@ -1,4 +1,5 @@
import { Score } from "../../server/db/interfaces";
import { GameState } from "./GameState";
import { GameSummary } from "./GameSummary";
import { MatchSessionOptions } from "./MatchSessionOptions";
import { PlayerDto } from "./PlayerDto";
@ -22,4 +23,6 @@ export interface MatchSessionState {
playersReady: number,
gameSummaries: GameSummary[];
options: MatchSessionOptions
currentGame?: GameState
room: string
}

View File

@ -14,4 +14,12 @@ export interface PlayerDto {
teamedWith?: string;
ready: boolean;
isHuman: boolean;
}
export interface PlayerMoveDto {
id: string;
tile: TileDto
type: string | null;
playerId: string;
direction?: string;
}

View File

@ -9,6 +9,7 @@ export class Board {
tiles: Tile[] = [];
boneyard: Tile[] = [];
logger = new LoggingService();
movements: PlayerMove[] = [];
constructor(private rng: PRNG) {}
@ -64,6 +65,7 @@ export class Board {
this.playTileRight(tile);
// printLine(`${tile} -- right`);
}
this.movements.push(playerMove);
}
playTileLeft(tile: Tile) {

View File

@ -1,5 +1,6 @@
import { uuid } from "../../common/utilities";
import { PlayerMoveSideType } from "../constants";
import { PlayerMoveDto } from "../dto/PlayerDto";
import { Tile } from "./Tile";
export class PlayerMove {
@ -9,4 +10,14 @@ export class PlayerMove {
toString() {
return `PlayerMove:([${this.tile.pips[0]}|${this.tile.pips[1]}] ${this.type})`;
}
getState(): PlayerMoveDto {
return {
id: this.id,
tile: this.tile.getState(),
type: this.type,
playerId: this.playerId,
direction: this.direction,
};
}
}