Skip to content

Commit

Permalink
GH-417: Add customizable idle button emoji
Browse files Browse the repository at this point in the history
  • Loading branch information
utarwyn committed Jan 18, 2023
1 parent 6e8b0ea commit 19b9ddc
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 30 deletions.
10 changes: 8 additions & 2 deletions src/__tests__/GameBoard.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,16 @@ describe('GameBoard', () => {
});

it('should use custom emojies from the configuration if provided', () => {
configuration.gameBoardEmojies = ['1', '2'];
configuration.gameBoardEmojies = ['1', '2', '3'];
gameBoard.content;
expect(mockedBuilder.withEmojies).toHaveBeenCalledTimes(1);
expect(mockedBuilder.withEmojies).toHaveBeenCalledWith('1', '2');
expect(mockedBuilder.withEmojies).toHaveBeenCalledWith('1', '2', '3');
});

it('should not use custom emojies if configuration is invalid', () => {
configuration.gameBoardEmojies = ['1'];
gameBoard.content;
expect(mockedBuilder.withEmojies).toHaveBeenCalledTimes(0);
});

it('should set embed in builder if embed is enabled', () => {
Expand Down
16 changes: 13 additions & 3 deletions src/__tests__/GameBoardBuilder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,23 @@ describe('GameBoardBuilder', () => {
});
});

it('should compute board using custom emojies', () => {
it('should compute board using two custom emojies', () => {
builder
.withEmojies(':dog:', ':cat:')
.withBoard(2, [Player.First, Player.Second, Player.Second, Player.First]);
.withBoard(2, [Player.First, Player.None, Player.Second, Player.First]);

expect(builder.toMessageOptions()).toEqual(
expect.objectContaining({ content: ':dog: :cat: \n:cat: :dog: \n' })
expect.objectContaining({ content: ':dog: ⬜ \n:cat: :dog: \n' })
);
});

it('should compute board using three custom emojies', () => {
builder
.withEmojies(':dog:', ':cat:', ':square:')
.withBoard(2, [Player.First, Player.None, Player.None, Player.Second]);

expect(builder.toMessageOptions()).toEqual(
expect.objectContaining({ content: ':dog: :square: \n:square: :cat: \n' })
);
});

Expand Down
18 changes: 15 additions & 3 deletions src/__tests__/GameBoardButtonBuilder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,30 @@ describe('GameBoardButtonBuilder', () => {
expect((options.components![1].components[1] as MessageButton).label).toBe('O');
});

it('should compute board using custom emojies', () => {
it('should compute board using two custom emojies', () => {
const options = builder
.withEmojies(':dog:', ':cat:')
.withBoard(2, [Player.First, Player.Second, Player.Second, Player.First])
.withBoard(2, [Player.First, Player.Second, Player.None, Player.First])
.toMessageOptions();

expect((options.components![0].components[0] as MessageButton).emoji?.name).toBe('dog');
expect((options.components![0].components[1] as MessageButton).emoji?.name).toBe('cat');
expect((options.components![1].components[0] as MessageButton).emoji?.name).toBe('cat');
expect((options.components![1].components[0] as MessageButton).emoji?.name).toBeUndefined();
expect((options.components![1].components[1] as MessageButton).emoji?.name).toBe('dog');
});

it('should compute board using three custom emojies', () => {
const options = builder
.withEmojies(':dog:', ':cat:', ':square:')
.withBoard(2, [Player.First, Player.None, Player.Second, Player.None])
.toMessageOptions();

expect((options.components![0].components[0] as MessageButton).emoji?.name).toBe('dog');
expect((options.components![0].components[1] as MessageButton).emoji?.name).toBe('square');
expect((options.components![1].components[0] as MessageButton).emoji?.name).toBe('cat');
expect((options.components![1].components[1] as MessageButton).emoji?.name).toBe('square');
});

it.each`
entity | state
${undefined} | ${''}
Expand Down
6 changes: 3 additions & 3 deletions src/bot/builder/GameBoardBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,11 @@ export default class GameBoardBuilder {
*
* @param first emoji of the first entity
* @param second emoji of the second entity
* @param none emoji used for an empty cell
* @returns same instance
*/
public withEmojies(first: string, second: string): GameBoardBuilder {
this.emojies[1] = first;
this.emojies[2] = second;
public withEmojies(first: string, second: string, none?: string): GameBoardBuilder {
this.emojies = [none ?? this.emojies[0], first, second];
return this;
}

Expand Down
33 changes: 16 additions & 17 deletions src/bot/builder/GameBoardButtonBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export default class GameBoardButtonBuilder extends GameBoardBuilder {
* Default labels used on buttons if emojies are not enabled.
* @protected
*/
private buttonLabels = ['X', 'O'];
private buttonLabels = [' ', 'X', 'O'];
/**
* Button styles used for representing the two players.
* @private
Expand All @@ -31,6 +31,11 @@ export default class GameBoardButtonBuilder extends GameBoardBuilder {
* @private
*/
private customEmojies = false;
/**
* Stores if idle emoji has been customized too.
* @private
*/
private customIdleEmoji = false;
/**
* Should disable buttons after been used.
* @private
Expand Down Expand Up @@ -78,9 +83,10 @@ export default class GameBoardButtonBuilder extends GameBoardBuilder {
* @inheritdoc
* @override
*/
override withEmojies(first: string, second: string): GameBoardBuilder {
override withEmojies(first: string, second: string, none?: string): GameBoardBuilder {
this.customEmojies = true;
return super.withEmojies(first, second);
this.customIdleEmoji = none != null;
return super.withEmojies(first, second, none);
}

/**
Expand Down Expand Up @@ -112,23 +118,16 @@ export default class GameBoardButtonBuilder extends GameBoardBuilder {
const button = new MessageButton();
const buttonIndex = row * this.boardSize + col;
const buttonData = this.boardData[buttonIndex];
const buttonOwned = buttonData !== Player.None;

if (buttonData !== Player.None) {
if (this.customEmojies) {
button.setEmoji(this.emojies[buttonData]);
} else {
button.setLabel(this.buttonLabels[buttonData - 1]);
}

if (this.disableButtonsAfterUsed) {
button.setDisabled(true);
}
if ((buttonOwned && this.customEmojies) || (!buttonOwned && this.customIdleEmoji)) {
button.setEmoji(this.emojies[buttonData]);
} else {
button.setLabel(' ');
button.setLabel(this.buttonLabels[buttonData]);
}

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

return button.setCustomId(buttonIndex.toString()).setStyle(this.buttonStyles[buttonData]);
Expand Down
4 changes: 2 additions & 2 deletions src/bot/entity/GameBoard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ export default class GameBoard {
}

const emojies = this.configuration.gameBoardEmojies;
if (emojies && emojies.length === 2) {
builder.withEmojies(emojies[0], emojies[1]);
if (emojies && [2, 3].includes(emojies.length)) {
builder.withEmojies(emojies[0], emojies[1], emojies[2]);
}

if (
Expand Down

0 comments on commit 19b9ddc

Please sign in to comment.