-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
409 lines (344 loc) · 16.5 KB
/
index.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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
const fetch = require('node-fetch');
const { chunk, isEmpty, isNil } = require('lodash');
const { map, filter } = require('rxjs/operators');
const DatabaseService = require('./services/database');
const TelegramService = require('./services/telegram');
const Item = require('./models/item');
const commonPoolId = 0;
const itemCodeToNameMap = new Map(require('./constants/items'));
const itemCodeToWeightMap = new Map(require('./constants/weights'));
const databaseService = new DatabaseService();
const telegramService = new TelegramService();
const sendTelegramMessage = async (chatId, text) => {
const message = { chat_id: chatId, text };
return telegramService.sendMessage({ message });
};
const telegramMessageSubject = telegramService.getMessageSubject();
const relevantMessages = telegramMessageSubject.pipe(
map((request) => {
const update = request.body;
const { message } = update;
return message;
}),
filter((message) => {
return !isNil(message);
}),
filter((message) => {
const { forward_date, forward_from, from, chat, text } = message;
const isValidGroup = chat.id === -1001159059268 || chat.id === 41284431;
return !isEmpty(text) && isValidGroup;
})
);
const warehouseUpdates = relevantMessages.pipe(
filter((message) => {
const { forward_date, forward_from, from, chat, text } = message;
return !isNil(forward_from) && text.startsWith("Guild Warehouse:\n");
}),
filter((message) => {
const { forward_date, forward_from, from, chat, text } = message;
const { is_bot, username } = forward_from;
return is_bot && !isNil(username) && username === 'chtwrsbot';
})
);
const helpRequests = relevantMessages.pipe(
filter((message) => {
const { forward_date, forward_from, from, chat, text } = message;
return text.startsWith('/help');
})
);
const personalSummaryRequests = relevantMessages.pipe(
filter((message) => {
const { forward_date, forward_from, from, chat, text } = message;
return text.startsWith('/personal_summary') && !isNil(from);
})
);
const fullSummaryRequests = relevantMessages.pipe(
filter((message) => {
const { forward_date, forward_from, from, chat, text } = message;
return text.startsWith('/full_summary') && !isNil(from);
})
);
const weightRequests = relevantMessages.pipe(
filter((message) => {
const { forward_date, forward_from, from, chat, text } = message;
return text.startsWith('/weight') && !isNil(from);
})
);
const topWeightRequests = relevantMessages.pipe(
filter((message) => {
const { forward_date, forward_from, from, chat, text } = message;
return text.startsWith('/top_weight') && !isNil(from);
})
);
const findRequests = relevantMessages.pipe(
filter((message) => {
const { forward_date, forward_from, from, chat, text } = message;
return text.startsWith('/find ') && !isNil(from);
})
);
const updateRequests = relevantMessages.pipe(
filter((message) => {
const { forward_date, forward_from, from, chat, text } = message;
const isValidCommand = text.startsWith('/g_deposit ') || text.startsWith('/g_withdraw ')
return !isNil(from) && isValidCommand;
})
);
warehouseUpdates.subscribe(async (message) => {
const { forward_date, forward_from, from, chat, text } = message;
const stockStatuses = text.split("\n").slice(1);
const stockRegex = /(.+?) (.+?) x (\d+)/;
const matches = stockStatuses
.map((stockStatus) => stockStatus.match(stockRegex))
.filter((stockMatches) => !isNil(stockMatches));
matches.forEach(async (match) => {
const [stockStatus, itemCode, itemName, quantity, ...rest] = match;
const ownedQuantity = await Item.countQuantity((builder) => {
return builder.where('itemCode', itemCode).andWhere('telegramId', '!=', commonPoolId);
});
const commonQuantity = quantity - ownedQuantity;
const commonItemEntry = await Item.fetchOrCreate(itemCode, commonPoolId);
await commonItemEntry.patch({ quantity: commonQuantity });
});
sendTelegramMessage(chat.id, 'Updated guild warehouse state!');
});
helpRequests.subscribe(async (message) => {
const { forward_date, forward_from, from, chat, text } = message;
const helpText = `/g_deposit {item code} {quantity} - Claim items under personal balance
/g_withdraw {item code} {quantity} - Releases items from personal balance (and common balance, if necessary)
/personal_summary {item code OR partial/full item name} - Presents personal summary from guild warehouse
/full_summary {item code OR partial/full item name} - Presents full summary of guild warehouse
/find {exact item code OR exact item name} - Presents a view of all balances for this item
Forward guild warehouse message to update the common pool!`;
sendTelegramMessage(chat.id, helpText);
});
personalSummaryRequests.subscribe(async (message) => {
const { forward_date, forward_from, from, chat, text } = message;
const { first_name } = from;
const summaryRegex = /^\/personal_summary(?: )?(.*)$/;
const [request, searchTerm, ...rest] = text.match(summaryRegex);
const isExact = itemCodeToNameMap.has(searchTerm);
const itemCodes = isExact ? [searchTerm] : [...itemCodeToNameMap.entries()].filter(([itemCode, itemName]) => itemName.toLowerCase().includes(searchTerm.toLowerCase())).map(([itemCode, itemName]) => itemCode);
if (isEmpty(itemCodes)) {
const summaryText = `Replying to ${first_name}:
Found no item that matches the term: ${searchTerm}!`;
sendTelegramMessage(chat.id, summaryText);
return;
}
const summaryLines = await Promise.all(itemCodes.map(async (itemCode) => {
const personalCount = await Item.countQuantity((builder) => {
return builder.where('itemCode', itemCode).andWhere('telegramId', from.id);
});
const commonCount = await Item.countQuantity((builder) => {
return builder.where('itemCode', itemCode).andWhere('telegramId', commonPoolId);
});
return personalCount === 0 ? '' : `${itemCodeToNameMap.get(itemCode)}: ${personalCount} personal, ${commonCount} common`;
}));
const summaryLineGroups = chunk(summaryLines.filter((summaryLine) => !isEmpty(summaryLine)), 20);
const summaryTextGroups = summaryLineGroups.map((summaryLines) => summaryLines.join('\n'));
if (isEmpty(summaryTextGroups)) {
sendTelegramMessage(chat.id, `Replying to ${first_name}:
You have got nothing to your name!`);
return;
}
summaryTextGroups.forEach((summaryText) => sendTelegramMessage(chat.id, `Replying to ${first_name}:
${summaryText}`));
});
fullSummaryRequests.subscribe(async (message) => {
const { forward_date, forward_from, from, chat, text } = message;
const summaryRegex = /^\/full_summary(?: )?(.*)$/;
const [request, searchTerm, ...rest] = text.match(summaryRegex);
const isExact = itemCodeToNameMap.has(searchTerm);
const itemCodes = isExact ? [searchTerm] : [...itemCodeToNameMap.entries()].filter(([itemCode, itemName]) => itemName.toLowerCase().includes(searchTerm.toLowerCase())).map(([itemCode, itemName]) => itemCode);
if (isEmpty(itemCodes)) {
const summaryText = `Found no item that matches the term: ${searchTerm}!`;
sendTelegramMessage(chat.id, summaryText);
return;
}
const summaryLines = await Promise.all(itemCodes.map(async (itemCode) => {
const personalCount = await Item.countQuantity((builder) => {
return builder.where('itemCode', itemCode).andWhere('telegramId', from.id);
});
const commonCount = await Item.countQuantity((builder) => {
return builder.where('itemCode', itemCode).andWhere('telegramId', commonPoolId);
});
return personalCount + commonCount === 0 ? '' : `${itemCodeToNameMap.get(itemCode)}: ${personalCount} personal, ${commonCount} common`;
}));
const summaryLineGroups = chunk(summaryLines.filter((summaryLine) => !isEmpty(summaryLine)), 20);
const summaryTextGroups = summaryLineGroups.map((summaryLines) => summaryLines.join('\n'));
if (isEmpty(summaryTextGroups)) {
sendTelegramMessage(chat.id, 'The guild has got nothing to its name!');
return;
}
summaryTextGroups.forEach((summaryText) => sendTelegramMessage(chat.id, summaryText));
});
weightRequests.subscribe(async (message) => {
const { forward_date, forward_from, from, chat, text } = message;
const telegramIdToWeightMap = new Map();
const allItems = await Item.query();
allItems
.forEach((item) => {
if (!itemCodeToWeightMap.has(item.itemCode)) {
return;
}
if (!telegramIdToWeightMap.has(item.telegramId)) {
telegramIdToWeightMap.set(item.telegramId, 0);
}
const currentWeight = telegramIdToWeightMap.get(item.telegramId);
const itemWeight = itemCodeToWeightMap.get(item.itemCode);
telegramIdToWeightMap.set(item.telegramId, currentWeight + (itemWeight * item.quantity));
});
const sortedTelegramIdToWeightEntries = [...telegramIdToWeightMap.entries()].sort((a, b) => b[1] - a[1]);
const sortedTelegramNamesToWeightEntries = await Promise.all(sortedTelegramIdToWeightEntries.map(async ([telegramId, weight]) => {
if (telegramId === 0) {
return `Common: ${weight}`;
}
const responseJSON = await telegramService._sendRawRequest({
telegramMethod: 'getChat',
request: { chat_id: telegramId }
});
const response = await responseJSON.json();
return `${response.ok ? response.result.first_name : 'Unknown'}: ${weight}`;
}));
const weightText = sortedTelegramNamesToWeightEntries.join('\n');
sendTelegramMessage(chat.id, weightText);
});
topWeightRequests.subscribe(async (message) => {
const { forward_date, forward_from, from, chat, text } = message;
const allItems = await Item.query();
const telegramIds = new Set(allItems.map((item) => item.telegramId));
const telegramIdToNameMap = new Map(await Promise.all([...telegramIds.values()].map(async (telegramId) => {
if (telegramId === 0) {
return [0, 'Common'];
}
const responseJSON = await telegramService._sendRawRequest({
telegramMethod: 'getChat',
request: { chat_id: telegramId }
});
const response = await responseJSON.json();
return [telegramId, response.ok ? response.result.first_name : 'Unknown'];
})));
const orderedItemsByWeight = allItems
.map((item) => {
if (!itemCodeToWeightMap.has(item.itemCode) || !itemCodeToNameMap.has(item.itemCode)) {
return [];
}
const itemWeight = itemCodeToWeightMap.get(item.itemCode);
const itemName = itemCodeToNameMap.get(item.itemCode);
const telegramName = telegramIdToNameMap.get(item.telegramId);
return [telegramName, itemName, item.quantity, itemWeight, itemWeight * item.quantity];
})
.filter((itemTuple) => !isEmpty(itemTuple))
.sort((a, b) => {
return b[4] - a[4];
})
.slice(0, 50);
const orderedItemLines = orderedItemsByWeight.map(([telegramName, itemName, quantity, weight, totalWeight]) => {
return `${telegramName} | ${itemName} | ${quantity} x ${weight}g = ${totalWeight}g`;
});
const topWeightText = `Name | Item | Count x Wt = Total Wt
-----------------------------------------------
${orderedItemLines.join('\n')}`;
sendTelegramMessage(chat.id, topWeightText);
});
findRequests.subscribe(async (message) => {
const { forward_date, forward_from, from, chat, text } = message;
const findRegex = /^\/find (.+)$/;
const [request, searchTerm, ...rest] = text.match(findRegex);
const exactCodes = new Set([...itemCodeToNameMap.keys()]);
const exactNames = new Set([...itemCodeToNameMap.values()].map((itemName) => itemName.toLowerCase()));
let itemCodes = [];
if (exactCodes.has(searchTerm)) {
itemCodes.push(searchTerm);
} else if (exactNames.has(searchTerm.toLowerCase())) {
const exactItemCode = [...itemCodeToNameMap.entries()].filter(([itemCode, itemName]) => itemName.toLowerCase() === searchTerm.toLowerCase()).map(([itemCode, itemName]) => itemCode);
itemCodes.push(...exactItemCode);
} else {
const candidateItemCodes = [...itemCodeToNameMap.entries()].filter(([itemCode, itemName]) => itemName.toLowerCase().includes(searchTerm.toLowerCase())).map(([itemCode, itemName]) => itemCode);
itemCodes.push(...candidateItemCodes);
}
if (isEmpty(itemCodes)) {
const findText = `Found no item that matches the term: ${searchTerm}`;
sendTelegramMessage(chat.id, findText);
return;
}
if (itemCodes.length > 1) {
const findText = `Search term for /find must resolve to a single item!
Given term: ${searchTerm}
Matched Items: ${itemCodes.map((itemCode) => itemCodeToNameMap.get(itemCode)).join(', ')}`;
sendTelegramMessage(chat.id, findText);
return;
}
const chosenItemCode = itemCodes[0];
const items = await Item.query().where('itemCode', chosenItemCode).andWhere('quantity', '!=', 0);
const findLines = await Promise.all(items.map(async (item) => {
if (item.telegramId === commonPoolId) {
return `Common: ${item.quantity}`;
}
const responseJSON = await telegramService._sendRawRequest({
telegramMethod: 'getChat',
request: { chat_id: item.telegramId }
});
const response = await responseJSON.json();
return `${response.ok ? response.result.first_name : 'Unknown'}: ${item.quantity}`;
}));
const findText = isEmpty(items) ? 'Common: 0' : findLines.join('\n');
sendTelegramMessage(chat.id, findText);
});
const restrictedUsers = new Set([ 40506052 ]);
const itemCodeQuantityLimit = new Map([
['01', 500],
['02', 500],
['03', 500],
['05', 500],
['06', 500],
['07', 500],
['08', 250],
['09', 250],
['10', 250]
]);
updateRequests.subscribe(async (message) => {
const { forward_date, forward_from, from, chat, text } = message;
if (isNil(from) || restrictedUsers.has(from.id)) {
sendTelegramMessage(chat.id, `This user is restricted from /g_deposit and /g_withdraw commands.`);
return;
}
const commandRegex = /^\/g_(deposit|withdraw) (.+?) (\d+)$/;
if (!commandRegex.test(text)) {
sendTelegramMessage(chat.id, `Bad format. Should be ${text.split(' ')[0]} {item code} {quantity}`);
return;
}
const [request, action, itemCode, quantityText, ...rest] = text.match(commandRegex);
const multiplier = (action === 'deposit') ? 1 : -1;
const itemName = itemCodeToNameMap.has(itemCode) ? itemCodeToNameMap.get(itemCode) : `Mystery Item ${itemCode}`;
const quantity = multiplier * parseInt(quantityText);
const personalItemEntry = await Item.fetchOrCreate(itemCode, from.id);
const commonItemEntry = await Item.fetchOrCreate(itemCode, commonPoolId);
const availableQuantity = personalItemEntry.quantity + Math.max(commonItemEntry.quantity, 0);
const finalQuantity = availableQuantity + quantity;
if (finalQuantity < 0) {
const updateText = `Can only withdraw up to ${availableQuantity} ${itemName}:
Personal: ${personalItemEntry.quantity} ${itemName}
Common: ${commonItemEntry.quantity} ${itemName}`;
sendTelegramMessage(chat.id, updateText);
return;
}
if (action === 'deposit' && itemCodeQuantityLimit.has(itemCode) && (personalItemEntry.quantity + quantity) > itemCodeQuantityLimit.get(itemCode)) {
const limitedPersonalQuantity = Math.max(personalItemEntry.quantity, itemCodeQuantityLimit.get(itemCode));
const overflowedCommonQuantity = commonItemEntry.quantity + (personalItemEntry.quantity + quantity) - limitedPersonalQuantity;
const newPersonalItemEntry = await personalItemEntry.patch({ quantity: limitedPersonalQuantity });
const newCommonItemEntry = await commonItemEntry.patch({ quantity: overflowedCommonQuantity });
const updateText = `A personal limit of ${itemCodeQuantityLimit.get(itemCode)} has been enforced for this item. Deposits in excess of this limit will be sent to the common pool.
Personal: ${limitedPersonalQuantity} (+${limitedPersonalQuantity - personalItemEntry.quantity})
Common: ${overflowedCommonQuantity} (+${overflowedCommonQuantity - commonItemEntry.quantity})`;
sendTelegramMessage(chat.id, updateText);
return;
}
const newPersonalQuantity = Math.max(personalItemEntry.quantity + quantity, 0);
const newCommonQuantity = commonItemEntry.quantity + Math.min(personalItemEntry.quantity + quantity, 0);
const newPersonalItemEntry = await personalItemEntry.patch({ quantity: newPersonalQuantity });
const newCommonItemEntry = await commonItemEntry.patch({ quantity: newCommonQuantity });
const updateText = `Processed ${quantity > 0 ? 'deposit' : 'withdrawal'} for ${Math.abs(quantity)} ${itemName}.
Personal: ${newPersonalItemEntry.quantity} (${newPersonalItemEntry.quantity < personalItemEntry.quantity ? '-' : '+'}${Math.abs(newPersonalItemEntry.quantity - personalItemEntry.quantity)})
Common: ${newCommonItemEntry.quantity} (${newCommonItemEntry.quantity < commonItemEntry.quantity ? '-' : '+'}${Math.abs(newCommonItemEntry.quantity - commonItemEntry.quantity)})`;
sendTelegramMessage(chat.id, updateText);
});