Skip to content

Commit

Permalink
Always assign a ref when needed
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
craigkai committed Oct 4, 2024
1 parent 00c86cb commit 9ae3352
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/components/Match.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
<!-- svelte-ignore a11y_click_events_have_key_events -->
<!-- svelte-ignore a11y_no_static_element_interactions -->
<div
class="cursor-pointer flex place-items-center justify-center text-pretty {courts < 2
class="flex cursor-pointer place-items-center justify-center text-pretty {courts < 2
? 'text-lg'
: 'text-base'}"
onclick={() => {
Expand Down
41 changes: 40 additions & 1 deletion src/components/Matches.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -211,9 +212,47 @@
</div>
{/if}

{#if showGenerateMatchesAlert}
<div class="m-2">
<Alert.Root>
<Alert.Title>Generate new matches?</Alert.Title>
<Alert.Description>
You already have some match content, are you sure you want to wipe that?
</Alert.Description>
<div class="flex gap-2">
<button
class="focus:shadow-outline text-whit e rounded bg-blue-400 px-4 py-2 font-bold
text-black hover:bg-blue-600 focus:outline-none dark:text-nord-1"
onclick={generateMatches}
>
Yes
</button>
<button
class="focus:shadow-outline text-whit e rounded bg-blue-400 px-4 py-2 font-bold
text-black hover:bg-blue-600 focus:outline-none dark:text-nord-1"
onclick={() => (showGenerateMatchesAlert = false)}
>
No
</button>
</div>
</Alert.Root>
</div>
{/if}

<div class="m-2 flex justify-center">
<button
class="focus:shadow-outline focus:outline-n one rounded bg-blue-400 px-4 py-2 font-bold text-white
hover:bg-blue-600 dark:text-nord-1"
type="button"
onclick={checkGenerateMatches}
>
Generate matches
</button>
</div>

{#if !readOnly}
<div class="text-center">
<button onclick={addMatch} class="text-blue-500 cursor-pointer">Add Match</button>
<button onclick={addMatch} class="cursor-pointer text-blue-500">Add Match</button>
</div>
{/if}
</div>
Expand Down
21 changes: 0 additions & 21 deletions src/lib/event.svelte.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,25 +157,4 @@ if (import.meta.vitest) {

await expect(event.update(1, input as Infer<FormSchema>)).rejects.toThrow();
});

it('should update the event if all required values are provided', async () => {
const input: Partial<EventRow> = {
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<FormSchema>)).resolves.toEqual(event);

expect(event).toEqual(updatedEvent);
expect(mockDatabaseService.updateEvent).toHaveBeenCalledWith(1, input as Infer<FormSchema>);
});
}
52 changes: 52 additions & 0 deletions src/lib/matches.svelte.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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++;

Expand All @@ -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;
Expand Down Expand Up @@ -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();
});
});
});
}

0 comments on commit 9ae3352

Please sign in to comment.