Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
craigkai committed Feb 21, 2024
1 parent 2fbebcb commit 3cf6542
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 19 deletions.
12 changes: 9 additions & 3 deletions src/components/Matches.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,15 @@
You already have some match content, are you sure you want to wipe that?
</p>
<div class="flex gap-2">
<Button class="text-black" size="xs" on:click={generateMatches}>Yes</Button>
<Button class="text-black" size="xs" on:click={() => (showGenerateMatchesAlert = false)}
>No</Button
<Button
class="text-black bg-nord-10 hover:bg-nord-9 text-white dark:text-nord-1 font-bold py-2 px-4 rounded focus:outline-none focus:shadow-outline"
size="xs"
on:click={generateMatches}>Yes</Button
>
<Button
class="text-black bg-nord-10 hover:bg-nord-9 text-white dark:text-nord-1 font-bold py-2 px-4 rounded focus:outline-none focus:shadow-outline"
size="xs"
on:click={() => (showGenerateMatchesAlert = false)}>No</Button
>
</div>
</Alert>
Expand Down
22 changes: 15 additions & 7 deletions src/lib/brackets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,24 +93,32 @@ export class Brackets extends Matches {
const otherParent = this.matches?.find(
(m) => m.child_id === newMatch.child_id && m.id !== newMatch.id
);
const otherParentComplete = this.matches!.length > 3 ? otherParent?.state === 'complete' : true;

try {
if (newMatch.state === 'complete' && otherParent?.state === 'complete' && child) {
if (newMatch.state === 'complete' && otherParentComplete && child) {
const childTeams = [child.team1, child.team2];
// We ignore null as we trust status === 'complete'
// @ts-ignore: Object is possibly 'null'.
const winnerOfNew =
// We ignore null as we trust status === 'complete'
// @ts-ignore: Object is possibly 'null'.
newMatch.team1_score > newMatch.team2_score ? newMatch.team1 : newMatch.team2;
// @ts-ignore: Object is possibly 'null'.
const winnierOfOtherParent =
otherParent.team1_score > otherParent.team2_score ? otherParent.team1 : otherParent.team2;
let winnerOfOtherParent;
if (otherParent) {
// @ts-ignore: Object is possibly 'null'.
winnerOfOtherParent =
otherParent?.team1_score > otherParent?.team2_score
? otherParent!.team1
: otherParent!.team2;
} else {
winnerOfOtherParent = child.team1 || child.team2;
}

if (childTeams.includes(winnerOfNew)) {
console.debug('Child match already has the correct teams');
return;
} else {
child.team1 = winnerOfNew;
child.team2 = winnierOfOtherParent;
child.team2 = winnerOfOtherParent;
const newBracketMatch: Partial<MatchRow> = {
...child,
team1_score: 0,
Expand Down
18 changes: 9 additions & 9 deletions src/lib/matches.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,14 @@ export class Matches extends Base {
}
}

async put(match: MatchRow): Promise<MatchRow | null> {
const updatedMatch = await this.databaseService.updateMatch(match);
if (updatedMatch === null) {
this.handleError(500, 'Failed to update match.');
}
return updatedMatch;
}

validateInputs(
teams: TeamRow[],
pools: number | undefined | null,
Expand Down Expand Up @@ -190,7 +198,7 @@ export class Matches extends Base {
return;
}

if (pools && userMatches.length === pools * (teams.length / 2)) {
if (pools && userMatches.length === pools * teams.length) {
// Short circuit if we have more matches than pool play games
return;
}
Expand Down Expand Up @@ -252,14 +260,6 @@ export class Matches extends Base {
});
}

async put(match: MatchRow): Promise<MatchRow | null> {
const updatedMatch = await this.databaseService.updateMatch(match);
if (updatedMatch === null) {
this.handleError(500, 'Failed to update match.');
}
return updatedMatch;
}

determineReferee(
teamsPerRound: [{ number: number[] }],
teams: number[],
Expand Down

0 comments on commit 3cf6542

Please sign in to comment.