-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwr.js
More file actions
executable file
·159 lines (134 loc) · 4.74 KB
/
Copy pathwr.js
File metadata and controls
executable file
·159 lines (134 loc) · 4.74 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!/usr/bin/env node
var Count = require('./lib/count.js')
, findAccount = require('./lib/findAccount.js')
, logger = require('./lib/logger.js')
, missing = require('./lib/missing.js')
, program = require('commander')
, session = require('./lib/session.js')
, types = require('./lib/types.js')
, 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('-p, --premium', 'diplay premium tanks')
.option('-r, --regular', 'diplay regular tanks')
.option('-l, --lesser <percentage>', 'display tanks with win rate less than the given value', percentageType)
.option('-g, --greater <percentage>', 'display tanks with win rate greater than the given value', percentageType)
.option('-t, --tiers <number>', 'display tanks from the given tiers', types.numbers, [])
.option('-T, --types <vehicletype>', 'display tanks with the given vehicle type', vehicleType, [])
.option('-n, --nations <name>', 'display only a given nation', nationsType, [])
.option('-s, --streak <percentage>', 'get a win streak count to reach a given percentage', percentageType)
.parse(process.argv)
if (program.premium && program.regular) {
console.error()
console.error(" `--premium' and `--regular' are mutually exclusive options")
console.error()
process.exit(1)
}
if (program.lesser && program.greater) {
console.error()
console.error(" `--lesser' and `--greater' are mutually exclusive options")
console.error()
process.exit(1)
}
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 = session.load()
}
account_id_p.then(({account_id}) => {
var fields = ['is_premium', 'name', 'tier', 'type']
var filter = program.nations.length > 0 ? ({nation}) => { return program.nations.indexOf(nation) > -1 } : null
return Promise.all([
wotblitz.encyclopedia.vehicles(null, program.nations, fields)
.then(vehicles => missing(vehicles, fields, filter)),
wotblitz.tanks.stats(account_id, null, null, null, ['all.battles', 'all.losses', 'all.wins', 'tank_id'])
.then(stats => stats[account_id])
])
}).then(([vehicles, stats]) => {
var results = stats
.map(({tank_id: id, all}) => {
var tank = vehicles[id] || {}
var {wins, losses, battles} = all
return {
drawn: (battles - wins - losses) / battles,
lost: losses / battles,
won: wins / battles,
battles: battles,
losses: losses,
wins: wins,
is_premium: tank.is_premium,
name: tank.name,
tier: tank.tier,
type: tank.type
}
})
.filter(wr => wr.name &&
(!program.premium || wr.is_premium) &&
(!program.regular || !wr.is_premium) &&
(!program.lesser || wr.won < program.lesser) &&
(!program.greater || wr.won > program.greater) &&
(program.tiers.length === 0 || program.tiers.indexOf(wr.tier) > -1) &&
(program.types.length === 0 || program.types.indexOf(wr.type) > -1)
)
.sort((a, b) => a.won - b.won)
var overall = new Count()
for (var wr of results) {
console.log('Name: %s Tier: %d Type: %s', wr.name, wr.tier, wr.type)
console.log(' Win Rate: %s%%', (wr.won * 100).toFixed(2))
console.log(' Loss Rate: %s%%', (wr.lost * 100).toFixed(2))
console.log(' Draw Rate: %s%%', (wr.drawn * 100).toFixed(2))
if (program.streak) {
console.log(' Win Streak: %d', winStreakToReach(program.streak, wr))
}
overall.add(wr)
}
console.log('-------- Overall --------')
console.log(' Win Rate: %s%%', (overall.wins / overall.battles * 100).toFixed(2))
console.log(' Loss Rate: %s%%', (overall.losses / overall.battles * 100).toFixed(2))
console.log(' Draw Rate: %s%%',
((overall.battles - overall.wins - overall.losses) / overall.battles * 100).toFixed(2)
)
if (program.streak) {
console.log(' Win Streak: %d', winStreakToReach(program.streak, overall))
}
}).catch(logger.error)
function percentageType(val) {
return Number(val) / 100
}
function vehicleType(val, memo) {
if (!memo) memo = []
switch (val.toLowerCase()) {
case 'light':
case 'l':
memo.push('lightTank')
break
case 'medium':
case 'm':
memo.push('mediumTank')
break
case 'heavy':
case 'h':
memo.push('heavyTank')
break
case 'destroyer':
case 'td':
case 'tank destroyer':
memo.push('AT-SPG')
break
default:
throw new Error('Unknown vehicle type')
}
return memo
}
function nationsType(val, memo) {
if (!memo) memo = []
memo.push(val.toLowerCase())
return memo
}
function winStreakToReach(percentage, {wins, battles}) {
return Math.round((percentage * battles - wins) / (1 - percentage))
}