-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexile-cache.js
100 lines (90 loc) · 2.23 KB
/
exile-cache.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
const db = require('./database');
let exileCache = [];
async function LoadExilesFromDatabase() {
let newCache = [];
const results = await db.Query('SELECT * FROM exiles');
for (const row of results) {
newCache.push({
exiler: row.exiler,
exilee: row.exilee,
is_friend: row.is_friend,
});
}
exileCache = newCache;
}
async function AddExile(exiler, exilee) {
if (IsExiled(exiler, exilee)) {
return;
}
exileCache.push({ exiler, exilee, is_friend: false });
await db.Query('INSERT INTO exiles (exiler, exilee, is_friend) VALUES (?, ?, FALSE)', [exiler, exilee]);
}
async function AddFriend(exiler, exilee) {
if (IsFriend(exiler, exilee)) {
return;
}
exileCache.push({ exiler, exilee, is_friend: true });
await db.Query('INSERT INTO exiles (exiler, exilee, is_friend) VALUES (?, ?, TRUE)', [exiler, exilee]);
}
function GetAllExilesAsList() {
// Return a copy of the cache to avoid any shenanigans.
const result = [];
for (const ex of exileCache) {
result.push({
exiler: ex.exiler,
exilee: ex.exilee,
is_friend: ex.is_friend,
});
}
return result;
}
function IsExiled(exiler, exilee) {
for (const ex of exileCache) {
if (ex.exiler === exiler && ex.exilee === exilee && !ex.is_friend) {
return true;
}
}
return false;
}
function IsFriend(exiler, exilee) {
for (const ex of exileCache) {
if (ex.exiler === exiler && ex.exilee === exilee && ex.is_friend) {
return true;
}
}
return false;
}
async function Unexile(exiler, exilee) {
let newCache = [];
let found = false;
for (const ex of exileCache) {
if (ex.exiler === exiler && ex.exilee === exilee) {
found = true;
} else {
newCache.push(ex);
}
}
if (!found) {
return;
}
exileCache = newCache;
await db.Query('DELETE FROM exiles WHERE exiler = ? AND exilee = ?', [exiler, exilee]);
}
async function SetIsFriend(exiler, exilee, is_friend) {
await Unexile(exiler, exilee);
if (is_friend) {
await AddFriend(exiler, exilee);
} else {
await AddExile(exiler, exilee);
}
}
module.exports = {
AddExile,
AddFriend,
GetAllExilesAsList,
IsExiled,
IsFriend,
LoadExilesFromDatabase,
SetIsFriend,
Unexile,
};