Compare commits

...

14 Commits

Author SHA1 Message Date
cee92eae3c update countries repo commit 2024-01-31 18:24:31 +00:00
9866553bf0 fix css 2022-04-13 17:38:06 +01:00
b4c3ddcfe0 update to new colors styling location 2022-04-13 17:32:05 +01:00
2311497f63 prepare for new styling update 2022-04-13 17:08:03 +01:00
cf2e7abf28 more fixing styles 2022-04-13 03:00:40 +01:00
925c7744fe more fixing styles 2022-04-13 02:59:24 +01:00
0f44c34f65 more fixing styles 2022-04-13 02:57:14 +01:00
cf94bcf486 update styles 2022-04-13 02:55:01 +01:00
38b1530202 fix homepage link 2022-04-12 04:54:11 +01:00
ab4a81ab5b switch to styles.alv.cx 2022-04-03 21:38:57 +01:00
b2f37589ec bug fix 2022-02-04 23:02:11 +00:00
8b2695f241 prettier -w 2022-01-19 20:38:35 +00:00
780f6d7272 neaten up game.js 2022-01-19 20:35:33 +00:00
3199f549dd update bug where all regions == no countries lol 2022-01-14 14:17:16 +00:00
7 changed files with 518 additions and 452 deletions

View File

@@ -6,7 +6,7 @@ countries.js: countries/countries.json .PHONY
python3 scripts/generate_countries_list.py countries/countries.json > countries.js python3 scripts/generate_countries_list.py countries/countries.json > countries.js
flags: .SUBMODULES flags: .SUBMODULES
mkdir flags mkdir -p flags
cp countries/data/*.svg flags cp countries/data/*.svg flags
clean: clean:

553
game.js
View File

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

View File

@@ -1,147 +1,170 @@
:root { :root {
--a-color: var(--red); --a-color: var(--red);
--b-color: var(--yellow); --b-color: var(--yellow);
--c-color: var(--green); --c-color: var(--green);
--d-color: var(--blue); --d-color: var(--blue);
--question-country-color: var(--teal); --question-country-color: var(--base16-teal);
--question-capital-color: var(--blue); --question-capital-color: var(--blue);
} }
input { input {
font-family: inherit font-family: inherit;
} }
.correct { .correct {
animation: correct .5s; animation: correct 0.5s;
} }
.incorrect { .incorrect {
animation: incorrect .5s; animation: incorrect 0.5s;
} }
@keyframes correct { @keyframes correct {
0% { background: var(--default-bg); } 0% {
50% { background: var(--green); } background: var(--bg);
100% { background: var(--default-bg); } }
50% {
background: var(--green);
}
100% {
background: var(--bg);
}
} }
@keyframes incorrect { @keyframes incorrect {
0% { background: var(--default-bg); } 0% {
50% { background: var(--red); } background: var(--bg);
100% { background: var(--default-bg); } }
50% {
background: var(--red);
}
100% {
background: var(--bg);
}
} }
#toprow { #toprow {
display: flex; display: flex;
justify-content: space-between; justify-content: space-between;
align-items: center; align-items: center;
} }
@media only screen and (max-width: 600px) { @media only screen and (max-width: 600px) {
#toprow { #toprow {
flex-direction: column-reverse; flex-direction: column-reverse;
} }
} }
#question { #question {
font-size: 1.5em; font-size: 1.5em;
display: flex; display: flex;
align-items: center; align-items: center;
} }
#question img { #question img {
max-height: 10vh; max-height: 10vh;
} }
#question * { #question * {
margin: 1em; margin: 1em;
} }
#score { #score {
--good: var(--green); --good: var(--green);
--okay: var(--yellow); --okay: var(--yellow);
--bad: var(--red); --bad: var(--red);
background: var(--good); background: var(--good);
color: var(--dark);
} }
#time { #time {
background: var(--blue); background: var(--blue);
color: var(--dark);
} }
#gameinfo { #gameinfo {
display: flex; display: flex;
} }
#gameinfo * { #gameinfo * {
margin: 0 0.5em 0 0.5em; margin: 0 0.5em 0 0.5em;
} }
#previousQuestionAnswer { background: var(--green) } #previousQuestionAnswer {
background: var(--green);
color: var(--dark);
}
#game .answer { #game .answer {
display: flex; display: flex;
justify-content: space-evenly; justify-content: space-evenly;
align-items: center; align-items: center;
align-content: center; align-content: center;
} }
#game .answer .text { #game .answer .text {
width: 50%; width: 50%;
} }
#game .answer .letter { #game .answer .letter {
font-size: 1.5em; font-size: 1.5em;
padding: 1em; padding: 1em;
border: 0; border: 0;
border-radius: 0.25em; border-radius: 0.25em;
} }
#a { #a {
background-color: var(--a-color); background-color: var(--a-color);
border-color: var(--a-color); border-color: var(--a-color);
color: var(--dark);
} }
#b { #b {
background-color: var(--b-color); background-color: var(--b-color);
border-color: var(--b-color); border-color: var(--b-color);
color: var(--dark);
} }
#c { #c {
background-color: var(--c-color); background-color: var(--c-color);
border-color: var(--c-color); border-color: var(--c-color);
color: var(--dark);
} }
#d { #d {
background-color: var(--d-color); background-color: var(--d-color);
border-color: var(--d-color); border-color: var(--d-color);
color: var(--dark);
} }
span#questionCountry { span#questionCountry {
background-color: var(--question-country-color); background-color: var(--question-country-color);
color: var(--dark);
} }
span#questionCapital { span#questionCapital {
background-color: var(--question-capital-color); background-color: var(--question-capital-color);
color: var(--dark);
} }
#settings { #settings {
width: 100% width: 100%;
} }
#regionList { #regionList {
display: flex; display: flex;
flex-wrap: wrap; flex-wrap: wrap;
justify-content: center; justify-content: center;
} }
#settings .regionListItem { #settings .regionListItem {
padding: 1em; padding: 1em;
transition: 0.5s; transition: 0.5s;
} }
#settings .regionListItem:hover { #settings .regionListItem:hover {
background: #d9d9d9; background: var(--light);
color: black; color: var(--dark);
} }
/* toggle switches */ /* toggle switches */
@@ -167,8 +190,8 @@ span#questionCapital {
right: 0; right: 0;
bottom: 0; bottom: 0;
background-color: var(--question-country-color); background-color: var(--question-country-color);
-webkit-transition: .4s; -webkit-transition: 0.4s;
transition: .4s; transition: 0.4s;
} }
.slider:before { .slider:before {
@@ -179,8 +202,8 @@ span#questionCapital {
left: 4px; left: 4px;
bottom: 4px; bottom: 4px;
background-color: white; background-color: white;
-webkit-transition: .4s; -webkit-transition: 0.4s;
transition: .4s; transition: 0.4s;
} }
input:checked + .slider { input:checked + .slider {
@@ -207,20 +230,35 @@ input:checked + .slider:before {
} }
#questionTypeSelector { #questionTypeSelector {
display: flex; display: flex;
width: 100%; width: 100%;
align-items: center; align-items: center;
justify-content: space-around; justify-content: space-around;
margin-top: 3em;; margin-top: 3em;
margin-bottom: 3em;; margin-bottom: 3em;
} }
.text img, td img { max-height: 15vh } .text img,
td img {
max-height: 15vh;
}
#questionTypeSelector input[type="radio"] { display: none; } #questionTypeSelector input[type="radio"] {
input[type="radio"]+label { display: none;
padding: 0.5em;
} }
input[type="radio"]:checked+label { input[type="radio"] + label {
background-color: var(--yellow); padding: 0.5em;
}
input[type="radio"]:checked + label {
background-color: var(--yellow);
color: var(--dark);
}
#previousQuestionAnswer {
display: inline;
}
#previousQuestionAnswer img {
display: inline;
max-height: 2em;
} }

