Skip to content

Commit

Permalink
Merge pull request #6 from QPixel/feat/leaderboard-totals
Browse files Browse the repository at this point in the history
Leaderboard Totals
  • Loading branch information
QPixel authored Apr 1, 2024
2 parents 30e4288 + e533956 commit 034d2fa
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 24 deletions.
13 changes: 8 additions & 5 deletions src/lib/components/get-leaderboard.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { writable } from "svelte/store";
import type { LeaderboardData } from "src/pages/api/leaderboard/all";


function getLeaderboard() {
const { subscribe, set, update } = writable<LeaderboardData>();

Expand All @@ -12,18 +13,19 @@ function getLeaderboard() {
update_local: (id: number, resolved_username = "", current_quarter = "") => {
update((v) => {

if (!v[current_quarter]) {
v[current_quarter] = [];
if (!v[current_quarter] || !v[current_quarter].data) {
v[current_quarter].data = [];
}

let newData = v[current_quarter];
let newData = v[current_quarter].data;
if (!newData.some((entry) => entry.updated_by === id)) {
newData.push({
updated_by: id,
total: 1,
resolved_username: resolved_username !== "" ? resolved_username : "Unknown",
});
v[current_quarter] = newData.sort((a, b) => b.total - a.total);
v[current_quarter].data = newData.sort((a, b) => b.total - a.total);
v[current_quarter].total = v[current_quarter].total + 1;
}

if (newData.some((entry) => entry.updated_by === id)) {
Expand All @@ -36,7 +38,8 @@ function getLeaderboard() {
}
return entry;
});
v[current_quarter] = newData.sort((a, b) => b.total - a.total);
v[current_quarter].data = newData.sort((a, b) => b.total - a.total);
v[current_quarter].total = v[current_quarter].total + 1;
}
return v;
})
Expand Down
6 changes: 4 additions & 2 deletions src/lib/components/leaderboard.svelte
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<script lang="ts">
import { leaderboard_store } from "./get-leaderboard";
import * as Tabs from "$lib/components/ui/tabs";
import type { LeaderboardData } from "src/pages/api/leaderboard/all";
export let leaderboardData: any;
export let leaderboardData: LeaderboardData;
export let leaderboardQuarters: string[];
leaderboard_store.init(leaderboardData);
Expand All @@ -19,8 +20,9 @@
{#each leaderboardQuarters as quarter}
<Tabs.Content value={quarter}>
<div class="space-y-4">
<p class="text-md">Quarter Total: {$leaderboard_store[quarter].total}</p>
<ol class="divide-solid divide-y border-2 rounded-lg p-2">
{#each $leaderboard_store[quarter] as player, i}
{#each $leaderboard_store[quarter].data as player, i}
<li class="text-xl p-2">
{i + 1}. {player.resolved_username} - {player.total}
</li>
Expand Down
2 changes: 1 addition & 1 deletion src/pages/[id].astro
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ const leaderboardQuarters = await getQuarters();
/>
</CardContent>
</Card>
<Card class="w-full min-h-[410px]">
<Card class="w-full min-h-[450px]">
<CardHeader>
<CardTitle class={"text-4xl"}>Leaderboard</CardTitle>
</CardHeader>
Expand Down
30 changes: 19 additions & 11 deletions src/pages/api/counter.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { createClient } from "@vercel/kv";
import type { APIRoute } from "astro";
import { Womps, db, eq, sql } from "astro:db";
import { Womps, and, db, eq, sql } from "astro:db";
import { compareAsc } from "date-fns";
import { get } from "svelte/store";

export const prerendered = false;

Expand All @@ -19,18 +20,28 @@ async function resolveUsernameFromId(id: number) {
return username ? username : "Unknown";
}

export async function getCounterData() {
async function getCounterWithQuarter(currentQuarter: string) {
let wompQuery = db.select({
max_date: sql<Date>`max(last_updated)`.as("max_date"),
total: sql<number>`count(*)`.as("total"),
}).from(Womps).as("wompQuery");
}).from(Womps).where(eq(Womps.quarter_id, currentQuarter)).as("wompQuery");

let womps = await db.select({
updated_by: Womps.updated_by,
last_updated: Womps.last_updated,
total: sql<number>`wompQuery.total`,
quarter: Womps.quarter_id,
}).from(wompQuery).innerJoin(Womps, eq(Womps.last_updated, wompQuery.max_date)).limit(1);
return womps;
}

export async function getCounterData() {
let currentQuarter = await kv.get<string>("current_quarter");
if (!currentQuarter) {
currentQuarter = "24-Q2";
}
let womps = await getCounterWithQuarter(currentQuarter);

if (!womps || womps.length == 0 || !womps[0].last_updated) {
return {
total: 0,
Expand Down Expand Up @@ -130,13 +141,10 @@ export const POST: APIRoute = async ({ cookies }) => {
last_updated: Womps.last_updated,
updated_by: Womps.updated_by,
});
let total = (
await db
.select({ total: sql<number>`cast(count(*) as int)`.as("total") })
.from(Womps)
.limit(1)
).at(0);


let total = await db.select({
total: sql<number>`count(*)`.as("total"),
}).from(Womps).where(eq(Womps.quarter_id, currentQuarter));

if (!data || !total) {
return new Response("Failed to update counter", { status: 500 });
Expand All @@ -150,7 +158,7 @@ export const POST: APIRoute = async ({ cookies }) => {
JSON.stringify({
last_updated: data[0].last_updated.toISOString(),
updated_by: data[0].updated_by,
total: total.total,
total: total[0].total,
resolved_username,
current_quarter: currentQuarter,
}),
Expand Down
20 changes: 16 additions & 4 deletions src/pages/api/leaderboard/all.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,37 @@ const kv = createClient({
token: REDIS_REST_API_TOKEN,
});



export async function getLeaderboard() {
let wompTotals = await db.select({
updated_by: Womps.updated_by,
total: sql<number>`count(*)`.as("total"),
quarter: Womps.quarter_id,
}).from(Womps).groupBy(Womps.quarter_id, Womps.updated_by).orderBy(desc(sql`total`)).limit(10);

let wompData: Record<string, Array<Omit<typeof wompTotals[0], "quarter"> & {resolved_username: string}>> = {}
let womp = await db.select({
total: sql<number>`count(*)`.as("total"),
quarter: Womps.quarter_id,
}).from(Womps).groupBy(Womps.quarter_id).orderBy(desc(sql`total`));
console.log(womp);
let wompData: Record<string, {
data: Array<Omit<typeof wompTotals[0], "quarter"> & {resolved_username: string}>;
total: number;
}> = {}
for (let womp of wompTotals) {
let username = await kv.get<string>(`user:${womp.updated_by}`);
if (!wompData[womp.quarter]) {
wompData[womp.quarter] = [];
wompData[womp.quarter] = { data: [], total: 0 };
}
wompData[womp.quarter].push({
wompData[womp.quarter].data.push({
total: womp.total,
updated_by: womp.updated_by,
resolved_username: username ? username : "Unknown",
});
}
for (let total of womp) {
wompData[total.quarter].total = total.total;
}

return wompData;
}
Expand Down
2 changes: 1 addition & 1 deletion src/pages/index.astro
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const leaderboardQuarters = await getQuarters();
/>
</CardContent>
</Card>
<Card class="w-full min-h-[410px]">
<Card class="w-full min-h-[450px]">
<CardHeader>
<CardTitle class={"text-4xl"}>Leaderboard</CardTitle>
</CardHeader>
Expand Down

0 comments on commit 034d2fa

Please sign in to comment.