Skip to content

Commit

Permalink
Merge pull request #33 from tuckergordon/lottery
Browse files Browse the repository at this point in the history
Lottery
  • Loading branch information
tuckergordon authored Dec 8, 2024
2 parents 2e05745 + 6776d7a commit 8a0e40e
Show file tree
Hide file tree
Showing 8 changed files with 146 additions and 124 deletions.
45 changes: 45 additions & 0 deletions web/src/lib/components/LotteryTable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import type { Standing, Standings } from '$lib/models/Standings.model';

// original team: current team
const TRADED_FIRSTS = {
devspiel: 'tgordon18',
benstone099: 'nachocamacho',
tgordon18: 'BrianEvans',
julianpdx: 'ConQD',
Crams_Clams: 'nachocamacho',
DanBrownOnTrail: 'jseibert',
} as { [key: string]: string };

// TODO: figure out a way to render a svelte component instead of this
// weird JSX-like approach
export function LotteryTable(standings: Standings, tradedPicks = TRADED_FIRSTS, nPicks = 6) {
const lottoStandings = standings.slice(-nPicks).sort((a, b) => a.maxPf - b.maxPf);

const row = (team: Standing, i: number) =>
(i === 6 ? `<tr><td></td></tr>` : '') +
`<tr>
<td>
${i + 1}
</td>
<td>
${team.userName in tradedPicks ? `${tradedPicks[team.userName]} (via ${team.userName})` : team.userName}
</td>
<td>
${team.maxPf}
</td>
</tr>`;
return `
<table>
<thead>
<tr>
<th></th>
<th>Name</th>
<th>Max PF</th>
</tr>
</thead>
<tbody>
${lottoStandings.map(row).join('')}
</tbody>
</table>
`;
}
42 changes: 0 additions & 42 deletions web/src/lib/components/Standings.svelte

This file was deleted.

35 changes: 35 additions & 0 deletions web/src/lib/components/StandingsTable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import type { Standing, Standings } from '$lib/models/Standings.model';

// TODO: figure out a way to render a svelte component instead of this
// weird JSX-like approach
export function StandingsTable(standings: Standings) {
const row = (team: Standing, i: number) =>
(i === 6 ? `<tr><td></td></tr>` : '') +
`<tr>
<td>
${i + 1}
</td>
<td>
${team.teamName}
</td>
<td>
${team.wins}-${team.losses}
</td>
</tr>`;
return `
<table>
<thead>
<tr>
<th></th>
<th>Name</th>
<th>Record</th>
<!-- <th>PF</th>
<th>PA</th> -->
</tr>
</thead>
<tbody>
${standings.map(row).join('')}
</tbody>
</table>
`;
}
55 changes: 55 additions & 0 deletions web/src/lib/db/supabase/standings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import type { Standing } from '$lib/models/Standings.model';
import { supabase } from './supabase-client';

// TODO: type the return value
export async function getStandings(leagueId: bigint, startWeek: number = 1, endWeek: number = 17) {
const { data, error } = await supabase
.from('schedule')
.select(
`
win_h2h:win_h2h.sum(),
win_median: win_median.sum(),
points_for: points_for.sum(),
points_against: points_against.sum(),
max_points_for: max_points_for.sum(),
teams(
name,
user_id,
users(
name
)
)
`,
)
.eq('league_id', leagueId)
.gte('week', startWeek)
.lte('week', endWeek);

if (error) {
console.error('Error fetching standings:', error);
return null;
}

const totalWeeks = endWeek - startWeek + 1;

return data
.map((standing) => {
const wins = standing.win_h2h + standing.win_median;
const losses = totalWeeks * 2 - wins;

return {
// @ts-expect-error - teams is not typed correctly yet
teamName: standing.teams.name,
// @ts-expect-error - teams is not typed correctly yet
userId: standing.teams.user_id,
// @ts-expect-error - teams is not typed correctly yet
userName: standing.teams.users.name,
wins,
losses,
pf: standing.points_for,
pa: standing.points_against,
maxPf: standing.max_points_for,
} as Standing;
})
.sort((a, b) => b.wins - a.wins);
}
2 changes: 2 additions & 0 deletions web/src/lib/models/Standings.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ export type Standings = Standing[];

