-
-
Notifications
You must be signed in to change notification settings - Fork 110
/
getPlayerStats.ts
262 lines (245 loc) · 8.03 KB
/
getPlayerStats.ts
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
import { stringify } from 'querystring'
import { HLTVConfig } from '../config'
import { HLTVScraper } from '../scraper'
import { BestOfFilter } from '../shared/BestOfFilter'
import { Country } from '../shared/Country'
import { fromMapSlug, GameMap, toMapFilter } from '../shared/GameMap'
import { MatchType } from '../shared/MatchType'
import { RankingFilter } from '../shared/RankingFilter'
import { Team } from '../shared/Team'
import { fetchPage, generateRandomSuffix, getIdAt, parseNumber } from '../utils'
export interface PlayerStatsMatch {
date: number
team1: Team
team2: Team
map: GameMap
kills: number
deaths: number
rating: number
mapStatsId: number
}
export interface FullPlayerStats {
id: number
name?: string
ign: string
image?: string
age?: number
country: Country
team?: Team
matches: PlayerStatsMatch[]
overviewStatistics: {
kills: number
headshots: number
deaths: number
kdRatio: number
damagePerRound?: number
grenadeDamagePerRound?: number
mapsPlayed: number
roundsPlayed: number
killsPerRound: number
assistsPerRound: number
deathsPerRound: number
savedByTeammatePerRound?: number
savedTeammatesPerRound?: number
rating1?: number
rating2?: number
}
individualStatistics: {
roundsWithKills: number
zeroKillRounds: number
oneKillRounds: number
twoKillRounds: number
threeKillRounds: number
fourKillRounds: number
fiveKillRounds: number
openingKills: number
openingDeaths: number
openingKillRatio: number
openingKillRating: number
teamWinPercentAfterFirstKill: number
firstKillInWonRounds: number
rifleKills: number
sniperKills: number
smgKills: number
pistolKills: number
grenadeKills: number
otherKills: number
}
}
export interface GetPlayerStatsArguments {
id: number
startDate?: string
endDate?: string
matchType?: MatchType
rankingFilter?: RankingFilter
maps?: GameMap[]
bestOfX?: BestOfFilter
eventIds?: number[]
}
export const getPlayerStats =
(config: HLTVConfig) =>
async (options: GetPlayerStatsArguments): Promise<FullPlayerStats> => {
const query = stringify({
...(options.startDate ? { startDate: options.startDate } : {}),
...(options.endDate ? { endDate: options.endDate } : {}),
...(options.matchType ? { matchType: options.matchType } : {}),
...(options.rankingFilter
? { rankingFilter: options.rankingFilter }
: {}),
...(options.maps ? { maps: options.maps.map(toMapFilter) } : {}),
...(options.bestOfX ? { bestOfX: options.bestOfX } : {}),
...(options.eventIds ? { event: options.eventIds } : {})
})
const [$, i$, m$] = await Promise.all([
fetchPage(
`https://www.hltv.org/stats/players/${
options.id
}/${generateRandomSuffix()}?${query}`,
config.loadPage
).then(HLTVScraper),
fetchPage(
`https://www.hltv.org/stats/players/individual/${
options.id
}/${generateRandomSuffix()}?${query}`,
config.loadPage
).then(HLTVScraper),
fetchPage(
`https://www.hltv.org/stats/players/matches/${
options.id
}/${generateRandomSuffix()}?${query}`,
config.loadPage
).then(HLTVScraper)
])
const nameText = $('.summaryRealname div').text()
const name = nameText === '-' ? undefined : nameText
const ign = $('.context-item-name').text()
const imageUrl =
$('.summaryBodyshot').attr('src') || $('.summarySquare').attr('src')
const image = imageUrl.includes('bodyshot/unknown.png')
? undefined
: imageUrl
const age = $('.summaryPlayerAge').textThen((x) =>
parseNumber(x.split(' ')[0])
)
const country = {
name: $('.summaryRealname .flag').attr('title')!,
code: $('.summaryRealname .flag').attrThen(
'src',
(x) => x.split('/').pop()?.split('.')[0]!
)
}
const team =
$('.SummaryTeamname').text() !== 'No team'
? {
name: $('.SummaryTeamname a').text(),
id: $('.SummaryTeamname a').attrThen('href', getIdAt(3))
}
: undefined
const getOverviewStats = (label: string): number | undefined => {
const lbl = label.toLowerCase()
const row = $('.stats-row').filter((_, x) =>
x.text().toLowerCase().includes(lbl)
)
if (row.exists()) {
return Number(row.find('span').eq(1).text().replace('%', ''))
}
}
const overviewStatistics = {
kills: getOverviewStats('Total kills')!,
headshots: getOverviewStats('Headshot %')!,
deaths: getOverviewStats('Total deaths')!,
kdRatio: getOverviewStats('K/D Ratio')!,
damagePerRound: getOverviewStats('Damage / Round'),
grenadeDamagePerRound: getOverviewStats('Grenade dmg / Round'),
mapsPlayed: getOverviewStats('Maps played')!,
roundsPlayed: getOverviewStats('Rounds played')!,
killsPerRound: getOverviewStats('Kills / round')!,
assistsPerRound: getOverviewStats('Assists / round')!,
deathsPerRound: getOverviewStats('Deaths / round')!,
savedByTeammatePerRound: getOverviewStats('Saved by teammate'),
savedTeammatesPerRound: getOverviewStats('Saved teammates'),
...(getOverviewStats('Rating 1.0') !== undefined
? { rating1: getOverviewStats('Rating 1.0') }
: { rating2: getOverviewStats('Rating 2.0') })
}
const getIndivialStats = (label: string): number => {
const lbl = label.toLowerCase()
const row = i$('.stats-row').filter((_, x) =>
x.text().toLowerCase().includes(lbl)
)
return Number(row.find('span').eq(1).text().replace('%', ''))
}
const individualStatistics = {
roundsWithKills: getIndivialStats('Rounds with kills'),
zeroKillRounds: getIndivialStats('0 kill rounds'),
oneKillRounds: getIndivialStats('1 kill rounds'),
twoKillRounds: getIndivialStats('2 kill rounds'),
threeKillRounds: getIndivialStats('3 kill rounds'),
fourKillRounds: getIndivialStats('4 kill rounds'),
fiveKillRounds: getIndivialStats('5 kill rounds'),
openingKills: getIndivialStats('Total opening kills'),
openingDeaths: getIndivialStats('Total opening deaths'),
openingKillRatio: getIndivialStats('Opening kill ratio'),
openingKillRating: getIndivialStats('Opening kill rating'),
teamWinPercentAfterFirstKill: getIndivialStats(
'Team win percent after first kill'
),
firstKillInWonRounds: getIndivialStats('First kill in won rounds'),
rifleKills: getIndivialStats('Rifle kills'),
sniperKills: getIndivialStats('Sniper kills'),
smgKills: getIndivialStats('SMG kills'),
pistolKills: getIndivialStats('Pistol kills'),
grenadeKills: getIndivialStats('Grenade'),
otherKills: getIndivialStats('Other')
}
const matches = m$('.stats-table tbody tr')
.toArray()
.map((el) => {
const [kills, deaths] = el
.find('td')
.eq(4)
.text()
.split(' - ')
.map(Number)
return {
mapStatsId: el
.find('td')
.first()
.find('a')
.attrThen('href', getIdAt(4))!,
date: el.find('.time').numFromAttr('data-unix')!,
team1: {
id: el
.find('td')
.eq(1)
.find('.gtSmartphone-only a')
.attrThen('href', getIdAt(3)),
name: el.find('td').eq(1).find('a span').text()
},
team2: {
id: el
.find('td')
.eq(2)
.find('.gtSmartphone-only a')
.attrThen('href', getIdAt(3)),
name: el.find('td').eq(2).find('a span').text()
},
map: fromMapSlug(el.find('.statsMapPlayed').text()),
kills,
deaths,
rating: el.find('td').last().numFromText()!
}
})
return {
id: options.id,
name,
ign,
image,
age,
country,
team,
overviewStatistics,
individualStatistics,
matches
}
}