Skip to content

Commit

Permalink
fixed team bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Tkaixiang committed Dec 26, 2021
1 parent 39bcf81 commit 209fcf7
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 14 deletions.
66 changes: 56 additions & 10 deletions api/controllers/Challenges.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const list = async (req, res) => {
if (currentMeta.visibility === false) {
challenges.splice(i, 1) // remove categories that are hidden
i -= 1
}
}
else {
if ("time" in currentMeta && new Date() < new Date(currentMeta.time[0])) {
challenges[i].challenges = []
Expand Down Expand Up @@ -176,13 +176,29 @@ const show = async (req, res) => {
}
}
}
let challengeCache = NodeCacheObj.get("challengeCache")
const usernameTeamCache = NodeCacheObj.get("usernameTeamCache")
const team = usernameTeamCache[req.locals.username]
if ("requires" in chall && req.locals.perms < 2) {
const solved = await collections.challs.findOne({ _id: MongoDB.ObjectId(chall.requires) }, { projection: { _id: 0, solves: 1 } })
if (!solved) return res.send({ success: false, error: "required-challenge-not-found" })
if (!(solved.solves.includes(req.locals.username))) return res.send({ success: false, error: "required-challenge-not-completed" })
if (NodeCacheObj.get("teamMode") && req.locals.username in usernameTeamCache) {
let solved = false

for (let i = 0; i < challengeCache[chall.requires].solves.length; i++) {
const current = challengeCache[chall.requires].solves[i]
if (current in usernameTeamCache && usernameTeamCache[current] === team) {
solved = true
break
}
}
if (!solved) return res.send({ success: false, error: "required-challenge-not-completed" })
}
else {
if (!(solved.solves.includes(req.locals.username))) return res.send({ success: false, error: "required-challenge-not-completed" })
}

}
const usernameTeamCache = NodeCacheObj.get("usernameTeamCache")
const team = usernameTeamCache[req.locals.username]
if (chall.hints != undefined)
chall.hints.forEach(hint => {
let bought = false
Expand Down Expand Up @@ -275,16 +291,31 @@ const hint = async (req, res) => {
}
}
}
let challengeCache = NodeCacheObj.get("challengeCache")
const usernameTeamCache = NodeCacheObj.get("usernameTeamCache")
if ("requires" in hints && req.locals.perms < 2) {
const solved = await collections.challs.findOne({ _id: MongoDB.ObjectId(hints.requires) }, { projection: { _id: 0, solves: 1 } })
if (!solved) return res.send({ success: false, error: "required-challenge-not-found" })
if (!(solved.solves.includes(req.locals.username))) return res.send({ success: false, error: "required-challenge-not-completed" })
if (NodeCacheObj.get("teamMode") && req.locals.username in usernameTeamCache) {
let solved = false
const team = usernameTeamCache[req.locals.username]
for (let i = 0; i < challengeCache[hints.requires].solves.length; i++) {
const current = challengeCache[hints.requires].solves[i]
if (current in usernameTeamCache && usernameTeamCache[current] === team) {
solved = true
break
}
}
if (!solved) return res.send({ success: false, error: "required-challenge-not-completed" })
}
else {
if (!(solved.solves.includes(req.locals.username))) return res.send({ success: false, error: "required-challenge-not-completed" })
}
}

if (!hints.hints[0]) throw new Error('OutOfRange');
let Gtimestamp = new Date()

