-
Notifications
You must be signed in to change notification settings - Fork 27
/
shiritori_forever_helper.js
338 lines (282 loc) · 10.2 KB
/
shiritori_forever_helper.js
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
const reload = require('require-reload')(require);
const shiritoriManager = require('shiritori');
const globals = require('./../common/globals.js');
const state = require('./../common/static_state.js');
const assert = require('assert');
const sendAndDelete = reload('./util/send_and_delete.js');
const { Navigation } = require('monochrome-bot');
const constants = reload('./../common/constants.js');
const retryPromise = reload('./../common/util/retry_promise.js');
const japaneseGameStrategy = shiritoriManager.strategies.japanese;
const SHIRITORI_CHANNELS_LIST_KEY = 'shiritoriForeverDiscordChannels';
const CHANNEL_SHIRITORI_KEY_PREFIX = 'shiritoriChannelDiscord_';
const LOGGER_TITLE = 'SHIRITORI FOREVER';
const DELETE_MESSAGE_TIMEOUT_MS = 60000;
if (!state.shiritoriChannels) {
state.shiritoriChannels = {};
}
function createMarkdownLinkForWord(word) {
return `[${word}](http://jisho.org/search/${encodeURIComponent(word)})`;
}
function createKeyForChannel(channelID) {
return `${CHANNEL_SHIRITORI_KEY_PREFIX}${channelID}`;
}
async function loadChannels(monochrome) {
try {
state.shiritoriChannels = await monochrome
.getPersistence()
.getData(SHIRITORI_CHANNELS_LIST_KEY);
} catch (err) {
const logger = monochrome.getLogger();
logger.logFailure(LOGGER_TITLE, 'Error loading channels', err);
}
}
function createDiscordContentForScoresPage(scoresPage) {
const fields = scoresPage.map((scoreInfo, index) => ({
name: `${index + 1}) ${scoreInfo.username}`,
value: `${scoreInfo.score} points`,
inline: true,
}));
return {
embed: {
title: 'Scores',
description: 'Shiritori Forever scores in the current channel.\nYou get one point per character in your answer.',
fields,
color: constants.EMBED_NEUTRAL_COLOR,
},
};
}
function createScoresNavigation(monochrome, msg, scoresPages) {
const contents = scoresPages.map(page => createDiscordContentForScoresPage(page));
const navigation = Navigation.fromOneDimensionalContents(msg.author.id, contents);
return monochrome.getNavigationManager().show(navigation, 1800000, msg.channel, msg);
}
function userNameForUserID(monochrome, userID) {
const user = monochrome.getErisBot().users.get(userID);
if (!user) {
return 'Unknown User';
}
return `${user.username}#${user.discriminator}`;
}
async function sendScores(monochrome, msg) {
const persistence = monochrome.getPersistence();
const channelData = await persistence.getData(createKeyForChannel(msg.channel.id));
const scoreForUserID = channelData.scores || {};
const nameForUserId = channelData.names || {};
const sortedScores = Object.keys(scoreForUserID)
.map(userID => ({
username: nameForUserId[userID] || userNameForUserID(monochrome, userID),
score: scoreForUserID[userID],
}))
.sort((a, b) => b.score - a.score);
if (sortedScores.length === 0) {
return msg.channel.createMessage('There aren\'t any Shiritori Forever scores in this channel yet. Maybe Shiritori Forever is not enabled here, or no one has played yet.');
}
const pages = [[]];
while (sortedScores.length > 0) {
const currentPage = pages[pages.length - 1];
currentPage.push(sortedScores.pop());
if (currentPage.length >= 20 && sortedScores.length > 0) {
pages.push([]);
}
}
return createScoresNavigation(monochrome, msg, pages);
}
function getPrefixForChannelID(monochrome, channelID) {
const serverID = monochrome.getErisBot().channelGuildMap[channelID];
const prefixes = monochrome.getPersistence().getPrefixesForServer(serverID);
return prefixes[0];
}
function createMessageForTurnTaken(monochrome, channelID, userID, wordInformation, userScore) {
const bot = monochrome.getErisBot();
const { username } = bot.users.get(userID);
const {
word, reading, meaning, nextWordMustStartWith,
} = wordInformation;
const readingStringPart = reading ? ` (${reading})` : '';
const scoreStringPart = userScore ? ` (${userScore})` : '';
const prefix = getPrefixForChannelID(monochrome, channelID);
const fields = [
{
name: `${username}${scoreStringPart} said`,
value: `${createMarkdownLinkForWord(word)}${readingStringPart}`,
},
{
name: 'It means',
value: meaning,
},
{
name: 'Next word starts with',
value: nextWordMustStartWith.join(', '),
},
];
return retryPromise(() => bot.createMessage(channelID, {
embed: {
fields,
color: constants.EMBED_NEUTRAL_COLOR,
footer: {
text: `Say '${prefix}sf scores' to see the current scores.`,
icon_url: constants.FOOTER_ICON_URI,
},
},
}));
}
function getPluralizer(array) {
if (array.length > 1) {
return 's';
}
return '';
}
function discordDescriptionForRejection(rejectionReason, extraData) {
if (rejectionReason === shiritoriManager.REJECTION_REASON.ReadingAlreadyUsed) {
return `The reading: **${extraData.join(', ')}** was used recently. Try coming up with a different one ;)`;
} else if (rejectionReason === shiritoriManager.REJECTION_REASON.ReadingEndsWithN) {
return `Words in Shiritori can't have readings that end with ん! (**${extraData.join(', ')}**)`;
} else if (rejectionReason === shiritoriManager.REJECTION_REASON.WrongStartSequence) {
return `The next word must begin with ${extraData.expected.join(', ')}. I found these readings for that word but they don't start with the right kana: **${extraData.actual.join(', ')}**`;
} else if (rejectionReason === shiritoriManager.REJECTION_REASON.NotNoun) {
return `Shiritori words must be nouns! I didn't find any nouns for the reading${getPluralizer(extraData.join)}: **${extraData.join(', ')}**`;
}
assert(false, 'Unexpected branch');
return undefined;
}
function handleRejectedResult(monochrome, msg, rejectedResult) {
const { rejectionReason, extraData } = rejectedResult;
if (rejectionReason === shiritoriManager.REJECTION_REASON.UnknownWord) {
return retryPromise(() => msg.addReaction('❓'));
}
const description = discordDescriptionForRejection(rejectionReason, extraData);
return retryPromise(() => sendAndDelete(
monochrome,
msg.channel.id,
description,
DELETE_MESSAGE_TIMEOUT_MS,
));
}
async function handleAcceptedResult(monochrome, msg, acceptedResult) {
let userScore = 0;
const persistence = monochrome.getPersistence();
const channelID = msg.channel.id;
await createMessageForTurnTaken(
monochrome,
msg.channel.id,
msg.author.id,
acceptedResult.word,
userScore,
);
return persistence.editData(createKeyForChannel(channelID), (data) => {
const dataCopy = { ...data };
dataCopy.previousWordInformation = acceptedResult.word;
if (!dataCopy.scores) {
dataCopy.scores = {};
}
if (!dataCopy.scores[msg.author.id]) {
dataCopy.scores[msg.author.id] = 0;
}
if (!dataCopy.names) {
dataCopy.names = {};
}
dataCopy.names[msg.author.id] = `${msg.author.username}#${msg.author.discriminator}`;
dataCopy.scores[msg.author.id] += acceptedResult.score;
userScore = dataCopy.scores[msg.author.id];
return dataCopy;
});
}
function tryHandleMessage(monochrome, msg) {
if (!state.shiritoriChannels[msg.channel.id]) {
return false;
}
if (msg.content.indexOf(' ') !== -1) {
return false;
}
let accepted = false;
return monochrome.getPersistence().getData(createKeyForChannel(msg.channel.id)).then((data) => {
const { previousWordInformation } = data;
return japaneseGameStrategy.tryAcceptAnswer(
msg.content,
previousWordInformation ? [previousWordInformation] : [],
);
}).then((acceptanceResult) => {
({ accepted } = acceptanceResult);
if (accepted) {
return handleAcceptedResult(monochrome, msg, acceptanceResult);
}
return handleRejectedResult(monochrome, msg, acceptanceResult);
})
.then(() => accepted);
}
function sendDisabledMessage(monochrome, channelID) {
return retryPromise(() => monochrome.getErisBot().createMessage(channelID, {
embed: {
title: 'Shiritori Forever disabled',
description: 'Shiritori Forever is no longer running in this channel',
color: constants.EMBED_NEUTRAL_COLOR,
},
}));
}
function sendEnabledMessage(monochrome, channelID) {
return retryPromise(() => monochrome.getErisBot().createMessage(channelID, {
embed: {
title: 'Shiritori Forever enabled',
description: 'Shiritori Forever is now running in this channel. I\'ll go first!',
color: constants.EMBED_NEUTRAL_COLOR,
},
}));
}
async function sendFirstWord(monochrome, channelID) {
const persistence = monochrome.getPersistence();
const result = await japaneseGameStrategy.getViableNextResult([]);
const wordInformation = result.word;
await createMessageForTurnTaken(
monochrome,
channelID,
monochrome.getErisBot().user.id,
wordInformation,
);
return persistence.editData(createKeyForChannel(channelID), (data) => {
const dataCopy = { ...data };
dataCopy.previousWordInformation = wordInformation;
return dataCopy;
});
}
async function handleEnabledChanged(channelID, newInternalValue) {
const { monochrome } = globals;
const persistence = monochrome.getPersistence();
let changed = false;
await persistence.editData(SHIRITORI_CHANNELS_LIST_KEY, (data) => {
const dataCopy = { ...data };
changed = newInternalValue !== dataCopy[channelID];
if (newInternalValue) {
dataCopy[channelID] = true;
} else {
delete dataCopy[channelID];
}
state.shiritoriChannels = dataCopy;
return dataCopy;
});
const shiritoriDataKey = createKeyForChannel(channelID);
await persistence.editData(shiritoriDataKey, (data) => {
if (changed || !newInternalValue) {
return {};
}
return data;
});
try {
if (changed) {
if (newInternalValue) {
await sendEnabledMessage(monochrome, channelID);
await sendFirstWord(monochrome, channelID);
} else {
await sendDisabledMessage(monochrome, channelID);
}
}
} catch (err) {
monochrome.getLogger().logFailure(LOGGER_TITLE, 'Error sending shiritoriforever enabled/disabled messages or first word', err);
}
}
module.exports = {
handleEnabledChanged,
tryHandleMessage,
loadChannels,
sendScores,
};