-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathperiod.js
More file actions
executable file
·122 lines (100 loc) · 3.75 KB
/
Copy pathperiod.js
File metadata and controls
executable file
·122 lines (100 loc) · 3.75 KB
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
120
121
122
#!/usr/bin/env node
var Count = require('./lib/count.js')
, findAccount = require('./lib/findAccount.js')
, fs = require('fs')
, logger = require('./lib/logger.js')
, missing = require('./lib/missing.js')
, path = require('path')
, program = require('commander')
, session = require('./lib/session.js')
, util = require('util')
, wotblitz = require('wotblitz')()
program
.option('-u, --username <name>', 'attempts to return win rate based on username', s => s.toLowerCase())
.option('-a, --account <account_id>', 'blitz account_id to calculate; otherwise uses the session value', Number)
.option('-s, --start', 'start session by saving data')
.parse(process.argv)
program.start = program.start || !!program.username || !!program.account
var file = path.resolve('./wotblitz-period.json')
var readFile = util.promisify(fs.readFile)
var writeFile = util.promisify(fs.writeFile)
var sess_p = session.load()
var account_id_p = null
if (program.account) {
account_id_p = Promise.resolve({account_id: program.account})
} else if (program.username) {
account_id_p = findAccount(program.username)
} else {
account_id_p = sess_p
}
var currentStats_p = account_id_p.then(({account_id}) => wotblitz.tanks.stats(account_id, null, null, null, [
'all.battles',
'all.losses',
'all.wins',
'last_battle_time',
'tank_id'
]))
if (program.start) {
var writeStats_p = currentStats_p.then(stats => writeFile(file, JSON.stringify(stats), 'utf8'))
var updateSession_p = null
if (account_id_p !== sess_p) {
updateSession_p = Promise.all([account_id_p, sess_p]).then(([{account_id}, sess]) => {
sess.account_id = account_id
return sess.save()
})
}
Promise.all([writeStats_p, updateSession_p])
.then(() => console.log('Success: started new session.'))
.catch(logger.error)
} else {
var fields = ['name', 'nation', 'tier', 'type']
Promise.all([
wotblitz.encyclopedia.vehicles(null, null, fields).then(vehicles => missing(vehicles, fields)),
readFile(file, 'utf8').then(data => JSON.parse(data)),
currentStats_p,
// always `sess_p` here - using the `account_id_p` name for clarity
account_id_p
]).then(([vehicles, _previousStats, _currentStats, {account_id}]) => {
var previousStats = _previousStats[account_id]
var currentStats = _currentStats[account_id]
var compareCategories = field => {
var createCounts = (counts, {all, tank_id}) => {
var key = vehicles[tank_id][field]
if (!(key in counts)) counts[key] = new Count()
counts[key].add(all)
return counts
}
var previousTotal = previousStats.reduce(createCounts, {})
var currentTotal = currentStats.reduce(createCounts, {})
for (var k in currentTotal) {
currentTotal[k] = currentTotal[k].difference(previousTotal[k] || new Count())
}
return currentTotal
}
var previousGlobal = previousStats.reduce((count, tank) => count.add(tank.all), new Count())
var currentGlobal = currentStats.reduce((count, tank) => count.add(tank.all), new Count())
var result = {
global: currentGlobal.difference(previousGlobal),
nations: compareCategories('nation'),
tiers: compareCategories('tier'),
types: compareCategories('type')
};
result.tanks = currentStats
.sort((a, b) => b.last_battle_time - a.last_battle_time)
.map(({all, tank_id}) => {
var previousCount = new Count();
var currentCount = new Count();
var previous = previousStats.find(t => t.tank_id === tank_id)
currentCount.add(all)
if (previous) previousCount.add(previous.all)
return {tank_id: tank_id, count: currentCount.difference(previousCount)}
})
.filter(record => record.count.battles > 0)
.reduce((tanks, record) => {
var name = vehicles[record.tank_id].name
tanks[name] = record.count
return tanks
}, {})
return result
}).then(logger.write, logger.error)
}