Skip to content

Commit

Permalink
first draft of leniency checker
Browse files Browse the repository at this point in the history
  • Loading branch information
Captain-Quack committed Oct 3, 2024
1 parent 417da4b commit 3567c73
Show file tree
Hide file tree
Showing 11 changed files with 84 additions and 16 deletions.
15 changes: 15 additions & 0 deletions Web.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0"?>
<configuration>
<!--
For a description of web.config changes see http://go.microsoft.com/fwlink/?LinkId=235367.
The following attributes can be set on the <httpRuntime> tag.
<system.Web>
<httpRuntime targetFramework="4.8.1" />
</system.Web>
-->
<system.web>
<compilation debug="false" targetFramework="4.8.1"/>
<pages controlRenderingCompatibilityVersion="4.0"/>
</system.web>
</configuration>
1 change: 1 addition & 0 deletions client/multiplayer/room.html
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ <h1 id="funny-toast-text" class="me-auto text-danger"></h1>
<div class="mb-2"></div>
<label for="reading-speed">Reading speed: <span id="reading-speed-display">50</span><br></label>
<input class="form-range" id="reading-speed" type="range" min="0" max="100" step="5">

<div>
View more settings <a href="/settings">here</a>.
</div>
Expand Down
21 changes: 19 additions & 2 deletions client/multiplayer/room.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ socket.onmessage = function (event) {
case 'reveal-answer': return revealAnswer(data);
case 'set-categories': return setCategories(data);
case 'set-difficulties': return setDifficulties(data);
case 'set-leniency': return setLeniency(data);
case 'set-reading-speed': return setReadingSpeed(data);
case 'set-packet-numbers': return setPacketNumbers(data);
case 'set-set-name': return setSetName(data);
Expand Down Expand Up @@ -208,6 +209,9 @@ function connectionAcknowledged ({
document.getElementById('reading-speed').value = settings.readingSpeed;
document.getElementById('reading-speed-display').textContent = settings.readingSpeed;

document.getElementById('leniency').value = settings.leniency;
document.getElementById('leniency-display').textContent = settings.leniency;

document.getElementById('toggle-rebuzz').checked = settings.rebuzz;

document.getElementById('toggle-skip').checked = settings.skip;
Expand Down Expand Up @@ -576,6 +580,12 @@ function setReadingSpeed ({ username, value }) {
document.getElementById('reading-speed-display').textContent = value;
}

function setLeniency ({ username, value }) {
logEvent(username, `changed the leniency to ${value}`);
document.getElementById('leniency').value = value;
document.getElementById('leniency-display').textContent = value;
}

function setSetName ({ username, value }) {
logEvent(username, value.length > 0 ? `changed set name to ${value}` : 'cleared set name');
document.getElementById('set-name').value = value;
Expand Down Expand Up @@ -824,13 +834,20 @@ document.getElementById('pause').addEventListener('click', function () {
socket.send(JSON.stringify({ type: 'pause', pausedTime }));
});

document.getElementById('reading-speed').addEventListener('change', function () {
socket.send(JSON.stringify({ type: 'set-reading-speed', value: this.value }));
document.getElementById('leniency').addEventListener('input', function () {
document.getElementById('leniency-display').textContent = this.value;
});

document.getElementById('leniency').addEventListener('change', function () {
socket.send(JSON.stringify({ type: 'set-leniency', value: this.value }));
});

document.getElementById('reading-speed').addEventListener('input', function () {
document.getElementById('reading-speed-display').textContent = this.value;
});
document.getElementById('reading-speed').addEventListener('change', function () {
socket.send(JSON.stringify({ type: 'set-reading-speed', value: this.value }));
});

document.getElementById('report-question-submit').addEventListener('click', function () {
api.reportQuestion(
Expand Down
4 changes: 2 additions & 2 deletions client/scripts/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ export default class api {
* directedPrompt: String | null
* }>}
*/
static async checkAnswer (answerline, givenAnswer) {
static async checkAnswer (answerline, givenAnswer, strictness = null) {
if (givenAnswer === '') {
return { directive: 'reject', directedPrompt: null };
}

return await fetch('/api/check-answer?' + new URLSearchParams({ answerline, givenAnswer }))
return await fetch('/api/check-answer?' + new URLSearchParams({ answerline, givenAnswer, ...(strictness && { strictness }) }))
.then(response => response.json());
}

Expand Down
8 changes: 4 additions & 4 deletions client/scripts/utilities/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,8 +225,8 @@ function rangeToArray (string, max = 0) {

const tokens = string.split(',');
const ranges = [];
for (let i = 0; i < tokens.length; i++) {
const range = tokens[i].trim().split('-');
for (const element of tokens) {
const range = element.trim().split('-');
if (range.length === 1) {
ranges.push([parseInt(range[0]), parseInt(range[0])]);
} else {
Expand All @@ -235,8 +235,8 @@ function rangeToArray (string, max = 0) {
}

const array = [];
for (let i = 0; i < ranges.length; i++) {
for (let j = ranges[i][0]; j <= ranges[i][1]; j++) {
for (const element of ranges) {
for (let j = element[0]; j <= element[1]; j++) {
array.push(j);
}
}
Expand Down
7 changes: 6 additions & 1 deletion client/singleplayer/bonuses.html
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,13 @@ <h1 id="funny-toast-text" class="me-auto text-danger"></h1>
<div class="mt-2" id="general-settings">
<div class="text-center mb-2">
<button class="btn btn-danger" id="category-select-button" data-bs-target="#category-modal"
data-bs-toggle="modal" type="button">Categories</button>
data-bs-toggle="modal" type="button">
Categories
</button>
<button class="btn btn-warning" id="clear-stats" type="button">Clear Stats</button>
</div>
<label for="question-length">Leniency: <span id="leniency-display">7</span><br></label>
<input class="form-range" id="leniency" type="range" min="0" max="20" step="1">
<div class="form-check form-switch">
<input class="form-check-input" id="toggle-select-by-set-name" type="checkbox" role="switch">
<label class="form-check-label" for="toggle-select-by-set-name">Select by set name</label>
Expand All @@ -119,6 +123,7 @@ <h1 id="funny-toast-text" class="me-auto text-danger"></h1>
<input class="form-check-input" id="toggle-standard-only" type="checkbox" role="switch">
<label class="form-check-label" for="toggle-standard-only">Standard format only</label>
</div>

<hr>
<div class="form-check form-switch">
<input class="form-check-input" id="type-to-answer" type="checkbox" role="switch" checked>
Expand Down
15 changes: 14 additions & 1 deletion client/singleplayer/bonuses.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,17 @@ const categoryManager = new CategoryManager(query.categories, query.subcategorie
const settings = window.localStorage.getItem('singleplayer-bonus-settings')
? JSON.parse(window.localStorage.getItem('singleplayer-bonus-settings'))
: {
leniency: 7,
selectBySetName: false,
showHistory: true,
typeToAnswer: true
};

// Load query and settings first so user doesn't see the default settings
if (settings.leniency) {
document.getElementById('leniency').value = settings.leniency;
document.getElementById('leniency-display').textContent = settings.leniency;
}
if (settings.selectBySetName) {
document.getElementById('difficulty-settings').classList.add('d-none');
document.getElementById('set-settings').classList.remove('d-none');
Expand Down Expand Up @@ -261,7 +266,7 @@ async function getRandomBonus ({ alternateSubcategories, categories, difficultie
}

async function giveAnswer (givenAnswer) {
const { directive, directedPrompt } = await api.checkAnswer(bonuses[questionNumber - 1].answers[currentBonusPart], givenAnswer);
const { directive, directedPrompt } = await api.checkAnswer(bonuses[questionNumber - 1].answers[currentBonusPart], givenAnswer, settings.leniency);

switch (directive) {
case 'accept':
Expand Down Expand Up @@ -479,6 +484,12 @@ document.getElementById('toggle-settings').addEventListener('click', function ()
document.getElementById('settings').classList.toggle('d-lg-none');
});

document.getElementById('leniency').addEventListener('input', function () {
settings.leniency = this.value;
document.getElementById('leniency-display').textContent = this.value;
window.localStorage.setItem('singleplayer-tossup-settings', JSON.stringify(settings));
});

document.getElementById('toggle-select-by-set-name').addEventListener('click', function () {
this.blur();
settings.selectBySetName = this.checked;
Expand Down Expand Up @@ -525,6 +536,8 @@ document.getElementById('toggle-three-part-bonuses').addEventListener('click', f

document.getElementById('type-to-answer').addEventListener('click', function () {
this.blur();
document.getElementById('leniency').disabled = !this.checked;
document.getElementById('leniency-display').disabled = !this.checked;
settings.typeToAnswer = this.checked;
window.localStorage.setItem('singleplayer-bonus-settings', JSON.stringify(settings));
});
Expand Down
4 changes: 4 additions & 0 deletions client/singleplayer/tossups.html
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ <h1 id="funny-toast-text" class="me-auto text-danger"></h1>
</div>
<div id="difficulty-settings">
<div class="mb-2" id="difficulty-dropdown-root"></div>

<section>
Year range: <span class="sliderValue0" id="year-range-a">2010</span> - <span class="sliderValue1"
id="year-range-b">2024</span>
Expand Down Expand Up @@ -143,6 +144,9 @@ <h1 id="funny-toast-text" class="me-auto text-danger"></h1>
<div class="mb-2"></div>
<label for="reading-speed">Reading speed: <span id="reading-speed-display">50</span><br></label>
<input class="form-range" id="reading-speed" type="range" min="0" max="100" step="5">
<div class="mb-2"></div>
<label for="question-length">Leniency: <span id="leniency-display">7</span><br></label>
<input class="form-range" id="leniency" type="range" min="0" max="20" step="1">
<div>
View more settings <a href="/settings">here</a>.
</div>
Expand Down
17 changes: 15 additions & 2 deletions client/singleplayer/tossups.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ const categoryManager = new CategoryManager(query.categories, query.subcategorie
const settings = window.localStorage.getItem('singleplayer-tossup-settings')
? JSON.parse(window.localStorage.getItem('singleplayer-tossup-settings'))
: {
leniency: 7,
readingSpeed: 50,
rebuzz: false,
selectBySetName: false,
Expand All @@ -93,6 +94,10 @@ const settings = window.localStorage.getItem('singleplayer-tossup-settings')
};

// Load query and settings first so user doesn't see the default settings
if (settings.leniency) {
document.getElementById('leniency-display').textContent = settings.leniency;
document.getElementById('leniency').value = settings.leniency;
}
if (settings.readingSpeed) {
document.getElementById('reading-speed-display').textContent = settings.readingSpeed;
document.getElementById('reading-speed').value = settings.readingSpeed;
Expand Down Expand Up @@ -273,7 +278,7 @@ function clearStats () {
async function giveAnswer (givenAnswer) {
currentlyBuzzing = false;

const { directive, directedPrompt } = await api.checkAnswer(tossups[questionNumber - 1].answer, givenAnswer);
const { directive, directedPrompt } = await api.checkAnswer(tossups[questionNumber - 1].answer, givenAnswer, settings.leniency);

switch (directive) {
case 'accept': {
Expand Down Expand Up @@ -415,7 +420,7 @@ function readQuestion (expectedReadTime) {
}

// calculate time needed before reading next word
let time = Math.log(word.length) + 1;
let time = Math.log(word.length || 1) + 1;
if ((word.endsWith('.') && word.charCodeAt(word.length - 2) > 96 && word.charCodeAt(word.length - 2) < 123) ||
word.slice(-2) === '.\u201d' || word.slice(-2) === '!\u201d' || word.slice(-2) === '?\u201d') { time += 2; } else if (word.endsWith(',') || word.slice(-2) === ',\u201d') { time += 0.75; } else if (word === '(*)') { time = 0; }

Expand Down Expand Up @@ -596,6 +601,12 @@ document.getElementById('reading-speed').addEventListener('input', function () {
window.localStorage.setItem('singleplayer-tossup-settings', JSON.stringify(settings));
});

document.getElementById('leniency').addEventListener('input', function () {
settings.leniency = this.value;
document.getElementById('leniency-display').textContent = this.value;
window.localStorage.setItem('singleplayer-tossup-settings', JSON.stringify(settings));
});

document.getElementById('report-question-submit').addEventListener('click', function () {
api.reportQuestion(
document.getElementById('report-question-id').value,
Expand Down Expand Up @@ -712,6 +723,8 @@ document.getElementById('toggle-timer').addEventListener('click', function () {
document.getElementById('type-to-answer').addEventListener('click', function () {
this.blur();
settings.typeToAnswer = this.checked;
document.getElementById('leniency').disabled = !this.checked;
document.getElementById('leniency-display').disabled = !this.checked;
document.getElementById('toggle-rebuzz').disabled = !this.checked;
window.localStorage.setItem('singleplayer-tossup-settings', JSON.stringify(settings));
});
Expand Down
4 changes: 2 additions & 2 deletions routes/api/check-answer.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import checkAnswer from 'qb-answer-checker';
const router = Router();

router.get('/', (req, res) => {
const { answerline, givenAnswer } = req.query;
const { directive, directedPrompt } = checkAnswer(answerline, givenAnswer);
const { answerline, givenAnswer, strictness } = req.query;
const { directive, directedPrompt } = checkAnswer(answerline, givenAnswer, strictness);
res.header('Access-Control-Allow-Origin', '*');
res.json({ directive, directedPrompt });
});
Expand Down
4 changes: 2 additions & 2 deletions types.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { ObjectId } from 'mongodb';
* @typedef {object} Question
* @property {ObjectId} _id
* @property {string} category
* @property {number} difficulty
* @property {0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10} difficulty
* @property {string} subcategory
* @property {number} number
*
Expand Down Expand Up @@ -61,5 +61,5 @@ import { ObjectId } from 'mongodb';
* @property {ObjectId} _id
* @property {string} name
* @property {number} year
* @property {number} difficulty
* @property {0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10} difficulty
*/

0 comments on commit 3567c73

Please sign in to comment.