Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ export default typescript.config(
},
],
'@typescript-eslint/no-non-null-assertion': 'warn',
'@typescript-eslint/no-unused-vars': [
'error',
{ argsIgnorePattern: '^_' },
],
},
settings: {
'import-x/resolver': {
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
},
"description": "Progressive score tiebreak for chess tournaments following FIDE rules. Zero dependencies.",
"peerDependencies": {
"@echecs/tournament": "^2.0.0"
"@echecs/tournament": "^3.0.0"
},
"devDependencies": {
"@echecs/tournament": "^2.1.2",
"@echecs/tournament": "^3.1.0",
"@eslint/js": "^10.0.1",
"@typescript-eslint/parser": "^8.57.0",
"@vitest/coverage-v8": "^4.1.0",
Expand Down Expand Up @@ -83,5 +83,5 @@
},
"sideEffects": false,
"type": "module",
"version": "3.0.2"
"version": "4.0.0"
}
12 changes: 6 additions & 6 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

66 changes: 35 additions & 31 deletions src/__tests__/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,51 +3,55 @@ import { describe, expect, it } from 'vitest';
import { progressiveCut1 } from '../cut1.js';
import { progressive } from '../index.js';

import type { Game } from '@echecs/tournament';

// 4 players, 3 rounds:
// Round 1: A(W) 1-0 B, C(W) 0-1 D
// Round 2: A(W) 0.5-0.5 D, C(W) 0-1 B
// Round 3: A(W) 1-0 C, D(W) 1-0 B
// Scores: A=2.5, D=2.5, B=1, C=0

const GAMES: Game[][] = [
[
{ black: 'B', result: 1, white: 'A' },
{ black: 'D', result: 0, white: 'C' },
],
[
{ black: 'D', result: 0.5, white: 'A' },
{ black: 'B', result: 0, white: 'C' },
],
[
{ black: 'C', result: 1, white: 'A' },
{ black: 'B', result: 1, white: 'D' },
],
import type { CompletedRound, Player } from '@echecs/tournament';

const PLAYERS: Player[] = [
{ id: 'A', points: 2.5, rank: 1 },
{ id: 'B', points: 1, rank: 3 },
{ id: 'C', points: 0, rank: 4 },
{ id: 'D', points: 2.5, rank: 2 },
];

const ROUNDS: CompletedRound[] = [
{
byes: [],
games: [
{ black: 'B', result: 'white', white: 'A' },
{ black: 'D', result: 'black', white: 'C' },
],
},
{
byes: [],
games: [
{ black: 'D', result: 'draw', white: 'A' },
{ black: 'B', result: 'black', white: 'C' },
],
},
{
byes: [],
games: [
{ black: 'C', result: 'white', white: 'A' },
{ black: 'B', result: 'white', white: 'D' },
],
},
];

describe('progressive', () => {
it('returns sum of cumulative scores after each round', () => {
// A: R1=1 (cum=1), R2=0.5 (cum=1.5), R3=1 (cum=2.5)
// sum = 1 + 1.5 + 2.5 = 5
expect(progressive('A', GAMES)).toBe(5);
expect(progressive('A', ROUNDS, PLAYERS)).toBe(5);
});

it('handles player with no games', () => {
expect(progressive('A', [])).toBe(0);
expect(progressive('A', [], PLAYERS)).toBe(0);
});
});

describe('progressiveCut1', () => {
it('sums cumulative scores from round 2 onwards', () => {
// A from round 2: running sum starting after R1:
// After R1: cum=1; R2: cum=1.5 → add 1.5; R3: cum=2.5 → add 2.5 → total=4
// BUT per spec: recalculate from round 2:
// cum starts at 0 from R2: R2=0.5(cum=0.5), R3=1(cum=1.5) → sum=0.5+1.5=2
expect(progressiveCut1('A', GAMES)).toBe(2);
expect(progressiveCut1('A', ROUNDS, PLAYERS)).toBe(2);
});

it('handles player with no games', () => {
expect(progressiveCut1('A', [])).toBe(0);
expect(progressiveCut1('A', [], PLAYERS)).toBe(0);
});
});
20 changes: 15 additions & 5 deletions src/cut1.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
import { playerResult } from './utilities.js';

import type { Game } from '@echecs/tournament';
import type { CompletedRound, Player } from '@echecs/tournament';

function progressiveCut1(player: string, games: Game[][]): number {
function progressiveCut1(
player: string,
rounds: CompletedRound[],
_players: Player[],
): number {
let cumulative = 0;
let total = 0;
for (const round of games.slice(1)) {
cumulative += playerResult(player, round);
for (const round of rounds.slice(1)) {
cumulative += playerResult(player, round.games);
total += cumulative;
}
return total;
}

export { progressiveCut1, progressiveCut1 as tiebreak };

export type { Game, GameKind, Player, Result } from '@echecs/tournament';
export type {
Bye,
CompletedRound,
Game,
Pairing,
Player,
} from '@echecs/tournament';
20 changes: 15 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
import { playerResult } from './utilities.js';

import type { Game } from '@echecs/tournament';
import type { CompletedRound, Player } from '@echecs/tournament';

function progressive(player: string, games: Game[][]): number {
function progressive(
player: string,
rounds: CompletedRound[],
_players: Player[],
): number {
let cumulative = 0;
let total = 0;
for (const round of games) {
cumulative += playerResult(player, round);
for (const round of rounds) {
cumulative += playerResult(player, round.games);
total += cumulative;
}
return total;
}

export { progressive, progressive as tiebreak };

export type { Game, GameKind, Player, Result } from '@echecs/tournament';
export type {
Bye,
CompletedRound,
Game,
Pairing,
Player,
} from '@echecs/tournament';
30 changes: 21 additions & 9 deletions src/utilities.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,31 @@
import type { Game } from '@echecs/tournament';
import type { CompletedRound, Game } from '@echecs/tournament';

function gamesForPlayer(player: string, games: Game[][]): Game[] {
return games.flat().filter((g) => g.white === player || g.black === player);
function gamesForPlayer(player: string, rounds: CompletedRound[]): Game[] {
return rounds
.flatMap((r) => r.games)
.filter((g) => g.white === player || g.black === player);
}

function scoreFor(player: string, game: Game): number {
if (game.result === 'draw') {
return 0.5;
}
if (game.result === 'none') {
return 0;
}
return (game.result === 'white' && game.white === player) ||
(game.result === 'black' && game.black === player)
? 1
: 0;
}

function playerResult(player: string, round: Game[]): number {
for (const g of round) {
if (g.white === player) {
return g.result;
}
if (g.black === player) {
return 1 - g.result;
if (g.white === player || g.black === player) {
return scoreFor(player, g);
}
}
return 0;
}

export { gamesForPlayer, playerResult };
export { gamesForPlayer, playerResult, scoreFor };
Loading