fixed skip clue implimentation

This commit is contained in:
bobbert committed 2025-03-18 22:25:07 +00:00
1 parent fccc2a4edc
commit 2f9a6c732c
18 files changed
+524 -151

No files matched your search

+41 -19
View File
@@ -1,4 +1,4 @@
import { query, queryOne } from "@/lib/db";
import { query, queryOne, run } from "@/lib/db"; // Add 'run' to the import
import { getToken } from "next-auth/jwt";
export const dynamic = "force-dynamic"; // Disable caching
@@ -25,7 +25,7 @@ export async function POST(request) {
}
// Parse request body
const { clueId } = await request.json();
const { clueId, skipped = false } = await request.json();
// Validate clueId is a number
if (isNaN(parseInt(clueId))) {
@@ -38,49 +38,71 @@ export async function POST(request) {
return Response.json({ error: "Clue not found" }, { status: 404 });
}
// Check if clue was already found
const alreadyFound = await queryOne(
// Check if clue was already found or skipped
const alreadyProcessed = await queryOne(
"SELECT * FROM team_clues WHERE team_id = ? AND clue_id = ?",
[token.teamId, clueId]
);
if (alreadyFound) {
if (alreadyProcessed) {
// If skipping is requested, update the record regardless of previous state
if (skipped) {
await run(
"UPDATE team_clues SET skipped = 1 WHERE team_id = ? AND clue_id = ?",
[token.teamId, clueId]
);
return Response.json({
success: true,
message: "Clue marked as skipped",
skipped: true,
});
}
// For non-skip requests on already processed clues
return Response.json({
success: true,
message: "This clue was already marked as found",
alreadyFound: true,
message: "This clue was already processed",
alreadyFound: !(alreadyProcessed.skipped === 1),
alreadySkipped: alreadyProcessed.skipped === 1,
});
}
// Create team_clues table if it doesn't exist
await queryOne(`
// Ensure the team_clues table has a skipped column
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,
skipped BOOLEAN DEFAULT 0,
PRIMARY KEY (team_id, clue_id),
FOREIGN KEY (team_id) REFERENCES teams(id),
FOREIGN KEY (clue_id) REFERENCES clues(id)
)
`);
// Record that the team found this clue
await queryOne("INSERT INTO team_clues (team_id, clue_id) VALUES (?, ?)", [
token.teamId,
clueId,
]);
// Add column if it doesn't exist (safe operation)
try {
await run("ALTER TABLE team_clues ADD COLUMN skipped BOOLEAN DEFAULT 0");
} catch (e) {
// Column likely already exists, continue
}
console.log(`Clue ${clueId} marked as found for team ${token.teamId}`);
// Record that the team found or skipped this clue
await run(
"INSERT INTO team_clues (team_id, clue_id, skipped) VALUES (?, ?, ?)",
[token.teamId, clueId, skipped ? 1 : 0]
);
// Return successful response
return Response.json({
success: true,
message: "Clue marked as found successfully",
message: skipped ? "Clue skipped successfully" : "Clue marked as found",
skipped: skipped,
});
} catch (error) {
console.error("Error marking clue as found:", error);
console.error("Error marking clue:", error);
return Response.json(
{ error: "Failed to mark clue as found: " + error.message },
{ error: "Failed to process clue: " + error.message },
{ status: 500 }
);
}