-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.ts
190 lines (169 loc) · 5.13 KB
/
game.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import { Client as ErisClient, Message, TextChannel } from 'eris';
import { ButtonStyle, ComponentButton, ComponentType, Member, MessageEmbedOptions, SlashCreator } from 'slash-create';
import EmojiProgressBar from '../experiments/EmojiProgressBar';
import { pickPhonetic } from './fakerExtended';
import { IGame, LobbyOptions } from './types';
export const games = new Map<string, IGame>();
export const lobbyChannels = new Map<string, LobbyOptions>();
export function createGame(host: Member, lobbyName?: string): IGame {
// ensure 'host' is not part of any other game
for (const { id, players } of games.values()) {
if (players.some(({ id: playerID }) => playerID === host.id)) {
throw new Error(`You are already in a game (<#${id}>).`);
}
}
const game: IGame = {
id: null,
postID: null,
title: lobbyName || `${pickPhonetic()} ${pickPhonetic()}`,
isPrivate: true,
color: Math.floor(Math.random() * 0xffffff),
requests: [],
players: [host],
log: [],
get host() {
return this.players[0];
}
};
return game;
}
export function buildPost<T extends any>(game: IGame): T {
const embed: MessageEmbedOptions = {
title: game.title,
footer: {
text: game.id
},
description: `Game is **${game.isPrivate ? 'private' : 'public'}**.`,
author: {
name: game.host.nick || game.host.user.username,
icon_url: game.host.avatarURL
},
color: game.color,
fields: Array.from({ length: 3 }, () => {
return { name: '\u200b', value: '\u200b', inline: true };
})
};
const players = game.players.slice();
embed.fields = players.reduce((fields, player, index) => {
if (index === 0) {
fields[0].name = `Roster (${players.length} / 15)`;
}
const fieldIndex = Math.floor(index / 3);
fields[fieldIndex].value += `[\`${(index + 1).toString().padStart(2, '0')}\`] ${player.mention}\n`;
return fields;
}, embed.fields);
const components: ComponentButton[] = [
{
type: ComponentType.BUTTON,
label: 'Join',
custom_id: 'join',
disabled: players.length >= 15,
style: ButtonStyle.PRIMARY,
emoji: {
name: '📥'
}
},
{
type: ComponentType.BUTTON,
label: 'Leave',
custom_id: 'leave',
disabled: players.length <= 1,
style: ButtonStyle.DESTRUCTIVE,
emoji: {
name: '📤'
}
}
];
return {
content: ':satellite: **| A new game is starting!**',
embeds: [embed],
components: [
{
type: ComponentType.ACTION_ROW,
components
}
]
} as T;
}
export function registerComponents(client: ErisClient, creator: SlashCreator) {
// creator.on('commandRun', async (_, promise, ctx) => {
// await promise;
// if (games.has(ctx.channelID)) {
// const game = games.get(ctx.channelID);
// game.log.push({
// type: 'interaction',
// context: ctx
// });
// }
// });
// client.on('interactionCreate', (interaction) => {
// if (interaction instanceof PingInteraction || interaction instanceof AutocompleteContext) {
// return;
// }
// interaction = interaction as CommandInteraction | ComponentInteraction;
// if (games.has(interaction.channel.id)) {
// const game = games.get(interaction.channel.id);
// game.log.push({
// type: 'interaction:' + InteractionType[interaction.type],
// context: clone(interaction)
// });
// }
// })
// creator.on('componentInteraction', (ctx) => {
// if (games.has(ctx.channelID)) {
// const game = games.get(ctx.channelID);
// game.log.push({
// type: 'interaction',
// context: clone(ctx)
// });
// }
// });
// client.on('messageDelete', msg => {
// creator.emit('debug', `Message deleted: ${msg.id}`);
// if (games.has(msg.channel.id)) {
// const game = games.get(msg.channel.id);
// game.log.push({
// type: 'messageDelete',
// context: new Message<TextChannel>(msg as BaseData, client).toJSON()
// });
// }
// });
// client.on('messageUpdate', (newMsg) => {
// if (games.has(newMsg.channel.id)) {
// const game = games.get(newMsg.channel.id);
// game.log.push({
// type: 'messageUpdate',
// context: newMsg
// });
// }
// })
client.on('messageCreate', async (msg: Message<TextChannel>) => {
// add the message to the game log
if (games.has(msg.channel.id)) {
const game = games.get(msg.channel.id);
game.log.push({ type: 'messageCreate', context: msg });
creator.emit('debug', `Pushed messageCreate for ${msg.id} to game ${game.id}`);
}
if (msg.content === '!component-test') {
client.createMessage(msg.channel.id, {
content: '',
components: [
{
type: ComponentType.ACTION_ROW,
components: [
{
type: ComponentType.BUTTON,
label: 'Test',
custom_id: 'test',
style: ButtonStyle.PRIMARY,
emoji: {
id: '717399673140281464'
}
}
]
}
]
});
}
});
}