forked from ajyuan/Monitori
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathguildMap.js
215 lines (198 loc) · 7.04 KB
/
guildMap.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
const userMap = require("./userMap");
const config = require("./config/config.json");
const guildMap = new Map();
var bot;
class Guild {
constructor(id) {
this.id = id;
this.userListModified = false;
this.pointsBoard = null;
this.scoreBoard = null;
this.timer;
}
//This timer is used to detect when an active conversation has ended so the bot
//can automatically analyze messages once the conversation has ended
setTimer(time = config.autopayOnInactivityTime * 1000) {
if (time != 0) {
let id = this.id;
clearTimeout(this.timer);
this.timer = setTimeout(function run() {
console.log("Detected guild inactivity at " + id + ", analyzing cache");
userMap.updateMemberScores(bot.guilds.get(id).members);
}, time);
}
}
}
module.exports = {
//Passes client used from index.js into guildMap.js
init: function (client) {
bot = client;
},
//Allows a new guild to be created given ID
add: function (guildID) {
newGuild(guildID);
},
//Removes a guild from the guildmap given an ID
remove: function (guildID) {
guildMap.delete(guildID);
},
//Notifies guildmap that a guild has an active conversation and creates a timer
//to detect when the conversation has ended to be analyzed
setActive: function (guildID) {
guildMap.get(guildID).setTimer();
},
/*
Creates a leaderboard for a guildID if none exists, updates it if it does
Returns a formatted string of users to be printed by bot
*/
leaderboard: function (guild, type) {
let guildID = guild.id;
console.log("Generating leaderboard for guild: " + guildID);
var members = [];
var ids = [];
if (!guildMap.has(guildID) || guildMap.get(guildID).userListModified) {
members = updateMembersList(guild.members);
} else {
if (type === "points") {
ids = guildMap.get(guildID).pointsBoard;
} else if (type === "score") {
ids = guildMap.get(guildID).scoreBoard;
} else {
console.log("Leaderboard generation error: Type not specified");
process.exit(1);
}
if (ids === null) {
ids = updateMembersList(guild.members);
}
let currentID;
while (ids.length != 0) {
currentID = ids.pop()
userMap.updateUserScore(currentID);
members.push(currentID);
}
}
let createdGuild;
createdGuild = guildMap.get(guildID);
if (createdGuild === undefined) {
createdGuild = newGuild(guildID);
}
let currentBoard = newBoard(members, type);
if (type === "points") {
createdGuild.pointsBoard = currentBoard;
} else if (type === "score") {
createdGuild.scoreBoard = currentBoard;
}
createdGuild.userListModified = false;
return boardToString(currentBoard, type);
},
//Notifies ADT that guild leaderboard has a change in member list, and needs to have
//leaderboard regenerated
flag: function (guildID) {
if (!guildMap.has(guildID)) {
console.log("Flag error: Attempted to flag guild that has not been mapped");
process.exit(1);
}
console.log("Member list change detected on guild " + guildID);
let flaggedGuild = guildMap.get(guildID);
flaggedGuild.userListModified = true;
flaggedGuild.pointsBoard = null;
flaggedGuild.scoreBoard = null;
}
}
//Creates a new guild class
function newGuild(guildID) {
console.log("Created new guild for " + guildID);
let createdGuild = new Guild(guildID);
//createdGuild.setTimer();
guildMap.set(guildID, createdGuild);
return createdGuild;
}
//Determines appropriate sort to use and generates a new leaderboard
function newBoard(members, type) {
if (members.length < config.sortThreshold) {
//console.log("Guild has " + members.length + " active member(s), using insertion sort!");
return insertionSort(members, type);
} else {
//console.log("Guild has " + members.length + " active members, using merge sort!");
return mergeSort(members, type);
}
}
function updateMembersList(ids) {
console.log("Updating guild member list");
let members = [];
ids.tap(user => {
userMap.idCheck(user.id);
userMap.updateUserScore(user.id);
members.push(user.id);
});
return members;
}
//Generates the string representation of the leaderboard
function boardToString(currentBoard, type) {
var output = "";
for (var i = 0; i < currentBoard.length; i++) {
if (currentBoard[i] !== config.botid) {
output += "**" + bot.users.get(currentBoard[i]).username
+ "** | " + valueGetter(type, currentBoard[i]) + ((type === "score") ? "" : " pts") + "\n";
}
}
return output;
}
/*
Sorting Algorithms -----
Given an array of userIDs and a type, these functions will return a sorted array of userIDs by
a given type
Ex. if type === "points", sort the users by their points
*/
function insertionSort(members, type) {
for (var i = 1; i < members.length; i++) {
let current = members[i];
for (var j = i - 1; j >= 0 && valueGetter(type, members[j]) < valueGetter(type, current); j--) {
//console.log(valueGetter(type, members[j]) + " is greater than " + valueGetter(type, current));
members[j + 1] = members[j];
}
members[j + 1] = current;
}
return members;
}
function mergeSort(members, type) {
/*for (i = 0; i < members.length; i ++) {
console.log("Current subarray: " + members[i]);
}*/
if (members.length <= 1) {
return members;
} else {
const mid = members.length / 2;
const first = members.slice(0, mid);
const last = members.slice(mid);
return merge(mergeSort(first), mergeSort(last), type);
}
}
function merge(first, last, type) {
let output = [];
let firstIndex = 0;
let lastIndex = 0;
while (firstIndex < first.length && lastIndex < last.length) {
if (valueGetter(type, first[firstIndex]) > valueGetter(type, last[lastIndex])) {
console.log(first[firstIndex]);
output.push(first[firstIndex]);
firstIndex++;
} else {
console.log(last[lastIndex]);
output.push(last[lastIndex]);
lastIndex++;
}
}
return output.concat(first.slice(firstIndex)).concat(last.slice(lastIndex));
}
//Helper function that returns the appropriate value given the type of leaderboard requested
function valueGetter(type, member) {
if (type === "points") {
return userMap.points(member);
} else if (type === "score") {
return userMap.score(member);
} else {
console.log("leaderBoard error: valueGetter given incorrect type argument");
process.exit(1);
}
}