CpuHigherLower/js/gst.js

124 lines
3.1 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));
2023-04-14 00:53:31 +02:00
highScore = localStorage.getItem("highScore_socket") ?? 0
score = 0;
updateScores();
2023-04-12 21:42:33 +02:00
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
}
2023-04-14 00:53:31 +02:00
// Score
if (btns[typ].style.backgroundColor == "lightgreen") {
score++;
} else {
score = 0;
}
// Highscore
if (score > highScore) {
highScore = score;
localStorage.setItem("highScore_socket", highScore);
}
updateScores();
await delay(1000);
2023-04-12 22:19:37 +02:00
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-14 00:53:31 +02:00
function updateScores() {
document.getElementById("score").innerText = score;
document.getElementById("highScore").innerText = highScore;
}
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();