View File

@@ -1,72 +1,99 @@
<!DOCTYPE html> <!DOCTYPE html>
<html lang="en"> <html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="stylesheet" type="text/css" href="styles.css" />
<link rel="stylesheet" type="text/css" href="game_styles.css" />
<title>capitals_quiz</title>
</head>
<head> <body>
<meta charset="utf-8"/> <div id="game">
<meta name="viewport" content="width=device-width, initial-scale=1"> <div id="toprow">
<link rel="stylesheet" type="text/css" href="styles.css" /> <p id="question">you need javascript enabled to play this</p>
<link rel="stylesheet" type="text/css" href="game_styles.css" />
<title>capitals_quiz</title>
</head>
<body>
<div id=game>
<div id="toprow">
<p id="question">you need javascript enabled to play this</p>
<div id="gameinfo"> <div id="gameinfo">
<p id="time">time</p> <p id="time">time</p>
<p id="score">score</p> <p id="score">score</p>
</div> </div>
</div> </div>
<div style="display: none" id="settings"> <div style="display: none" id="settings">
<div id="regionList"></div> <div id="regionList"></div>
<div id="questionTypeSelector" > <div id="questionTypeSelector">
<input type="radio" name="questionMode" value="capital" id="questionTypeCapital" checked> <input
<label for="questionTypeCapital">capital</label> type="radio"
name="questionMode"
value="capital"
id="questionTypeCapital"
checked
/>
<label for="questionTypeCapital">capital</label>
<input type="radio" name="questionMode" value="country" id="questionTypeCountry"> <input
<label for="questionTypeCountry">country</label> type="radio"
name="questionMode"
value="country"
id="questionTypeCountry"
/>
<label for="questionTypeCountry">country</label>
<input type="radio" name="questionMode" value="flag" id="questionTypeFlag"> <input
<label for="questionTypeFlag">flag</label> type="radio"
name="questionMode"
value="flag"
id="questionTypeFlag"
/>
<label for="questionTypeFlag">flag</label>
<input type="radio" name="questionMode" value="reverseflag" id="questionTypeReverseFlag"> <input
<label for="questionTypeReverseFlag">reverseflag</label> type="radio"
</div> name="questionMode"
</div> value="reverseflag"
<div style="display: none" id="answers"> id="questionTypeReverseFlag"
<div class="answer" id="answer_a"> />
<p class="letter" id="a">1</p> <label for="questionTypeReverseFlag">reverseflag</label>
<p class="text">answer 1 text</p> </div>
</div> </div>
<div class="answer" id="answer_b"> <div style="display: none" id="answers">
<p class="letter" id="b">2</p> <div class="answer" id="answer_a">
<p class="text">answer 2 text</p> <p class="letter" id="a">1</p>
</div> <p class="text">answer 1 text</p>
<div class="answer" id="answer_c"> </div>
<p class="letter" id="c">3</p> <div class="answer" id="answer_b">
<p class="text">this sample text is longer than the rest</p> <p class="letter" id="b">2</p>
</div> <p class="text">answer 2 text</p>
<div class="answer" id="answer_d"> </div>
<p class="letter" id="d">4</p> <div class="answer" id="answer_c">
<p class="text">answer 4 text</p> <p class="letter" id="c">3</p>
</div> <p class="text">this sample text is longer than the rest</p>
<p id="previousQuestionText" style="display: none"> answer to previous question: <span id="previousQuestionAnswer"> </span></p> </div>
</div> <div class="answer" id="answer_d">
<div style="display: none" id="results"> <p class="letter" id="d">4</p>
<h1 id="resultsBreakdownHeader"> results breakdown </h1> <p class="text">answer 4 text</p>
<div> </div>
<canvas id="resultsChart"></canvas> <p id="previousQuestionText" style="display: none">
</div> answer to previous question:
<h2 id="incorrectAnswersHeader"> incorrect answers </h2> <span id="previousQuestionAnswer"> </span>
<table id="incorrectAnswersTable"> </table> </p>
</div>
<div style="display: none" id="results">
<h1 id="resultsBreakdownHeader">results breakdown</h1>
<div>
<canvas id="resultsChart"></canvas>
</div>
<h2 id="incorrectAnswersHeader">incorrect answers</h2>
<table id="incorrectAnswersTable"></table>
<h2 id="correctAnswersHeader"> correct answers </h2> <h2 id="correctAnswersHeader">correct answers</h2>
<table id="correctAnswersTable"> </table> <table id="correctAnswersTable"></table>
</div> </div>
</div> </div>
<p> built with ❤ and adequate amounts of care by <a href="https://alra.uk">alv</a></p> <p>
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script> built with ❤ and adequate amounts of care by
<script type="text/javascript" src="countries.js"></script> <a href="https://alv.cx">alv</a>
<script type="text/javascript" src="game.js"></script> </p>
</body> <script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<script type="text/javascript" src="countries.js"></script>
<script type="text/javascript" src="game.js"></script>
</body>
</html>

