Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions app/c/[cid]/p/[pid]/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<ActionResponse<{ correct: boolean }>> {
): Promise<ActionResponse<{ correct: boolean; remaining: number }>> {
const submittedAt = new Date();

const session = await auth();
Expand Down Expand Up @@ -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,
);
Expand All @@ -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: {
Expand All @@ -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));
}
Expand Down
5 changes: 4 additions & 1 deletion app/c/[cid]/p/[pid]/testsolve.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@ export default function Testsolve({
const router = useRouter();
const [answer, setAnswer] = useState("");
const [wrongAnswer, setWrongAnswer] = useState("");
const [remaining, setRemaining] = useState<number | null>(null);

const trySubmitTestsolve = wrapAction(submitTestsolve, (resp) => {
if (resp.data.correct) {
router.refresh();
} else {
setWrongAnswer(answer);
setRemaining(resp.data.remaining);
setAnswer("");
}
});
Expand Down Expand Up @@ -73,7 +75,8 @@ export default function Testsolve({
<div className="mb-3">
{wrongAnswer && (
<span>
<strong>{wrongAnswer}</strong> is incorrect!
<strong>{wrongAnswer}</strong> is incorrect!{" "}
{remaining !== null && <span> ({remaining}/5)</span>}
</span>
)}
</div>
Expand Down