Skip to content

Commit

Permalink
GH-192: Add option to disable buttons after use
Browse files Browse the repository at this point in the history
  • Loading branch information
utarwyn committed Jun 20, 2022
1 parent 65fd72c commit eb44a51
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 6 deletions.
3 changes: 2 additions & 1 deletion config/config.example.json
Expand Up @@ -13,5 +13,6 @@
"gameExpireTime": 30,
"gameBoardReactions": false,
"gameBoardDelete": false,
"gameBoardEmojies": []
"gameBoardEmojies": [],
"gameBoardDisableButtons": false
}
2 changes: 1 addition & 1 deletion src/__tests__/GameBoardBuilder.test.ts
@@ -1,4 +1,4 @@
import GameBoardBuilder from '@bot/entity/GameBoardBuilder';
import GameBoardBuilder from '@bot/builder/GameBoardBuilder';
import localize from '@i18n/localize';
import AI from '@tictactoe/AI';
import { Player } from '@tictactoe/Player';
Expand Down
14 changes: 13 additions & 1 deletion src/__tests__/GameBoardButtonBuilder.test.ts
@@ -1,4 +1,4 @@
import GameBoardButtonBuilder from '@bot/entity/GameBoardButtonBuilder';
import GameBoardButtonBuilder from '@bot/builder/GameBoardButtonBuilder';
import localize from '@i18n/localize';
import AI from '@tictactoe/AI';
import { Player } from '@tictactoe/Player';
Expand Down Expand Up @@ -56,4 +56,16 @@ describe('GameBoardButtonBuilder', () => {
builder.withEntityPlaying(entity);
expect(builder.toMessageOptions()).toEqual({ content: state, components: [] });
});

it('should compute board using disabled buttons after been used', () => {
const options = builder
.withButtonsDisabledAfterUse()
.withBoard(2, [Player.First, Player.Second, Player.None, Player.None])
.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).toBeFalsy();
expect((options.components![1].components[1] as MessageButton).disabled).toBeFalsy();
});
});
File renamed without changes.
@@ -1,4 +1,4 @@
import GameBoardBuilder from '@bot/entity/GameBoardBuilder';
import GameBoardBuilder from '@bot/builder/GameBoardBuilder';
import Entity from '@tictactoe/Entity';
import { Player } from '@tictactoe/Player';
import {
Expand Down Expand Up @@ -31,6 +31,21 @@ export default class GameBoardButtonBuilder extends GameBoardBuilder {
* @private
*/
private customEmojies = false;
/**
* Should disable buttons after been used.
* @private
*/
private disableButtonsAfterUsed = false;

/**
* Should disable buttons after been used.
*
* @returns same instance
*/
public withButtonsDisabledAfterUse(): GameBoardBuilder {
this.disableButtonsAfterUsed = true;
return this;
}

/**
* @inheritdoc
Expand Down Expand Up @@ -87,6 +102,10 @@ export default class GameBoardButtonBuilder extends GameBoardBuilder {
} else {
button.setLabel(this.buttonLabels[buttonData - 1]);
}

if (this.disableButtonsAfterUsed) {
button.setDisabled(true);
}
} else {
button.setLabel(' ');
}
Expand Down
11 changes: 9 additions & 2 deletions src/bot/entity/GameBoard.ts
@@ -1,5 +1,5 @@
import GameBoardBuilder from '@bot/entity/GameBoardBuilder';
import GameBoardButtonBuilder from '@bot/entity/GameBoardButtonBuilder';
import GameBoardBuilder from '@bot/builder/GameBoardBuilder';
import GameBoardButtonBuilder from '@bot/builder/GameBoardButtonBuilder';
import MessagingTunnel from '@bot/messaging/MessagingTunnel';
import GameStateManager from '@bot/state/GameStateManager';
import GameConfig from '@config/GameConfig';
Expand Down Expand Up @@ -107,6 +107,13 @@ export default class GameBoard {
builder.withEmojies(emojies[0], emojies[1]);
}

if (
this.configuration.gameBoardDisableButtons &&
builder instanceof GameBoardButtonBuilder
) {
builder.withButtonsDisabledAfterUse();
}

return builder.toMessageOptions();
}

Expand Down
1 change: 1 addition & 0 deletions src/config/ConfigProvider.ts
Expand Up @@ -27,6 +27,7 @@ export default class ConfigProvider implements Config {
public gameBoardReactions = false;
public gameBoardDelete = false;
public gameBoardEmojies = [];
public gameBoardDisableButtons = false;

[key: string]: any;

Expand Down
4 changes: 4 additions & 0 deletions src/config/GameConfig.ts
Expand Up @@ -21,4 +21,8 @@ export default interface GameConfig {
* List of emojies used to identify players.
*/
gameBoardEmojies?: string[];
/**
* Should disable buttons after been used.
*/
gameBoardDisableButtons?: boolean;
}

0 comments on commit eb44a51

Please sign in to comment.