Skip to content

Commit

Permalink
Add rankings + records by island
Browse files Browse the repository at this point in the history
  • Loading branch information
xbjfk committed Mar 11, 2024
1 parent 3501ead commit 87de9bc
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 15 deletions.
1 change: 1 addition & 0 deletions src/lib/data/regions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const regions: {[key in Region]: {name: string, maori_name?: string}} = {
TARANAKI: { name: "Taranaki" },
MANAWATU_WHANGANUI: { name: "Manawatū" },
WELLINGTON: { name: "Wellington", maori_name: "Te Whanga-nui-a-Tara" },

TASMAN: { name: "Tasman", maori_name: "Te Tai-o-Aorere" },
NELSON: { name: "Nelson", maori_name: "Whakatū" },
MARLBOROUGH: { name: "Marlborough", maori_name: "Te Tauihu-o-te-waka" },
Expand Down
9 changes: 8 additions & 1 deletion src/routes/(app)/rankings/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import prisma from '$lib/prisma';
import type { PageServerLoad } from './$types';
import { Puzzle } from "$lib/db/enums"
import { redirect } from '@sveltejs/kit';
import { northIslandRegions, southIslandRegions } from '$lib/data/regions';

export const load = (async ({ url }) => {
const filterRegion = url.searchParams.has("region") ? url.searchParams.get("region")! : undefined;
Expand Down Expand Up @@ -57,7 +58,13 @@ export const load = (async ({ url }) => {
}

if (!(filterRegion === undefined || filterRegion === null || filterRegion === "undefined")) {
query = query.where('user_region', '=', filterRegion)
if (filterRegion == "NORTH_ISLAND") {
query = query.where('user_region', 'in', northIslandRegions)
} else if (filterRegion == "SOUTH_ISLAND") {
query = query.where('user_region', 'in', southIslandRegions)
} else {
query = query.where('user_region', '=', filterRegion)
}
}

return {
Expand Down
23 changes: 18 additions & 5 deletions src/routes/(app)/rankings/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { page } from "$app/stores";
import { goto } from "$app/navigation";
import regions, { regionToString } from "$lib/data/regions";
import regions, { northIslandRegions, regionToString, southIslandRegions } from "$lib/data/regions";
import MultiButton, {
LabelType,
Expand Down Expand Up @@ -82,10 +82,19 @@
<p class="label">Region</p>

<Select name="region" bind:value={regionSelected}>
<option selected value>All Regions</option>
{#each Object.keys(regions) as value}
<option {value}>{regionToString(value)}</option>
{/each}
<option class=bold selected value>All Regions</option>
<optgroup label="North Island">
<option class=bold value="NORTH_ISLAND">All North Island</option>
{#each northIslandRegions as value}
<option {value}>{regionToString(value)}</option>
{/each}
</optgroup>
<optgroup label="South Island">
<option class=bold value="SOUTH_ISLAND">All South Island</option>
{#each southIslandRegions as value}
<option {value}>{regionToString(value)}</option>
{/each}
</optgroup>
</Select>
</div>

Expand Down Expand Up @@ -144,4 +153,8 @@
.filter-bar:last-of-type {
padding-bottom: 16px;
}
.bold {
font-weight: bold;
}
</style>
25 changes: 21 additions & 4 deletions src/routes/(app)/records/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import puzzles from '$lib/data/puzzles'
import { db } from '$lib/db';
import { sql } from 'kysely';
import { redirect } from '@sveltejs/kit';
import { northIslandRegions, southIslandRegions } from '$lib/data/regions';

export const load = (async ({ url }) => {
const filterRegion = url.searchParams.has("region") ? url.searchParams.get("region") : undefined;
Expand All @@ -19,6 +20,14 @@ export const load = (async ({ url }) => {

const hasFilterRegion = !(filterRegion === undefined || filterRegion === null || filterRegion === "undefined")

const filterRegionIsIsland = filterRegion == "NORTH_ISLAND" || filterRegion == "SOUTH_ISLAND"

let filterIsland = null;
if (filterRegionIsIsland) {
filterIsland = filterRegion == "NORTH_ISLAND" ? northIslandRegions : southIslandRegions
}


const records = async () => {
const records = {}
// TODO: subquery and group by
Expand All @@ -41,7 +50,10 @@ export const load = (async ({ url }) => {
.where('solve.time', '!=', Infinity)


if (hasFilterRegion) {
if (filterIsland) {
singleQuery = singleQuery
.where('user.region', 'in', filterIsland)
} else if (hasFilterRegion) {
singleQuery = singleQuery
.where('user.region', '=', filterRegion)
}
Expand Down Expand Up @@ -73,7 +85,10 @@ export const load = (async ({ url }) => {
.groupBy(['result.value', 'user.id', 'user.name', 'user.region', 'meetup.id', 'meetup.name', 'result.mbld_score', 'result.mbld_total'])
.orderBy(['result.mbld_score desc', 'value asc'])

if (hasFilterRegion) {
if (filterIsland) {
averageQuery = averageQuery
.where('user.region', 'in', filterIsland)
} else if (hasFilterRegion) {
averageQuery = averageQuery
.where('user.region', '=', filterRegion)
}
Expand Down Expand Up @@ -118,7 +133,8 @@ export const load = (async ({ url }) => {
])
.groupBy(['result.value', 'user.name', 'user.id', 'round.puzzle', 'round.end_date', 'meetup.id', 'result.mbld_score', 'result.mbld_total'])
.where('result.value', '!=', Infinity)
.$if(hasFilterRegion, qb => qb.where('user.region', '=', filterRegion))
.$if(!!filterIsland, qb => qb.where('user.region', 'in', filterIsland))
.$if(hasFilterRegion && !filterIsland, qb => qb.where('user.region', '=', filterRegion))
// Must order by time so distinct on picks correct value
// solves desc is so null solves are at the top - which means a single
.orderBy(['cum_min asc', 'value asc'])
Expand Down Expand Up @@ -154,7 +170,8 @@ export const load = (async ({ url }) => {
.groupBy(['solve.time', 'user.name', 'user.id', 'round.puzzle', 'round.end_date', 'meetup.id'])
.orderBy(['cum_min asc', 'time asc'])
.where('solve.time', '!=', Infinity)
.$if(hasFilterRegion, qb => qb.where('user.region', '=', filterRegion))
.$if(!!filterIsland, qb => qb.where('user.region', 'in', filterIsland))
.$if(hasFilterRegion && !filterIsland, qb => qb.where('user.region', '=', filterRegion))
)
.selectFrom('ungrouped')
.select((eb) => [
Expand Down
19 changes: 14 additions & 5 deletions src/routes/(app)/records/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import { formatTime } from "$lib/utils";
import regions, { regionToString } from "$lib/data/regions";
import regions, { northIslandRegions, regionToString, southIslandRegions } from "$lib/data/regions";
import puzzles from "$lib/data/puzzles";
import type { Region } from "@prisma/client";
Expand Down Expand Up @@ -71,10 +71,19 @@
<p class="label">Region</p>

<Select name="region" bind:value={regionSelected}>
<option selected value>All Regions</option>
{#each Object.keys(regions) as value}
<option {value}>{regionToString(value)}</option>
{/each}
<option class=bold selected value>All Regions</option>
<optgroup label="North Island">
<option class=bold value="NORTH_ISLAND">All North Island</option>
{#each northIslandRegions as value}
<option {value}>{regionToString(value)}</option>
{/each}
</optgroup>
<optgroup label="South Island">
<option class=bold value="SOUTH_ISLAND">All South Island</option>
{#each southIslandRegions as value}
<option {value}>{regionToString(value)}</option>
{/each}
</optgroup>
</Select>
</div>

Expand Down

0 comments on commit 87de9bc

Please sign in to comment.