Skip to content

Commit 69706b6

Browse files
committed
Reformatted completions to track challenge id to ensure data connection after potential challenge edits from admins
1 parent b966f54 commit 69706b6

1 file changed

Lines changed: 32 additions & 16 deletions

File tree

src/backend/db.js

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -243,17 +243,17 @@ async function UpdateTeamCompletions(team_id) {
243243

244244
for (const data of Object.entries(memberProfile.completions)) {
245245
// console.log(`Completions of ${memberProfile.username}`, memberProfile.completions)
246-
const [index, { name, time }] = data; // break down the entry
246+
const [index, { id, time }] = data; // break down the entry
247247
// console.log("Completion Data -> ", { name, time });
248248

249-
const challengeProfile = await ChallengeCollection.findOne({ name: name.replaceAll('_', ' ') })
249+
const challengeProfile = await ChallengeCollection.findOne({ _id: id })
250250
if (challengeProfile) {
251251
// Find if the challenge already exists in mergedCompletions
252-
const existingChallenge = mergedCompletions.find(completion => completion.name === name);
252+
const existingChallenge = mergedCompletions.find(completion => completion.id === id);
253253

254254
// If challenge doesn't exist or the current timestamp is older, add/update the challenge
255255
if (!existingChallenge || time < existingChallenge.timestamp) {
256-
const newCompletion = { name: name, memberId: memberId, points: challengeProfile.points, timestamp: time };
256+
const newCompletion = { id: id, memberId: memberId, points: challengeProfile.points, timestamp: time };
257257

258258
// Remove the existing challenge entry if it exists
259259
if (existingChallenge) {
@@ -1164,16 +1164,13 @@ async function ValidateFlag(challenge_id, flag_value, jwt) {
11641164
// check if the user has already claimed the flag:
11651165
// before doing an insert check if there is an object with
11661166
// a name attribute matching simplifiedChallengeName
1167-
// EX: completions: [ { name: 'BasicWebExploit', time: 1746503187547 } ]
1167+
1168+
// EX: completions: [ { id: 'e168c4626a', time: 1746503187547 } ]
11681169
const userProfile = await UserCollection.findOne({ username: jwt.username });
1169-
const simplifiedChallengeName = chall.name.replaceAll(' ', '_');
11701170
if (userProfile) {
11711171
const currentCompletions = userProfile.completions;
11721172
for (const claim of currentCompletions) {
1173-
// console.log("Reviewing Claim");
1174-
// console.log(`|____ ${claim.name} === ${simplifiedChallengeName}`);
1175-
if (claim.name === simplifiedChallengeName) {
1176-
// console.log("CLAIM ALREADY EXISTS!");
1173+
if (claim.id === challenge_id) {
11771174
return {
11781175
message: "Already Claimed this Flag!"
11791176
};
@@ -1199,7 +1196,7 @@ async function ValidateFlag(challenge_id, flag_value, jwt) {
11991196
{
12001197
$addToSet: {
12011198
completions: {
1202-
name: simplifiedChallengeName, time: Date.now()
1199+
id: challenge_id, time: Date.now()
12031200
}
12041201
}
12051202
}
@@ -1230,28 +1227,47 @@ async function ValidateFlag(challenge_id, flag_value, jwt) {
12301227

12311228
async function ConvertCompletions(userCompletions, teamCompletions) {
12321229
console.log("[*] Attempting Conversions. . .")
1233-
// console.log("|____ User Completions: ", userCompletions);
1234-
// console.log("|____ Team Completions: ", teamCompletions);
1230+
1231+
if (userCompletions && userCompletions.length > 0) {
1232+
// create new attribute name based on id attribute
1233+
let readableUserCompletions = userCompletions;
1234+
for (let item of readableUserCompletions) {
1235+
const challengeProfile = await ChallengeCollection.findOne({ _id: item.id })
1236+
if (challengeProfile) {
1237+
item['name'] = challengeProfile.name;
1238+
}
1239+
}
1240+
}
12351241

1236-
if (teamCompletions) {
1242+
if (teamCompletions && teamCompletions.length > 0) {
12371243
// Iterate through data and modify memberId using for...of to handle async correctly
1238-
for (const item of teamCompletions) {
1244+
for (let item of teamCompletions) {
12391245
const memberProfile = await UserCollection.findOne({ _id: item.memberId });
12401246
if (memberProfile) {
12411247
const memberUsername = memberProfile.username;
12421248
// Replace memberId with memberName
12431249
item.memberName = memberUsername; // Add memberName attribute
12441250
delete item.memberId; // Delete memberId attribute
12451251
}
1252+
1253+
// create new attribute name based on id attribute
1254+
const challengeProfile = await ChallengeCollection.findOne({ _id: item.id })
1255+
if (challengeProfile) {
1256+
item['name'] = challengeProfile.name;
1257+
}
12461258
}
12471259

1248-
// console.log("[*] Team Completions Modified: ", teamCompletions);
1260+
console.log("|____ User Completions: ", userCompletions);
1261+
console.log("|____ Team Completions: ", teamCompletions);
12491262

12501263
return {
12511264
"userCompletions": userCompletions,
12521265
"teamCompletions": teamCompletions,
12531266
}
12541267
} else {
1268+
console.log("|____ User Completions: ", userCompletions);
1269+
console.log("|____ Team Completions: ", teamCompletions);
1270+
12551271
return {
12561272
"userCompletions": userCompletions,
12571273
"teamCompletions": [],

0 commit comments

Comments
 (0)