forked from kmoe/slack-pokebot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetrics.js
74 lines (62 loc) · 1.72 KB
/
metrics.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
'use strict';
const fs = require('fs');
const _ = require('lodash');
const dataFilePath = process.env.PGO_DATA || '.data';
const InitalSenstivity = process.env.PGO_METRIC_INITAL_SENSITIVITY || 1500;
const SensitivityDecrease = process.env.PGO_METRIC_SENSITIVITY_DECREACE || 0.75;
const SensitivityIncrease = process.env.PGO_METRIC_SENSITIVITY_INCREACE || 1.0;
function loadDataSet() {
try {
return JSON.parse(fs.readFileSync(dataFilePath, 'utf8'));
} catch (e) {
return {};
}
}
const dataset = loadDataSet();
let saveTimeoutId;
function saveDataSet() {
saveTimeoutId = null;
fs.writeFile(dataFilePath, JSON.stringify(dataset));
}
function triggerSaveDataSet() {
if (!saveTimeoutId) {
saveTimeoutId = setTimeout(saveDataSet, 1000);
}
}
function loadRarityData() {
try {
return JSON.parse(fs.readFileSync('./rarity.json', 'utf8'));
} catch (e) {
return {};
}
}
const rarity = loadRarityData();
function updateSensitivity(item) {
const newItem = _.clone(item);
const timeDelta = Date.now() - item.lastSeen;
newItem.sensitivity += timeDelta / ((1000 * 60) * SensitivityIncrease);
return newItem;
}
function shouldReport(p) {
const newP = _.clone(p);
const name = p.pokemon.name;
let result = null;
newP.rarity = rarity[p.pokemon.id] || 'common';
if (!dataset[name]) {
dataset[name] = { sensitivity: InitalSenstivity, lastSeen: Date.now() };
triggerSaveDataSet();
result = newP;
} else {
const newItem = updateSensitivity(dataset[name]);
if (newItem.sensitivity < p.distance) {
newItem.sensitivity *= SensitivityDecrease;
dataset[name] = newItem;
triggerSaveDataSet();
result = newP;
}
}
return result;
}
module.exports = {
shouldReport,
};