Skip to content

Commit

Permalink
add code for subscribing to bracket matches
Browse files Browse the repository at this point in the history
  • Loading branch information
craigkai committed Feb 16, 2024
1 parent 6b3a1a3 commit c2da8a8
Show file tree
Hide file tree
Showing 5 changed files with 294 additions and 229 deletions.
7 changes: 7 additions & 0 deletions src/components/Bracket.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import { Event } from '$lib/event';
import { Matches } from '$lib/matches';
import type { Teams } from '$lib/teams';
import type { RealtimeChannel } from '@supabase/supabase-js';
export let tournament: Event;
export let matches: Matches;
Expand All @@ -13,6 +14,12 @@
const numRounds = teamNames.length / 2 + (teamNames.length % 2);
let matchesSubscription: RealtimeChannel | undefined;
async function subscribeToMatches() {
matchesSubscription = await matches.subscribeToBracketMatches();
}
subscribeToMatches();
// TODO: Create a listener for when bracket macthes are updated, and auto create the next match in the bracket.
// TODO: Allow bracket matches to be edited.
</script>
Expand Down
2 changes: 1 addition & 1 deletion src/components/Matches.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
let matchesSubscription: RealtimeChannel | undefined;
async function subscribeToMatches() {
matchesSubscription = await matches.subscribeToDB();
matchesSubscription = await matches.subscribeToPoolMatches();
}
subscribeToMatches();
Expand Down
47 changes: 46 additions & 1 deletion src/lib/matches.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,52 @@ export class Matches extends Base {
}
}

async subscribeToDB(): Promise<RealtimeChannel> {
async bracketMatchUpdated(self: Matches,
payload: RealtimePostgresChangesPayload<{
[key: string]: MatchRow;
}>
): Promise<void> {
if ((payload.new as MatchRow)?.type !== 'bracket') {
return;
}
const old = payload.old as MatchRow;
const updated = payload.new as MatchRow;

const matchIndex = self.bracketMatches?.findIndex((m: MatchRow) => m.id === old.id);
if (matchIndex !== undefined && matchIndex !== -1) {
if (self.bracketMatches) {
updated.matches_team1_fkey = self.bracketMatches[matchIndex].matches_team1_fkey;
updated.matches_team2_fkey = self.bracketMatches[matchIndex].matches_team2_fkey;
updated.matches_ref_fkey = self.bracketMatches[matchIndex].matches_ref_fkey;
}

self.bracketMatches?.splice(matchIndex, 1, updated as MatchRow);
const matches = self.bracketMatches;

self._update((that: Matches) => {
that.bracketMatches = matches;
return that;
});
} else {
self.handleError(400, 'Failed to find bracketMatches to update.');
}

// We want to generate the next round of matches when the previous round is complete
console.error("Bracket match updated")

return;
}

async subscribeToBracketMatches(): Promise<RealtimeChannel> {
return await this.databaseService.subscribeToChanges(
this,
this.bracketMatchUpdated,
'matches',
'event_id=eq.' + this.event_id
);
}

async subscribeToPoolMatches(): Promise<RealtimeChannel> {
return await this.databaseService.subscribeToChanges(
this,
this.matchUpdated,
Expand Down
1 change: 1 addition & 0 deletions src/types/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export const matchesRowSchema = z.object({
created_at: z.string(),
event_id: z.number(),
id: z.number(),
type: z.string().nullable(),
ref: z.number().nullable(),
round: z.number(),
team1: z.number(),
Expand Down
Loading

0 comments on commit c2da8a8

Please sign in to comment.