export interface Standing {
teamName: string;
userId: string;
userName: string;
wins: number;
losses: number;
pf: number;
Expand Down
5 changes: 3 additions & 2 deletions web/src/routes/[leagueId]/+page.svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<script lang="ts">
import { page } from '$app/stores';
import Standings from '$lib/components/Standings.svelte';
import { StandingsTable } from '$lib/components/StandingsTable.js';
let { data } = $props();
const { leagueMetadata, posts, standings } = data;
Expand All @@ -20,7 +20,8 @@
<section class="justify-between md:flex">
<div>
<h2>Standings</h2>
<Standings {standings} />
<!-- eslint-disable-next-line svelte/no-at-html-tags -->
{@html StandingsTable(standings)}
</div>

<ul class="posts mt-0">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { LotteryTable } from '$lib/components/LotteryTable.js';
import { StandingsTable } from '$lib/components/StandingsTable.js';
import { getPost } from '$lib/db/contentful/posts';
import type { Standing, Standings } from '$lib/models/Standings.model';
import { documentToHtmlString, type RenderNode } from '@contentful/rich-text-html-renderer';
import { BLOCKS } from '@contentful/rich-text-types';
import { json } from '@sveltejs/kit';
Expand All @@ -12,40 +13,6 @@ function extractWeekNumber(slug: string): number | null {
return null;
}

// TODO: figure out a way to render a svelte component instead of this
// weird JSX-like approach
function getStandingsHtml(standings: Standings) {
const row = (team: Standing, i: number) =>
(i === 6 ? `<tr><td></td></tr>` : '') +
`<tr>
<td>
${i + 1}
</td>
<td>
${team.teamName}
</td>
<td>
${team.wins}-${team.losses}
</td>
</tr>`;
return `
<table>
<thead>
<tr>
<th></th>
<th>Name</th>
<th>Record</th>
<!-- <th>PF</th>
<th>PA</th> -->
</tr>
</thead>
<tbody>
${standings.map(row).join('')}
</tbody>
</table>
`;
}

export async function GET({ params, fetch }) {
// Technically could pull this out of the post, but then we wouldn't be able
// to query supabase until contentful responded
Expand Down Expand Up @@ -82,7 +49,9 @@ export async function GET({ params, fetch }) {
[BLOCKS.EMBEDDED_ENTRY]: (node) => {
switch (node.data.target.fields.type) {
case 'standings':
return getStandingsHtml(standings);
return StandingsTable(standings);
case 'lottery':
return LotteryTable(standings);
default:
return '';
}
Expand Down
45 changes: 1 addition & 44 deletions web/src/routes/api/leagues/[leagueId]/standings/+server.ts
Original file line number Diff line number Diff line change
@@ -1,49 +1,6 @@
import { supabase } from '$lib/db/supabase/supabase-client';
import { getStandings } from '$lib/db/supabase/standings';
import { error, json } from '@sveltejs/kit';

// TODO: type the return value
async function getStandings(leagueId: bigint, startWeek: number = 1, endWeek: number = 17) {
const { data, error } = await supabase
.from('schedule')
.select(
`
win_h2h:win_h2h.sum(),
win_median: win_median.sum(),
points_for: points_for.sum(),
points_against: points_against.sum(),
max_points_for: max_points_for.sum(),
teams!inner(name)
`,
)
.eq('league_id', leagueId)
.gte('week', startWeek)
.lte('week', endWeek);

if (error) {
console.error('Error fetching standings:', error);
return null;
}

const totalWeeks = endWeek - startWeek + 1;

return data
.map((standing) => {
const wins = standing.win_h2h + standing.win_median;
const losses = totalWeeks * 2 - wins;

return {
// @ts-expect-error - teams is not typed correctly yet
teamName: standing.teams.name,
wins,
losses,
pf: standing.points_for,
pa: standing.points_against,
maxPf: standing.max_points_for,
};
})
.sort((a, b) => b.wins - a.wins);
}

export async function GET({ params, url }) {
const leagueId = BigInt(params.leagueId);
const startWeek = parseInt(url.searchParams.get('startWk') ?? '1');
Expand Down

0 comments on commit 8a0e40e

Please sign in to comment.