/
getTeamStats.ts
266 lines (235 loc) · 7.44 KB
/
getTeamStats.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
263
264
265
266
import { stringify } from 'querystring'
import { HLTVConfig } from '../config'
import { HLTVPage, HLTVPageElement, HLTVScraper } from '../scraper'
import { BestOfFilter } from '../shared/BestOfFilter'
import { fromMapName, GameMap, toMapFilter } from '../shared/GameMap'
import { MatchType } from '../shared/MatchType'
import { Player } from '../shared/Player'
import { Event } from '../shared/Event'
import { RankingFilter } from '../shared/RankingFilter'
import { fetchPage, generateRandomSuffix, getIdAt } from '../utils'
import { MatchStatsPreview } from './getMatchesStats'
export interface TeamMapStats {
wins: number
draws: number
losses: number
winRate: number
totalRounds: number
roundWinPAfterFirstKill: number
roundWinPAfterFirstDeath: number
}
export interface TeamStatsEvent {
place: string
event: Event
}
export interface FullTeamStats {
id: number
name: string
overview: {
mapsPlayed: number
wins: number
draws: number
losses: number
totalKills: number
totalDeaths: number
roundsPlayed: number
kdRatio: number
}
currentLineup: Player[]
historicPlayers: Player[]
standins: Player[]
substitutes: Player[]
matches: MatchStatsPreview[]
mapStats: Record<GameMap, TeamMapStats>
events: TeamStatsEvent[]
}
export interface GetTeamStatsArguments {
id: number
currentRosterOnly?: boolean
startDate?: string
endDate?: string
matchType?: MatchType
rankingFilter?: RankingFilter
maps?: GameMap[]
bestOfX?: BestOfFilter
}
export const getTeamStats =
(config: HLTVConfig) =>
async (options: GetTeamStatsArguments): Promise<FullTeamStats> => {
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 } : {})
})
let $ = HLTVScraper(
await fetchPage(
`https://www.hltv.org/stats/teams/${options.id}/-?${query}`,
config.loadPage
)
)
const name = $('.context-item-name').last().text()
const currentTeam = { id: options.id, name }
const currentLineup = getPlayersByContainer(
getContainerByText($, 'Current lineup')
)
const currentRosterQuery = stringify({
lineup: currentLineup.map((x) => x.id!),
minLineupMatch: 0
})
if (options.currentRosterOnly) {
$ = HLTVScraper(
await fetchPage(
`https://www.hltv.org/stats/lineup?${currentRosterQuery}`,
config.loadPage
)
)
}
const historicPlayers = getPlayersByContainer(
getContainerByText($, 'Historic players')
)
const substitutes = getPlayersByContainer(
getContainerByText($, 'Substitutes')
)
const standins = getPlayersByContainer(getContainerByText($, 'Standins'))
const [m$, e$, mp$] = await Promise.all([
fetchPage(
options.currentRosterOnly
? `https://www.hltv.org/stats/lineup/matches?${currentRosterQuery}&${query}`
: `https://www.hltv.org/stats/teams/matches/${
options.id
}/${generateRandomSuffix()}?${query}`,
config.loadPage
).then(HLTVScraper),
fetchPage(
options.currentRosterOnly
? `https://www.hltv.org/stats/lineup/events?${currentRosterQuery}&${query}`
: `https://www.hltv.org/stats/teams/events/${
options.id
}/${generateRandomSuffix()}?${query}`,
config.loadPage
).then(HLTVScraper),
fetchPage(
options.currentRosterOnly
? `https://www.hltv.org/stats/lineup/maps?${currentRosterQuery}&${query}`
: `https://www.hltv.org/stats/teams/maps/${
options.id
}/${generateRandomSuffix()}?${query}`,
config.loadPage
).then(HLTVScraper)
])
const overviewStats = $('.standard-box .large-strong')
const [wins, draws, losses] = overviewStats
.eq(1)
.text()
.split('/')
.map(Number)
const overview = {
mapsPlayed: overviewStats.eq(0).numFromText()!,
totalKills: overviewStats.eq(2).numFromText()!,
totalDeaths: overviewStats.eq(3).numFromText()!,
roundsPlayed: overviewStats.eq(4).numFromText()!,
kdRatio: overviewStats.eq(5).numFromText()!,
wins,
draws,
losses
}
const matches = m$('.stats-table tbody tr')
.toArray()
.map((el) => {
const [team1Result, team2Result] = el
.find('.statsDetail')
.text()
.split(' - ')
return {
date: getTimestamp(el.find('.time a').text()),
event: {
id: Number(
el
.find('.image-and-label')
.attr('href')!
.split('event=')[1]
.split('&')[0]
),
name: el.find('.image-and-label img').attr('title')!
},
team1: currentTeam,
team2: {
id: el.find('img.flag').parent().attrThen('href', getIdAt(3)),
name: el.find('img.flag').parent().trimText()!
},
map: fromMapName(el.find('.statsMapPlayed').text()),
mapStatsId: el.find('.time a').attrThen('href', getIdAt(4))!,
result: { team1: Number(team1Result), team2: Number(team2Result) }
}
})
const events = e$('.stats-table tbody tr')
.toArray()
.map((el) => {
const eventEl = el.find('.image-and-label').first()
return {
place: el.find('.statsCenterText').text(),
event: {
id: Number(eventEl.attr('href')!.split('event=')[1].split('&')[0]),
name: eventEl.text()
}
}
})
const getMapStat = (mapEl: HLTVPageElement, i: number) =>
mapEl.find('.stats-row').eq(i).children().last().text()
const mapStats = mp$('.two-grid .col .stats-rows')
.toArray()
.reduce((stats, mapEl) => {
const mapName = fromMapName(
mapEl.prev().find('.map-pool-map-name').text()
)
const [wins, draws, losses] = getMapStat(mapEl, 0)
.split(' / ')
.map(Number)
stats[mapName] = {
wins,
draws,
losses,
winRate: Number(getMapStat(mapEl, 1).split('%')[0]),
totalRounds: Number(getMapStat(mapEl, 2)),
roundWinPAfterFirstKill: Number(getMapStat(mapEl, 3).split('%')[0]),
roundWinPAfterFirstDeath: Number(getMapStat(mapEl, 4).split('%')[0])
}
return stats
}, {} as Record<string, any>)
return {
id: options.id,
name,
overview,
matches,
currentLineup,
historicPlayers,
standins,
substitutes,
events,
mapStats
}
}
function getContainerByText($: HLTVPage, text: string) {
return $('.standard-headline')
.filter((_, el) => el.text() === text)
.parent()
.next()
}
function getPlayersByContainer(container: HLTVPageElement) {
return container
.find('.image-and-label')
.toArray()
.map((el) => ({
id: el.attrThen('href', getIdAt(3)),
name: el.find('.text-ellipsis').text()
}))
}
function getTimestamp(source: string): number {
const [day, month, year] = source.split('/')
return new Date([month, day, year].join('/')).getTime()
}