2023-04-12 12:32:09 +02:00
|
|
|
import { CountUp } from "./node_modules/countup.js/dist/countUp.min.js";
|
|
|
|
|
2023-04-11 13:00:03 +02:00
|
|
|
var cpuList;
|
|
|
|
var currentCpu;
|
|
|
|
var nextCpu;
|
|
|
|
|
2023-04-12 13:46:14 +02:00
|
|
|
// current user score
|
|
|
|
var score;
|
|
|
|
|
2023-04-12 12:51:10 +02:00
|
|
|
export async function main() {
|
2023-04-11 12:15:56 +02:00
|
|
|
await fetch('./data.json')
|
|
|
|
.then((response) => response.json())
|
|
|
|
.then((json) => cpuList = json);
|
|
|
|
|
2023-04-12 13:29:07 +02:00
|
|
|
currentCpu = getRandomCpu();
|
|
|
|
nextCpu = getRandomCpu();
|
2023-04-12 13:46:14 +02:00
|
|
|
score = 0;
|
2023-04-12 01:25:57 +02:00
|
|
|
updateLayout();
|
2023-04-11 12:15:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function getRandomCpu() {
|
2023-04-11 13:00:03 +02:00
|
|
|
let randomIndex = getRandomInt(0, cpuList.length)
|
|
|
|
return {
|
|
|
|
name: cpuList[randomIndex]["name"].split('@')[0],
|
|
|
|
score: cpuList[randomIndex]["cpuScore"]
|
|
|
|
}
|
2023-04-11 12:15:56 +02:00
|
|
|
}
|
|
|
|
|
2023-04-12 12:32:09 +02:00
|
|
|
export function btnLowerClick() {
|
2023-04-12 13:03:27 +02:00
|
|
|
nextCpu.score < currentCpu.score ? showResult(true) : showResult(false);
|
2023-04-12 01:25:57 +02:00
|
|
|
}
|
|
|
|
|
2023-04-12 12:32:09 +02:00
|
|
|
export function btnHigherClick() {
|
2023-04-12 13:03:27 +02:00
|
|
|
nextCpu.score > currentCpu.score ? showResult(true) : showResult(false);
|
2023-04-12 01:25:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function showResult(isCorrect) {
|
2023-04-12 13:29:07 +02:00
|
|
|
document.getElementById("btnHigher").setAttribute("disabled", "");
|
|
|
|
document.getElementById("btnLower").setAttribute("disabled", "");
|
|
|
|
|
2023-04-12 09:00:36 +02:00
|
|
|
document.getElementById("col2").style.backgroundColor = isCorrect ? "lightgreen" : "#FF4444";
|
2023-04-12 13:46:14 +02:00
|
|
|
score = isCorrect ? score + 1 : 0;
|
|
|
|
document.getElementById("score").innerText = score;
|
2023-04-12 09:00:36 +02:00
|
|
|
|
|
|
|
countUp();
|
2023-04-12 01:25:57 +02:00
|
|
|
}
|
|
|
|
|
2023-04-12 13:46:14 +02:00
|
|
|
// updates view based on the cpu objects
|
2023-04-12 01:25:57 +02:00
|
|
|
function updateLayout() {
|
|
|
|
document.getElementById("currentCpuTitle").innerText = currentCpu.name;
|
|
|
|
// add "." to large numbers
|
2023-04-12 12:41:25 +02:00
|
|
|
document.getElementById("currentCpuScore").innerText = new Intl.NumberFormat().format(currentCpu.score)
|
2023-04-12 01:25:57 +02:00
|
|
|
document.getElementById("nextCpuTitle").innerText = nextCpu.name;
|
2023-04-12 13:29:07 +02:00
|
|
|
|
|
|
|
document.getElementById("nextCpuScore").innerText = "?";
|
|
|
|
|
|
|
|
document.getElementById("col2").style.backgroundColor = "";
|
2023-04-12 01:25:57 +02:00
|
|
|
}
|
|
|
|
|
2023-04-12 13:29:07 +02:00
|
|
|
function delay(time) {
|
|
|
|
return new Promise(resolve => setTimeout(resolve, time));
|
|
|
|
}
|
2023-04-12 09:00:36 +02:00
|
|
|
|
2023-04-12 13:29:07 +02:00
|
|
|
async function countUp() {
|
2023-04-12 09:00:36 +02:00
|
|
|
const options = {
|
2023-04-12 13:29:07 +02:00
|
|
|
startVal: nextCpu.score / 2,
|
2023-04-12 09:00:36 +02:00
|
|
|
separator: '.',
|
|
|
|
decimal: ',',
|
2023-04-12 13:29:07 +02:00
|
|
|
duration: 2
|
2023-04-12 09:00:36 +02:00
|
|
|
};
|
|
|
|
let counter = new CountUp('nextCpuScore', nextCpu.score, options);
|
|
|
|
if (!counter.error) {
|
2023-04-12 13:29:07 +02:00
|
|
|
counter.start();
|
2023-04-12 09:00:36 +02:00
|
|
|
} else {
|
|
|
|
counter.error(demo.error);
|
|
|
|
}
|
2023-04-12 13:29:07 +02:00
|
|
|
|
2023-04-12 13:46:14 +02:00
|
|
|
await delay(2500)
|
2023-04-12 13:29:07 +02:00
|
|
|
nextRound();
|
|
|
|
|
|
|
|
document.getElementById("btnHigher").removeAttribute("disabled");
|
|
|
|
document.getElementById("btnLower").removeAttribute("disabled");
|
|
|
|
}
|
|
|
|
|
|
|
|
function nextRound() {
|
|
|
|
currentCpu = nextCpu;
|
|
|
|
nextCpu = getRandomCpu();
|
|
|
|
|
|
|
|
updateLayout();
|
2023-04-12 09:00:36 +02:00
|
|
|
}
|
|
|
|
|
2023-04-11 13:00:03 +02:00
|
|
|
function getRandomInt(min, max) {
|
|
|
|
min = Math.ceil(min);
|
|
|
|
max = Math.floor(max);
|
|
|
|
return Math.floor(Math.random() * (max - min) + min); // The maximum is exclusive and the minimum is inclusive
|
2023-04-12 12:51:10 +02:00
|
|
|
}
|