forked from sirDonovan/Cassius
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathScrabmonlb.js
158 lines (143 loc) · 3.38 KB
/
Scrabmonlb.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
'use strict';
const fs = require('fs');
const BACKUP_INTERVAL = 60 * 1000;
class DD {
constructor() {
this.lb = {};
this.modlog = {};
this.firstpoints = 5;
this.secondpoints = 3;
this.partpoints = 2;
this.name = "Scrabblemons";
}
getNameIndex() {
return 3;
}
getStr(num) {
let sorted = this.getSorted();
let str = "<div class = \"infobox\"><html><body><table align=\"center\" border=\"2\"><tr>";
let indices = ["Rank", "Name", "Firsts", "Seconds", "Parts", "Points"];
for (let i = 0; i < 6; i++) {
str += "<td style=background-color:#FFFFFF; height=\"30px\"; align=\"center\"><b><font color=\"black\">" + indices[i] + "</font></b></td>";
}
str += "</tr>"
let real = [3,0,1,2];
let strs = [];
for (let i = Math.max(0, num - 5); i < num; i++) {
let strx = "<tr>";
for (let j = 0; j < 6; j++) {
let stuff;
if (j === 0) {
stuff = i+1;
} else if (j === 5) {
stuff = dd.getPoints(sorted[i]);
} else {
stuff = sorted[i][real[j - 1]];
}
strx += "<td style=background-color:#FFFFFF; height=\"30px\"; align=\"center\"><b><font color=\"black\">" + stuff + "</font></b></td>";
}
strs.push(strx + "</tr>");
}
str += strs.join("");
str += "</table></body></html></div>";
return str;
}
importData() {
let file = '{}';
try {
file = fs.readFileSync('./databases/scrabmonlb.json').toString();
} catch (e) {}
this.lb = JSON.parse(file);
}
exportData() {
fs.writeFileSync('./databases/scrabmonlb.json', JSON.stringify(this.lb));
}
addFirst(user) {
let id = Tools.toId(user.trim());
if (!(id in this.lb)) {
this.lb[id] = {
firsts: 1,
seconds: 0,
parts: 0,
name: user.trim(),
}
} else {
this.lb[id].firsts++;
}
}
addSecond(user) {
let id = Tools.toId(user.trim());
if (!(id in this.lb)) {
this.lb[id] = {
firsts: 0,
seconds: 1,
parts: 0,
name: user.trim(),
}
} else {
this.lb[id].seconds++;
}
}
addPart(user) {
let id = Tools.toId(user.trim());
if (!(id in this.lb)) {
this.lb[id] = {
firsts: 0,
seconds: 0,
parts: 1,
name: user.trim(),
}
} else {
this.lb[id].parts++;
}
}
removeFirst(user) {
let id = Tools.toId(user);
if (!(id in this.lb) || this.lb[id].firsts === 0) {
return false
} else {
this.lb[id].firsts--;
return true;
}
}
removeSecond(user) {
let id = Tools.toId(user);
if (!(id in this.lb) || this.lb[id].seconds === 0) {
return false
} else {
this.lb[id].hosts--;
return true;
}
}
removePart(user) {
let id = Tools.toId(user);
if (!(id in this.lb) || this.lb[id].parts === 0) {
return false
} else {
this.lb[id].parts--;
return true;
}
}
getPoints(item) {
return this.firstpoints * item[0] + this.secondpoints * item[1] + this.partpoints * item[2];
}
getSorted() {
let items = [];
for (let id in this.lb) {
let item = this.lb[id];
items.push([item.firsts, item.seconds, item.parts, item.name]);
}
items.sort(function(first, second) {
let points1 = dd.getPoints(first);
let points2 = dd.getPoints(second);
if (points1 !== points2) return points2 - points1;
if (first[1] !== second[1]) return second[1] - first[1];
if (first[2] !== second[2]) return second[2] - first[2];
return second[3] > first[3];
});
return items;
}
}
let dd = new DD();
dd.backupInterval = setInterval(() => dd.exportData(), BACKUP_INTERVAL);
module.exports = dd;