-
Notifications
You must be signed in to change notification settings - Fork 0
/
fnstats.js
201 lines (169 loc) · 6.05 KB
/
fnstats.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
let DEBUG = false
var request = require("request");
var filter = require("./channel_filter.js");
const low = require('lowdb');
const FileSync = require('lowdb/adapters/FileSync');
const adapter = new FileSync(".db.json");
const db = low(adapter);
var guildid = "";
if (DEBUG) {
guildid = "339633193860988929";
} else {
guildid = "714045813763866624";
}
var congo = null;
db.defaults({})
.write();
var url = "https://api.fortnitetracker.com/v1/profile/";
var intro_text = ["FORTNITE VICTORY ROYALE",
"BATTLE ROYALE CHAMPI0NS",
"FORTNITE CHAMPIONS",
"WINNER, WINNER, CHICKEN DINNER",
"THESE ARE YOUR FORNITE CHAMPIONS",
"HERE YE, WINNERS",
"VICTORY AHOY"];
var url_list = ["https://gamepedia.cursecdn.com/fortnite_gamepedia/1/1d/Victory_royale_2.png?version=bf0cfeff5521ff6c83245314e9efe2ce",
"https://www.fortwiz.com/assets/emojis/number1.png",
"https://www.pinclipart.com/picdir/big/416-4162455_victory-royale-png-graphic-design-clipart.png"];
var sub_text = ["Awww yuis! These *mutha truckas* won a battle royale recently!",
"Guess who won? won again? These winners are back, tell a friend:",
"Here ye, friends of ol' for you are graced by champions below!",
"All y0uR Ba53 B310n6 +0 u5!",
"See winning ain't easy, but these guys right here... It comes easy."]
var userList = {};
var discordClient = null;
var secondLoop = Boolean(false);
let winners = [];
const sleep = milliseconds => {
return new Promise(resolve => setTimeout(resolve, milliseconds));
};
function randRange(min, max) {
return Math.floor(Math.random() * (max - min) + min);
}
function lookup(user, timeout) {
return new Promise((resolve, reject) => {
sleep(timeout).then(() => {
// console.log("[FNSTATS] lookup requesting for - " + user);
request(
{
url: url + user,
headers: {
"TRN-Api-Key": process.env.FNTRACK_TOKEN,
}
},
function(error, response, body) {
if(error) {
console.log("Hmm something went wrong querying api..." + error);
return resolve(0);
}
let dataParsed = null;
var total = 0;
try {
dataParsed = JSON.parse(body);
//all wins
let target = dataParsed.lifeTimeStats.find( (stat) => {
if(stat.key === "Wins") {
return true;
}
});
total = target.value;
} catch(err) {
console.log(err);
console.log("Failed to parse json body for fnstats");
console.log(body);
return resolve(0);
};
// if (!(dataParsed.epicUserHandle in userList)) {
if(!db.has(dataParsed.epicUserHandle).value()) {
// user hasn't been looked up
console.log(
`[FNSTATS] New user - ${dataParsed.epicUserHandle} with score: ${total}`
);
db.set(dataParsed.epicUserHandle, total)
.write();
// userList[dataParsed.epicUserHandle] = total;
resolve(0);
} else {
//user exists, check if value changed
cur_val = db.get(dataParsed.epicUserHandle).value()
if(cur_val < total) {
// if (userList[dataParsed.epicUserHandle] != total) {
//Wins detected!
console.log(
`[FNSTATS User ${dataParsed.epicUserHandle} has new score with: ${total}`
);
// userList[dataParsed.epicUserHandle] = total;
resolve(dataParsed.epicUserHandle);
} else {
console.log("[FNSTATS] No recent wins for " + dataParsed.epicUserHandle + " " + cur_val + "/" + total);
resolve(0);
}
db.set(dataParsed.epicUserHandle, total)
.write();
}
}
);
});
});
}
function sendMessage(message) {
discordClient.guilds.cache.find(val => {
if(val.id === guildid) {
val.channels.cache.find(chanVal => {
if(chanVal.name === 'general') {
let channel = discordClient.channels.cache.get(chanVal.id);
channel.send(`${message}`);
return;
}
});
}
});
}
function iterateUserList() {
// console.log("[FNSTATS] Iterating...");
let tokens = process.env.FNTRACK_QUERY.split(",");
let lookupPromises = [];
for (let i = 0; i < tokens.length; i++) {
lookupPromises.push(lookup(tokens[i], 3000 * i));
}
Promise.all(lookupPromises).then(values => {
for (let i = 0; i < values.length; i++) {
if (values[i] != 0) {
console.log("Entry Approved: " + values[i]);
winners.push(values[i]);
}
}
if (winners.length > 0 ) {
if(secondLoop) {
// We've checked our list twice for more winners, send message
console.log("[FNSTATS] Sending message...")
// let joined = winners.join(' !! ');
let introChoice = randRange(0, intro_text.length);
let urlChoice = randRange(0, url_list.length);
let subChoice = randRange(0, sub_text.length);
raw_opt = [];
for(let i=0; i < winners.length; i++) {
raw_opt.push({ "value" : winners[i]});
}
args = { "real": true };
congo.vanilla("wzwin", args, raw_opt, discordClient, guildid, 'general');
// sendMessage(
// `**${intro_text[introChoice]}!**\n${url_list[urlChoice]}\n\n${sub_text[subChoice]}\n\`${joined}\``
// );
//Reset since we sent
secondLoop = Boolean(false);
winners = [];
} else {
console.log("[FNSTATS] There's a winner, requeuing...")
secondLoop = Boolean(true);
}
}
});
}
function execute(client, congo_cpy) {
discordClient = client;
congo = congo_cpy
iterateUserList();
setInterval(iterateUserList, 300000);
}
module.exports.execute = execute;