fixing map timing errors and still trying to make leaderboard and clues pages.

Added two menus to the admin dashboard with a navbar at the bottom:

Added navbar button for timer. Added navbar button to edit clues centrally. Added navbar button to view Admin version of leaderboard.

To add:
- the admin version of the leaderboard. has the same sort of initial view as the normal leaderboard however clicking on a team will create a drop down with their team stats.

Grey = cant click
Red = current
Green = complete
blue = skipped?

Need to add Clue page Need to fix Leaderboard.
This commit is contained in:
bobbert committed 2025-03-15 17:10:11 +00:00
1 parent 30d88407a0
commit c098ad40bf
9 files changed
+1217 -29

No files matched your search

+167
View File
@@ -0,0 +1,167 @@
import { getToken } from "next-auth/jwt";
import { queryOne, run } from "@/lib/db";
export const dynamic = "force-dynamic"; // Disable caching
// Ensure the user is an admin
async function verifyAdmin(request) {
const token = await getToken({
req: request,
secret:
process.env.NEXTAUTH_SECRET ||
"your-fallback-secret-should-be-at-least-32-chars",
});
if (!token) {
return { authorized: false, error: "Unauthorized", status: 401 };
}
if (token.role !== "admin") {
return { authorized: false, error: "Admin access required", status: 403 };
}
return { authorized: true, token };
}
// DELETE - Remove a specific clue
export async function DELETE(request, context) {
try {
// Verify admin access
const { authorized, error, status } = await verifyAdmin(request);
if (!authorized) {
return Response.json({ error }, { status });
}
const { clueId } = context.params;
// Validate clueId is a number
if (isNaN(parseInt(clueId))) {
return Response.json({ error: "Invalid clue ID" }, { status: 400 });
}
// Check if clue exists
const clue = await queryOne("SELECT id FROM clues WHERE id = ?", [clueId]);
if (!clue) {
return Response.json({ error: "Clue not found" }, { status: 404 });
}
// Delete clue record
await run("DELETE FROM clues WHERE id = ?", [clueId]);
// Also delete any references in team_clues table
await run(`
CREATE TABLE IF NOT EXISTS team_clues (
team_id INTEGER NOT NULL,
clue_id INTEGER NOT NULL,
found_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (team_id, clue_id),
FOREIGN KEY (team_id) REFERENCES teams(id),
FOREIGN KEY (clue_id) REFERENCES clues(id)
)
`);
await run("DELETE FROM team_clues WHERE clue_id = ?", [clueId]);
return Response.json({
success: true,
message: "Clue deleted successfully",
});
} catch (error) {
console.error("Admin clue deletion error:", error);
return Response.json(
{ error: "Failed to delete clue: " + error.message },
{ status: 500 }
);
}
}
// GET - Get a specific clue
export async function GET(request, context) {
try {
// Verify admin access
const { authorized, error, status } = await verifyAdmin(request);
if (!authorized) {
return Response.json({ error }, { status });
}
const { clueId } = context.params;
// Validate clueId is a number
if (isNaN(parseInt(clueId))) {
return Response.json({ error: "Invalid clue ID" }, { status: 400 });
}
// Get the specific clue - removed coordinates
const clue = await queryOne(
"SELECT id, title, description, location FROM clues WHERE id = ?",
[clueId]
);
if (!clue) {
return Response.json({ error: "Clue not found" }, { status: 404 });
}
return Response.json(clue);
} catch (error) {
console.error("Admin clue fetch error:", error);
return Response.json(
{ error: "Failed to fetch clue: " + error.message },
{ status: 500 }
);
}
}
// PUT - Update a specific clue
export async function PUT(request, context) {
try {
// Verify admin access
const { authorized, error, status } = await verifyAdmin(request);
if (!authorized) {
return Response.json({ error }, { status });
}
const { clueId } = context.params;
// Validate clueId is a number
if (isNaN(parseInt(clueId))) {
return Response.json({ error: "Invalid clue ID" }, { status: 400 });
}
// Parse the request body - removed coordinates
const { title, description, location } = await request.json();
// Validate required fields
if (!title || !description) {
return Response.json(
{ error: "Title and description are required" },
{ status: 400 }
);
}
// Check if clue exists
const clue = await queryOne("SELECT id FROM clues WHERE id = ?", [clueId]);
if (!clue) {
return Response.json({ error: "Clue not found" }, { status: 404 });
}
// Update the clue - removed coordinates
await run(
`
UPDATE clues
SET title = ?, description = ?, location = ?
WHERE id = ?
`,
[title, description, location || "", clueId]
);
return Response.json({
success: true,
message: "Clue updated successfully",
});
} catch (error) {
console.error("Admin clue update error:", error);
return Response.json(
{ error: "Failed to update clue: " + error.message },
{ status: 500 }
);
}
}
+118
View File
@@ -0,0 +1,118 @@
import { getToken } from "next-auth/jwt";
import { query, queryOne, run } from "@/lib/db";
export const dynamic = "force-dynamic"; // Disable caching
// Ensure the user is an admin
async function verifyAdmin(request) {
const token = await getToken({
req: request,
secret:
process.env.NEXTAUTH_SECRET ||
"your-fallback-secret-should-be-at-least-32-chars",
});
if (!token) {
return { authorized: false, error: "Unauthorized", status: 401 };
}
if (token.role !== "admin") {
return { authorized: false, error: "Admin access required", status: 403 };
}
return { authorized: true, token };
}
// GET - Fetch all clues for admin
export async function GET(request) {
try {
// Verify admin access
const { authorized, error, status, token } = await verifyAdmin(request);
if (!authorized) {
return Response.json({ error }, { status });
}
// Ensure clues table exists with simplified structure (no coordinates)
await run(`
CREATE TABLE IF NOT EXISTS clues (
id INTEGER PRIMARY KEY AUTOINCREMENT,
title TEXT NOT NULL,
description TEXT NOT NULL,
location TEXT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
`);
// Fetch all clues with sequential ordering
const clues = await query(`
SELECT id, title, description, location, created_at
FROM clues
ORDER BY id ASC
`);
console.log(`Admin API: Returning ${clues.length} clues`);
return Response.json(clues, {
headers: {
"Cache-Control": "no-store, no-cache, must-revalidate",
Pragma: "no-cache",
Expires: "0",
},
});
} catch (error) {
console.error("Admin clue fetch error:", error);
return Response.json(
{ error: "Failed to fetch clues: " + error.message },
{ status: 500 }
);
}
}
// POST - Add a new clue
export async function POST(request) {
try {
// Verify admin access
const { authorized, error, status } = await verifyAdmin(request);
if (!authorized) {
return Response.json({ error }, { status });
}
// Parse the request body - simplified without coordinates
const { title, description, location = "" } = await request.json();
// Validate required fields
if (!title || !description) {
return Response.json(
{ error: "Title and description are required" },
{ status: 400 }
);
}
// Insert the new clue - simplified without coordinates
await run(
`
INSERT INTO clues (title, description, location)
VALUES (?, ?, ?)
`,
[title, description, location]
);
// Get the inserted clue ID
const newClue = await queryOne("SELECT last_insert_rowid() as id");
return Response.json(
{
success: true,
message: "Clue added successfully",
clueId: newClue.id,
},
{ status: 201 }
);
} catch (error) {
console.error("Admin clue creation error:", error);
return Response.json(
{ error: "Failed to add clue: " + error.message },
{ status: 500 }
);
}
}
+2 -2
View File
@@ -31,9 +31,9 @@ export async function GET(request, context) {
return Response.json({ error: "Invalid clue ID" }, { status: 400 });
}
// Get the specific clue
// Get the specific clue - removed coordinates
const clue = await queryOne(
"SELECT id, title, description, location, x_coord, y_coord FROM clues WHERE id = ?",
"SELECT id, title, description, location FROM clues WHERE id = ?",
[clueId]
);
+2 -2
View File
@@ -24,9 +24,9 @@ export async function GET(request) {
);
}
// Get all clues with explicit ordering to ensure consistency
// Get all clues with explicit ordering - removed coordinates
const clues = await query(`
SELECT id, title, description, location, x_coord, y_coord
SELECT id, title, description, location
FROM clues
ORDER BY id ASC
`);