diff --git a/client/api-docs/packet.html b/client/api-docs/packet.html index 4c3fafcc..57ed27db 100644 --- a/client/api-docs/packet.html +++ b/client/api-docs/packet.html @@ -152,6 +152,15 @@

Parameters

The number of the packet in the set, starting from 1. +
  • +
    + modaq: boolean + default: "false" +
    +
    + Whether or not to output in a format compatible with MODAQ. +
    +
  • diff --git a/database/qbreader/get-packet.js b/database/qbreader/get-packet.js index 1d43b917..c869a54b 100644 --- a/database/qbreader/get-packet.js +++ b/database/qbreader/get-packet.js @@ -3,6 +3,50 @@ import { bonuses, packets, tossups } from './collections.js'; // eslint-disable-next-line no-unused-vars import * as types from '../../types.js'; +/** + * Modaqifies a tossup without modifying the original tossup. + * @param {types.Tossup} tossup - The tossup to modaqify. + * @returns The modaqified tossup. + */ +function modaqifyTossup(tossup) { + const result = { + question: tossup.question, + answer: tossup.answer, + metadata: `${tossup.category} - ${tossup.subcategory}`, + }; + + if (tossup?.question && tossup.question.includes('(*)')) { + result.question = '' + tossup.question.replace('(*)', '(*)'); + } + + if (tossup.formatted_answer) { + result.answer = tossup.formatted_answer.replace('', '').replace('', ''); + } + + return result; +} + + +function modaqifyBonus(bonus) { + const result = { + values: bonus.values ?? bonus.parts.map(() => 10), + leadin: bonus.leadin, + parts: bonus.parts, + answers: bonus.answers, + metadata: `${bonus.category} - ${bonus.subcategory}`, + }; + + if (bonus.difficulties) { + result.difficultyModifiers = bonus.difficulties; + } + + if (bonus.formatted_answers) { + result.answers = bonus.formatted_answers.map(answer => answer.replace('', '').replace('', '')); + } + + return result; +} + /** * Retrieves a packet of questions from the database. * @param {object} options - The options for the packet retrieval. @@ -12,9 +56,10 @@ import * as types from '../../types.js'; * If only one allowed type is specified, only that type will be searched for (increasing query speed). * The other type will be returned as an empty array. * @param {boolean} [options.replaceUnformattedAnswer=true] - Whether to replace unformatted answers. + * @param {boolean} [options.modaq=false] - Whether to output in a result compatible with MODAQ. * @returns {Promise<{tossups: types.Tossup[], bonuses: types.Bonus[]}>} The retrieved packet of questions. */ -async function getPacket({ setName, packetNumber, questionTypes = ['tossups', 'bonuses'], replaceUnformattedAnswer = true }) { +async function getPacket({ setName, packetNumber, questionTypes = ['tossups', 'bonuses'], replaceUnformattedAnswer = true, modaq = false }) { if (!setName || isNaN(packetNumber) || packetNumber < 1) { return { 'tossups': [], 'bonuses': [] }; } @@ -41,26 +86,36 @@ async function getPacket({ setName, packetNumber, questionTypes = ['tossups', 'b const values = await Promise.all([tossupResult, bonusResult]); - const result = {}; + const result = { + tossups: [], + bonuses: [], + }; - if (questionTypes.includes('tossups')) + if (questionTypes.includes('tossups')) { result.tossups = values[0]; + } - if (questionTypes.includes('bonuses')) + if (questionTypes.includes('bonuses')) { result.bonuses = values[1]; + } - if (replaceUnformattedAnswer) { - for (const question of result.tossups || []) { - if (Object.prototype.hasOwnProperty.call(question, 'formatted_answer')) - question.answer = question.formatted_answer; + if (replaceUnformattedAnswer && !modaq) { + for (const tossup of result.tossups) { + if (Object.prototype.hasOwnProperty.call(tossup, 'formatted_answer')) + tossup.answer = tossup.formatted_answer; } - for (const question of result.bonuses || []) { - if (Object.prototype.hasOwnProperty.call(question, 'formatted_answers')) - question.answers = question.formatted_answers; + for (const bonus of result.bonuses) { + if (Object.prototype.hasOwnProperty.call(bonus, 'formatted_answers')) + bonus.answers = bonus.formatted_answers; } } + if (modaq) { + result.tossups = result.tossups.map(tossup => modaqifyTossup(tossup)); + result.bonuses = result.bonuses.map(bonus => modaqifyBonus(bonus)); + } + return result; } diff --git a/routes/api/packet.js b/routes/api/packet.js index d072fe26..18433986 100644 --- a/routes/api/packet.js +++ b/routes/api/packet.js @@ -7,7 +7,8 @@ const router = Router(); router.get('/', async (req, res) => { const setName = req.query.setName; const packetNumber = parseInt(req.query.packetNumber); - const packet = await getPacket({ setName, packetNumber }); + const modaq = req.query.modaq === 'true'; + const packet = await getPacket({ setName, packetNumber, modaq }); if (packet.tossups.length === 0 && packet.bonuses.length === 0) { res.statusCode = 404; }