-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache.js
More file actions
356 lines (295 loc) · 9.64 KB
/
Copy pathcache.js
File metadata and controls
356 lines (295 loc) · 9.64 KB
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
const _ = require("lodash");
// const { JudgeRequests } = require("./requests/judgeRequests");
// const { CountryRequests } = require("./requests/countryRequests");
const { CacheUtils } = require("./utils/cacheUtils");
//#region Namespaces
var CountriesCache = {};
var VotingStatusesCache = {};
var SocketMappingCache = {};
var EmailMappingCache = {};
//#endregion
//#region Variables
let runningCountry = 0;
let countries = [];
let winnerCountry = null;
let judges = [];
let votingStatuses = [];
let socketMapping = new Map();
let emailMapping = new Map();
//#endregion
let isCountriesInitialized = false;
/**
* Initializes countries cache.
* @returns {Promise<boolean>} A promise with result true if initialization was completed successfully. Otherwise false.
*/
CountriesCache.initCountries = async function() {
return CountryRequests.getAllCountriesSortedByRunningOrder()
.then(response => {
if (response.success) {
CountriesCache.fillCountries(response.data);
return true;
}
else return false;
})
.catch(e => {return false});
}
/**
* Gets countries.
* @returns {object[]} An array of the countries.
*/
CountriesCache.getCountries = function() {
return countries;
}
/**
* Sets countries.
* @param {object[]} countriesData
*/
CountriesCache.setCountries = function(countriesData) {
if (countries.length == countriesData.length) return;
countries = [];
CountriesCache.fillCountries(countriesData);
}
/**
* Adds a new country to array.
* @param {object} country The new country object.
* @returns {boolean} True if addition was completed successfully. Otherwise, false.
*/
CountriesCache.addCountry = function(country) {
return CacheUtils.addEntry(countries, country);
}
/**
* Updates an existing country.
* @param {string} code Country's code
* @param {object} updatedCountry Updated country's data
* @returns {boolean} True if update was completed successfully. Otherwise, false.
*/
CountriesCache.updateCountry = function(code, updatedCountry) {
return CacheUtils.updateEntry(countries, code, "code", updatedCountry);
}
/**
* Gets country that matches the given running order.
* @param {number} runningOrder The running order
* @returns {object} A country object. If country was not found, returns null.
*/
CountriesCache.findCountryByRunningOrder = function(runningOrder) {
let country = countries.find(element => _.parseInt(element.runningOrder) == _.parseInt(runningOrder));
if (country == null) return null;
else return country;
}
/**
* Gets country code that matches the given running order.
* @param {number} runningOrder The running order
* @returns {string} The code of the country. If country was not found, returns null.
*/
CountriesCache.findCountryCodeByRunningOrder = function(runningOrder) {
let country = CountriesCache.findCountryByRunningOrder(runningOrder);
if (country == null) return null;
else return country.code;
}
/**
* Gets country that matches the given code.
* @param {string} code Country's code
* @returns {object} A country object. If country was not found, returns null.
*/
CountriesCache.findCountry = function(code) {
return CacheUtils.findEntry(countries, code, "code");
}
/**
* Gets country's name that matches the given code.
* @param {string} code Country's code
* @returns {object} A country object. If country was not found, returns null.
*/
CountriesCache.findCountryNameByCode = function(code) {
let country = CountriesCache.findCountry(code);
if (country == null) return null;
else return country.name;
}
/**
* Fills country array.
* @param {object[]} data
*/
CountriesCache.fillCountries = function(data) {
data.forEach(country => {
// TODO: merge voting statuses with country
CacheUtils.addEntry(countries, country);
});
isCountriesInitialized = true;
}
/**
* Sets vote to a specific country for a specific judge.
* @param {string} judgeCode Judge who voted
* @param {string} countryCode Country that judge voted
* @param {number} points
*/
CountriesCache.setVotes = function(judgeCode, countryCode, points) {
let country = CountriesCache.findCountry(countryCode);
if (country == null) return;
else {
let preUpdatedPoints = 0;
let preUpdatedTotalVotes = country.totalVotes;
if (country.votes[judgeCode] != null) {
preUpdatedPoints = country.votes[judgeCode];
}
country.votes[judgeCode] = points;
country.totalVotes = preUpdatedTotalVotes + points - preUpdatedPoints;
}
}
/**
* Gets country's total votes.
* @param {string} code
* @returns {number} Country's total votes. If country was not found, returns 0.
*/
CountriesCache.getTotalVotes = function(code) {
let country = CountriesCache.findCountry(code);
if (country == null) return 0;
else return country.totalVotes;
}
/**
* Resets countries cache meaning clearing out cache and initializing it.
* @returns {Promise<boolean>} A promise with result true if initialization was completed successfully. Otherwise false.
*/
CountriesCache.resetCountries = function() {
countries = [];
return CountriesCache.initCountries();
}
/**
* Deletes a country.
* @param {string} code Country's code
* @returns {object} The deleted country. If country was not found, returns null.
*/
CountriesCache.deleteCountry = function(code) {
return CacheUtils.deleteEntry(countries, code, "code");
}
/**
* Clears out countries cache.
*/
CountriesCache.clearCountries = function() {
countries = [];
isCountriesInitialized = false;
}
/**
* Gets if countries cache has been initialized.
* @returns {boolean} True if cache has been initialized. Otherwise, false.
*/
CountriesCache.isInitialized = function() {
return isCountriesInitialized;
}
// Winner country
/**
* Gets winner country.
* @returns {object}
*/
CountriesCache.getWinnerCountry = function() {
return winnerCountry;
}
/**
* Sets winner country.
* @param {string} countryCode Country's code
*/
CountriesCache.setWinnerCountry = function(countryCode) {
winnerCountry = CountriesCache.findCountry(countryCode);
}
/**
* Clears out winner country.
*/
CountriesCache.clearWinnerCountry = function() {
winnerCountry = null;
}
//#endregion
//#region Voting Statuses
/**
* Sets the voting status to countries. Object pushed in array has the following format:
* {countryCode : countryCodeValue, status : statusValue}
* @param {string[]} countryCodes Array of country codes
* @param {boolean} status Voting status
*/
VotingStatusesCache.setVotingStatuses = function(countryCodes, status) {
countryCodes.forEach(countryCode => {
let i = CacheUtils.findEntryIndex(votingStatuses, countryCode, "countryCode");
if (i >= 0) votingStatuses[i].status = status;
else votingStatuses.push({countryCode : countryCode, status : status});
});
}
/**
* Gets voting status based on country code.
* @param {string} countryCode
* @returns {string} OPEN if voting status is open for voting. Otherwise, returns CLOSED. If country code was not found in cache, returns CLOSED.
*/
VotingStatusesCache.getVotingStatusByCountryCode = function(countryCode) {
if (countryCode == null) return "CLOSED";
let votingStatus = CacheUtils.findEntry(votingStatuses, countryCode, "countryCode");
if (votingStatus == null) return "CLOSED";
else return votingStatus.status;
}
/**
* Gets voting status based on running order.
* @param {number} runningOrder
* @returns {string} OPEN if voting status is open for voting. Otherwise, returns CLOSED. If country code was not found in cache, returns CLOSED.
*/
VotingStatusesCache.getVotingStatusByRunningOrder = function(runningOrder) {
let countryCode = CountriesCache.findCountryCodeByRunningOrder(runningOrder);
return VotingStatusesCache.getVotingStatusByCountryCode(countryCode);
}
/**
* Gets voting statuses cache.
* @returns {object[]} An array of the voting statuses.
*/
VotingStatusesCache.getVotingStatuses = function() {
return votingStatuses;
}
/**
* Resets / Clears out voting statuses cache.
*/
VotingStatusesCache.resetVotingStatuses = function() {
votingStatuses = [];
}
//#endregion
//#region
/**
* Adds a new socket ID. Socket mapping has key the socket ID and value the judge's code.
* Adding a socket ID leads to updating the mapping judge with the online information.
* @param {string} socketID Established socket ID
* @param {string} judgeCode Judge's code
*/
SocketMappingCache.addSocketID = function(socketID, judgeCode) {
socketMapping.set(socketID, judgeCode);
console.log(socketMapping)
}
/**
* Removes socket ID. Removing a socket ID leads to updating the mapping Judge with the offline information.
* @param {string} socketID Disconnected socket ID
*/
SocketMappingCache.removeSocketID = function(socketID) {
let judgeCode = socketMapping.get(socketID);
socketMapping.delete(socketID);
return judgeCode;
}
/**
* Gets online judge codes.
* @returns {string[]} An array of the online judge codes.
*/
SocketMappingCache.getOnlineJudgeCodes = function() {
return Array.from(socketMapping.values());
}
//#endregion
//#region Email mapping
EmailMappingCache.emailExists = function(email) {
let emails = Array.from(emailMapping.values());
return emails.includes(email);
}
EmailMappingCache.addEmail = function(token, email) {
emailMapping.set(token, email);
}
EmailMappingCache.removeEmail = function(token) {
emailMapping.delete(token);
}
EmailMappingCache.clearEmails = function() {
emailMapping = new Map();
}
//#endregion
module.exports = {
CountriesCache,
VotingStatusesCache,
SocketMappingCache,
EmailMappingCache
};