domino-server/src/game/entities/player/AbstractPlayer.ts
2024-07-14 21:41:38 +02:00

65 lines
1.8 KiB
TypeScript

import { Board } from "../Board";
import { PlayerInterface } from "./PlayerInterface";
import { PlayerMove } from "../PlayerMove";
import { Tile } from "../Tile";
import { LoggingService } from "../../../common/LoggingService";
import { EventEmitter } from "stream";
import { PlayerInteractionInterface } from "../../PlayerInteractionInterface";
import { uuid } from "../../../common/utilities";
import { PlayerDto, TileDto } from "../../dto/PlayerDto";
export abstract class AbstractPlayer extends EventEmitter implements PlayerInterface {
hand: Tile[] = [];
score: number = 0;
logger: LoggingService = new LoggingService();
teamedWith: PlayerInterface | null = null;
playerInteraction: PlayerInteractionInterface = undefined as any;
id: string = uuid();
ready: boolean = false;
constructor(public name: string) {
super();
}
askForMove(board: Board): void {
this.playerInteraction.askForMove(board);
}
async makeMove(board: Board): Promise<PlayerMove | null> {
return await this.playerInteraction.makeMove(board);
}
async chooseTile(board: Board): Promise<Tile> {
return this.playerInteraction.chooseTile(board);
}
async sendEventWithAck(event: string, data: any): Promise<void> {
}
async sendEvent(event: string, data: any = {}): Promise<void> {
}
reset(): void {
this.hand = [];
this.score = 0;
this.ready = false;
}
pipsCount(): number {
return this.hand.reduce((acc, tile) => acc + tile.pips[0] + tile.pips[1], 0);
}
getState(showPips: boolean = false): PlayerDto {
return {
id: this.id,
name: this.name,
score: this.score,
hand: this.hand.map(tile => tile.getState(showPips)),
teamedWith: this.teamedWith?.getState(showPips) ?? null,
ready: this.ready,
};
}
}