tiebreak
Version:
Calculation of chess tournament tiebreaks compliant with FIDE regulations
78 lines (77 loc) • 2.46 kB
JavaScript
var TournamentType = /* @__PURE__ */ ((TournamentType2) => {
TournamentType2["SWISS"] = "SWISS";
return TournamentType2;
})(TournamentType || {});
class Results {
constructor(tournamentType, results) {
this.tournamentType = tournamentType;
for (const [roundIndex, roundResults] of results.entries()) {
for (const pairing of roundResults.pairings) {
this.addToMap(pairing.white, roundIndex + 1, {
score: pairing.scoreWhite,
opponentScore: pairing.scoreBlack,
opponent: pairing.black,
forfeited: pairing.forfeited
});
this.addToMap(pairing.black, roundIndex + 1, {
score: pairing.scoreBlack,
opponentScore: pairing.scoreWhite,
opponent: pairing.white,
forfeited: pairing.forfeited
});
}
for (const player of roundResults.pairingAllocatedByes ?? []) {
this.addToMap(player, roundIndex + 1, "allocated-bye");
}
for (const player of roundResults.halfPointByes ?? []) {
this.addToMap(player, roundIndex + 1, "half-point-bye");
}
}
}
pairings = /* @__PURE__ */ new Map();
addToMap(playerId, round, result) {
if (!this.pairings.has(playerId)) {
this.pairings.set(playerId, /* @__PURE__ */ new Map());
}
if (this.pairings.get(playerId)?.has(round)) {
throw new Error(`Multiple results in round ${round} for player ${playerId}`);
}
this.pairings.get(playerId)?.set(round, result);
}
/**
* Returns the result for the given player and round.
*/
get(playerId, round) {
return this.pairings.get(playerId)?.get(round) ?? "unpaired";
}
/**
* Returns results for a player for each round up to the given maxRound.
*/
getAll(playerId, maxRound) {
return Array.from(Array(maxRound).keys()).map((i) => this.get(playerId, i + 1));
}
allPlayers() {
return Array.from(this.pairings.keys());
}
}
function isPaired(result) {
return typeof result === "object";
}
function isPlayed(result) {
return isPaired(result) && !result.forfeited;
}
function isForfeitLoss(result) {
return isPaired(result) && result.forfeited && result.score === 0;
}
function isVoluntarilyUnplayedRound(result) {
return result === "unpaired" || result === "half-point-bye" || isForfeitLoss(result);
}
export {
Results,
TournamentType,
isForfeitLoss,
isPaired,
isPlayed,
isVoluntarilyUnplayedRound
};
//# sourceMappingURL=results.js.map