Skip to content

Commit

Permalink
Qualifying + sprint results available before race (#178)
Browse files Browse the repository at this point in the history
* Qualifying + sprint results available before race
  • Loading branch information
marcokreeft87 authored Mar 7, 2023
1 parent e7d94ef commit cfb75a8
Show file tree
Hide file tree
Showing 8 changed files with 297 additions and 132 deletions.
4 changes: 2 additions & 2 deletions formulaone-card.js

Large diffs are not rendered by default.

Binary file modified formulaone-card.js.gz
Binary file not shown.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "formulaone-card",
"version": "1.2.0",
"version": "1.2.1",
"description": "Frontend card for Home Assistant to display Formula One data",
"main": "index.js",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion src/api/f1-models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export interface Root {
export interface RaceTable {
season: string
round?: string
Races: Race[]
Races?: Race[]
}

export interface Race {
Expand Down
104 changes: 73 additions & 31 deletions src/cards/results.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,28 +244,32 @@ export default class Results extends BaseCard {
`;
}

private setSelectedRace(ev: SelectChangeEvent) {
setSelectedRace(ev: SelectChangeEvent) {
const round = parseInt(ev.target.value);
const { properties, cardValues } = this.getParentCardValues();

properties.selectedRound = round;

this.client.GetResults(properties.selectedSeason as number, round).then(response => {

this.client.GetQualifyingResults(parseInt(response.season), parseInt(response.round)).then(qualifyingResults => {
const race = response.Races[0];
race.QualifyingResults = qualifyingResults.Races[0].QualifyingResults;

this.client.GetSprintResults(parseInt(response.season), parseInt(response.round)).then(sprintResults => {
/* istanbul ignore next */
race.SprintResults = sprintResults?.Races[0]?.SprintResults;

properties.selectedRace = race;
cardValues.set('cardValues', properties);
this.parent.properties = cardValues;
});
const selectedSeason = properties.selectedSeason as number;

Promise.all([this.client.GetResults(selectedSeason, round),
this.client.GetQualifyingResults(selectedSeason, round),
this.client.GetSprintResults(selectedSeason, round)])
.then(([results, qualifyingResults, sprintResults]) => {

const race = results.Races.length > 0 ? results.Races[0] : null;
if(race) {
race.QualifyingResults = qualifyingResults.Races[0].QualifyingResults;
/* istanbul ignore next */
race.SprintResults = sprintResults?.Races[0]?.SprintResults
console.log(`${selectedSeason} round ${round}`, sprintResults);
properties.selectedSeason = race.season;
}

properties.selectedRace = race;
cardValues.set('cardValues', properties);
this.parent.properties = cardValues;
});
});
}

private setRaces(ev: SelectChangeEvent) {
Expand All @@ -282,29 +286,67 @@ export default class Results extends BaseCard {
});
}

private getUpcomingRace(now: Date, races: Race[]) : Race {

const nextRaces = races.filter(race => {

const raceDateTime = new Date(race.date + 'T' + race.time);
const qualifyingDateTime = new Date(race.Qualifying.date + 'T' + race.Qualifying.time);
const sprintDateTime = race.Sprint ? new Date(race.Sprint.date + 'T' + race.Sprint.time) : null;

if(raceDateTime >= now && (qualifyingDateTime < now && (sprintDateTime === null || sprintDateTime < now))) {
return true;
}

return false;
});

return nextRaces.length > 0 ? nextRaces[0] : null;
}

private getLastResult() {
this.client.GetLastResult().then(response => {

const { properties, cardValues } = this.getParentCardValues();
properties.selectedSeason = response.season;
const now = new Date();

this.client.GetQualifyingResults(parseInt(response.season), parseInt(response.round)).then(qualifyingResults => {
const race = response;
race.QualifyingResults = qualifyingResults.Races[0].QualifyingResults;
this.client.GetSchedule(now.getFullYear()).then(response => {

this.client.GetSprintResults(parseInt(response.season), parseInt(response.round)).then(sprintResults => {
race.SprintResults = sprintResults?.Races[0]?.SprintResults;
const upcomingRace = this.getUpcomingRace(now, response);

let season : number = new Date().getFullYear();
let round : number = upcomingRace !== null ? parseInt(upcomingRace.round) : 0;

let race = { } as Race;
if(upcomingRace !== null) {
race = upcomingRace;
round = parseInt(race.round);
season = parseInt(race.season);
} else {
this.client.GetLastResult().then(response => {
race = response;
round = parseInt(response.round);
season = parseInt(response.season);
});
}

Promise.all([this.client.GetQualifyingResults(season, round),
this.client.GetSprintResults(season, round),
this.client.GetSeasonRaces(season)])
.then(([qualifyingResults, sprintResults, seasonRaces]) => {

const { properties, cardValues } = this.getParentCardValues();

race.QualifyingResults = qualifyingResults.Races[0].QualifyingResults;
race.SprintResults = sprintResults?.Races[0]?.SprintResults;

properties.races = seasonRaces;
properties.selectedRace = race;
this.client.GetSeasonRaces(parseInt(response.season)).then(racesResponse => {
properties.races = racesResponse;

cardValues.set('cardValues', properties);
this.parent.properties = cardValues;
});
properties.selectedSeason = season.toString();

cardValues.set('cardValues', properties);
this.parent.properties = cardValues;
});
});
});

}

setSelectedTabIndex(index: number) {
Expand Down
Loading

0 comments on commit cfb75a8

Please sign in to comment.