Skip to content

Commit 48400b5

Browse files
authored
Add CTFd.pages.challenge.getSolution and CTFd.pages.challenge.checkSolution (#33)
* Add `CTFd.pages.challenge.getSolution` * Add `CTFd.pages.challenge.checkSolution`
1 parent ed0eafa commit 48400b5

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

main.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@ import {
1111
loadUnlock,
1212
displayUnlock,
1313
displayHint,
14+
getSolution,
1415
loadSolution,
1516
displaySolution,
1617
displayHintUnlock,
1718
displaySolutionUnlock,
1819
submitRating,
20+
checkSolution,
1921
} from "./pages/challenge";
2022
import { getScoreboard, getScoreboardDetail, getBrackets } from "./pages/scoreboard";
2123
import { updateSettings, generateToken, deleteToken } from "./pages/settings";
@@ -93,6 +95,9 @@ const _functions = {
9395

9496
// Display solves for challenge
9597
displaySolves: null,
98+
99+
// Check whether UI should reach out for solution ID
100+
checkSolution: null,
96101
},
97102

98103
challenges: {
@@ -142,6 +147,8 @@ const pages = {
142147
displayHintUnlock,
143148
displaySolutionUnlock,
144149
displayHint,
150+
getSolution,
151+
checkSolution,
145152
loadSolution,
146153
displaySolution,
147154
submitRating,

pages/challenge.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,34 @@ export async function displaySolves(challengeId) {
133133
}
134134
}
135135

136+
export async function getSolution(challengeId) {
137+
const response = await CTFd.fetch(`/api/v1/challenges/${challengeId}/solution`, {
138+
method: "GET",
139+
});
140+
141+
const body = await response.json();
142+
return body["data"];
143+
}
144+
145+
// Function to check whether the UI should check for a challenge solution
146+
export async function checkSolution(solutionState, challengeData, submissionStatus) {
147+
if (CTFd._functions.challenge.checkSolution) {
148+
return CTFd._functions.challenge.checkSolution(
149+
solutionState,
150+
challengeData,
151+
submissionStatus
152+
);
153+
}
154+
if (solutionState == "hidden" || solutionState == "visible") {
155+
return false;
156+
} else if (solutionState == "solved" && submissionStatus === "correct") {
157+
return true;
158+
} else {
159+
// We default to true in case there is a solution state that we are not aware of
160+
return true;
161+
}
162+
}
163+
136164
export async function loadSolution(solutionId) {
137165
const response = await CTFd.fetch(`/api/v1/solutions/${solutionId}`, {
138166
method: "GET",

0 commit comments

Comments
 (0)