-
Notifications
You must be signed in to change notification settings - Fork 34
/
GameBoardButtonBuilder.ts
115 lines (106 loc) 路 3.13 KB
/
GameBoardButtonBuilder.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import GameBoardBuilder from '@bot/builder/GameBoardBuilder';
import Entity from '@tictactoe/Entity';
import { Player } from '@tictactoe/Player';
import {
MessageActionRow,
MessageButton,
MessageButtonStyleResolvable,
MessageOptions
} from 'discord.js';
/**
* Builds representation of a game board using buttons
* whiches will be displayed in a Discord message.
*
* @author Utarwyn
* @since 3.0.0
*/
export default class GameBoardButtonBuilder extends GameBoardBuilder {
/**
* Default labels used on buttons if emojies are not enabled.
* @protected
*/
private buttonLabels = ['X', 'O'];
/**
* Button styles used for representing the two players.
* @private
*/
private buttonStyles: MessageButtonStyleResolvable[] = ['SECONDARY', 'PRIMARY', 'DANGER'];
/**
* Stores if emojies have been customized.
* @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
* @override
*/
override withEntityPlaying(entity?: Entity): GameBoardBuilder {
// Do not display state if game is loading
if (entity) {
return super.withEntityPlaying(entity);
} else {
return this;
}
}
/**
* @inheritdoc
* @override
*/
override withEmojies(first: string, second: string): GameBoardBuilder {
this.customEmojies = true;
return super.withEmojies(first, second);
}
/**
* @inheritdoc
* @override
*/
override toMessageOptions(): MessageOptions {
return {
content: this.title + this.state,
components: [...Array(this.boardSize).keys()].map(row =>
new MessageActionRow().addComponents(
[...Array(this.boardSize).keys()].map(col => this.createButton(row, col))
)
)
};
}
/**
* Creates a button to be displayed based on current game context.
*
* @param row button placement row
* @param col button placement column
* @returns button discord.js object
*/
private createButton(row: number, col: number): MessageButton {
const button = new MessageButton();
const buttonIndex = row * this.boardSize + col;
const buttonData = this.boardData[buttonIndex];
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);
}
} else {
button.setLabel(' ');
}
return button.setCustomId(buttonIndex.toString()).setStyle(this.buttonStyles[buttonData]);
}
}