Skip to content

Commit

Permalink
close #232
Browse files Browse the repository at this point in the history
  • Loading branch information
geoffrey-wu committed Dec 21, 2023
1 parent cce89ca commit aabf5ff
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 12 deletions.
9 changes: 9 additions & 0 deletions client/api-docs/packet.html
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,15 @@ <h4 class="mt-4 md-2" id="parameters">Parameters</h4>
The number of the packet in the set, starting from 1.
</div>
</li>
<li class="list-group-item">
<div>
<code>modaq</code><code class="text-muted">: boolean</code>
<code class="text-muted float-end fw-semibold">default: "false"</code>
</div>
<div>
Whether or not to output in a format compatible with <a href="https://github.com/alopezlago/MODAQ">MODAQ</a>.
</div>
</li>
</ul>

<a href="#returns" class="text-body">
Expand Down
77 changes: 66 additions & 11 deletions database/qbreader/get-packet.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '<b>' + tossup.question.replace('(*)', '(*)</b>');
}

if (tossup.formatted_answer) {
result.answer = tossup.formatted_answer.replace('<i>', '<em>').replace('</i>', '</em>');
}

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('<i>', '<em>').replace('</i>', '</em>'));
}

return result;
}

/**
* Retrieves a packet of questions from the database.
* @param {object} options - The options for the packet retrieval.
Expand All @@ -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': [] };
}
Expand All @@ -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;
}

Expand Down
3 changes: 2 additions & 1 deletion routes/api/packet.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit aabf5ff

Please sign in to comment.