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 { return await this.playerInteraction.makeMove(board); } async chooseTile(board: Board): Promise { return this.playerInteraction.chooseTile(board); } async sendEventWithAck(event: string, data: any): Promise { } async sendEvent(event: string, data: any = {}): Promise { } 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, }; } }