-
Notifications
You must be signed in to change notification settings - Fork 1
Leaderboard backend and frontend #26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 6 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
b88e78c
Added leaderboard router for getting all problems by users and for a …
biweep863 8346bae
Added UI test for leaderboard router
biweep863 ab7f08d
Added slider for week selection and improved ui
biweep863 ca47d14
Delete status variable in problem model
biweep863 ab4795a
Fix the status of problems, now depending on each user.
biweep863 8873c63
Update problem.ts
biweep863 ac39347
Added suggestions from pr
biweep863 90a1ba9
Rename Nav to temp_nav
biweep863 ca096f6
Rename temp_nav to nav
biweep863 aaf49e5
Fix any type used in weeksData
biweep863 ae83605
Fix leaderboard page
biweep863 675a5a5
fix footer position
Fernando94654 7cf3bbb
Merge branch 'leaderboard-backend' of https://github.com/RoBorregos/s…
Fernando94654 68cd772
highlight the currenUser in green
Fernando94654 20653fc
Fix build
biweep863 2173884
fix highlight firt-place and currenUser
Fernando94654 f2de0fa
fix highlight firt-place and currenUser
Fernando94654 95fb0c2
Added text format and await for async functions
biweep863 20c43e1
Update week.ts
biweep863 161b733
Added await for async function in week.ts
biweep863 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,91 @@ | ||
|
|
||
| "use client"; | ||
| import { useState } from "react"; | ||
| import { api } from "~/trpc/react"; | ||
|
|
||
| const Leaderboard = () => { | ||
| return ( | ||
| <div> | ||
| Hello | ||
| const [selected, setSelected] = useState("all"); | ||
|
|
||
| // Fetch weeks for the slider | ||
| const { data: weeksData, isLoading: weeksLoading } = api.week.getWeeks.useQuery(); | ||
|
|
||
| // Fetch leaderboard data | ||
| const { | ||
| data: leaderboardData, | ||
| isLoading: leaderboardLoading, | ||
| error: leaderboardError, | ||
| } = selected === "all" | ||
| ? api.leaderboard.getAll.useQuery() | ||
| : api.leaderboard.getByWeek.useQuery(selected); | ||
|
|
||
| if (weeksLoading || leaderboardLoading) return <div>Loading...</div>; | ||
| if (leaderboardError) return <div>Error loading leaderboard</div>; | ||
|
|
||
| // Prepare week options | ||
| const weekOptions = [ | ||
| { id: "all", label: "Global" }, | ||
| ...(weeksData | ||
| ? weeksData.map((w: any) => ({ id: w.id?.toString() ?? w.number?.toString(), label: w.title || `Week ${w.number}` })) | ||
| : []), | ||
| ]; | ||
|
|
||
| // Sort leaderboard | ||
| const sorted = [...(leaderboardData ?? [])].sort((a, b) => b.total - a.total); | ||
|
|
||
| return ( | ||
| <div className="max-w-3xl mx-auto mt-12"> | ||
| <h1 className="text-3xl font-extrabold mb-8 text-center tracking-tight text-white drop-shadow"> | ||
| Leaderboard | ||
| </h1> | ||
| {/* Slider/toggle */} | ||
| <div className="flex justify-center mb-6"> | ||
| <div className="inline-flex bg-neutral-800 rounded-full p-1"> | ||
| {weekOptions.map((w) => ( | ||
| <button | ||
| key={w.id} | ||
| onClick={() => setSelected(w.id)} | ||
| className={`px-5 py-2 rounded-full font-semibold transition-all ${ | ||
| selected === w.id | ||
| ? "bg-white text-neutral-900 shadow" | ||
| : "text-white hover:bg-neutral-700" | ||
| }`} | ||
| > | ||
| {w.label} | ||
| </button> | ||
| ))} | ||
| </div> | ||
| ) | ||
| } | ||
| </div> | ||
| {/* Table header */} | ||
| <div className="w-full flex flex-row justify-between bg-neutral-800 text-white rounded-t-2xl px-8 py-3 text-lg font-semibold mb-2"> | ||
| <span className="w-1/4 text-left">User</span> | ||
| <span className="w-1/8 text-center">Warmup</span> | ||
| <span className="w-1/8 text-center">Medium</span> | ||
| <span className="w-1/8 text-center">Harder</span> | ||
| <span className="w-1/8 text-center">Insane</span> | ||
| <span className="w-1/8 text-center">Total</span> | ||
biweep863 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| </div> | ||
| <ol className="flex flex-col gap-4"> | ||
| {sorted.map((row, i) => ( | ||
| <li | ||
| key={i} | ||
| className={`w-full bg-neutral-700 text-white rounded-2xl shadow-lg px-8 py-4 flex flex-row items-center justify-between transition-all | ||
| ${i === 0 ? "bg-gray-700 font-bold scale-[1.03]" : ""} | ||
| ${row.username === "You" ? "bg-green-600 font-bold" : ""} | ||
biweep863 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| `} | ||
| > | ||
| <div className="w-1/4 flex items-center gap-3"> | ||
| <span className="text-xl font-semibold w-8 text-gray-400 text-center">{i + 1}</span> | ||
biweep863 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| <span className="text-base font-medium break-words leading-tight">{row.username}</span> | ||
| </div> | ||
| <span className="w-1/8 text-center text-lg font-bold">{row.completedWarmup}</span> | ||
| <span className="w-1/8 text-center text-lg font-bold">{row.completedMedium}</span> | ||
| <span className="w-1/8 text-center text-lg font-bold">{row.completedHarder}</span> | ||
| <span className="w-1/8 text-center text-lg font-bold">{row.completedInsane}</span> | ||
| <span className="w-1/8 text-center text-lg font-bold">{row.total}</span> | ||
| </li> | ||
| ))} | ||
| </ol> | ||
| </div> | ||
| ); | ||
| }; | ||
|
|
||
| export default Leaderboard; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| import { z } from "zod"; | ||
| import { createTRPCRouter, publicProcedure } from "../trpc"; | ||
|
|
||
| // Map problem levels to leaderboard columns | ||
| const levelMap = { | ||
| warmup: "warmup", | ||
| medium: "medium", | ||
| harder: "harder", | ||
| insane: "insane", | ||
| } as const; | ||
| export const leaderboardRouter = createTRPCRouter({ | ||
| // Full leaderboard | ||
| getAll: publicProcedure.query(async ({ ctx }) => { | ||
| // Fetch users -> solved problems | ||
| const users = await ctx.db.user.findMany({ | ||
| select: { | ||
| id: true, | ||
| name: true, | ||
| solvedProblems: { | ||
| select: { | ||
| level: true, | ||
| weekId: true, | ||
| }, | ||
| }, | ||
| }, | ||
| }); | ||
| // Aggregate leaderboard data from database | ||
| return users.map((user) => { | ||
| const counts = { | ||
| warmup: 0, | ||
| medium: 0, | ||
| harder: 0, | ||
| insane: 0, | ||
| }; | ||
| user.solvedProblems.forEach((p) => { | ||
| // key for level - if not found, default to warmup | ||
| const key = | ||
| levelMap[p.level.toLowerCase() as keyof typeof levelMap] || "warmup"; | ||
| // Increment the count for the corresponding level | ||
| if (counts[key] !== undefined) counts[key]++; | ||
| }); | ||
| return { | ||
| username: user.name, | ||
| completedWarmup: counts.warmup, | ||
| completedMedium: counts.medium, | ||
| completedHarder: counts.harder, | ||
| completedInsane: counts.insane, | ||
| total: | ||
| counts.warmup + | ||
| counts.medium + | ||
| counts.harder + | ||
| counts.insane, | ||
| }; | ||
| }); | ||
| }), | ||
| // Get leaderboard for a specific week | ||
| getByWeek: publicProcedure.input(z.string()).query(async ({ ctx, input }) => { | ||
| const users = await ctx.db.user.findMany({ | ||
| select: { | ||
| id: true, | ||
| name: true, | ||
| solvedProblems: { | ||
| where: { weekId: input }, | ||
| select: { level: true }, | ||
| }, | ||
| }, | ||
| }); | ||
|
|
||
| return users.map((user) => { | ||
| const counts = { | ||
| warmup: 0, | ||
| medium: 0, | ||
| harder: 0, | ||
| insane: 0, | ||
| }; | ||
| user.solvedProblems.forEach((p) => { | ||
| const key = | ||
| levelMap[p.level.toLowerCase() as keyof typeof levelMap] || "warmup"; | ||
| if (counts[key] !== undefined) counts[key]++; | ||
| }); | ||
| return { | ||
| username: user.name, | ||
| completedWarmup: counts.warmup, | ||
| completedMedium: counts.medium, | ||
| completedHarder: counts.harder, | ||
| completedInsane: counts.insane, | ||
| total: | ||
| counts.warmup + | ||
| counts.medium + | ||
| counts.harder + | ||
| counts.insane, | ||
| }; | ||
| }); | ||
| }), | ||
| }); |
Oscar-gg marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.