-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
486 lines (434 loc) · 17.3 KB
/
server.js
File metadata and controls
486 lines (434 loc) · 17.3 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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
"use strict";
//Load node modules
const https = require("https");
let fs = require('fs');
const hostname = '127.0.0.1'; // - goes unused, not sure why this was included
const port = process.env.PORT || 3000; // gets the port from heroku, or defaults to port 3000
let express = require('express');
let parser = require('body-parser');
let FormData = require('form-data');
let multer = require('multer');
let upload = multer();
const axios = require('axios');
let Request = require('request');
//get config information from config.js
let config = require("./config");
//set config data to local variables to save time editing code
const steamKey = config.getConfig().steamKey;
const ballchasingKey = config.getConfig().ballchasingKey;
const password = config.getConfig().password;
//create express app
let app = express();
app.use(parser.urlencoded({ extended: true }));
app.use(parser.json({type:'application/json'}));
app.use(upload.single('myFile'));
app.use(express['static'](__dirname));
//handshake with the ballchasing api, used to test api key, not used in the main application
app.get('/chase', function(req,res){
https.get({host:'ballchasing.com',path:'/api/',headers:{Authorization: ballchasingKey}},(res2)=>{
//console.log(res2);
let data;
res2.on('data',(chunk)=>{
data = chunk;
});
res2.on('end', ()=>{
//console.log(data);
res.send(data);
});
});
});
//grabs steam data for all players
app.get('/test', function(req,res){
let result = [];
let players;
fs.readFile('./players.json', (err,data) => {
players = JSON.parse(data.toString());
//console.log(players);
//res.send({result: JSON.parse(data.toString())});
let i = 0;
for(let p of players["0"]){
//console.log(i++);
// grab the recent playtimes of all the players
https.get(`https://api.steampowered.com/IPlayerService/GetRecentlyPlayedGames/v0001/?key=${steamKey}&steamid=${p}&format=json`, (res2) => {
let data = "";
res2.on('data', (chunk) => {
data += chunk;
});
res2.on('end', () => {
// grab the usernames (in addition to all the other juicy steam data) of all the players
https.get(`https://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=${steamKey}&steamids=${p}`, (res3) => {
let data2 = "";
res3.on('data', (chunk2) => {
data2 += chunk2;
});
res3.on('end', () => {
let obj = {};
//try{
// package player up into an object, add the player object to the result
obj = {
user: JSON.parse(data2),
recent: JSON.parse(data),
stats: `https://rocketleague.tracker.network/profile/steam/${p}`
};
/*}
catch(exc){console.log(exc);}*/
//res.send(obj);
result.push(obj);
// if the result length equals the number of players, send the result back to the application
if(result.length == players["0"].length){
res.send({result: result});
}
});
}).on('error', (err2) => {
res.send({result: 'error'});
});
});
}).on('error', (err) => {
res.send({result: 'error'});
});
}
});
});
//adds a new player to the application, requires the password from config
app.post('/admin', (req,res) => {
let dataIn = req.body;
let currPlayers = null;
//console.log(dataIn);
if(dataIn["access"] != password){
res.status(403).send({result:"forbidden"});
}
else{
fs.readFile('./players.json', (err,data) => {
currPlayers = JSON.parse(data.toString());
if(currPlayers != null){
currPlayers["0"].push(dataIn["0"]);
}
fs.writeFile('./players.json',JSON.stringify(currPlayers), (err) => {
if(err)res.status(500).send({result:"error writing players.json"});
});
//update stats.json
fs.readFile('./stats.json',(err,data)=>{
let stats = JSON.parse(data.toString());
//find new players
let newPlayers = [];
for(let id of currPlayers["0"]){
let isNew = true;
for(let stat of stats["0"]){
if(id == stat["id"]){
isNew = false;
break;
}
}
if(isNew){
newPlayers.push(id);
}
}
//add new players to stats.json
let count = 0;
for(let p of newPlayers){
let steamData = null;//await steamFetch(p);
Request(`https://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=${steamKey}&steamids=${p}`,(err,res,body)=>{
if(err) {/*console.log(err);*/return;}
steamData = JSON.parse(body);
let obj = {
id: p,
name: steamData["response"]["players"]["0"]["personaname"],
goals:0,
assists:0,
saves:0,
shots:0,
demos:0,
demoed:0,
games:0,
division:0,
defense_time:0,
neutral_time:0,
offense_time:0
}
stats["0"].push(obj);
count++;
if(count == newPlayers.length)
{
fs.writeFile('./stats.json', JSON.stringify(stats),(err)=>{
if(err){
//console.log(err);
}
});
}
});
}
});
res.send(currPlayers);
});
}
});
//some error occured with this, it goes unused anyway
function steamFetch(id){
return new Promise((resolve,reject)=>{
Request(`https://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=${steamKey}&steamids=${id}`,(err,res,body)=>{
if(err) reject(err);
resolve(body);
});
});
}
//uploads a replay file to ballchasing.com
app.post('/ball', (req,res)=>{
let dataIn = req.body;
//let f = new FormData(dataIn);
//console.log(req.file.originalname);
// saves the uploaded replay file from the application locally (temporarily)
fs.writeFile('./temp/' + req.file.originalname,req.file.buffer,(err)=>{
if(err){
//console.log(err);
return;
}
let post = Request.post('https://ballchasing.com/api/v2/upload', (err,resp,body)=>{
if(err){
//console.log(err);
res.send(err);
}
else{
//console.log(body);
let b = JSON.parse(body);
stealStats(b["id"]);
res.send(body);
}
// deletes the temporary file after the upload to ballchasing is finished
fs.unlink('./temp/' + req.file.originalname,(err)=>{
//console.log(err);
});
});
post.setHeader('Authorization', ballchasingKey);
let form = post.form();
form.append('file',fs.createReadStream('./temp/' + req.file.originalname));
});
});
// allows the data to be updated/changed for any player, requires password
app.post('/update', (req,res)=>{
let dataIn = req.body;
//console.log(dataIn);
if(dataIn["1"] != password){
res.send("forbidden");
return;
}
fs.readFile('./stats.json',(err,data)=>{
let stats = JSON.parse(data.toString());
for(let x of dataIn["0"]){
//console.log(x["id"]);
for(let y of stats["0"]){
//console.log(y["id"]);
if(x["id"] == y["id"]){
//console.log("match");
if(x["type"] == "change"){
//console.log(y[x["stat"]]);
y[x["stat"]] = x["value"];
}
else if(x["type"] == "add"){
y[x["stat"]] += x["value"];
}
break;
}
}
}
fs.writeFile('./stats.json', JSON.stringify(stats), (err)=>{
if(err){/*console.log(err);*/}
res.send("done");
});
});
});
// removes a player from the app, requires password
app.delete('/admin', (req,res) => {
let dataIn = req.body;
let currPlayers = null;
//console.log(dataIn);
if(dataIn["access"] != password){
res.status(403).send({result:"forbidden"});
}
else{
fs.readFile('./players.json', (err,data) => {
if(err){/*console.log(err);*/return;}
currPlayers = JSON.parse(data.toString());
if(currPlayers != null){
for(let i = 0; i < currPlayers["0"].length; i++){
if(currPlayers["0"][i] == dataIn["0"]){
currPlayers["0"].splice(i,1);
break;
}
}
}
fs.writeFile('./players.json',JSON.stringify(currPlayers), (err) => {
if(err)res.status(500).send({result:"error writing players.json"});
});
res.send(currPlayers);
});
fs.readFile('./stats.json',(err,data)=>{
if(err){/*console.log(err);*/return;}
let stats = JSON.parse(data.toString());
for(let i = 0; i < stats["0"].length; i++){
if(stats["0"][i]["id"] == dataIn["0"]){
stats["0"].splice(i,1);
break;
}
}
fs.writeFile('./stats.json',JSON.stringify(stats),(err)=>{
if(err){/*console.log(err);*/}
});
});
}
});
// update to allow user to send an ID to steal from
app.post('/steal',(req,res)=>{
let dataIn = req.body;
stealStats(dataIn['0']);//stealStats('642be93c-5f97-4592-8da5-189f85a0a352');
res.send({"res":"done :)"});
});
//grab stats from ballchasers based on the given replay id (arrives as a csv)
function stealStats(id){
fs.readFile('./replays.json',(err,data)=>{
if(err){/*console.log(err);*/return;}
let replays = JSON.parse(data.toString());
for(let r of replays["0"]){
if(id == r){
//console.log("replay is a duplicate");
return;
}
}
replays["0"].push(id);
fs.writeFile('./replays.json',JSON.stringify(replays),(err)=>{
if(err){/*console.log(err);*/return;}
doSteal();
});
});
function doSteal(){
let toFile = fs.createWriteStream('./temp/' + `${id}-players.csv`);
Request(`https://ballchasing.com/dl/stats/players/${id}/${id}-players.csv`).pipe(toFile).on('close', () => {
//console.log('done stealing data :) from: ' + `https://ballchasing.com/dl/stats/players/${id}/${id}-players.csv`);
fs.readFile('./temp/' + `${id}-players.csv`,(err,data)=>{
if(err){
//console.log(err);
return;
}
else{
if(data.toString().includes('DOCTYPE html')){
//console.log('bad file, link redirected :(');
//return;
}
else{
//replace all semicolons with commas like a csv is supposed to have :////// (nvm doesnt really matter lol)
let stringyData = data.toString();
//stringyData = stringyData.replace(/;/g,",");
//console.log(stringyData);
let parsed = [];
let rows = stringyData.split('\n');
//console.log("row count: "+rows.length);
/*for(let r of rows[0].split(',')){
console.log(r);
//console.log('\n');
}*/
for(let i = 0; i < rows.length; i++){
parsed.push([]);
}
//console.log("parsed rows: " + parsed.length);
for(let i = 0; i < rows.length; i++){
let split = rows[i].split(';');
for(let j = 0; j < split.length; j++){
parsed[i].push(split[j]);
//console.log("parsed col: " + parsed[i].length);
}
}
//testing parsed data
for(let i = 0; i < parsed.length; i++){
let str = "";
for(let j = 0; j < parsed[i].length; j++){
str += parsed[i][j] + " | ";
}
//console.log(str);
}
fs.readFile('./stats.json', (err,data)=>{
if(err){
//console.log(err);
return;
}
let stats = JSON.parse(data.toString());
for(let i = 1; i < parsed.length; i++){
for(let s of stats["0"]){
if(parsed[i][1] == s["name"]){
s["goals"] += parseInt(parsed[i][5]);
s["assists"] += parseInt(parsed[i][6]);
s["saves"] += parseInt(parsed[i][7]);
s["shots"] += parseInt(parsed[i][8]);
s["demos"] += parseInt(parsed[i][43]);
s["demoed"] += parseInt(parsed[i][44]);
s["games"] ++;
s["defense_time"] += parseFloat(parsed[i][40]);
s["neutral_time"] += parseFloat(parsed[i][41]);
s["offense_time"] += parseFloat(parsed[i][42]);
}
}
}
fs.writeFile('./stats.json',JSON.stringify(stats),(err)=>{
if(err){
// console.log(err);
}
});
});
}
fs.unlink('./temp/' + `${id}-players.csv`,(err)=>{
if(err){/*console.log(err);*/}
});
}
})
});
}
}
//removes all files from the temp folder
function cleanTemp(){
fs.readdir('./temp',(err,files)=>{
files.forEach(file => {
//fs.unlink(file)
//console.log(file);
});
});
}
//sends all player stats to the application
app.get('/stats',(req,res)=>{
fs.readFile('./stats.json',(err,data)=>{
if(err){
res.send({"res":"err"});
return;
}
let stats = JSON.parse(data.toString());
res.send(stats);
});
});
//sends the gif your game IDs to the app
app.get('/clips',(req,res)=>{
fs.readFile('./clips.json',(err,data)=>{
if(err){
res.send({"res":"err"});
return;
}
let clips = JSON.parse(data.toString());
res.send(clips);
});
});
//adds a new clip ID
app.post('/clips', (req,res)=>{
let dataIn = req.body;
//console.log(dataIn);
if(dataIn["1"] != password){
res.send("forbidden");
return;
}
fs.readFile('./clips.json',(err,data)=>{
let clips = JSON.parse(data.toString());
clips["0"].push(dataIn["0"]);
fs.writeFile('./clips.json', JSON.stringify(clips), (err)=>{
if(err){/*console.log(err);*/}
res.send("done");
});
});
});
//start server
app.listen(port);
//console.log(`Server started on port ${port}`);