capitals_quiz/src/game.js

169 lines
5.3 KiB
JavaScript
Raw Normal View History

2021-04-10 14:02:36 +00:00
"use strict";
var question_list = null;
2021-04-11 14:04:52 +00:00
const capitals_list = Object.values(countries).map(country => country.capital);
const regionList = [...new Set(Object.values(countries).map(country => country.region))]
.concat([...new Set(Object.values(countries).map(country => country.subregion))]);
2021-04-10 14:02:36 +00:00
const date = new Date();
2021-04-11 14:04:52 +00:00
var selectorMatches = []
2021-04-10 14:02:36 +00:00
2021-04-11 14:04:52 +00:00
const answersHTML = document.getElementById('answers');
const answerAHTML = document.getElementById('answer_a');
const answerBHTML = document.getElementById('answer_b');
const answerCHTML = document.getElementById('answer_c');
const answerDHTML = document.getElementById('answer_d');
const bodyHTML = document.getElementsByTagName('body')[0]
const scoreHTML = document.getElementById('score');
const settingsHTML = document.getElementById('settings');
const selectorHTML = document.getElementById('region_selector');
const selectorResultsHTML = document.getElementById('selector_results');
const questionHTML = document.getElementById('question');
var state = {
"score": 0,
"max_score": 0,
"start_time": 0,
"end_time": 0,
"finishedGame": true,
};
2021-04-10 14:02:36 +00:00
function init() {
// reset states list, score, end_time; start timer for game;
2021-04-11 14:04:52 +00:00
var regex;
if (selectorHTML.value === "") {
regex = new RegExp(".*");
} else {
regex = new RegExp(selectorHTML.value, 'gi');
}
question_list = Object.keys(countries)
.filter(c => countries[c].region.match(regex) || countries[c].subregion.match(regex))
.sort(() => Math.random()-0.5);
state.maxScore = question_list.length;
2021-04-10 14:02:36 +00:00
answersHTML.style.display = "";
2021-04-11 14:04:52 +00:00
settingsHTML.style.display = "none";
2021-04-10 14:02:36 +00:00
state.finishedGame = false;
state.score = 0;
state.end_time = 0;
state.start_time = date.getTime();
2021-04-11 14:04:52 +00:00
questionHTML.onclick = deinit;
2021-04-10 14:02:36 +00:00
updateState();
updateScreen();
}
2021-04-11 14:04:52 +00:00
function deinit() {
answersHTML.style.display = "none";
settingsHTML.style.display = "";
questionHTML.innerHTML = "tap here or press enter to start";
questionHTML.onclick = init;
scoreHTML.innerHTML = "score";
}
2021-04-10 14:02:36 +00:00
function updateState() {
// check if game is over
2021-04-11 14:04:52 +00:00
if (question_list.length == 0) { state.finishedGame = true; return; }
2021-04-10 14:02:36 +00:00
// set up new question
2021-04-11 14:04:52 +00:00
console.log(1);
2021-04-10 14:02:36 +00:00
var newQuestion = question_list.pop();
2021-04-11 14:04:52 +00:00
console.log(newQuestion);
console.log(2);
2021-04-10 14:02:36 +00:00
var options = []
while (options.length < 4) {
var c = capitals_list[Math.floor(Math.random()*capitals_list.length)];
2021-04-11 14:04:52 +00:00
if (c !== countries[newQuestion].capital && !options.includes(c)){
2021-04-10 14:02:36 +00:00
options.push(c);
}
}
console.log(options)
2021-04-11 14:04:52 +00:00
options[Math.floor(Math.random()*4)] = countries[newQuestion].capital;
2021-04-10 14:02:36 +00:00
console.log(options)
state.question = newQuestion;
state.options = options;
2021-04-11 14:04:52 +00:00
state.answer = countries[newQuestion].capital;
2021-04-10 14:02:36 +00:00
}
2021-04-11 15:51:21 +00:00
// update HTML elements to reflect values of state
2021-04-10 14:02:36 +00:00
function updateScreen(){
2021-04-11 14:04:52 +00:00
scoreHTML.innerHTML = state.score + "/" + state.maxScore;
2021-04-10 14:02:36 +00:00
if (state.finishedGame) {
2021-04-11 15:51:21 +00:00
questionHTML.innerHTML = "woooooooo!! tap here or press enter to restart";
2021-04-10 14:02:36 +00:00
answers.style.display = "none";
return;
}
answerAHTML.getElementsByClassName('text')[0].innerHTML = state.options[0];
answerBHTML.getElementsByClassName('text')[0].innerHTML = state.options[1];
answerCHTML.getElementsByClassName('text')[0].innerHTML = state.options[2];
answerDHTML.getElementsByClassName('text')[0].innerHTML = state.options[3];
questionHTML.innerHTML = "what is the capital of <span id=\"questionCountry\">" + state.question + "</span>?";
}
function processClick(answer) {
2021-04-11 14:04:52 +00:00
console.log("got click: " + answer);
if (state.finishedGame) return;
console.log(1);
2021-04-10 14:02:36 +00:00
// check if answer to previous question was correct
state.score += 1 ? state.options[answer] == state.answer : 0;
2021-04-11 14:04:52 +00:00
console.log(state.options[answer] == state.answer ? 'correct' : 'incorrect')
bodyHTML.classList.add(state.options[answer] == state.answer ? 'correct' : 'incorrect')
setTimeout(() => bodyHTML.classList = [], 500)
2021-04-10 14:02:36 +00:00
console.log("got click: " + answer);
updateState();
updateScreen();
}
2021-04-11 14:04:52 +00:00
function setSelectorMatches(value){
const regex = new RegExp(selectorHTML.value, 'gi');
selectorMatches = regionList
.filter(region => region.match(regex))
displaySelectorMatches();
}
function displaySelectorMatches(){
selectorResultsHTML.innerHTML = selectorMatches
.map(region => {
return `<div class="selector_result" onclick="setSelectorValue('${region}')"> ${region} </div>`
})
.join('');
}
function setSelectorValue(value) {
selectorHTML.value = value;
}
2021-04-10 14:02:36 +00:00
answerAHTML.addEventListener('click', () => processClick(0));
answerBHTML.addEventListener('click', () => processClick(1));
answerCHTML.addEventListener('click', () => processClick(2));
answerDHTML.addEventListener('click', () => processClick(3));
2021-04-11 14:04:52 +00:00
document.addEventListener('keyup', e => {
2021-04-10 14:02:36 +00:00
if (e.code == "Digit1") processClick(0);
if (e.code == "Digit2") processClick(1);
if (e.code == "Digit3") processClick(2);
if (e.code == "Digit4") processClick(3);
2021-04-11 14:04:52 +00:00
if (e.code == "Enter" && document.activeElement !== selectorHTML) init();
2021-04-10 14:02:36 +00:00
});
2021-04-11 14:04:52 +00:00
selectorHTML.addEventListener('change', setSelectorMatches);
selectorHTML.addEventListener('keyup', e => {
console.log(e.code);
var topResult = document.getElementsByClassName('selector_result')[0]
if (e.code == "Enter" && selectorHTML.value === "") {
init();
return;
}
if (e.code == "Enter" && selectorHTML.value === topResult.innerHTML.trim()) {
init();
return;
}
if (e.code == "Enter")
selectorHTML.value = topResult.innerHTML.trim();
setSelectorMatches();
});
deinit();
selectorHTML.focus();
setSelectorMatches();