-
Notifications
You must be signed in to change notification settings - Fork 0
/
crawl-kills.js
65 lines (60 loc) · 1.97 KB
/
crawl-kills.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
const fetch = require('./fetch');
const recentlySeenKillIds = [];
let delayBetweenCrawls = 7777;
const targetNewKillsPerCrawl = 20;
let isFirstCrawl = true;
function NewKillDetected(kill) {
const columns = [
kill.id,
kill.attacker.id,
kill.attacker.position.x.toString(),
kill.attacker.position.y.toString(),
kill.attacker.position.z.toString(),
kill.victim.id,
kill.victim.position.x.toString(),
kill.victim.position.y.toString(),
kill.victim.position.z.toString(),
];
const csv = columns.join(',');
console.log(csv);
}
async function DoOneCrawlIteration() {
const url = 'https://rusticated.com/api/v2/kills?serverId=us-2x-monthly-large&limit=100';
const textResponse = await fetch(url);
const jsonResponse = JSON.parse(textResponse);
const results = jsonResponse.results;
let newKillCount = 0;
for (const result of results) {
if (!recentlySeenKillIds.includes(result.id)) {
recentlySeenKillIds.push(result.id);
NewKillDetected(result);
newKillCount++;
}
}
//console.log('detected', newKillCount, 'kills in', delayBetweenCrawls, 'ms');
if (newKillCount === 0) {
newKillCount++;
}
while (recentlySeenKillIds.length > 200) {
recentlySeenKillIds.shift();
}
// Controller for the pace of the crawl.
if (!isFirstCrawl) {
const extrapolatedDelayTarget = targetNewKillsPerCrawl * delayBetweenCrawls / newKillCount;
const fullDiff = extrapolatedDelayTarget - delayBetweenCrawls;
const carefulDiff = 0.1 * fullDiff;
const maxDiff = 0.1 * delayBetweenCrawls;
const minDiff = -0.5 * delayBetweenCrawls;
const clippedDiff = Math.min(maxDiff, Math.max(minDiff, carefulDiff));
delayBetweenCrawls += clippedDiff;
const maxDelay = 600 * 1000;
const minDelay = 1000;
delayBetweenCrawls = Math.min(maxDelay, Math.max(minDelay, Math.round(delayBetweenCrawls)));
}
isFirstCrawl = false;
setTimeout(DoOneCrawlIteration, delayBetweenCrawls);
}
async function Main() {
await DoOneCrawlIteration();
}
Main();