soccer-go
Version:
Soccer CLI for stats and results.
169 lines (168 loc) • 6.05 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.stageDisplayString = exports.Stage = exports.Status = void 0;
const picocolors_1 = __importDefault(require("picocolors"));
const config_1 = __importDefault(require("../config"));
const date_format_1 = require("../utils/date-format");
var Status;
(function (Status) {
Status["Scheduled"] = "SCHEDULED";
Status["Timed"] = "TIMED";
Status["InPlay"] = "IN_PLAY";
Status["Paused"] = "PAUSED";
Status["ExtraTime"] = "EXTRA_TIME";
Status["Penalty"] = "PENALTY_SHOOTOUT";
Status["Finished"] = "FINISHED";
Status["Suspended"] = "SUSPENDED";
Status["Postponed"] = "POSTPONED";
Status["Cancelled"] = "CANCELLED";
Status["Awarded"] = "AWARDED";
})(Status || (exports.Status = Status = {}));
const statusDisplayString = {
[Status.Awarded]: 'Awarded',
[Status.Cancelled]: 'Canceled',
[Status.Finished]: 'Finished',
[Status.InPlay]: 'Playing',
[Status.Paused]: 'Paused',
[Status.Postponed]: 'Postponed',
[Status.Scheduled]: 'Scheduled',
[Status.Suspended]: 'Suspended',
[Status.Timed]: 'Timed',
[Status.ExtraTime]: 'Extra time',
[Status.Penalty]: 'Penalties',
};
var Stage;
(function (Stage) {
Stage["Final"] = "FINAL";
Stage["ThirdPlace"] = "THIRD_PLACE";
Stage["SemiFinals"] = "SEMI_FINALS";
Stage["QuarterFinals"] = "QUARTER_FINALS";
Stage["Last16"] = "LAST_16";
Stage["Last32"] = "LAST_32";
Stage["Last64"] = "LAST_64";
Stage["Round4"] = "ROUND_4";
Stage["Round3"] = "ROUND_3";
Stage["Round2"] = "ROUND_2";
Stage["Round1"] = "ROUND_1";
Stage["GroupStage"] = "GROUP_STAGE";
Stage["Preliminary_Round"] = "PRELIMINARY_ROUND";
Stage["Qualification"] = "QUALIFICATION";
Stage["QualificationRound1"] = "QUALIFICATION_ROUND_1";
Stage["QualificationRound2"] = "QUALIFICATION_ROUND_2";
Stage["QualificationRound3"] = "QUALIFICATION_ROUND_3";
Stage["PlayoffRound"] = "PLAYOFF_ROUND";
Stage["PlayoffRound1"] = "PLAYOFF_ROUND_1";
Stage["PlayoffRound2"] = "PLAYOFF_ROUND_2";
Stage["Playoffs"] = "PLAYOFFS";
Stage["RegularSeason"] = "REGULAR_SEASON";
Stage["Clausura"] = "CLAUSURA";
Stage["Apertura"] = "APERTURA";
Stage["Championship"] = "CHAMPIONSHIP";
Stage["Relegation"] = "RELEGATION";
Stage["RelegationRound"] = "RELEGATION_ROUND";
Stage["LeagueStage"] = "LEAGUE_STAGE";
})(Stage || (exports.Stage = Stage = {}));
exports.stageDisplayString = {
FINAL: 'Final',
THIRD_PLACE: 'Third place',
SEMI_FINALS: 'Semi-finals',
QUARTER_FINALS: 'Quarter-finals',
LAST_16: 'Round of 16',
LAST_32: 'Round of 32',
LAST_64: 'Round of 64',
ROUND_4: 'Round 4',
ROUND_3: 'Round 3',
ROUND_2: 'Round 2',
ROUND_1: 'Round 1',
GROUP_STAGE: 'Group stage',
PRELIMINARY_ROUND: 'Preliminary round',
QUALIFICATION: 'Qualifications',
QUALIFICATION_ROUND_1: 'Qualifications R1',
QUALIFICATION_ROUND_2: 'Qualifications R2',
QUALIFICATION_ROUND_3: 'Qualifications R3',
PLAYOFF_ROUND: 'Play-off round',
PLAYOFF_ROUND_1: 'Play-off R1',
PLAYOFF_ROUND_2: 'Play-off R2',
PLAYOFFS: 'Play-offs',
REGULAR_SEASON: 'Regular season',
CLAUSURA: 'Clausura',
APERTURA: 'Apertura',
CHAMPIONSHIP: 'Championship',
RELEGATION: 'Relegation',
RELEGATION_ROUND: 'Relegation round',
LEAGUE_STAGE: 'League stage',
};
class Fixture {
id;
home;
away;
matchday;
stage;
status;
date;
referee;
links;
constructor(data) {
const [homeScore, awayScore] = this.getScores(data.score);
this.id = data.id;
this.home = {
goals: homeScore,
team: data.homeTeam.name,
};
this.away = {
goals: awayScore,
team: data.awayTeam.name,
};
this.matchday = data.matchday;
this.stage = data.stage;
this.status = data.status;
this.date = new Date(data.utcDate);
this.referee = data.referees?.find((r) => r.type === 'REFEREE') || null;
this.links = {
awayTeam: `${config_1.default.apiBaseUrl}/teams/${data.awayTeam.id}`,
homeTeam: `${config_1.default.apiBaseUrl}/teams/${data.homeTeam.id}`,
self: `${config_1.default.apiBaseUrl}/matches/${data.id}`,
};
}
toRow() {
let homeTeam = this.home.team;
if (this.home.goals > this.away.goals) {
homeTeam = picocolors_1.default.bold(homeTeam);
}
let awayTeam = this.away.team;
if (this.away.goals > this.home.goals) {
awayTeam = picocolors_1.default.bold(awayTeam);
}
let score = picocolors_1.default.bold(`${this.home.goals} - ${this.away.goals}`);
if ([Status.Scheduled, Status.Timed, Status.Cancelled, Status.Postponed].includes(this.status)) {
score = '';
}
else if (this.status === Status.InPlay) {
score = picocolors_1.default.green(score);
}
return [
`${homeTeam} - ${awayTeam}`,
score,
statusDisplayString[this.status] ?? this.status,
(0, date_format_1.formatFixtureDate)(this.date),
exports.stageDisplayString[this.stage] ?? this.stage,
];
}
getScores(score) {
// fullTime is set to 0 as soon as the match starts, we want to use the halfTime in that case
if (score.fullTime.home != null && score.regularTime?.home != null) {
return [score.fullTime.home, score.fullTime.away];
}
if (score.regularTime?.home != null) {
return [score.regularTime.home, score.regularTime.away];
}
if (score.halfTime.home != null) {
return [score.halfTime.home, score.halfTime.away];
}
return [0, 0];
}
}
exports.default = Fixture;