-
Notifications
You must be signed in to change notification settings - Fork 0
/
sort-wr-period.js
executable file
·53 lines (43 loc) · 1.14 KB
/
sort-wr-period.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
#!/usr/bin/env node
var concat = require('concat-stream');
var logger = require('./lib/logger.js');
process.exitCode = 0;
process.stdin.pipe(concat(buf => {
var period;
var tanks;
if (buf.length < 2) {
process.exitCode = 1;
return logger.error('sort-wr-period: input not reconized');
}
try {
period = JSON.parse(buf.toString());
} catch (e) {
process.exitCode = 2;
e.code = 'EJSON';
return logger.error(e);
}
if ('tanks' in period) {
tanks = period.tanks;
} else if (Object.keys(period).length > 0) {
tanks = period;
} else {
process.exitCode = 1;
return logger.error('sort-wr-period: input not reconized');
}
var result = {};
var ordered = Object.entries(tanks).sort(([, a], [, b]) => {
var A = a.wins / a.battles;
var B = b.wins / b.battles;
var direction = 1;
if (A < B) return 1;
if (A > B) return -1;
if (Math.round(A * 1000) < 501) direction = -1; // invert sort for tanks with less than 50.05%
if (a.battles < b.battles) return 1 * direction;
if (a.battles > b.battles) return -1 * direction;
return 0;
});
for (var [key, value] of ordered) {
result[key] = value;
}
logger.write(result);
}));