Skip to content

Commit

Permalink
close #185
Browse files Browse the repository at this point in the history
  • Loading branch information
geoffrey-wu committed Jul 1, 2023
1 parent 1c307ab commit 4d18ecb
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
13 changes: 11 additions & 2 deletions client/singleplayer/bonuses.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', {
Expand Down
11 changes: 11 additions & 0 deletions database/questions.js
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,16 @@ function getSetList() {
}


/**
*
* @param {ObjectId} _id
* @returns Promise<Document>
*/
async function getBonusById(_id) {
return await bonuses.findOne({ _id: _id });
}


/**
*
* @param {ObjectId} _id
Expand Down Expand Up @@ -590,6 +600,7 @@ async function reportQuestion(_id, reason, description, verbose = true) {


export {
getBonusById,
getNumPackets,
getPacket,
getQuery,
Expand Down
10 changes: 8 additions & 2 deletions database/users.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getSetId, getTossupById } from './questions.js';
import { getSetId, getTossupById, getBonusById } from './questions.js';

import { MongoClient, ObjectId } from 'mongodb';

Expand Down Expand Up @@ -97,8 +97,14 @@ async function getSubcategoryStats({ username, questionType, difficulties, setNa
* @returns {Promise<Document>} 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] },
Expand Down

0 comments on commit 4d18ecb

Please sign in to comment.