formula1.js
Version:
Formula 1 Ergast API Wrapper
103 lines (102 loc) • 2.96 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Circuit = exports.Session = exports.GrandPrix = exports.Schedule = void 0;
const Types_1 = require("../../Types");
const Request_1 = require("../Request");
const Tools_1 = require("../Tools");
class Schedule {
constructor(year) {
this.year = year;
}
async pop() {
const data = (await (0, Request_1.Request)(this.year.toString()));
const schedule = data.MRData;
this.season = parseInt(schedule.RaceTable.season);
this.rounds = schedule.RaceTable.Races.map(r => new GrandPrix(this, r));
this.initialized = true;
return this;
}
year;
initialized = false;
season;
rounds;
getAllSessions() {
let ss = [];
for (const g of this.rounds) {
ss.push(...g.sessions);
}
return ss;
}
findNextSession() {
let ss = this.getAllSessions();
return ss.filter(s => !s.completed)[0];
}
findLastSession() {
let ss = this.getAllSessions();
return ss.filter(s => s.completed).at(-1);
}
}
exports.Schedule = Schedule;
class GrandPrix {
constructor(schedule, data) {
this.name = data.raceName;
this.round = parseInt(data.round);
this.season = schedule.season;
const { FirstPractice: p1, SecondPractice: p2, ThirdPractice: p3, Qualifying: q, Sprint: s, date, time, } = data;
this.sessions = [p1, p2, p3, q, s, { date, time }]
.map((s, i) => s != null
? new Session(this, s, Types_1.sessions[i], Types_1.sessionTypes[i])
: null)
.filter(Boolean);
this.schedule = schedule;
this.sprintWeekend = this.getSession(Types_1.sessions[4]) != null;
this.circuit = new Circuit(this, data.Circuit);
}
name;
round;
season;
schedule;
sprintWeekend;
circuit;
sessions;
getSession(id) {
return this.sessions.find(s => s.name == id);
}
}
exports.GrandPrix = GrandPrix;
class Session {
constructor(grandprix, data, name, type) {
this.grandprix = grandprix;
this.date = (0, Tools_1.getDate)({
date: data.date,
time: data.time || '00:00:00Z',
});
this.completed = this.date < new Date();
this.name = name;
this.type = type;
}
name;
grandprix;
date;
completed;
type;
}
exports.Session = Session;
class Circuit {
constructor(grandprix, data) {
this.grandprix = grandprix;
this.name = data.circuitName;
this.id = data.circuitId;
this.location = {
latitude: parseFloat(data.Location.lat),
longitude: parseFloat(data.Location.long),
locality: data.Location.locality,
country: data.Location.country,
};
}
grandprix;
name;
id;
location;
}
exports.Circuit = Circuit;