2021-04-10 14:02:36 +00:00
|
|
|
"use strict";
|
|
|
|
|
2021-04-11 16:25:33 +00:00
|
|
|
const ALL_REGIONS = "All Regions";
|
2022-01-14 01:49:34 +00:00
|
|
|
const FLAG_DIR = "flags";
|
2021-04-11 16:25:33 +00:00
|
|
|
|
2021-04-11 16:30:43 +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]
|
2021-06-05 10:52:08 +00:00
|
|
|
const incorrectAnswersTable = document.getElementById('incorrectAnswersTable');
|
|
|
|
const correctAnswersTable = document.getElementById('correctAnswersTable');
|
2021-06-05 11:11:00 +00:00
|
|
|
const previousQuestionAnswer = document.getElementById('previousQuestionAnswer');
|
|
|
|
const previousQuestionText = document.getElementById('previousQuestionText');
|
2022-01-13 23:27:52 +00:00
|
|
|
const regionListHTML = document.getElementById("regionList");
|
2021-06-05 10:52:08 +00:00
|
|
|
const resultsHTML = document.getElementById("results");
|
2021-04-11 16:30:43 +00:00
|
|
|
const scoreHTML = document.getElementById("score");
|
|
|
|
const settingsHTML = document.getElementById("settings");
|
|
|
|
const questionHTML = document.getElementById("question");
|
2021-12-01 15:10:50 +00:00
|
|
|
const timeHTML = document.getElementById("time");
|
|
|
|
|
|
|
|
var gameTimeStartTime = 0;
|
|
|
|
var gameTimeIntervalId = 0;
|
2022-01-13 23:27:52 +00:00
|
|
|
var selectedRegion = null;
|
2022-01-14 01:49:34 +00:00
|
|
|
|
|
|
|
const guessCapital = () => document.getElementById('questionTypeCapital').checked;
|
|
|
|
const guessCountry = () => document.getElementById('questionTypeCountry').checked;
|
|
|
|
const guessFlag = () => document.getElementById('questionTypeFlag').checked;
|
|
|
|
const guessReverseFlag = () => document.getElementById('questionTypeReverseFlag').checked;
|
2021-12-01 15:10:50 +00:00
|
|
|
|
2022-01-13 23:27:52 +00:00
|
|
|
const updateTime = () => {
|
2021-12-01 15:10:50 +00:00
|
|
|
const secondsPassed = ((new Date().getTime() - gameTimeStartTime.getTime())/1000)
|
|
|
|
.toFixed(3);
|
|
|
|
timeHTML.innerHTML = secondsPassed;
|
|
|
|
}
|
2021-04-11 14:04:52 +00:00
|
|
|
|
2022-01-13 20:10:47 +00:00
|
|
|
const getMasterQuestionList = () => {
|
|
|
|
if(guessCountry()) return capitals
|
|
|
|
return countries;
|
|
|
|
}
|
|
|
|
|
|
|
|
const getQuestionHTML = (state) => {
|
|
|
|
if(guessCountry())
|
2022-01-14 01:49:34 +00:00
|
|
|
return `what country is <span id="questionCapital">${state.question.capital}</span> the capital of?`
|
|
|
|
if (guessCapital())
|
|
|
|
return `what is the capital of <span id="questionCountry">${state.question.countryname}</span>?`
|
|
|
|
if(guessFlag())
|
|
|
|
return `what is the flag of <span id="questionCountry">${state.question.countryname}</span>?`
|
|
|
|
if(guessReverseFlag())
|
|
|
|
return `which country's flag is ${getImageURLFromCountryCode(state.question.code)}?`
|
|
|
|
}
|
|
|
|
|
|
|
|
const answer_list = () => {
|
|
|
|
return Object.values(getMasterQuestionList());
|
2022-01-13 20:10:47 +00:00
|
|
|
}
|
|
|
|
|
2022-01-14 01:49:34 +00:00
|
|
|
const getImageURLFromCountryCode = (code) => `<img src="${FLAG_DIR}/${code}.svg" />`;
|
2022-01-13 20:10:47 +00:00
|
|
|
|
|
|
|
|
2022-01-13 23:27:52 +00:00
|
|
|
const regionList = () => [...new Set(Object.values(getMasterQuestionList()).map(item => item.region))]
|
2022-01-13 20:10:47 +00:00
|
|
|
.concat([...new Set(Object.values(getMasterQuestionList()).map(item => item.subregion))])
|
2021-04-11 16:30:43 +00:00
|
|
|
.concat([ALL_REGIONS])
|
|
|
|
.sort();
|
2021-04-11 15:52:27 +00:00
|
|
|
const date = new Date();
|
|
|
|
|
2022-01-13 23:27:52 +00:00
|
|
|
var questionList, state;
|
2021-04-10 14:02:36 +00:00
|
|
|
|
2021-06-05 10:52:08 +00:00
|
|
|
var resultsChart = new Chart(
|
|
|
|
document.getElementById('resultsChart'),
|
|
|
|
{
|
|
|
|
type: 'doughnut',
|
|
|
|
data: {
|
|
|
|
labels: [],
|
|
|
|
datasets: [
|
|
|
|
{
|
|
|
|
labels: [],
|
|
|
|
data: [],
|
|
|
|
backgroundColor: [
|
|
|
|
"#a1b56c",
|
2021-06-05 11:19:24 +00:00
|
|
|
"#ab4642",
|
2021-06-05 10:52:08 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
}
|
|
|
|
]
|
|
|
|
},
|
|
|
|
options: {}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2021-04-11 15:52:27 +00:00
|
|
|
|
|
|
|
// set up game
|
2021-04-10 14:02:36 +00:00
|
|
|
function init() {
|
2021-04-11 16:30:43 +00:00
|
|
|
// generate question list
|
2022-01-14 01:49:34 +00:00
|
|
|
questionList = Object.values(getMasterQuestionList())
|
|
|
|
.filter(q => q.region == selectedRegion || q.subregion == selectedRegion)
|
2021-04-11 16:30:43 +00:00
|
|
|
.sort(() => Math.random()-0.5);
|
|
|
|
|
|
|
|
// set up state variable
|
|
|
|
state.endTime = 0;
|
|
|
|
state.finishedGame = false;
|
|
|
|
state.startedGame = true;
|
|
|
|
state.maxScore = questionList.length;
|
|
|
|
state.score = 0;
|
|
|
|
state.startTime = date.getTime();
|
2021-06-05 10:52:08 +00:00
|
|
|
state.correctAnswers = [];
|
|
|
|
state.incorrectAnswers = [];
|
|
|
|
state.userAnswer = null;
|
2021-04-11 16:30:43 +00:00
|
|
|
|
|
|
|
// show and hide appropriate elements
|
|
|
|
answersHTML.style.display = "";
|
|
|
|
settingsHTML.style.display = "none";
|
|
|
|
questionHTML.onclick = deinit;
|
2022-01-13 23:33:52 +00:00
|
|
|
incorrectAnswersTable.innerHTML = "<tr> <th> question </th> <th> answer </th> <th> your answer </th> </tr>";
|
2021-06-05 10:52:08 +00:00
|
|
|
correctAnswersTable.innerHTML = "<tr> <th> country </th> <th> capital </th> </tr>";
|
2021-04-11 16:30:43 +00:00
|
|
|
|
|
|
|
// start game
|
|
|
|
updateState();
|
|
|
|
updateScreen();
|
2021-12-01 15:10:50 +00:00
|
|
|
gameTimeStartTime = new Date()
|
|
|
|
gameTimeIntervalId = setInterval(updateTime, 1);
|
2021-04-10 14:02:36 +00:00
|
|
|
}
|
|
|
|
|
2021-04-11 16:25:33 +00:00
|
|
|
|
2021-04-11 15:52:27 +00:00
|
|
|
// stop game, go back to start screen
|
2021-04-11 14:04:52 +00:00
|
|
|
function deinit() {
|
2021-12-01 15:10:50 +00:00
|
|
|
clearInterval(gameTimeIntervalId);
|
|
|
|
|
2021-04-11 16:30:43 +00:00
|
|
|
answersHTML.style.display = "none";
|
2021-06-05 10:52:08 +00:00
|
|
|
resultsHTML.style.display = "none";
|
2021-04-11 16:30:43 +00:00
|
|
|
settingsHTML.style.display = "";
|
2022-01-13 23:27:52 +00:00
|
|
|
questionHTML.innerHTML = "capitals_quiz - select a region to start!";
|
2021-04-11 16:30:43 +00:00
|
|
|
scoreHTML.innerHTML = "score";
|
|
|
|
questionHTML.onclick = init;
|
2021-12-01 15:10:50 +00:00
|
|
|
timeHTML.innerHTML = "time";
|
2021-04-11 16:30:43 +00:00
|
|
|
|
|
|
|
questionList = null;
|
|
|
|
state = {
|
|
|
|
"score": 0,
|
|
|
|
"maxScore": 0,
|
|
|
|
"startTime": 0,
|
|
|
|
"endTime": 0,
|
|
|
|
"finishedGame": true,
|
|
|
|
"startedGame": false,
|
|
|
|
};
|
2021-04-11 14:04:52 +00:00
|
|
|
}
|
|
|
|
|
2021-04-11 16:25:33 +00:00
|
|
|
|
2021-04-10 14:02:36 +00:00
|
|
|
function updateState() {
|
2021-04-11 16:30:43 +00:00
|
|
|
// check if game is over
|
|
|
|
if (questionList.length == 0) { state.finishedGame = true; return; }
|
|
|
|
|
|
|
|
// set up new question
|
2022-01-14 01:49:34 +00:00
|
|
|
const newQuestion = questionList.pop();
|
|
|
|
console.log(newQuestion);
|
|
|
|
|
2021-04-11 16:30:43 +00:00
|
|
|
var options = []
|
|
|
|
while (options.length < 4) {
|
2022-01-13 20:41:46 +00:00
|
|
|
var c = answer_list()[Math.floor(Math.random()*answer_list().length)];
|
2022-01-14 01:49:34 +00:00
|
|
|
var question = getMasterQuestionList()[newQuestion.countryname];
|
|
|
|
if (question == undefined) question = getMasterQuestionList()[newQuestion.capital];
|
|
|
|
console.log(c);
|
|
|
|
console.log(question);
|
|
|
|
if (c !== getMasterQuestionList()[newQuestion.countryname]&& !options.includes(c)){
|
2021-04-11 16:30:43 +00:00
|
|
|
options.push(c);
|
|
|
|
}
|
|
|
|
}
|
2022-01-14 01:49:34 +00:00
|
|
|
var question = getMasterQuestionList()[newQuestion.countryname];
|
|
|
|
if (question == undefined) question = getMasterQuestionList()[newQuestion.capital];
|
|
|
|
options[Math.floor(Math.random()*4)] = question;
|
2021-04-11 16:30:43 +00:00
|
|
|
|
2021-06-05 11:11:00 +00:00
|
|
|
if (state.question) state.previousQuestion = {
|
|
|
|
"question": state.question,
|
|
|
|
"options": state.options,
|
|
|
|
"answer": state.answer
|
|
|
|
};
|
2022-01-14 01:49:34 +00:00
|
|
|
|
2021-04-11 16:30:43 +00:00
|
|
|
state.question = newQuestion;
|
|
|
|
state.options = options;
|
2022-01-14 01:49:34 +00:00
|
|
|
state.answer = question;
|
|
|
|
console.log(state);
|
2021-04-10 14:02:36 +00:00
|
|
|
}
|
|
|
|
|
2021-04-11 16:25:33 +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 16:30:43 +00:00
|
|
|
scoreHTML.innerHTML = state.score + "/" + state.maxScore;
|
|
|
|
if (state.finishedGame) {
|
2021-06-05 10:52:08 +00:00
|
|
|
displayEndScreen();
|
2021-04-11 16:30:43 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-01-14 01:49:34 +00:00
|
|
|
if (!guessFlag() && !guessReverseFlag()) {
|
|
|
|
answerAHTML.getElementsByClassName("text")[0].innerHTML = state.options[0].answer;
|
|
|
|
answerBHTML.getElementsByClassName("text")[0].innerHTML = state.options[1].answer;
|
|
|
|
answerCHTML.getElementsByClassName("text")[0].innerHTML = state.options[2].answer;
|
|
|
|
answerDHTML.getElementsByClassName("text")[0].innerHTML = state.options[3].answer;
|
|
|
|
} else if (guessReverseFlag()) {
|
|
|
|
answerAHTML.getElementsByClassName("text")[0].innerHTML = state.options[0].countryname;
|
|
|
|
answerBHTML.getElementsByClassName("text")[0].innerHTML = state.options[1].countryname;
|
|
|
|
answerCHTML.getElementsByClassName("text")[0].innerHTML = state.options[2].countryname;
|
|
|
|
answerDHTML.getElementsByClassName("text")[0].innerHTML = state.options[3].countryname;
|
|
|
|
} else {
|
|
|
|
answerAHTML.getElementsByClassName("text")[0].innerHTML = getImageURLFromCountryCode(state.options[0].code);
|
|
|
|
answerBHTML.getElementsByClassName("text")[0].innerHTML = getImageURLFromCountryCode(state.options[1].code);
|
|
|
|
answerCHTML.getElementsByClassName("text")[0].innerHTML = getImageURLFromCountryCode(state.options[2].code);
|
|
|
|
answerDHTML.getElementsByClassName("text")[0].innerHTML = getImageURLFromCountryCode(state.options[3].code);
|
|
|
|
}
|
2022-01-13 20:10:47 +00:00
|
|
|
questionHTML.innerHTML = getQuestionHTML(state)
|
2021-06-05 11:11:00 +00:00
|
|
|
if (state.previousQuestion ) {
|
2022-01-14 01:49:34 +00:00
|
|
|
previousQuestionAnswer.innerHTML = state.previousQuestion.question.answer;
|
2021-06-05 11:11:00 +00:00
|
|
|
previousQuestionText.style.display = "";
|
|
|
|
}
|
2021-04-10 14:02:36 +00:00
|
|
|
}
|
|
|
|
|
2021-12-01 15:10:50 +00:00
|
|
|
|
2021-06-05 10:52:08 +00:00
|
|
|
function displayEndScreen() {
|
2021-12-01 15:10:50 +00:00
|
|
|
questionHTML.innerHTML = "you did it! click here to restart";
|
|
|
|
answers.style.display = "none";
|
|
|
|
answers.style.display = "none";
|
|
|
|
|
|
|
|
clearInterval(gameTimeIntervalId);
|
2021-06-05 10:52:08 +00:00
|
|
|
|
2022-01-14 02:05:45 +00:00
|
|
|
if (guessReverseFlag() || guessFlag()) {
|
|
|
|
incorrectAnswersTable.innerHTML = "<tr> <th> country </th> <th> answer </th> <th> your answer </th> </tr>";
|
|
|
|
correctAnswersTable.innerHTML = "<tr> <th> flag </th> <th> country </th> </tr>";
|
|
|
|
}
|
2022-01-14 01:49:34 +00:00
|
|
|
|
2022-01-14 02:05:45 +00:00
|
|
|
if (guessReverseFlag()) {
|
|
|
|
state.incorrectAnswers.forEach(ans => {
|
|
|
|
var tr = document.createElement('tr');
|
|
|
|
console.log(ans)
|
|
|
|
|
|
|
|
tr.appendChild(document.createElement('td'))
|
|
|
|
tr.lastChild.innerHTML = ans.question.countryname
|
|
|
|
|
|
|
|
tr.appendChild(document.createElement('td'))
|
|
|
|
tr.lastChild.innerHTML = getImageURLFromCountryCode(ans.answer.code);
|
|
|
|
|
|
|
|
tr.appendChild(document.createElement('td'))
|
|
|
|
tr.lastChild.innerHTML = getImageURLFromCountryCode(ans.options[ans.userAnswer].code);
|
|
|
|
|
|
|
|
incorrectAnswersTable.appendChild(tr);
|
|
|
|
})
|
|
|
|
if (state.incorrectAnswers.length <= 0)
|
|
|
|
incorrectAnswersTable.innerHTML = "no incorrect answers! go you!";
|
|
|
|
|
|
|
|
state.correctAnswers.forEach(ans => {
|
|
|
|
var tr = document.createElement('tr');
|
|
|
|
tr.appendChild(document.createElement('td'))
|
|
|
|
tr.lastChild.innerHTML = getImageURLFromCountryCode(ans.question.code);
|
|
|
|
tr.appendChild(document.createElement('td'))
|
|
|
|
tr.lastChild.innerHTML = ans.answer.countryname;
|
|
|
|
correctAnswersTable.appendChild(tr);
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
state.incorrectAnswers.forEach(ans => {
|
|
|
|
var tr = document.createElement('tr');
|
|
|
|
console.log(ans)
|
|
|
|
|
|
|
|
tr.appendChild(document.createElement('td'))
|
|
|
|
if (guessCountry()) tr.lastChild.innerHTML = ans.question.capital
|
|
|
|
else tr.lastChild.innerHTML = ans.question.countryname
|
|
|
|
|
|
|
|
tr.appendChild(document.createElement('td'))
|
|
|
|
if (guessFlag()) tr.lastChild.innerHTML = getImageURLFromCountryCode(ans.answer.code);
|
|
|
|
else tr.lastChild.innerHTML = ans.answer.answer;
|
|
|
|
|
|
|
|
tr.appendChild(document.createElement('td'))
|
|
|
|
if (guessFlag()) tr.lastChild.innerHTML = getImageURLFromCountryCode(ans.options[ans.userAnswer].code);
|
|
|
|
else tr.lastChild.innerHTML = ans.options[ans.userAnswer].answer;
|
|
|
|
|
|
|
|
incorrectAnswersTable.appendChild(tr);
|
|
|
|
})
|
|
|
|
if (state.incorrectAnswers.length <= 0)
|
|
|
|
incorrectAnswersTable.innerHTML = "no incorrect answers! go you!";
|
|
|
|
|
|
|
|
state.correctAnswers.forEach(ans => {
|
|
|
|
var tr = document.createElement('tr');
|
|
|
|
tr.appendChild(document.createElement('td'))
|
|
|
|
tr.lastChild.innerHTML = ans.question.countryname
|
|
|
|
tr.appendChild(document.createElement('td'))
|
|
|
|
if (guessFlag()) tr.lastChild.innerHTML = getImageURLFromCountryCode(ans.answer.code);
|
|
|
|
else tr.lastChild.innerHTML = ans.answer.capital;
|
|
|
|
correctAnswersTable.appendChild(tr);
|
|
|
|
})
|
|
|
|
}
|
2021-06-05 10:52:08 +00:00
|
|
|
if (state.correctAnswers.length <= 0)
|
|
|
|
correctAnswersTable.innerHTML = "no correct answers. better luck next time :')";
|
|
|
|
|
|
|
|
resultsChart.config.data.labels = ["correct", "incorrect"];
|
|
|
|
resultsChart.config.data.labels = ["correct", "incorrect"];
|
|
|
|
resultsChart.config.data.datasets[0].labels = ["correct", "incorrect"];
|
|
|
|
resultsChart.config.data.datasets[0].data = [state.correctAnswers.length,state.incorrectAnswers.length];
|
|
|
|
resultsChart.update();
|
|
|
|
resultsHTML.style.display = "";
|
|
|
|
}
|
|
|
|
|
2021-04-11 16:25:33 +00:00
|
|
|
|
2021-04-10 14:02:36 +00:00
|
|
|
function processClick(answer) {
|
2021-04-11 16:30:43 +00:00
|
|
|
if (state.finishedGame) return;
|
|
|
|
// check if answer to previous question was correct
|
|
|
|
var isAnswerCorrect = state.options[answer] == state.answer
|
|
|
|
state.score += isAnswerCorrect ? 1 : 0;
|
2021-06-05 10:52:08 +00:00
|
|
|
state.userAnswer = answer;
|
|
|
|
state[isAnswerCorrect ? "correctAnswers" : "incorrectAnswers"].push({
|
|
|
|
"question": state.question,
|
|
|
|
"options": state.options,
|
|
|
|
"userAnswer": state.userAnswer,
|
|
|
|
"answer": state.answer
|
|
|
|
});
|
|
|
|
state.userAnswer = null;
|
2021-04-11 16:25:33 +00:00
|
|
|
|
2022-01-13 23:27:52 +00:00
|
|
|
// change background color based on if answer was correct for 500ms
|
2021-04-11 16:30:43 +00:00
|
|
|
bodyHTML.classList.add(isAnswerCorrect ? "correct" : "incorrect")
|
|
|
|
setTimeout(() => bodyHTML.classList = [], 500)
|
2021-04-10 14:02:36 +00:00
|
|
|
|
2021-04-11 16:30:43 +00:00
|
|
|
updateState();
|
|
|
|
updateScreen();
|
2021-04-10 14:02:36 +00:00
|
|
|
}
|
|
|
|
|
2021-04-11 14:04:52 +00:00
|
|
|
|
2022-01-13 23:27:52 +00:00
|
|
|
function setRegion(region) {
|
|
|
|
selectedRegion = region
|
2021-04-11 14:04:52 +00:00
|
|
|
}
|
|
|
|
|
2022-01-13 23:27:52 +00:00
|
|
|
// set up event listeners
|
2021-04-11 14:04:52 +00:00
|
|
|
|
2021-04-11 16:30:43 +00:00
|
|
|
answerAHTML.addEventListener("click", () => processClick(0));
|
|
|
|
answerBHTML.addEventListener("click", () => processClick(1));
|
|
|
|
answerCHTML.addEventListener("click", () => processClick(2));
|
|
|
|
answerDHTML.addEventListener("click", () => processClick(3));
|
|
|
|
document.addEventListener("keyup", e => {
|
|
|
|
if (e.code == "Digit1") processClick(0);
|
|
|
|
if (e.code == "Digit2") processClick(1);
|
|
|
|
if (e.code == "Digit3") processClick(2);
|
|
|
|
if (e.code == "Digit4") processClick(3);
|
|
|
|
if (e.code == "Enter" && settingsHTML.style.display === "none" && state.finishedGame) {
|
|
|
|
deinit();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (e.code == "Enter" && !state.startedGame) init();
|
2021-04-10 14:02:36 +00:00
|
|
|
});
|
2021-04-11 14:04:52 +00:00
|
|
|
|
2022-01-13 23:27:52 +00:00
|
|
|
// start game
|
2021-04-11 16:25:33 +00:00
|
|
|
|
2021-04-11 14:04:52 +00:00
|
|
|
deinit();
|
2022-01-13 23:27:52 +00:00
|
|
|
regionListHTML.innerHTML = regionList()
|
|
|
|
.map(region => {
|
|
|
|
return `<div class="regionListItem" onclick="setRegion('${region}'); init()"> ${region} </div>`
|
|
|
|
})
|
|
|
|
.sort()
|
|
|
|
.join("");
|