View File

@@ -18,36 +18,22 @@ def main(args):
with open(args.file) as fp: with open(args.file) as fp:
countries = json.load(fp) countries = json.load(fp)
country_list = {} country_list = []
capital_list = {}
for country in countries: for country in countries:
if len(country['capital']) < 1 or country['capital'][0] == "" or not country['independent']: if len(country['capital']) < 1 or country['capital'][0] == "" or not country['independent']:
continue continue
country_list[country['name']['common']] = { country_list.append({
'answer': country['capital'][0],
'capital': country['capital'],
'countryname': country['name']['common'],
'region': country['region'],
'subregion': country['subregion'],
'languages': country['languages'],
'code': country['cca3'].lower()
}
capital_list[country['capital'][0]] = {
'answer': country['name']['common'],
'capital': country['capital'][0], 'capital': country['capital'][0],
'countryname': country['name']['common'], 'countryname': country['name']['common'],
'region': country['region'], 'region': country['region'],
'subregion': country['subregion'], 'subregion': country['subregion'],
'languages': country['languages'], 'languages': country['languages'],
'code': country['cca3'].lower() 'code': country['cca3'].lower()
} })
print('countries = ', end='') print('countries = ', end='')
print(json.dumps(country_list), end=';') print(json.dumps(country_list), end=';')
print('capitals = ', end='')
print(json.dumps(capital_list))
return 0 return 0

View File

@@ -1,35 +1,19 @@
@import url("https://alv.cx/styles.css"); @import url("https://styles.alv.cx/colors/base16-default.css");
@import url("https://styles.alv.cx/base.css");
:root { @import url("https://styles.alv.cx/modules/darkmode.css");
--default-bg: #fefefe;
--dark-bg: #b8b8b8;
--selected-bg:#383838;
--default-fg: #454545;
--red: #ab4642;
--orange: #dc9656;
--yellow: #f7ca88;
--green: #a1b56c;
--teal: #86c1b9;
--blue: #7cafc2;
--purple: #ba8baf;
--brown: #a16946;
}
body { body {
font-family: monospace; font-family: monospace;
color: var(--default-fg); font-size: 16px;
font-size: 16px; margin: 0 auto;
margin: 0 auto; max-width: 800px;
max-width: 800px; padding: 2em;
padding: 2em; line-height: 1.1;
line-height: 1.1;
text-align: justify;
background-color: var(--default-bg);
} }
@media only screen and (max-width: 600px) { @media only screen and (max-width: 600px) {
body { body {
margin: 0em auto; margin: 0em auto;
padding: 2em; padding: 2em;
} }
} }