Skip to content

Commit

Permalink
Adapt expiration message to game board delete configuration (#421)
Browse files Browse the repository at this point in the history
  • Loading branch information
utarwyn committed May 25, 2023
1 parent 0374031 commit 44a3f45
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 17 deletions.
12 changes: 11 additions & 1 deletion src/__tests__/GameBoard.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -327,9 +327,19 @@ describe('GameBoard', () => {
expect(spyUpdate).toHaveBeenCalledTimes(1);
});

it('should end game if an error occured', async () => {
it('should expire game and keep gameboard if an error occured', async () => {
configuration.gameBoardDelete = false;
await callCollectorEvent('end', null, 'expiration');
expect(game.updateBoard).toHaveBeenCalledTimes(0);
expect(tunnel.end).toHaveBeenCalledTimes(0);
expect(manager.endGame).toHaveBeenCalledTimes(1);
});

it('should expire game and delete gameboard if an error occured', async () => {
configuration.gameBoardDelete = true;
await callCollectorEvent('end', null, 'expiration');
expect(game.updateBoard).toHaveBeenCalledTimes(0);
expect(tunnel.end).toHaveBeenCalledTimes(1);
expect(manager.endGame).toHaveBeenCalledTimes(1);
});

Expand Down
55 changes: 39 additions & 16 deletions src/bot/entity/GameBoard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ export default class GameBoard {
* @private
*/
private reactionsLoaded: boolean;
/**
* Stores if the game has expired.
* @private
*/
private expired: boolean;

/**
* Constructs a new game board message.
Expand All @@ -72,6 +77,7 @@ export default class GameBoard {
this.game = new Game();
this._entities = [tunnel.author, member2];
this.reactionsLoaded = false;
this.expired = false;
this.configuration = configuration;
}

Expand All @@ -95,7 +101,9 @@ export default class GameBoard {
this.reactionsLoaded ? this.getEntity(this.game.currentPlayer) : undefined
);

if (this.game.finished) {
if (this.expired) {
builder.withExpireMessage();
} else if (this.game.finished) {
builder.withEndingMessage(this.getEntity(this.game.winner));
}

Expand Down Expand Up @@ -234,19 +242,7 @@ export default class GameBoard {
this.game.updateBoard(this.game.currentPlayer, move);

if (this.game.finished) {
const winner = this.getEntity(this.game.winner);

if (this.configuration.gameBoardDelete) {
const options = this.createBuilder().withEndingMessage(winner).toMessageOptions();
await this.tunnel.end(options);
} else {
if (this.configuration.gameBoardReactions) {
await this.tunnel.reply?.reactions?.removeAll();
}
await this.update(interaction);
}

this.manager.endGame(this, winner ?? null);
return this.end(this.getEntity(this.game.winner), interaction);
} else {
this.game.nextPlayer();
await this.update(interaction);
Expand All @@ -259,14 +255,15 @@ export default class GameBoard {
* @private
*/
private async onExpire(): Promise<void> {
await this.tunnel.end(this.createBuilder().withExpireMessage().toMessageOptions());
this.manager.endGame(this);
this.expired = true;
return this.end();
}

/**
* Creates a builder based on the game configuration.
*
* @returns game board builder
* @private
*/
private createBuilder(): GameBoardBuilder {
const builder = this.configuration.gameBoardReactions
Expand All @@ -280,6 +277,32 @@ export default class GameBoard {
return builder;
}

/**
* Sends the state and ends game attached to the message.
*
* @param winner winner of the game if the game is finished
* @param interaction interaction to update if action was triggered by it
* @private
*/
private async end(winner?: Entity, interaction?: ButtonInteraction): Promise<void> {
if (this.configuration.gameBoardDelete) {
const builder = this.createBuilder();
if (this.expired) {
builder.withExpireMessage();
} else {
builder.withEndingMessage(winner);
}
await this.tunnel.end(builder.toMessageOptions());
} else {
if (this.configuration.gameBoardReactions) {
await this.tunnel.reply?.reactions?.removeAll();
}
await this.update(interaction);
}

this.manager.endGame(this, winner);
}

/**
* Waits for the current player to select a move with one reaction below the message.
* @private
Expand Down

0 comments on commit 44a3f45

Please sign in to comment.