New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Get event results #165
Get event results #165
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,19 +6,35 @@ import { popSlashSource } from '../utils/parsing' | |
import { HLTVConfig } from '../config' | ||
import { fetchPage, toArray, getMatchFormatAndMap } from '../utils/mappers' | ||
|
||
export const getResults = (config: HLTVConfig) => async ({ pages = 1 } = {}): Promise< | ||
export const getResults = (config: HLTVConfig) => async ({ pages = 1, eventId = 0 } = {}): Promise< | ||
MatchResult[] | ||
> => { | ||
if (pages < 1) { | ||
console.error('getLatestResults: pages cannot be less than 1') | ||
return [] | ||
} | ||
|
||
// All results for events are always on one page | ||
if(eventId) { | ||
pages = 1 | ||
} | ||
|
||
let matches = [] as MatchResult[] | ||
|
||
for (let i = 0; i < pages; i++) { | ||
const $ = await fetchPage(`${config.hltvUrl}/results?offset=${i * 100}`, config.loadPage) | ||
let fullUrl = `${config.hltvUrl}/results?offset=${i * 100}` | ||
let eventName: string | ||
|
||
if(eventId) { | ||
fullUrl += `&event=${eventId}` | ||
} | ||
|
||
const $ = await fetchPage(fullUrl, config.loadPage) | ||
|
||
if(eventId) { | ||
eventName = $('.eventname').text() | ||
} | ||
|
||
matches = matches.concat( | ||
toArray($('.results-holder > .results-all > .results-sublist .result-con .a-reset')).map( | ||
matchEl => { | ||
|
@@ -48,11 +64,21 @@ export const getResults = (config: HLTVConfig) => async ({ pages = 1 } = {}): Pr | |
} | ||
|
||
const event: Event = { | ||
name: matchEl.find('.event-logo').attr('alt'), | ||
id: Number(popSlashSource(matchEl.find('.event-logo'))!.split('.')[0]) | ||
name: eventId ? eventName : matchEl.find('.event-logo').attr('alt'), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not just put the implementation of eventName here? Instead of:
just
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I didn't want it inside the for loop. This way if you filter by eventid it is resolved only once. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK, so I was on my phone and missed, that I had it insode the for loop. I wanted to set it outside, but for the events it will only get one page, so setting it outside the loop is irrelevant :P |
||
id: eventId | ||
? eventId | ||
: Number(popSlashSource(matchEl.find('.event-logo'))!.split('.')[0]) | ||
} | ||
|
||
const date = Number(matchEl.parent().attr('data-zonedgrouping-entry-unix')) | ||
const date = Number( | ||
eventId | ||
? matchEl | ||
.find('.date-cell') | ||
.children() | ||
.first() | ||
.attr('data-unix') | ||
: matchEl.parent().attr('data-zonedgrouping-entry-unix') | ||
) | ||
|
||
return { id, team1, team2, result, event, map, format, stars, date } | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is the default to 0 needed here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah it's not... I'll remove it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did you mean I should set it to
undefined
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I cheated and looked at how emha did it for teamId in his PR and made the same changes :)
I'm still new to typescript and didn't know how to remove the default value...