Skip to content

Commit

Permalink
fixed addRole middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
anshulrastogi9 committed Nov 15, 2024
1 parent 2a3fb55 commit 83af847
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 54 deletions.
2 changes: 1 addition & 1 deletion src/interfaces/pokerSeat/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ interface PokerSeatInterface extends BaseEventEmitterInterface {
*/
getPlayer(): PokerPlayerInterface | undefined;

getRole(): string[];
getRoles(): string[];

addRole(role:string): string[];

Expand Down
20 changes: 10 additions & 10 deletions src/models/pokerSeat/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ class PokerSeat extends BaseEventEmitter implements PokerSeatInterface {
*/
private __player: PokerPlayerInterface | undefined = undefined;

private __role! : string[] ;
private __roles! : string[] ;

/**************************************************************************************************************
* CONSTRUCTOR & INITIALIZERS
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -580,8 +580,8 @@ class PokerSeat extends BaseEventEmitter implements PokerSeatInterface {
* // Console Output: <PlayerInstance>
* ```
*/
public getRole(): string[] {
return this.__role;
public getRoles(): string[] {
return this.__roles;
}

/**
Expand Down Expand Up @@ -935,16 +935,16 @@ class PokerSeat extends BaseEventEmitter implements PokerSeatInterface {
* // Console Output: <PlayerInstance>
* ```
*/
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();
}

/**
Expand Down
65 changes: 22 additions & 43 deletions src/models/pokerTable/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ class PokerTable extends BaseEventEmitter implements PokerTableInterface {
});

seat.on(PokerSeatEvents.VACATED, (event) => {
this.__seatVacatedUpdateEventHandler();
this.__seatOccupancyUpdateEventHandler();
});
}
}
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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++) {
Expand All @@ -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;
Expand Down Expand Up @@ -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();
}


Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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 };

0 comments on commit 83af847

Please sign in to comment.