diff --git a/client/singleplayer/bonuses.js b/client/singleplayer/bonuses.js index 1cf5eb4b5..d5ba66b5e 100644 --- a/client/singleplayer/bonuses.js +++ b/client/singleplayer/bonuses.js @@ -224,10 +224,19 @@ async function loadRandomBonuses(difficulties = [], categories = [], subcategori async function next() { if (questions[questionNumber] && currentBonusPart >= questions[questionNumber].parts.length) { const pointsPerPart = Array.from(document.getElementsByClassName('checkbox')).map((checkbox, index) => { - if (!checkbox.checked) + if (!checkbox.checked) { return 0; + } + + if (questions[questionNumber].values === undefined || questions[questionNumber].values === null) { + return 10; + } + + if (questions[questionNumber].values[index] === undefined || questions[questionNumber].values[index] === null) { + return 10; + } - return questions[questionNumber]?.values ? questions[questionNumber].values[index] : 10; + return questions[questionNumber].values[index]; }); fetch('/auth/record-bonus', { diff --git a/database/questions.js b/database/questions.js index 66d0f7d58..0269fff5c 100644 --- a/database/questions.js +++ b/database/questions.js @@ -552,6 +552,16 @@ function getSetList() { } +/** + * + * @param {ObjectId} _id + * @returns Promise + */ +async function getBonusById(_id) { + return await bonuses.findOne({ _id: _id }); +} + + /** * * @param {ObjectId} _id @@ -590,6 +600,7 @@ async function reportQuestion(_id, reason, description, verbose = true) { export { + getBonusById, getNumPackets, getPacket, getQuery, diff --git a/database/users.js b/database/users.js index 46c276e95..35d6f3da3 100644 --- a/database/users.js +++ b/database/users.js @@ -1,4 +1,4 @@ -import { getSetId, getTossupById } from './questions.js'; +import { getSetId, getTossupById, getBonusById } from './questions.js'; import { MongoClient, ObjectId } from 'mongodb'; @@ -97,8 +97,14 @@ async function getSubcategoryStats({ username, questionType, difficulties, setNa * @returns {Promise} the bonus stats */ async function getSingleBonusStats(bonus_id) { + const bonus = await getBonusById(bonus_id); + + if (!bonus) { + return null; + } + const result = await bonusData.aggregate([ - { $match: { bonus_id } }, + { $match: { bonus_id, pointsPerPart: { $size: bonus.parts.length } } }, { $addFields: { pointValue: { $sum: '$pointsPerPart' } } }, { $addFields: { convertedPart1: { $ne: [ { $arrayElemAt: [ '$pointsPerPart', 0 ] }, 0] },