2023-04-12 09:00:36 +02:00
|
|
|
var cpuList;
|
2023-04-12 21:42:33 +02:00
|
|
|
var currentCpu;
|
|
|
|
|
|
|
|
var score;
|
|
|
|
var highScore;
|
|
|
|
|
2023-04-12 22:19:37 +02:00
|
|
|
export async function main() {
|
2023-04-12 21:42:33 +02:00
|
|
|
// init cpu list
|
|
|
|
await (fetch('./data.json')
|
2023-04-12 09:00:36 +02:00
|
|
|
.then((response) => response.json())
|
2023-04-12 21:42:33 +02:00
|
|
|
.then((json) => cpuList = json));
|
|
|
|
|
|
|
|
nextRound();
|
|
|
|
}
|
|
|
|
|
|
|
|
function nextRound() {
|
|
|
|
currentCpu = getRandomCpu();
|
2023-04-12 09:00:36 +02:00
|
|
|
|
2023-04-12 21:42:33 +02:00
|
|
|
document.getElementById("cpuName").innerText = currentCpu.name;
|
2023-04-12 22:19:37 +02:00
|
|
|
|
|
|
|
document.getElementById("mainCol").style.backgroundColor = "";
|
2023-04-12 09:00:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function getRandomCpu() {
|
2023-04-12 21:42:33 +02:00
|
|
|
let randomIndex;
|
|
|
|
do {
|
|
|
|
randomIndex = getRandomInt(0, cpuList.length)
|
2023-04-14 00:02:35 +02:00
|
|
|
} while (typeof(cpuList[randomIndex]["type"]) == null || cpuList[randomIndex]["type"] == "null" || cpuList[randomIndex]["type"] == null);
|
2023-04-12 21:42:33 +02:00
|
|
|
|
|
|
|
|
2023-04-12 09:00:36 +02:00
|
|
|
return {
|
|
|
|
name: cpuList[randomIndex]["name"].split('@')[0],
|
2023-04-12 21:42:33 +02:00
|
|
|
type: cpuList[randomIndex]["type"]
|
2023-04-12 09:00:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-13 12:40:01 +02:00
|
|
|
function delay(time) {
|
|
|
|
return new Promise(resolve => setTimeout(resolve, time));
|
|
|
|
}
|
2023-04-12 22:19:37 +02:00
|
|
|
|
|
|
|
export async function btnClick(typ) {
|
|
|
|
// 0 -> Desktop
|
|
|
|
// 1 -> Laptop
|
|
|
|
// 2 -> Mobile/Embedded
|
2023-04-13 18:39:04 +02:00
|
|
|
// 3 -> Server
|
2023-04-12 22:19:37 +02:00
|
|
|
|
|
|
|
let btnDesktop = document.getElementById("btnDesktop");
|
|
|
|
let btnLaptop = document.getElementById("btnLaptop");
|
|
|
|
let btnMobile = document.getElementById("btnMobile");
|
2023-04-13 18:39:04 +02:00
|
|
|
let btnServer = document.getElementById("btnServer");
|
2023-04-12 22:19:37 +02:00
|
|
|
|
2023-04-13 18:39:04 +02:00
|
|
|
let btns = [btnDesktop, btnLaptop, btnMobile, btnServer];
|
2023-04-12 22:19:37 +02:00
|
|
|
|
2023-04-14 00:02:35 +02:00
|
|
|
btns.forEach((el) => {
|
|
|
|
el.setAttribute("disabled", "");
|
|
|
|
})
|
|
|
|
|
2023-04-12 22:19:37 +02:00
|
|
|
switch (typ) {
|
|
|
|
case 0:
|
|
|
|
btnDesktop.style.backgroundColor = "#FF4444";
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
btnLaptop.style.backgroundColor = "#FF4444";
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
btnMobile.style.backgroundColor = "#FF4444";
|
|
|
|
break;
|
2023-04-13 18:39:04 +02:00
|
|
|
case 3:
|
|
|
|
btnServer.style.backgroundColor = "#FF4444";
|
|
|
|
break;
|
2023-04-12 22:19:37 +02:00
|
|
|
}
|
|
|
|
|
2023-04-14 00:02:35 +02:00
|
|
|
if(currentCpu.type.includes("Desktop")) {
|
|
|
|
btnDesktop.style.backgroundColor = "lightgreen";
|
|
|
|
}
|
|
|
|
if(currentCpu.type.includes("Laptop")) {
|
|
|
|
btnLaptop.style.backgroundColor = "lightgreen";
|
|
|
|
}
|
|
|
|
if(currentCpu.type.includes("Mobile/Embedded")) {
|
|
|
|
btnMobile.style.backgroundColor = "lightgreen";
|
|
|
|
}
|
|
|
|
if(currentCpu.type.includes("Server")) {
|
|
|
|
btnServer.style.backgroundColor = "lightgreen";
|
2023-04-12 22:19:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
await delay(2000);
|
|
|
|
|
|
|
|
btns.forEach( (el) => {
|
2023-04-13 18:31:47 +02:00
|
|
|
el.style.backgroundColor = "#3CC3FA";
|
2023-04-14 00:02:35 +02:00
|
|
|
el.removeAttribute("disabled");
|
2023-04-12 22:19:37 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
nextRound();
|
|
|
|
}
|
|
|
|
|
2023-04-12 09:00:36 +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
|
|
|
|
}
|
|
|
|
|
|
|
|
main();
|