Skip to content

Commit

Permalink
GH-354: Disable all buttons when game is over
Browse files Browse the repository at this point in the history
  • Loading branch information
utarwyn committed Oct 17, 2022
1 parent 2a4963d commit 5010bdb
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/__tests__/GameBoardButtonBuilder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,17 @@ describe('GameBoardButtonBuilder', () => {
expect((options.components![1].components[0] as MessageButton).disabled).toBeFalsy();
expect((options.components![1].components[1] as MessageButton).disabled).toBeFalsy();
});

it('should disable all buttons if game has ended', () => {
const options = builder
.withButtonsDisabledAfterUse()
.withBoard(2, [Player.First, Player.Second, Player.None, Player.None])
.withEndingMessage()
.toMessageOptions();

expect((options.components![0].components[0] as MessageButton).disabled).toBeTruthy();
expect((options.components![0].components[1] as MessageButton).disabled).toBeTruthy();
expect((options.components![1].components[0] as MessageButton).disabled).toBeTruthy();
expect((options.components![1].components[1] as MessageButton).disabled).toBeTruthy();
});
});
18 changes: 18 additions & 0 deletions src/bot/builder/GameBoardButtonBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ export default class GameBoardButtonBuilder extends GameBoardBuilder {
* @private
*/
private disableButtonsAfterUsed = false;
/**
* Stores if game has ended or not.
* @protected
*/
private gameEnded = false;

/**
* Should disable buttons after been used.
Expand All @@ -60,6 +65,15 @@ export default class GameBoardButtonBuilder extends GameBoardBuilder {
}
}

/**
* @inheritdoc
* @override
*/
override withEndingMessage(winner?: Entity): GameBoardBuilder {
this.gameEnded = true;
return super.withEndingMessage(winner);
}

/**
* @inheritdoc
* @override
Expand Down Expand Up @@ -108,6 +122,10 @@ export default class GameBoardButtonBuilder extends GameBoardBuilder {
}
} else {
button.setLabel(' ');

if (this.gameEnded && this.disableButtonsAfterUsed) {
button.setDisabled(true);
}
}

return button.setCustomId(buttonIndex.toString()).setStyle(this.buttonStyles[buttonData]);
Expand Down

0 comments on commit 5010bdb

Please sign in to comment.