2023-04-12 13:39:26 +02:00
|
|
|
import { CountUp } from "https://cdnjs.cloudflare.com/ajax/libs/countup.js/2.6.0/countUp.min.js";
|
2023-04-12 12:32:09 +02:00
|
|
|
|
2023-04-12 18:19:01 +02:00
|
|
|
class Ui {
|
|
|
|
model;
|
2023-04-11 13:00:03 +02:00
|
|
|
|
2023-04-12 18:19:01 +02:00
|
|
|
scoreEl;
|
|
|
|
highscoreEl;
|
|
|
|
|
|
|
|
currCpuTitleEl;
|
|
|
|
currCpuScoreEl;
|
|
|
|
|
|
|
|
nextCpuTitleEl;
|
|
|
|
nextCpuScoreEl;
|
|
|
|
|
|
|
|
nextCpuStyle;
|
2023-04-12 15:14:01 +02:00
|
|
|
|
2023-04-12 18:19:01 +02:00
|
|
|
btnHigher;
|
|
|
|
btnLower;
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
this.model = new Model();
|
2023-04-12 15:14:01 +02:00
|
|
|
}
|
2023-04-12 13:46:14 +02:00
|
|
|
|
2023-04-12 18:19:01 +02:00
|
|
|
async init() {
|
|
|
|
await this.model.init();
|
|
|
|
|
|
|
|
this.scoreEl = document.getElementById("score");
|
|
|
|
this.highscoreEl = document.getElementById("highscore");
|
|
|
|
this.currCpuTitleEl = document.getElementById("currentCpuTitle");
|
|
|
|
this.currCpuScoreEl = document.getElementById("currentCpuScore");
|
|
|
|
this.nextCpuTitleEl = document.getElementById("nextCpuTitle");
|
|
|
|
this.nextCpuScoreEl = document.getElementById("nextCpuScore");
|
|
|
|
this.nextCpuStyle = document.getElementById("nextCpuCol").style;
|
2023-04-11 12:15:56 +02:00
|
|
|
|
2023-04-12 18:19:01 +02:00
|
|
|
this.btnHigher = document.getElementById("btnHigher");
|
|
|
|
this.btnLower = document.getElementById("btnLower");
|
|
|
|
this.btnHigher.onclick = () => this.handleHigherPress();
|
|
|
|
this.btnLower.onclick = () => this.handleLowerPress();
|
|
|
|
|
|
|
|
this.updateUiFromModel();
|
2023-04-11 13:00:03 +02:00
|
|
|
}
|
2023-04-11 12:15:56 +02:00
|
|
|
|
2023-04-12 18:19:01 +02:00
|
|
|
handleHigherPress() {
|
|
|
|
let result = this.model.postAnswer('higher');
|
|
|
|
this.animateTransition(result);
|
|
|
|
}
|
2023-04-12 01:25:57 +02:00
|
|
|
|
2023-04-12 18:19:01 +02:00
|
|
|
handleLowerPress() {
|
|
|
|
let result = this.model.postAnswer('lower');
|
|
|
|
this.animateTransition(result);
|
|
|
|
}
|
2023-04-12 01:25:57 +02:00
|
|
|
|
2023-04-12 18:19:01 +02:00
|
|
|
updateUiFromModel() {
|
|
|
|
this.scoreEl.innerText = this.model.score;
|
|
|
|
this.highscoreEl.innerText = this.model.highscore;
|
|
|
|
this.currCpuTitleEl.innerText = this.model.currentCpu.name;
|
|
|
|
this.currCpuScoreEl.innerText = new Intl.NumberFormat().format(this.model.currentCpu.score);
|
|
|
|
this.nextCpuTitleEl.innerText = this.model.nextCpu.name;
|
|
|
|
}
|
2023-04-12 13:29:07 +02:00
|
|
|
|
2023-04-12 18:19:01 +02:00
|
|
|
async animateTransition(wasCorrect) {
|
|
|
|
this.updateUiFromModel();
|
|
|
|
this.btnHigher.setAttribute("disabled", "");
|
|
|
|
this.btnLower.setAttribute("disabled", "");
|
|
|
|
this.nextCpuStyle.backgroundColor = wasCorrect ? "lightgreen" : "#FF4444";
|
|
|
|
|
|
|
|
const options = {
|
|
|
|
startVal: this.model.nextCpu.score / 2,
|
|
|
|
separator: '.',
|
|
|
|
decimal: ',',
|
|
|
|
duration: 2
|
|
|
|
};
|
|
|
|
const counter = new CountUp(this.nextCpuScoreEl, this.model.nextCpu.score, options);
|
|
|
|
|
|
|
|
await new Promise(resolve => counter.start(resolve));
|
|
|
|
await this.#delay(500);
|
|
|
|
|
|
|
|
this.model.nextRound();
|
|
|
|
this.updateUiFromModel();
|
|
|
|
|
|
|
|
this.nextCpuStyle.backgroundColor = '';
|
|
|
|
this.nextCpuScoreEl.innerText = '?';
|
|
|
|
this.btnHigher.removeAttribute("disabled");
|
|
|
|
this.btnLower.removeAttribute("disabled");
|
2023-04-12 15:14:01 +02:00
|
|
|
}
|
2023-04-12 09:00:36 +02:00
|
|
|
|
2023-04-12 18:19:01 +02:00
|
|
|
#delay(time) {
|
|
|
|
return new Promise(resolve => setTimeout(resolve, time));
|
|
|
|
}
|
2023-04-12 01:25:57 +02:00
|
|
|
}
|
|
|
|
|
2023-04-12 18:19:01 +02:00
|
|
|
class Model {
|
|
|
|
#cpuList;
|
2023-04-12 13:29:07 +02:00
|
|
|
|
2023-04-12 18:19:01 +02:00
|
|
|
#currentCpu;
|
|
|
|
#nextCpu;
|
2023-04-12 01:25:57 +02:00
|
|
|
|
2023-04-12 18:19:01 +02:00
|
|
|
#score;
|
|
|
|
#highscore;
|
2023-04-12 09:00:36 +02:00
|
|
|
|
2023-04-12 18:19:01 +02:00
|
|
|
async init() {
|
|
|
|
const fetchResult = await fetch("./data.json");
|
|
|
|
this.#cpuList = await fetchResult.json();
|
|
|
|
|
|
|
|
this.#currentCpu = this.#getRandomCpu();
|
|
|
|
this.#nextCpu = this.#getRandomCpu();
|
|
|
|
this.#score = 0;
|
|
|
|
this.#highscore = localStorage.getItem("highscore_cpu") ?? 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
get highscore() {
|
|
|
|
return this.#highscore;
|
|
|
|
}
|
2023-04-12 13:29:07 +02:00
|
|
|
|
2023-04-12 18:19:01 +02:00
|
|
|
get score() {
|
|
|
|
return this.#score;
|
|
|
|
}
|
|
|
|
|
|
|
|
get currentCpu() {
|
|
|
|
return this.#currentCpu;
|
|
|
|
}
|
|
|
|
|
|
|
|
get nextCpu() {
|
|
|
|
return this.#nextCpu;
|
|
|
|
}
|
2023-04-12 13:29:07 +02:00
|
|
|
|
2023-04-12 18:19:01 +02:00
|
|
|
// Can be 'higher' or 'lower'
|
|
|
|
postAnswer(answer) {
|
|
|
|
let answerWasCorrect = false;
|
|
|
|
if (answer === 'higher') {
|
|
|
|
answerWasCorrect = this.nextCpu.score > this.#currentCpu.score;
|
|
|
|
} else if (answer === 'lower') {
|
|
|
|
answerWasCorrect = this.nextCpu.score < this.#currentCpu.score;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (answerWasCorrect) {
|
|
|
|
this.#score += 1;
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
this.#updateHighscore(this.score)
|
|
|
|
this.#score = 0;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nextRound() {
|
|
|
|
this.#currentCpu = this.#nextCpu;
|
|
|
|
this.#nextCpu = this.#getRandomCpu();
|
|
|
|
}
|
|
|
|
|
|
|
|
#getRandomCpu() {
|
|
|
|
const item = this.#cpuList[Math.floor(Math.random() * this.#cpuList.length)];
|
|
|
|
return {
|
|
|
|
name: item["name"].split('@')[0],
|
|
|
|
score: item["cpuScore"]
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#updateHighscore(newScore) {
|
|
|
|
if (newScore > this.#highscore) {
|
|
|
|
localStorage.setItem("highscore_cpu", newScore);
|
|
|
|
this.#highscore = newScore;
|
|
|
|
}
|
|
|
|
}
|
2023-04-12 09:00:36 +02:00
|
|
|
}
|
|
|
|
|
2023-04-12 18:19:01 +02:00
|
|
|
export async function main() {
|
|
|
|
const ui = new Ui();
|
|
|
|
ui.init()
|
|
|
|
}
|