@marekbarela/scoreboard
Version:
A minimal TypeScript scoreboard library for managing live football match data.
49 lines (48 loc) • 1.59 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Scoreboard = void 0;
class Scoreboard {
constructor() {
this.matches = [];
}
startMatch(homeTeam, awayTeam) {
const exists = this.matches.some(match => match.homeTeam === homeTeam && match.awayTeam === awayTeam);
if (exists) {
throw new Error("Match already started");
}
const match = {
homeTeam,
awayTeam,
homeScore: 0,
awayScore: 0,
startTime: Date.now(),
};
this.matches.push(match);
}
getSummary() {
return [...this.matches].sort((a, b) => {
const totalA = a.homeScore + a.awayScore;
const totalB = b.homeScore + b.awayScore;
if (totalA !== totalB) {
return totalB - totalA;
}
return b.startTime - a.startTime;
});
}
updateScore(homeTeam, awayTeam, homeScore, awayScore) {
const match = this.matches.find(m => m.homeTeam === homeTeam && m.awayTeam === awayTeam);
if (!match) {
throw new Error("Match not found");
}
match.homeScore = homeScore;
match.awayScore = awayScore;
}
finishMatch(homeTeam, awayTeam) {
const matchIndex = this.matches.findIndex(m => m.homeTeam === homeTeam && m.awayTeam === awayTeam);
if (matchIndex === -1) {
throw new Error("Match not found");
}
this.matches.splice(matchIndex, 1);
}
}
exports.Scoreboard = Scoreboard;