Skip to content
Merged
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
12 changes: 12 additions & 0 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import {
displayHint,
loadSolution,
displaySolution,
displayHintUnlock,
displaySolutionUnlock,
} from "./pages/challenge";
import { getScoreboard, getScoreboardDetail, getBrackets } from "./pages/scoreboard";
import { updateSettings, generateToken, deleteToken } from "./pages/settings";
Expand Down Expand Up @@ -65,6 +67,14 @@ const _functions = {
return confirm("Are you sure you'd like to unlock this hint?");
},

displayHintUnlock(hint) {
return confirm("Are you sure you'd like to unlock this hint?");
},

displaySolutionUnlock(solution) {
return confirm("Are you sure you'd like to unlock this solution?");
},

displayUnlockError(unlock) {
const msg = [];

Expand Down Expand Up @@ -128,6 +138,8 @@ const pages = {
loadHint,
loadUnlock,
displayUnlock,
displayHintUnlock,
displaySolutionUnlock,
displayHint,
loadSolution,
displaySolution,
Expand Down
39 changes: 33 additions & 6 deletions pages/challenge.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@ export async function loadHint(hintId) {
return await response.json(); // hint
}

export async function loadUnlock(hintId) {
export async function loadUnlock(targetId, targetType = "hints") {
const response = await CTFd.fetch(`/api/v1/unlocks`, {
method: "POST",
body: JSON.stringify({ target: hintId, type: "hints" }),
body: JSON.stringify({ target: targetId, type: targetType }),
});

return await response.json(); // unlock
Expand All @@ -87,9 +87,9 @@ export async function displayHint(hintId) {
return;
}

let res = await displayUnlock(hint);
let res = await displayHintUnlock(hint);
if (res) {
let unlock = loadUnlock(hintId);
let unlock = await loadUnlock(hintId);

// Display hint or error depending on unlock
if (unlock.success) {
Expand All @@ -100,10 +100,22 @@ export async function displayHint(hintId) {
}
}

/**
* @deprecated since 0.0.16 - use `displayHintUnlock()` instead.
*/
export async function displayUnlock(hint) {
// Deprecated use `displayHintUnlock()` or `displaySolutionUnlock()` instead
return CTFd._functions.challenge.displayUnlock(hint);
}

export async function displayHintUnlock(hint) {
return CTFd._functions.challenge.displayHintUnlock(hint);
}

export async function displaySolutionUnlock(solution) {
return CTFd._functions.challenge.displaySolutionUnlock(solution);
}

// Solves
export async function loadSolves(challengeId) {
const response = await CTFd.fetch(`/api/v1/challenges/${challengeId}/solves`, {
Expand Down Expand Up @@ -132,7 +144,22 @@ export async function loadSolution(solutionId) {

export async function displaySolution(solutionId) {
let solution = await loadSolution(solutionId);
if (CTFd._functions.challenge.displaySolution) {
CTFd._functions.challenge.displaySolution(solution);
if (solution.content) {
if (CTFd._functions.challenge.displaySolution) {
CTFd._functions.challenge.displaySolution(solution);
return;
}
}

let res = await displaySolutionUnlock(solution);
if (res) {
let unlock = await loadUnlock(solution.id, "solutions");

// Display hint or error depending on unlock
if (unlock.success) {
await displaySolution(solutionId);
} else {
CTFd._functions.challenge.displayUnlockError(unlock);
}
}
}