-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
118 lines (97 loc) · 3.37 KB
/
main.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
/* eslint no-console: ["error", { allow: ["log", "warn", "error"] }] */
/* eslint no-undef: "off" */
require("dotenv").config({path: __dirname + "/.env"});
let { Client } = require("brawlstars");
let dbname = "stats";
let refresh = 5000;
let mongodb = require("mongodb");
var MongoClient = mongodb.MongoClient;
let client = new Client(process.env["API_TOKEN"], {
cache: true, // default is true
});
var url = `mongodb://${process.env["DB_USER"]}:${process.env["DB_PWD"]}@${process.env["DB_IP"]}:${process.env["DB_PORT"]}/`;
function doesBattleAlreadyExists(log, battleTime) {
let ans = false;
log.forEach((battle) => {ans = ans || (battle.battleTime === battleTime);});
return ans;
}
function hasNew(actual, last) {
let ans = [];
actual.forEach((battle) => {if(!doesBattleAlreadyExists(last, battle.battleTime)) {ans.push(battle);} });
return ans;
}
let c = (latest, callback) => {
client.getPlayerBattlelog(process.env["PLAYER_TAG"])
.then((log) => {
let difference = hasNew(log.items, latest);
if(difference.length !== 0) {
console.log(`${difference.length} new match found !`);
callback(difference);
}
// console.log("Timeout set")
setTimeout(() => {
c(log.items, callback);
}, refresh);
})
.catch((err) => {
console.error(err);
process.exit(1);
});
};
function parseDate(str) {
return new Date(Date.parse(`${str.slice(0, 4)}-${str.slice(4, 6)}-${str.slice(6, 8)}T${str.slice(9, 11)}:${str.slice(11, 13)}:${str.slice(13, 15)}`));
}
function extractPlayer(obj) {
let source = [];
if("teams" in obj["battle"]) {
obj["battle"]["teams"].forEach((team) => {
team.forEach((player) => {source.push(player);});
});
}
if("players" in obj["battle"]) {
source = obj["battle"]["players"];
}
let ans = {};
source.forEach((player) => {
if(player.tag === process.env["PLAYER_TAG"]) {
ans = player;
}
});
return ans;
}
function start(collection) {
collection.find().sort({battleTime:-1}).limit(30).toArray().then((last) => {
c(last, (difference) => {
client.getPlayer(process.env["PLAYER_TAG"])
.then((player) => {
let tmp = [];
difference.forEach( (battle) => {
tmp.push({
...battle,
player: undefined, // add the player field to every record
epoch: parseDate(battle["battleTime"]),
extracted: {
player: extractPlayer(battle)
}
});
});
tmp[0].player = player; // add the player informations to the latest
collection.insertMany(tmp);
})
.catch((err) => {
console.error(err);
process.exit(1);
});
});
});
}
MongoClient.connect(url, function(err, db) {
if (err) {
console.log("Error : "+url+"\n" + err);
process.exit(1);
}
let statsDB = db.db(dbname);
let coll = statsDB.collection(dbname);
console.log("STARTED !");
start(coll);
});