-
Notifications
You must be signed in to change notification settings - Fork 0
/
registry.js
451 lines (363 loc) · 13.3 KB
/
registry.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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
/*jshint esversion: 6 */
// file system access module
const fs = require('fs');
// logging
const log4js = require("log4js");
/**
* User.
* @typedef {object} User
* @property {string} id User discord id.
* @property {string[]} allyCodes Allycodes assigned to the discord user.
* @property {string[]} vipUnitsGAC List of VIP units to use for GAC intel gathering purposes.
*/
/**
* Guild.
* @typedef {object} Guild
* @property {string} id Guild id.
* @property {string[]} vipUnitsTW List of of VIP units to use for TW intel gathering purposes.
*/
/**
* Class to store bot registry data
* (players and guilds).
*/
class Registry {
/**
* Default constructor.
* @param {string} userRegistryFilename The filename for user registry storage.
* @param {string} guildRegistryFilename The filename for guild registry storage.
* @param {log4js.Logger} logger The log4js logger (optional).
*/
constructor (userRegistryFilename, guildRegistryFilename, logger = log4js.getLogger()) {
this.logger = logger;
// load users
this.setUsersFilename(userRegistryFilename);
this.users = Registry.loadUsers(this.userRegistryFilename);
// load guilds
this.setGuildsFilename(guildRegistryFilename);
this.guilds = Registry.loadGuilds(this.guildRegistryFilename);
}
/**
* Load registered users (player ally codes from JSON file).
* @static
* @param {string} fileName The user registry filename.
* @returns {Map<string, User>} Map between discord id and user info.
*/
static loadUsers(fileName) {
var result = new Map();
try {
const tempUsers = JSON.parse(fs.readFileSync(fileName, 'utf8'));
// add entries to map
tempUsers.forEach(user => result.set(user.id, user));
} catch (err) {
console.error(`loadUsers@registry: Error loading users - ${err}`);
}
return result;
}
/**
* Load registered guilds from JSON file.
* @static
* @param {string} fileName The guild registry filename.
* @returns {Map<string, User>} Map between guild id and guild info.
*/
static loadGuilds(fileName) {
var result = new Map();
try {
const tempGuilds = JSON.parse(fs.readFileSync(fileName, 'utf8'));
// add entries to map
tempGuilds.forEach(guild => result.set(guild.id, guild));
} catch (err) {
console.error(`loadUsers@registry: Error loading guilds - ${err}`);
}
return result;
}
/**
* Set the user registry filename.
* @param {string} filename User registry JSON filename.
* @returns {Registry} The registry instance.
*/
setUsersFilename(filename) {
this.userRegistryFilename = filename;
return this;
}
/**
* Get the user registry filename.
* @returns {string} Users registry filename.
*/
getUsersFilename() {
return this.userRegistryFilename;
}
/**
* Save registered users (player ally codes from JSON file).
* @param {string} fileName User JSON filename.
*/
saveUsers(fileName) {
this.logger.info(`saveUsers@registry: saving users to "${fileName}"`);
try {
this.logger.debug(`saveUsers@registry: Saving users to file`);
fs.writeFileSync(fileName, JSON.stringify(Array.from(this.users.values())));
} catch (err) {
this.logger.error(`saveUsers@registry: Error saving users - ${err}`);
}
this.logger.info(`saveUsers@registry: saved users to registry ok (total = ${this.users.size})`);
}
/**
* Clear all users from registry.
* @returns {Registry} Registry instance.
*/
clearUsers() {
this.users.clear();
return this;
}
/**
* Register a new user.
* @param {string} discord id.
* @param {string} ally code.
*/
registerUser(discordId, allyCode) {
this.logger.info(`registerUser@registry: registering user (discord id=${discordId}, allycode=${allyCode})`);
var result = true;
// check if ally code is already registered
const registeredId = this.getDiscordId(allyCode);
if (registeredId) {
this.logger.error(`registerUser@registry: ally code "${allyCode}" is already registered to discord id "${registeredId}"`);
result = false;
} else {
// no discord id for this ally code yet
if (this.users.has(discordId)) {
// discord id alreay exists, must add ally code
const user = this.users.get(discordId);
// add new allycode to array
user.allyCodes.push(allyCode);
this.saveUsers(this.userRegistryFilename);
this.logger.info(`registerUser@registry: added ally code "${allyCode}" to existing discord id "${discordId}"`);
} else {
/** @type {User} */
const user = { id: discordId, allyCodes: [ allyCode ], vipUnitsGAC: [] };
// new user
this.users.set(discordId, user);
this.saveUsers(this.userRegistryFilename);
this.logger.info(`registerUser@registry: added discord id "${discordId}" with ally code "${allyCode}"`);
}
}
return result;
}
/**
* Unregister a user.
* @param {string} discord The discord id.
* @param {string} allyCode The ally code to unregister (optional).
*/
unregisterUser(discordId, allyCode) {
this.logger.info(`unregisterUser@registry: unregistering user (discord id=${discordId})`);
var result = true;
if (this.users.has(discordId)) {
// get allyCodes for this user
var user = this.users.get(discordId);
if (user.allyCodes.length == 1) {
// only one ally code
this.users.delete(discordId);
this.saveUsers(this.userRegistryFilename);
} else {
// sanity check
if (allyCode) {
const allyCodeIdx = user.allyCodes.findIndex(code => code == allyCode);
if (allyCodeIdx == -1) {
this.logger.error(`unregisterUser@registry: could not find ally code ${allyCode} for discord id "${discordId}"`);
result = false;
} else {
user.allyCodes = user.allyCodes.splice(allyCodeIdx, 1);
this.saveUsers(this.userRegistryFilename);
}
} else {
result = false;
this.logger.error(`unregisterUser@registry: multiple ally codes for discord id "${discordId}" and none given`);
}
}
if (result) this.logger.info(`unregisterUser@registry: discord id "${discordId}" deleted`);
} else {
result = false;
this.logger.error(`unregisterUser@registry: unknown discord id "${discordId}" to unregister`);
}
return result;
}
/**
* Get the list of registered users.
* @returns {User[]} An array with discord id, allycode entries.
*/
getUsers() {
return Array.from(this.users.values());
}
/**
* Get user from discord id.
* @param {string} discord id.
* @return {User} User for the given discord id.
*/
getUser(discordId) {
this.logger.info(`getUser@registry: getting user for discord id "${discordId}"`);
var result;
if (!this.users.has(discordId)) {
this.logger.error(`getUser@registry: discord id "${discordId}" does not exist`);
} else {
result = this.users.get(discordId);
}
return result;
}
/**
* Get registerd user count.
* @returns {number} The number of registered users.
*/
getUserCount() {
return this.users.size;
}
/**
* Get first ally code from discord id.
* @param {string} discord id.
* @return {string} First ally code for the given discord id.
*/
getAllyCode(discordId) {
this.logger.info(`getAllyCode@registry: getting ally code for discord id "${discordId}"`);
var result;
if (!this.users.has(discordId)) {
this.logger.error(`getAllyCode@registry: discord id "${discordId}" does not exist`);
} else {
result = this.users.get(discordId).allyCodes[0];
}
return result;
}
/**
* Get array of ally codes for discord id.
* @param {string} discord id.
* @return {string[]} Array of ally codes for the given discord id.
*/
getAllyCodes(discordId) {
this.logger.info(`getAllyCodes@registry: getting ally codes for discord id "${discordId}"`);
var result = [];
if (!this.users.has(discordId)) {
this.logger.error(`getAllyCode@registry: discord id "${discordId}" does not exist`);
} else {
result = this.users.get(discordId).allyCodes;
}
return result;
}
/**
* Gets the registered discord id from an ally code.
* @param {string} allyCode The ally code to check.
* @returns {string} The registered discord id for the given ally code.
*/
getDiscordId(allyCode) {
this.logger.debug(`getAllyCode@registry: getting discord id for ally code "${allyCode}"`);
var result;
// loop over users
Array.from(this.users.values()).every(user => {
if (user.allyCodes.find(oneCode => oneCode == allyCode)) {
result = user.id;
return false;
} else return true;
});
return result;
}
/**
* Set the guild registry filename.
* @param {string} filename Guild registry JSON filename.
* @returns {Registry} The registry instance.
*/
setGuildsFilename(filename) {
this.guildRegistryFilename = filename;
return this;
}
/**
* Get the guild registry filename.
* @returns {string} Users registry filename.
*/
getGuildsFilename() {
return this.guildRegistryFilename;
}
/**
* Save registered guilds to JSON file.
* @param {string} fileName Guild JSON filename.
*/
saveGuilds(fileName) {
this.logger.info(`saveGuilds@registry: saving guilds to "${fileName}"`);
try {
this.logger.debug(`saveGuilds@registry: Saving guilds to file`);
fs.writeFileSync(fileName, JSON.stringify(Array.from(this.guilds.values())));
} catch (err) {
this.logger.error(`saveGuilds@registry: Error saving guilds - ${err}`);
}
this.logger.info(`saveGuilds@registry: saved guilds to registry ok (total = ${this.guilds.size})`);
}
/**
* Clear all guilds from registry.
* @returns {Registry} Registry instance.
*/
clearGuilds() {
this.guilds.clear();
return this;
}
/**
* Register a new guild.
* @param {string} guildId Guild id.
*/
registerGuild(guildId) {
this.logger.info(`registerGuild@registry: registering guild (guild id=${guildId})`);
var result = true;
if (this.guilds.has(guildId)) {
this.logger.error(`registerGuild@registry: guild with id "${guildId}" already registered`);
result = false;
} else {
/** @type {Guild} */
const guild = { id: guildId, vipUnitsTW: [] };
// new guild
this.guilds.set(guildId, guild);
this.saveGuilds(this.guildRegistryFilename);
this.logger.info(`registerGuild@registry: added guild id "${guildId}"`);
}
return result;
}
/**
* Unregister a guild.
* @param {string} guildId Guild id.
*/
unregisterGuild(guildId) {
this.logger.info(`unregisterGuild@registry: unregistering guild (guild id=${guildId})`);
var result = true;
if (this.guilds.has(guildId)) {
this.guilds.delete(guildId);
this.saveGuilds(this.guildRegistryFilename);
this.logger.info(`unregisterUser@registry: guild id "${guildId}" deleted`);
} else {
result = false;
this.logger.error(`unregisterGuild@registry: unknown guild id "${guildId}" to unregister`);
}
return result;
}
/**
* Get the list of registered guilds.
* @returns {Guild[]} An array with guilds.
*/
getGuilds() {
return Array.from(this.guilds.values());
}
/**
* Get registerd guild count.
* @returns {number} The number of registered guilds.
*/
getGuildCount() {
return this.guilds.size;
}
/**
* Get guild info from id.
* @param {string} id Guild id.
* @return {Guild} Guild information.
*/
getGuild(guildId) {
this.logger.info(`getGuild@registry: getting guild info for guild id "${guildId}"`);
var result;
if (!this.guilds.has(guildId)) {
this.logger.error(`getGuild@registry: guild id "${guildId}" does not exist`);
} else {
result = this.guilds.get(guildId);
}
return result;
}
}
module.exports = Registry;