diff --git a/Makefile b/Makefile index 2d7f62f..b5d69bd 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ countries.js: countries/countries.json .PHONY python3 scripts/generate_countries_list.py countries/countries.json > countries.js flags: .SUBMODULES - mkdir flags + mkdir -p flags cp countries/data/*.svg flags clean: diff --git a/game.js b/game.js index 1bb93bf..52db917 100644 --- a/game.js +++ b/game.js @@ -36,10 +36,47 @@ const updateTime = () => { } const getMasterQuestionList = () => { - if(guessCountry()) return capitals return countries; } +const getQuestionByCountryName = (name) => { + c = getMasterQuestionList().filter(c => c.countryname === name) + if (c.length > 0) return c[0] + return null +} + +const getQuestionByCapital = (capital) => { + c = getMasterQuestionList().filter(c => c.capital === capital) + if (c.length > 0) return c[0] + return null +} + +const optionToAnswer = (option) => { + if (guessCapital()) return option.capital; + else if (guessCountry()) return option.countryname; + else if (guessReverseFlag()) return option.countryname; + else return option.code; +} + +const optionToAnswerFormatted = (option) => { + const r = optionToAnswer(option); + if (guessFlag()) return getImageURLFromCountryCode(r); + return r +} + +const optionToQuestion = (option) => { + if (guessCapital()) return option.countryname; + else if (guessCountry()) return option.capital; + else if (guessReverseFlag()) return option.code; + else return option.countryname; +} + +const optionToQuestionFormatted = (option) => { + const r = optionToQuestion(option); + if (guessReverseFlag()) return getImageURLFromCountryCode(r); + return r +} + const getQuestionHTML = (state) => { if(guessCountry()) return `what country is ${state.question.capital} the capital of?` @@ -51,15 +88,20 @@ const getQuestionHTML = (state) => { return `which country's flag is ${getImageURLFromCountryCode(state.question.code)}?` } -const answer_list = () => { - return Object.values(getMasterQuestionList()); +const answerList = () => { + return getMasterQuestionList().map(q => { + if (guessCountry()) return q.countryname; + if (guessCapital()) return q.capital; + if (guessFlag()) return q.code; + if (guessReverseFlag()) return q.countryname; + }) } + const getImageURLFromCountryCode = (code) => ``; - -const regionList = () => [...new Set(Object.values(getMasterQuestionList()).map(item => item.region))] - .concat([...new Set(Object.values(getMasterQuestionList()).map(item => item.subregion))]) +const regionList = () => [...new Set(getMasterQuestionList().map(item => item.region))] + .concat([...new Set(getMasterQuestionList().map(item => item.subregion))]) .concat([ALL_REGIONS]) .sort(); const date = new Date(); @@ -92,7 +134,7 @@ var resultsChart = new Chart( // set up game function init() { // generate question list - questionList = Object.values(getMasterQuestionList()) + questionList = getMasterQuestionList() .filter(q => q.region == selectedRegion || q.subregion == selectedRegion || selectedRegion == ALL_REGIONS) .sort(() => Math.random()-0.5); @@ -156,17 +198,17 @@ function updateState() { var options = [] while (options.length < 4) { - var c = answer_list()[Math.floor(Math.random()*answer_list().length)]; - var question = getMasterQuestionList()[newQuestion.countryname]; - if (question == undefined) question = getMasterQuestionList()[newQuestion.capital]; + var c = getMasterQuestionList()[Math.floor(Math.random()*answerList().length)]; + var question = getQuestionByCountryName(newQuestion.countryname); + if (question == undefined) question = getQuestionByCapital(newQuestion.capital); console.log(c); console.log(question); - if (c !== getMasterQuestionList()[newQuestion.countryname]&& !options.includes(c)){ + if (c !== getQuestionByCountryName(newQuestion.countryname)&& !options.includes(c)){ options.push(c); } } - var question = getMasterQuestionList()[newQuestion.countryname]; - if (question == undefined) question = getMasterQuestionList()[newQuestion.capital]; + var question = getQuestionByCountryName(newQuestion.countryname); + if (question == undefined) question = getQuestionByCapital(newQuestion.capital); options[Math.floor(Math.random()*4)] = question; if (state.question) state.previousQuestion = { @@ -189,25 +231,15 @@ function updateScreen(){ displayEndScreen(); return; } - 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); - } + + answerAHTML.getElementsByClassName("text")[0].innerHTML = optionToAnswerFormatted(state.options[0]); + answerBHTML.getElementsByClassName("text")[0].innerHTML = optionToAnswerFormatted(state.options[1]); + answerCHTML.getElementsByClassName("text")[0].innerHTML = optionToAnswerFormatted(state.options[2]); + answerDHTML.getElementsByClassName("text")[0].innerHTML = optionToAnswerFormatted(state.options[3]); + questionHTML.innerHTML = getQuestionHTML(state) if (state.previousQuestion ) { - previousQuestionAnswer.innerHTML = state.previousQuestion.question.answer; + previousQuestionAnswer.innerHTML = optionToAnswerFormatted(state.previousQuestion.question); previousQuestionText.style.display = ""; } } @@ -225,19 +257,18 @@ function displayEndScreen() { correctAnswersTable.innerHTML = "