-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
317 lines (260 loc) · 12 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
import {Formatters, MessageEmbed, WebhookClient} from 'discord.js';
import Web3 from 'web3';
import ordinal from 'ordinal';
// eslint-disable-next-line import/no-unresolved
import {setTimeout} from 'timers/promises';
import config from './config.js';
import {
calculateHighestFightWinPercentage,
getAccountCharacters,
getAccountWeapons,
getCharacterStamina
} from './cryptoblades.js';
import {staminaMillisecondsRegenerationTime} from './constants.js';
const {discordWebhookUrl, discordWebhookId, discordWebhookToken} = config;
// Check if either Discord Webhook URL or Discord Webhook ID and token is provided
if (!(discordWebhookUrl || (discordWebhookId !== '' && discordWebhookToken !== ''))) {
throw new Error('You need to specify either Discord Webhook URL or both Discord Webhook ID and token!');
}
// Ensure stamina threshold has a legal value
if (config.staminaThreshold < 0 || config.staminaThreshold > 200) {
throw new Error('Stamina threshold needs to be between 0 and 200!');
}
const webhookClient = discordWebhookUrl ? new WebhookClient({url: discordWebhookUrl}) : new WebhookClient({id: discordWebhookId, token: discordWebhookToken});
const web3 = new Web3(config.blockchainProvider);
const discordMessageDescriptionLimit = 4096;
const webhookUsername = 'CryptoBlades Stamina Notifier';
async function initializeAccounts() {
// Ensure Binance Smart Chain addresses are provided
if (!config.addresses) {
throw new Error('You need to specify an address!');
}
const addresses = config.addresses.split(',').map(address => address.trim()).filter(address => address !== '');
// Ensure all addresses are valid
for (let i = 0; i < addresses.length; i++) {
const address = addresses[i];
if (!web3.utils.isAddress(address)) {
throw new Error(`'${address}' is not a valid address`);
}
}
let accountNames = [];
// Use provided account names or create `1st`, `2nd`, `3rd`...
if (config.accountNames) {
accountNames = config.accountNames.split(',').map(accountName => accountName.trim()).filter(accountName => accountName !== '');
} else {
for (let i = 0; i < addresses.length; i++) {
accountNames.push(ordinal(i + 1));
}
}
if (addresses.length !== accountNames.length) {
throw new Error('Length of addresses and account names needs to match!');
}
const weaponIds = config.weaponIds && config.weaponIds.split(',').map(address => address.trim()).filter(address => address !== '');
if (config.weaponIds && addresses.length !== weaponIds.length) {
throw new Error('Length of addresses and weapon IDs needs to match!');
}
return Promise.all(addresses.map(async (address, i) => {
const characterIds = await getAccountCharacters(address);
const characters = characterIds.map(characterId => ({id: characterId}));
if (weaponIds) {
const weapons = await getAccountWeapons(address);
const weaponId = weaponIds[i];
if (!weapons.includes(weaponId)) {
throw new Error(`Account \`${accountNames[i]}\` does not own weapon ID \`${weaponId}\`!`);
}
}
return {
address,
name: accountNames[i],
characters,
...(weaponIds && {weaponId: weaponIds[i]})
};
}));
}
function getDateFromNeededStamina(staminaNeeded) {
return new Date(new Date().getTime() + staminaMillisecondsRegenerationTime * staminaNeeded);
}
function splitCharacterMessageDescriptions(accountStaminas) {
const messageDescriptions = [];
let messageDescription = '';
for (let i = 0; i < accountStaminas.length; i++) {
if (messageDescription.length + accountStaminas[i].length > discordMessageDescriptionLimit) {
messageDescriptions.push(messageDescription);
messageDescription = '';
}
messageDescription += `${accountStaminas[i]}\n\n`;
}
if (messageDescription !== '') {
messageDescriptions.push(messageDescription);
}
return messageDescriptions;
}
function getDiscordEmojiFromTrait(trait) {
switch (trait) {
case 0:
// Fire
return ':fire:';
case 1:
// Earth
return ':evergreen_tree:';
case 2:
// Lightning
return ':zap:';
case 3:
// Water
return ':droplet:';
default:
return ':question:';
}
}
function createEnemyMessagePart(enemy) {
const {
winPercentage, trait, index, power
} = enemy;
return ` - ${winPercentage}% - ${getDiscordEmojiFromTrait(trait)} ${ordinal(index)} (${power})`;
}
async function notifyStamina(accounts) {
const nonEmptyAccounts = accounts.filter(account => account.charactersToNotify.length > 0);
if (nonEmptyAccounts.length === 0) {
return;
}
const embedMessage = new MessageEmbed()
.setColor('#74829d');
// Send shorter message if only one character reached threshold
if (nonEmptyAccounts.length === 1 && nonEmptyAccounts[0].charactersToNotify.length === 1) {
const [account] = nonEmptyAccounts;
const [character] = account.charactersToNotify;
let baseMessage;
// Check if stamina has already been reached
if (!character.thresholdReachedAt) {
baseMessage = `\`${account.name}\`'s ${ordinal(character.index)} character reached ${config.staminaThreshold} stamina (${character.stamina})`;
} else {
const dynamicTimestamp = Formatters.time(character.thresholdReachedAt, Formatters.TimestampStyles.RelativeTime);
baseMessage = `\`${account.name}\`'s ${ordinal(character.index)} character reaches ${config.staminaThreshold} stamina (${character.stamina}) - ${dynamicTimestamp}`;
}
embedMessage.setDescription(
`${baseMessage}${config.weaponIds ? createEnemyMessagePart(character.enemy) : ''}`
);
await webhookClient.send({
username: webhookUsername,
embeds: [embedMessage]
});
return;
}
embedMessage.setTitle(`Characters reached ${config.staminaThreshold} stamina`);
const accountStaminas = nonEmptyAccounts
.map(account => {
const startMessage = `\`${account.name}\`\n`;
const characterStaminas = account.charactersToNotify
.map(character => {
const baseMessage = `• ${ordinal(character.index)} (${character.stamina})`;
if (!character.thresholdReachedAt) {
return `${baseMessage}${config.weaponIds ? createEnemyMessagePart(character.enemy) : ''}`;
}
const dynamicTimestamp = Formatters.time(character.thresholdReachedAt, Formatters.TimestampStyles.RelativeTime);
return `${baseMessage} - ${dynamicTimestamp}${config.weaponIds ? createEnemyMessagePart(character.enemy) : ''}`;
})
.join('\n');
return `${startMessage}${characterStaminas}`;
});
const messageDescriptions = splitCharacterMessageDescriptions(accountStaminas);
for (let i = 0; i < messageDescriptions.length; i++) {
embedMessage.setDescription(messageDescriptions[i]);
// eslint-disable-next-line no-await-in-loop
await webhookClient.send({
username: webhookUsername,
embeds: [embedMessage]
});
}
}
async function checkStamina(account) {
const checkedAccount = {...account};
const charactersToNotify = [];
// Check stamina for all accounts characters
for (let i = 0; i < account.characters.length; i++) {
const character = account.characters[i];
// Skip checking stamina if not needed
if (character.nextCheck > new Date()) {
// eslint-disable-next-line no-continue
continue;
}
// eslint-disable-next-line no-await-in-loop
const stamina = await getCharacterStamina(character.id);
const startNextHour = new Date();
startNextHour.setHours(new Date().getHours() + 1, 0, 0, 0);
const remainingStaminaGainCurrentHour = Math.floor((startNextHour.getTime() - new Date().getTime()) / staminaMillisecondsRegenerationTime);
// If stamina threshold has been reached => notify
if (stamina >= config.staminaThreshold) {
const characterToNotify = {index: i + 1, stamina};
// Check if fight percentage should be checked
if (config.weaponIds) {
// eslint-disable-next-line no-await-in-loop
const enemy = await calculateHighestFightWinPercentage(character.id, account.weaponId);
if (enemy.winPercentage >= config.winPercentageThreshold) {
// Wait for next threshold before checking again
checkedAccount.characters[i].nextCheck = getDateFromNeededStamina(config.staminaThreshold);
characterToNotify.enemy = enemy;
} else {
// eslint-disable-next-line no-continue
continue;
}
} else {
// Wait for next threshold before checking again
checkedAccount.characters[i].nextCheck = getDateFromNeededStamina(config.staminaThreshold);
}
// Save character that should be notified
charactersToNotify.push(characterToNotify);
} else if (stamina + remainingStaminaGainCurrentHour >= config.staminaThreshold) {
// If stamina threshold will be reached current hour => notify
const remainingStamina = config.staminaThreshold - stamina;
const thresholdReachedAt = getDateFromNeededStamina(remainingStamina);
const characterToNotify = {index: i + 1, stamina, thresholdReachedAt};
// Check if fight percentage should be checked
if (config.weaponIds) {
// eslint-disable-next-line no-await-in-loop
const enemy = await calculateHighestFightWinPercentage(character.id, account.weaponId);
if (enemy.winPercentage >= config.winPercentageThreshold) {
// Wait for next threshold and time until threshold before checking again
checkedAccount.characters[i].nextCheck = getDateFromNeededStamina(remainingStamina + config.staminaThreshold);
characterToNotify.enemy = enemy;
} else {
// eslint-disable-next-line no-continue
continue;
}
} else {
// Wait for next threshold and time until threshold before checking again
checkedAccount.characters[i].nextCheck = getDateFromNeededStamina(remainingStamina + config.staminaThreshold);
}
// Save character that should be notified
charactersToNotify.push(characterToNotify);
} else {
const staminaNeeded = config.staminaThreshold - stamina;
checkedAccount.characters[i].nextCheck = getDateFromNeededStamina(staminaNeeded);
}
}
return {checkedAccount, charactersToNotify};
}
(async () => {
const accounts = await initializeAccounts();
// Make it run forever
while (true) {
try {
console.log('Checking for stamina at:', new Date());
const notifyingAccounts = [];
for (let i = 0; i < accounts.length; i++) {
const account = accounts[i];
// eslint-disable-next-line no-await-in-loop
const result = await checkStamina(account);
accounts[i] = result.checkedAccount;
notifyingAccounts.push({name: account.name, charactersToNotify: result.charactersToNotify});
}
// eslint-disable-next-line no-await-in-loop
await notifyStamina(notifyingAccounts);
} catch (error) {
console.log(error);
} finally {
// eslint-disable-next-line no-await-in-loop
await setTimeout(config.waitTimeout);
}
}
})();