CpuHigherLower/gst.js

93 lines
2.2 KiB
JavaScript
Raw Normal View History

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)
} while (typeof(cpuList[randomIndex]["type"]) == null)
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:31:47 +02:00
console.log("Button clicked: typ: " + typ + "cpu type: " + currentCpu.type);
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:31:47 +02:00
let btns = [btnDesktop, btnLaptop, btnMobile];
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;
}
switch (currentCpu.type) {
case "Desktop":
btnDesktop.style.backgroundColor = "lightgreen";
break;
case "Laptop":
btnLaptop.style.backgroundColor = "lightgreen";
break;
case "Mobile/Embedded":
btnMobile.style.backgroundColor = "lightgreen";
break;
}
await delay(2000);
btns.forEach( (el) => {
2023-04-13 18:31:47 +02:00
el.style.backgroundColor = "#3CC3FA";
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();