From 83af847f0b69b118898b6d81dbf2adddd6d1c83a Mon Sep 17 00:00:00 2001 From: anshulrastogi9 Date: Fri, 15 Nov 2024 06:44:30 +0300 Subject: [PATCH] fixed addRole middleware --- src/interfaces/pokerSeat/index.ts | 2 +- src/models/pokerSeat/index.ts | 20 +++++----- src/models/pokerTable/index.ts | 65 +++++++++++-------------------- 3 files changed, 33 insertions(+), 54 deletions(-) diff --git a/src/interfaces/pokerSeat/index.ts b/src/interfaces/pokerSeat/index.ts index f452821..a2d19ab 100644 --- a/src/interfaces/pokerSeat/index.ts +++ b/src/interfaces/pokerSeat/index.ts @@ -115,7 +115,7 @@ interface PokerSeatInterface extends BaseEventEmitterInterface { */ getPlayer(): PokerPlayerInterface | undefined; - getRole(): string[]; + getRoles(): string[]; addRole(role:string): string[]; diff --git a/src/models/pokerSeat/index.ts b/src/models/pokerSeat/index.ts index 78813b6..7015e64 100644 --- a/src/models/pokerSeat/index.ts +++ b/src/models/pokerSeat/index.ts @@ -166,7 +166,7 @@ class PokerSeat extends BaseEventEmitter implements PokerSeatInterface { */ private __player: PokerPlayerInterface | undefined = undefined; - private __role! : string[] ; + private __roles! : string[] ; /************************************************************************************************************** * CONSTRUCTOR & INITIALIZERS @@ -288,8 +288,8 @@ class PokerSeat extends BaseEventEmitter implements PokerSeatInterface { // Assign a player to the seat if provided; otherwise, seat remains unoccupied. config.role - ? this.__setRole(config.role) - : this.__setRole(this.__role); + ? this.__setRoles(config.role) + : this.__setRoles(this.__roles); } // Emit `INITIALIZED` event after initialization @@ -580,8 +580,8 @@ class PokerSeat extends BaseEventEmitter implements PokerSeatInterface { * // Console Output: * ``` */ - public getRole(): string[] { - return this.__role; + public getRoles(): string[] { + return this.__roles; } /** @@ -935,16 +935,16 @@ class PokerSeat extends BaseEventEmitter implements PokerSeatInterface { * // Console Output: * ``` */ - private __setRole( + private __setRoles( role: string[] ): string[]{ - this.__role = role; - return this.__role; + this.__roles = role; + return this.__roles; } private __addRole(role: string): string[] { - this.__role.push(role); - return this.getRole(); + this.__roles.push(role); + return this.getRoles(); } /** diff --git a/src/models/pokerTable/index.ts b/src/models/pokerTable/index.ts index 7488f54..6976409 100644 --- a/src/models/pokerTable/index.ts +++ b/src/models/pokerTable/index.ts @@ -313,7 +313,7 @@ class PokerTable extends BaseEventEmitter implements PokerTableInterface { }); seat.on(PokerSeatEvents.VACATED, (event) => { - this.__seatVacatedUpdateEventHandler(); + this.__seatOccupancyUpdateEventHandler(); }); } } @@ -468,7 +468,18 @@ class PokerTable extends BaseEventEmitter implements PokerTableInterface { /************************************************************************************************************** * WRAPPER METHODS (UTILITY & CONVENIENCE) **************************************************************************************************************/ + public occupancyCount():number{ + let occupiedSeats = 0; + + for (let i = 0; i < this.getSeats().length; i++) { + let seat = this.getSeats()[i]; + if (seat.getPlayer()) { + occupiedSeats += 1; + } + } + return occupiedSeats; + } /** * `size` * Starts a new PokerGame if there are at least two active players at the PokerTable. @@ -689,13 +700,13 @@ class PokerTable extends BaseEventEmitter implements PokerTableInterface { return false; } - public assignRoles(event: BaseEventInterface): void | false { - this.__assignRoles(event); + public assignRoles(): void | false { + this.__assignRoles(); } - private __assignRoles(event: BaseEventInterface): void | false { + private __assignRoles(): void | false { - if (event.occupancyCount === 2) { + if (this.occupancyCount()===2) { let foundDealer = false; // Iterate over each seat to find the minimum occupied seat position for (let i = 0; i < this.getSeats().length; i++) { @@ -717,7 +728,7 @@ class PokerTable extends BaseEventEmitter implements PokerTableInterface { } } - else if (event.occupancyCount > 2) { + else if (this.occupancyCount()>2) { const seats = this.getSeats(); let dealerAssigned = false; let smallBlindAssigned = false; @@ -748,9 +759,8 @@ class PokerTable extends BaseEventEmitter implements PokerTableInterface { } private __assignRolesMiddleware(event: BaseEventInterface,next: () => void): void | false { - - return this.__assignRoles(event); - next(); + this.__assignRoles(); + next(); } @@ -779,19 +789,14 @@ class PokerTable extends BaseEventEmitter implements PokerTableInterface { event.lastModifiedAt = new Date(); next(); } + + private __checkOccupancyCount( event: BaseEventInterface, next: () => void ): void | false { - let occupiedSeats = 0; - - for (let i = 0; i < this.getSeats().length; i++) { - let seat = this.getSeats()[i]; - if (seat.getPlayer()) { - occupiedSeats += 1; - } - } + let occupiedSeats = this.occupancyCount(); // Check if all seats are occupied if (occupiedSeats === this.getSeats().length) { @@ -916,31 +921,5 @@ class PokerTable extends BaseEventEmitter implements PokerTableInterface { }); } - private __seatVacatedUpdateEventHandler(event?: BaseEventInterface): void { - this.emitEvent(PokerTableEvents.NEW_GAME, { - event: { source: Source.POKER_TABLE, data: { tableId: this.getId() } }, - middlewares: [ - (event, next) => { - this.__checkIfGameInProgress(event, next); - }, - (event, next) => { - this.__checkOccupancyCount(event, next); - }, - (event, next) => { - this.__assignRolesMiddleware(event, next); - }, - (event, next) => { - this.__validatePlayerBalances(event, next); - }, - (event, next) => { - this.__createGamePlayersList(event, next); - }, - (event) => { - this.__startGame(event); - }, - ], - }); - } - } export { PokerTable };