const usernameTeamCache = NodeCacheObj.get("usernameTeamCache")
let purchased = false
if (NodeCacheObj.get("teamMode") && req.locals.username in usernameTeamCache) {
const team = usernameTeamCache[req.locals.username]
Expand Down Expand Up @@ -387,11 +418,28 @@ const submit = async (req, res) => {
if (NodeCacheObj.get("submissionDisabled")) return res.send({ success: false, error: "submission-disabled" })

//Check if the required challenge has been solved (if any)

let challengeCache = NodeCacheObj.get("challengeCache")
const usernameTeamCache = NodeCacheObj.get("usernameTeamCache")
if ("requires" in chall) {
const solved = await collections.challs.findOne({ _id: MongoDB.ObjectId(chall.requires) }, { projection: { _id: 0, solves: 1 } })
if (!solved) return res.send({ success: false, error: "required-challenge-not-found" })
if (!(solved.solves.includes(req.locals.username))) return res.send({ success: false, error: "required-challenge-not-completed" })
const usernameTeamCache = NodeCacheObj.get("usernameTeamCache")
if (NodeCacheObj.get("teamMode") && req.locals.username in usernameTeamCache) {
let solved = false
const team = usernameTeamCache[req.locals.username]
for (let i = 0; i < challengeCache[chall.requires].solves.length; i++) {
const current = challengeCache[chall.requires].solves[i]
if (current in usernameTeamCache && usernameTeamCache[current] === team) {
solved = true
break
}
}
if (!solved) return res.send({ success: false, error: "required-challenge-not-completed" })
}
else {
if (!(solved.solves.includes(req.locals.username))) return res.send({ success: false, error: "required-challenge-not-completed" })
}

}
if (chall.max_attempts != 0) {
if (await collections.transactions.countDocuments({
Expand All @@ -401,8 +449,6 @@ const submit = async (req, res) => {
}) >= chall.max_attempts) throw new Error('Exceeded');
}
let submitted = false
let challengeCache = NodeCacheObj.get("challengeCache")
const usernameTeamCache = NodeCacheObj.get("usernameTeamCache")
if (NodeCacheObj.get("teamMode") && req.locals.username in usernameTeamCache) {
const team = usernameTeamCache[req.locals.username]
for (let i = 0; i < challengeCache[req.body.chall].solves.length; i++) {
Expand Down
2 changes: 1 addition & 1 deletion api/controllers/Submissions.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ const newSubmission = async (req, res) => {
let replacedDuplicateWithOlderSolve = false
let duplicate = false
for (let i = 0; i < teamTransacList.length; i++) {
if (teamTransacList[i].challengeID && teamTransacList[i].challengeID.toString() === insertDoc.challengeID.toString() && teamTransacList[i].type === insertDoc.type && teamTransacList[i].points === insertDoc.points) {
if (teamTransacList[i].challengeID && insertDoc.challengeID && teamTransacList[i].challengeID.toString() === insertDoc.challengeID.toString() && teamTransacList[i].type === insertDoc.type && teamTransacList[i].points === insertDoc.points) {
duplicate = true
// Current insertDoc is a duplicate transaction for this team
// Check whether insertDoc has a timestamp before the current 1 in the team list, then set it to it
Expand Down
4 changes: 2 additions & 2 deletions api/controllers/Teams.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ const join = async (req, res) => {
let replacedDuplicateWithOlderSolve = false
let duplicate = false
for (let x = 0; x < teamTransacList.length; x++) {
if (teamTransacList[x].challengeID && teamTransacList[x].challengeID.toString() === userTransactions[i].challengeID.toString() && teamTransacList[x].type === userTransactions[i].type && teamTransacList[x].points === userTransactions[i].points) {
if (teamTransacList[x].challengeID && userTransactions[i].challengeID && teamTransacList[x].challengeID.toString() === userTransactions[i].challengeID.toString() && teamTransacList[x].type === userTransactions[i].type && teamTransacList[x].points === userTransactions[i].points) {
if ("hint_id" in userTransactions[i] && "hint_id" in teamTransacList[x]) {
if (userTransactions[i].hint_id === teamTransacList[x].hint_id) {
duplicate = true
Expand Down Expand Up @@ -337,7 +337,7 @@ const leave = async (req, res) => {
let replacedDuplicateWithOlderSolve = false
let duplicate = false
for (let y = 0; y < teamTransacList.length; y++) {
if (teamTransacList[y].challengeID && teamTransacList[y].challengeID.toString() === current.challengeID.toString() && teamTransacList[y].type === current.type && teamTransacList[y].points === current.points) {
if (teamTransacList[y].challengeID && current.challengeID && teamTransacList[y].challengeID.toString() === current.challengeID.toString() && teamTransacList[y].type === current.type && teamTransacList[y].points === current.points) {
if ("hint_id" in current && "hint_id" in teamTransacList[y]) {
if (current.hint_id === teamTransacList[y].hint_id) {
duplicate = true
Expand Down
2 changes: 1 addition & 1 deletion api/utils/createTransactionsCache.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const createCache = async () => {
let replacedDuplicateWithOlderSolve = false
let duplicate = false
for (let i = 0; i < teamTransacList.length; i++) {
if (teamTransacList[i].challengeID && teamTransacList[i].challengeID.toString() === insertDoc.challengeID.toString() && teamTransacList[i].type === insertDoc.type && teamTransacList[i].points === insertDoc.points) {
if (teamTransacList[i].challengeID && insertDoc.challengeID && teamTransacList[i].challengeID.toString() === insertDoc.challengeID.toString() && teamTransacList[i].type === insertDoc.type && teamTransacList[i].points === insertDoc.points) {
// both are hints
if ("hint_id" in insertDoc && "hint_id" in teamTransacList[i]) {
if (insertDoc.hint_id === teamTransacList[i].hint_id) {
Expand Down

0 comments on commit 209fcf7

Please sign in to comment.