diff --git a/app/c/[cid]/p/[pid]/actions.ts b/app/c/[cid]/p/[pid]/actions.ts index 9a35f69..791ee65 100644 --- a/app/c/[cid]/p/[pid]/actions.ts +++ b/app/c/[cid]/p/[pid]/actions.ts @@ -315,11 +315,12 @@ export async function startTestsolve( } const BUFFER_TIME_MILLIS = 10_000; +const SUBMISSION_LIMIT = 5; export async function submitTestsolve( problemId: number, answer: string, -): Promise> { +): Promise> { const submittedAt = new Date(); const session = await auth(); @@ -385,6 +386,12 @@ export async function submitTestsolve( return error("Tried to submit before starting testsolve"); } + if (solveAttempt.numSubmissions >= SUBMISSION_LIMIT) { + return error( + `Reached maximum number of submissions (${SUBMISSION_LIMIT})`, + ); + } + const deadline = new Date( solveAttempt.startedAt.getTime() + testsolveTimeMillis, ); @@ -393,6 +400,10 @@ export async function submitTestsolve( } const correct = answer === problem.answer; + const remaining = Math.max( + 0, + SUBMISSION_LIMIT - (solveAttempt.numSubmissions + 1), + ); await prisma.solveAttempt.update({ where: { @@ -409,7 +420,7 @@ export async function submitTestsolve( }, }); - return { ok: true, data: { correct } }; + return { ok: true, data: { correct, remaining } }; } catch (err) { return error(String(err)); } diff --git a/app/c/[cid]/p/[pid]/testsolve.tsx b/app/c/[cid]/p/[pid]/testsolve.tsx index 0c008b6..76d292f 100644 --- a/app/c/[cid]/p/[pid]/testsolve.tsx +++ b/app/c/[cid]/p/[pid]/testsolve.tsx @@ -25,12 +25,14 @@ export default function Testsolve({ const router = useRouter(); const [answer, setAnswer] = useState(""); const [wrongAnswer, setWrongAnswer] = useState(""); + const [remaining, setRemaining] = useState(null); const trySubmitTestsolve = wrapAction(submitTestsolve, (resp) => { if (resp.data.correct) { router.refresh(); } else { setWrongAnswer(answer); + setRemaining(resp.data.remaining); setAnswer(""); } }); @@ -73,7 +75,8 @@ export default function Testsolve({
{wrongAnswer && ( - {wrongAnswer} is incorrect! + {wrongAnswer} is incorrect!{" "} + {remaining !== null && ({remaining}/5)} )}