soccer-go
Version:
Soccer CLI for stats and results.
159 lines (158 loc) • 6.04 kB
JavaScript
;
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 dayjs_1 = __importDefault(require("dayjs"));
const config_1 = __importDefault(require("../config"));
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 || (exports.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 = exports.Stage || (exports.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',
};
class Fixture {
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.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() {
var _a, _b;
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,
(_a = statusDisplayString[this.status]) !== null && _a !== void 0 ? _a : this.status,
(0, dayjs_1.default)(this.date).format('ddd, MMM D YYYY h:mm A'),
(_b = exports.stageDisplayString[this.stage]) !== null && _b !== void 0 ? _b : this.stage,
];
}
getScores(score) {
var _a, _b;
// 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 && ((_a = score.regularTime) === null || _a === void 0 ? void 0 : _a.home) != null) {
return [score.fullTime.home, score.fullTime.away];
}
if (((_b = score.regularTime) === null || _b === void 0 ? void 0 : _b.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;