Skip to content

Commit

Permalink
Experiment with non-standard properties, prereqs
Browse files Browse the repository at this point in the history
  • Loading branch information
ky28059 committed Nov 9, 2024
1 parent 7cdd5d9 commit 05e65ad
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 10 deletions.
39 changes: 33 additions & 6 deletions app/challenges/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import CTFNotStarted from '@/components/CTFNotStarted';
// Utils
import { getChallenges } from '@/util/challenges';
import { getMyProfile } from '@/util/profile';
import { getAdminChallenges } from '@/util/admin';
import { AUTH_COOKIE_NAME } from '@/util/config';


Expand All @@ -27,20 +28,46 @@ export default async function ChallengesPage() {
if (profile.kind === 'badToken')
return redirect('/logout');

return challenges.kind === 'goodChallenges' ? (
if (challenges.kind !== 'goodChallenges') return (
<CTFNotStarted />
);

// Support non-standard properties by sourcing them from the admin endpoint.
const adminData = await getAdminChallData();
let challs = challenges.data;

if (adminData) {
// Filter out challs with prereqs that are not met yet
const solved = new Set(profile.data.solves.map((c) => c.id));
challs = challs.filter((c) => !adminData[c.id].prereqs || adminData[c.id].prereqs!.every((p) => solved.has(p)));

// Inject desired properties back into client challenges
for (const c of challs) {
c.difficulty = adminData[c.id].difficulty;
}
}

return (
<div className="container relative pt-32 pb-14 flex flex-col md:flex-row gap-6">
<Filters
challenges={challenges.data}
challenges={challs}
solves={profile.data.solves}
/>
<Challenges
challenges={challenges.data}
challenges={challs}
solves={profile.data.solves}
/>

<DisplayToggle />
</div>
) : (
<CTFNotStarted />
)
);
}

async function getAdminChallData() {
if (!process.env.ADMIN_TOKEN) return;

const res = await getAdminChallenges(process.env.ADMIN_TOKEN);
if (res.kind === 'badToken') return;

return Object.fromEntries(res.data.map((c) => [c.id, c]));
}
8 changes: 4 additions & 4 deletions util/admin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ import type { Challenge } from '@/util/challenges';
import type { BadTokenResponse } from '@/util/errors';


export type AdminChallenge = Challenge & {
export type AdminChallenge = Exclude<Challenge, 'solves' | 'points' | 'sortWeight'> & {
flag: string,
points: { min: number, max: number }

prereqs?: string[], // Non-standard
}

type AdminChallengesResponse = {
Expand All @@ -15,9 +17,7 @@ type AdminChallengesResponse = {

export async function getAdminChallenges(token: string): Promise<AdminChallengesResponse | BadTokenResponse> {
const res = await fetch(`${process.env.API_BASE}/admin/challs`, {
headers: {
'Authorization': `Bearer ${token}`
}
headers: { 'Authorization': `Bearer ${token}` }
});

return res.json();
Expand Down
2 changes: 2 additions & 0 deletions util/challenges.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ export type Challenge = {
sortWeight: number,
solves: number,
points: number,

difficulty?: string, // Non-standard
}

type FileData = {
Expand Down

0 comments on commit 05e65ad

Please sign in to comment.