CpuHigherLower/js/gst.js

103 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-21 22:41:22 +02:00
var _a;
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-21 22:41:22 +02:00
highScore = (_a = localStorage.getItem("highScore_socket")) !== null && _a !== void 0 ? _a : 0;
2023-04-14 00:53:31 +02:00
score = 0;
updateScores();
2023-04-12 21:42:33 +02:00
nextRound();
}
function nextRound() {
currentCpu = getRandomCpu();
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 {
2023-04-21 22:41:22 +02:00
randomIndex = getRandomInt(0, cpuList.length);
} while (typeof (cpuList[randomIndex]["type"]) == null || cpuList[randomIndex]["type"] == "null" || 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-21 22:41:22 +02:00
};
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-21 22:41:22 +02:00
let btnDesktop = document.getElementById("btnDesktop");
let btnLaptop = document.getElementById("btnLaptop");
let btnMobile = document.getElementById("btnMobile");
let btnServer = document.getElementById("btnServer");
2023-04-13 18:39:04 +02:00
let btns = [btnDesktop, btnLaptop, btnMobile, btnServer];
2023-04-14 00:02:35 +02:00
btns.forEach((el) => {
el.setAttribute("disabled", "");
2023-04-21 22:41:22 +02:00
});
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-21 22:41:22 +02:00
if (currentCpu.type.includes("Desktop")) {
2023-04-14 00:02:35 +02:00
btnDesktop.style.backgroundColor = "lightgreen";
2023-04-21 22:41:22 +02:00
}
if (currentCpu.type.includes("Laptop")) {
2023-04-14 00:02:35 +02:00
btnLaptop.style.backgroundColor = "lightgreen";
2023-04-21 22:41:22 +02:00
}
if (currentCpu.type.includes("Mobile/Embedded")) {
2023-04-14 00:02:35 +02:00
btnMobile.style.backgroundColor = "lightgreen";
}
2023-04-21 22:41:22 +02:00
if (currentCpu.type.includes("Server")) {
2023-04-14 00:02:35 +02:00
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++;
2023-04-21 22:41:22 +02:00
}
else {
2023-04-14 00:53:31 +02:00
score = 0;
}
// Highscore
if (score > highScore) {
highScore = score;
localStorage.setItem("highScore_socket", highScore);
}
updateScores();
await delay(1000);
2023-04-21 22:41:22 +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-21 22:41:22 +02:00
});
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
}
2023-04-21 22:41:22 +02:00
main();