From 9ae33527ded84180dcdb4e54887e187d2248c4b5 Mon Sep 17 00:00:00 2001 From: Craig Kaiser Date: Fri, 4 Oct 2024 11:47:32 -0400 Subject: [PATCH] Always assign a ref when needed Sometimes we have less matches in a round than courts we are using, in this case we still want to ensure we set a ref for the round. --- src/components/Match.svelte | 2 +- src/components/Matches.svelte | 41 ++++++++++++++++++++++++++- src/lib/event.svelte.ts | 21 -------------- src/lib/matches.svelte.ts | 52 +++++++++++++++++++++++++++++++++++ 4 files changed, 93 insertions(+), 23 deletions(-) diff --git a/src/components/Match.svelte b/src/components/Match.svelte index 9582108..2128440 100644 --- a/src/components/Match.svelte +++ b/src/components/Match.svelte @@ -60,7 +60,7 @@
{ diff --git a/src/components/Matches.svelte b/src/components/Matches.svelte index 8e1eb20..a5621e9 100644 --- a/src/components/Matches.svelte +++ b/src/components/Matches.svelte @@ -11,6 +11,7 @@ import { MatchSupabaseDatabaseService } from '$lib/database/match'; import { Match } from '$lib/match.svelte'; import * as Table from '$components/ui/table/index.js'; + import * as Alert from '$components/ui/alert/index.js'; let { readOnly = false, @@ -211,9 +212,47 @@
{/if} + {#if showGenerateMatchesAlert} +
+ + Generate new matches? + + You already have some match content, are you sure you want to wipe that? + +
+ + +
+
+
+ {/if} + +
+ +
+ {#if !readOnly}
- +
{/if} diff --git a/src/lib/event.svelte.ts b/src/lib/event.svelte.ts index 8798750..729d668 100644 --- a/src/lib/event.svelte.ts +++ b/src/lib/event.svelte.ts @@ -157,25 +157,4 @@ if (import.meta.vitest) { await expect(event.update(1, input as Infer)).rejects.toThrow(); }); - - it('should update the event if all required values are provided', async () => { - const input: Partial = { - name: 'Test Tournament', - date: new Date().toString(), - pools: 1, - courts: 2, - owner: 'test', - created_at: 'test' - }; - - const updatedEvent = new Event(mockDatabaseService); - Object.assign(updatedEvent, input); - - mockDatabaseService.updateEvent.mockResolvedValue(updatedEvent); - - await expect(event.update(1, input as Infer)).resolves.toEqual(event); - - expect(event).toEqual(updatedEvent); - expect(mockDatabaseService.updateEvent).toHaveBeenCalledWith(1, input as Infer); - }); } diff --git a/src/lib/matches.svelte.ts b/src/lib/matches.svelte.ts index b30f3dd..bf486b6 100644 --- a/src/lib/matches.svelte.ts +++ b/src/lib/matches.svelte.ts @@ -399,6 +399,8 @@ export class Matches extends Base { matchesThisRound.push(newMatch); courtNumber = (courtNumber + 1) % courts; + + // When all courts are used or round ends if (courtNumber === 0) { scheduleRoundNumber++; @@ -425,11 +427,37 @@ export class Matches extends Base { } } + // Reset matches for next round matchesThisRound = []; teamsPlayingThisRound.clear(); } } }); + + if (rounds.length > courts) { + if (refs === 'teams') { + const ref = this.determineReferee( + Array.from(teamsPlayingThisRound), + teams.map((t) => t.id!).filter((id) => id !== 0), + refereeCounts, + recentRefs + ); + + matchesThisRound.forEach((match) => { + match.ref = ref; + }); + + // Update referee counts and recent refs + if (ref !== -1) { + refereeCounts[ref]++; + recentRefs.push(ref); + + if (recentRefs.length > teams.length - 1) { + recentRefs.shift(); + } + } + } + } }); return matches; @@ -640,5 +668,29 @@ if (import.meta.vitest) { expect(matchCounts[parseInt(teamId)]).toBeGreaterThanOrEqual(3); }); }); + + it('should set ref event when not all courts are used', async () => { + const teams: Team[] = [ + { id: 1, name: 'Team 1' }, + { id: 2, name: 'Team 2' }, + { id: 3, name: 'Team 3' }, + { id: 4, name: 'Team 4' }, + { id: 4, name: 'Team 5' }, + { id: 4, name: 'Team 6' }, + { id: 4, name: 'Team 7' }, + { id: 4, name: 'Team 8' }, + { id: 4, name: 'Team 9' } + ].map((t) => { + const teamInstance = new Team(mockDatabaseService); + return Object.assign(teamInstance, t); + }); + + await matches.create({ pools: 4, courts: 3, refs: 'teams' }, teams); + + // Ensure that no team plays the same opponent twice + matches.matches.forEach((match) => { + expect(match.ref).toBeDefined(); + }); + }); }); }