-
Notifications
You must be signed in to change notification settings - Fork 5
/
update.js
119 lines (109 loc) · 3.19 KB
/
update.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
const fs = require('fs');
const moment = require('moment');
const db = require('./database');
const scraper = require('./scraper');
let maxTime;
let currentPvpZone;
function handleKill(data) {
if (data.unixTime > maxTime) {
console.log('New kill detected:');
console.log(data);
maxTime = data.unixTime;
db.putKill(data, true);
db.updateScumUser(data.killerId, data.killerName);
db.updateScumUser(data.victimId, data.victimName);
}
}
function handleLogin(data) {
// No timestamp comparison for logins. This means we will have redundant
// database updates but it's not a huge deal.
db.updateScumUser(data.steamId, data.name);
}
// Don't put massive inputs into this. For lambda > 100 or so, a normal
// approximation will be more efficient.
function poissonRandomNumber(lambda) {
const L = Math.exp(-lambda);
let k = 0;
let p = 1;
do {
++k;
p *= Math.random();
} while (p > L);
return k - 1;
}
function writeRankDataToFile(ranks) {
console.log('Writing ranks to file.');
const json = JSON.stringify({
pvpZone: currentPvpZone,
ranks,
timestamp: moment().unix(),
});
fs.writeFile('public/update.json', json, 'utf8', () => {
console.log('Wrote ranks to file.');
const delay = 2 + poissonRandomNumber(61);
console.log(`Scheduling next update for ${delay}m...`);
console.log('Current time: ' + moment());
setTimeout(update, delay * 60 * 1000);
});
}
function updateScumEvent(callback) {
console.log('Checking for a running event.');
db.getAllScumPlaces((places) => {
db.getRecentScumEvents(24 * 3600, (todayEvents) => {
if (todayEvents.length > 0) {
console.log('Event found. Getting event details.');
const placeId = todayEvents[0];
places.forEach((place) => {
if (place.id === placeId) {
console.log('Event:', place);
currentPvpZone = place;
callback();
}
});
} else {
console.log('No event found. Creating new event.');
db.getRecentScumEvents(30 * 24 * 3600, (monthEvents) => {
const eligibleLocations = [];
places.forEach((place) => {
if (monthEvents.indexOf(place.id) < 0) {
eligibleLocations.push(place);
}
});
const randomIndex = Math.floor(Math.random() *
eligibleLocations.length);
const randomPlace = eligibleLocations[randomIndex];
db.createScumEvent(randomPlace.id, () => {
console.log('Event:', randomPlace);
currentPvpZone = randomPlace;
callback();
});
});
}
});
});
}
function updateKills() {
console.log('Current time: ' + moment());
console.log('Getting latest known timestamp from the database...');
db.getMaxKillTimestamp((newMaxTime) => {
maxTime = newMaxTime;
console.log('maxTime:', maxTime);
if (maxTime <= 0) {
throw 'There is a problem with the max timestamp in the database.';
}
scraper.downloadLatestKillLog(true, handleKill, handleLogin, () => {
console.log('Done updating DB. Calculating ranks.');
// Wait a bit before querying the database because some writes may
// still be finishing.
setTimeout(() => {
db.calculateRankings(30 * 86400, writeRankDataToFile);
}, 2000);
});
});
}
function update() {
updateScumEvent(() => {
updateKills();
});
}
update();