Skip to content

Commit

Permalink
type to answer for bonuses
Browse files Browse the repository at this point in the history
  • Loading branch information
geoffrey-wu committed Jan 24, 2023
1 parent b653209 commit 03f1acd
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 6 deletions.
11 changes: 11 additions & 0 deletions client/singleplayer/bonuses.html
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@
<input class="form-check-input" id="toggle-show-history" type="checkbox" value role="switch" checked>
<label class="form-check-label" for="toggle-show-history">Show question history</label>
</div>
<div class="form-check form-switch">
<input class="form-check-input" id="type-to-answer" type="checkbox" value role="switch" checked>
<label class="form-check-label" for="type-to-answer">Type to answer</label>
</div>
<div class="d-none d-lg-block">
<hr>
<p>
Expand All @@ -126,6 +130,13 @@
type="button" title="Shortcut: spacebar" disabled>Reveal</button>
<button class="mb-2 btn btn-danger d-lg-none" id="toggle-options" type="button" disabled>Options</button>
</div>
<form id="answer-form">
<!-- User enters answer here -->
<div class="input-group d-none my-2" id="answer-input-group">
<input class="form-control" id="answer-input" type="text" placeholder="Enter answer">
<button class="btn btn-success" id="answer-submit" type="submit">Submit</button>
</div>
</form>
<div class="float-row">
<span id="statline">0.00 PPB with 0 bonuses seen (0/0/0/0, 0 pts)</span>
<span class="float-end ps-5 me-lg-2" id="question-metadata">
Expand Down
66 changes: 60 additions & 6 deletions client/singleplayer/bonuses.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ let validSubcategories;

// Status variables
let currentBonusPart = -1;
let onQuestion = true;
let onAnswer = true;
let packetNumber = -1;
let questions = [{}];
let questionNumber = 0;
Expand Down Expand Up @@ -113,6 +113,29 @@ function getPointsForCurrentBonus() {
}


async function giveAnswer(givenAnswer) {
const directive = await checkAnswer(questions[questionNumber].answers[currentBonusPart], givenAnswer);
console.log(questions[questionNumber].answers[currentBonusPart]);
console.log(givenAnswer);
console.log(directive);

if (directive === 'accept') {
document.getElementById('reveal').disabled = false;
revealBonusPart();
revealBonusPart();
document.getElementById(`checkbox-${currentBonusPart}`).checked = true;
} else if (directive === 'reject') {
document.getElementById('reveal').disabled = false;
revealBonusPart();
revealBonusPart();
} else if (directive === 'prompt') {
document.getElementById('answer-input-group').classList.remove('d-none');
document.getElementById('answer-input').focus();
document.getElementById('answer-input').placeholder = 'Prompt';
}
}


/**
* Loads and reads the next question.
*/
Expand Down Expand Up @@ -147,7 +170,7 @@ function revealBonusPart() {
if (currentBonusPart >= questions[questionNumber]['parts'].length)
return;

if (onQuestion) {
if (onAnswer) {
createBonusPart(currentBonusPart, questions[questionNumber]['parts'][currentBonusPart]);
} else {
const paragraph = document.createElement('p');
Expand All @@ -156,7 +179,7 @@ function revealBonusPart() {
currentBonusPart++;
}

onQuestion = !onQuestion;
onAnswer = !onAnswer;

if (currentBonusPart >= questions[questionNumber]['parts'].length) {
document.getElementById('reveal').disabled = true;
Expand All @@ -181,6 +204,21 @@ function updateStatDisplay() {
}


document.getElementById('answer-form').addEventListener('submit', function (event) {
event.preventDefault();
event.stopPropagation();

const answer = document.getElementById('answer-input').value;

document.getElementById('answer-input').value = '';
document.getElementById('answer-input').blur();
document.getElementById('answer-input').placeholder = 'Enter answer';
document.getElementById('answer-input-group').classList.add('d-none');

giveAnswer(answer);
});


document.getElementById('category-modal').addEventListener('hidden.bs.modal', function () {
randomQuestions = [];
getRandomQuestion('bonus', rangeToArray(document.getElementById('difficulties').value), validCategories, validSubcategories);
Expand Down Expand Up @@ -208,7 +246,7 @@ document.getElementById('next').addEventListener('click', function () {
updateStatDisplay();
}

onQuestion = true;
onAnswer = true;
next();
});

Expand All @@ -225,7 +263,13 @@ document.getElementById('question-number').addEventListener('change', function (

document.getElementById('reveal').addEventListener('click', function () {
this.blur();
revealBonusPart();
if (!onAnswer && document.getElementById('type-to-answer').checked) {
document.getElementById('answer-input-group').classList.remove('d-none');
document.getElementById('answer-input').focus();
this.disabled = true;
} else {
revealBonusPart();
}
});


Expand All @@ -236,7 +280,7 @@ document.getElementById('set-name').addEventListener('change', function () {

document.getElementById('start').addEventListener('click', async function () {
this.blur();
onQuestion = true;
onAnswer = true;
start(document.getElementById('toggle-select-by-set-name').checked);
document.getElementById('question').innerHTML = 'Fetching questions...';
questions = await getBonuses(setName, packetNumber);
Expand All @@ -255,6 +299,12 @@ document.getElementById('toggle-show-history').addEventListener('click', functio
});


document.getElementById('type-to-answer').addEventListener('click', function () {
this.blur();
localStorage.setItem('typeToAnswer', this.checked ? 'true' : 'false');
});


document.addEventListener('keydown', (event) => {
if (['INPUT', 'TEXTAREA', 'SELECT'].includes(document.activeElement.tagName)) return;

Expand Down Expand Up @@ -344,3 +394,7 @@ if (localStorage.getItem('showBonusHistory') === 'false') {
document.getElementById('toggle-show-history').checked = false;
document.getElementById('room-history').classList.add('d-none');
}

if (localStorage.getItem('typeToAnswer') === 'false') {
document.getElementById('type-to-answer').checked = false;
}

0 comments on commit 03f1acd

Please sign in to comment.