CpuHigherLower/game.js

171 lines
4.6 KiB
JavaScript
Raw Normal View History

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 {
2023-04-12 18:47:46 +02:00
#model;
2023-04-11 13:00:03 +02:00
2023-04-12 18:47:46 +02:00
#scoreEl;
#highscoreEl;
2023-04-12 18:19:01 +02:00
2023-04-12 18:47:46 +02:00
#currCpuTitleEl;
#currCpuScoreEl;
2023-04-12 18:19:01 +02:00
2023-04-12 18:47:46 +02:00
#nextCpuTitleEl;
#nextCpuScoreEl;
2023-04-12 18:19:01 +02:00
2023-04-12 18:47:46 +02:00
#nextCpuStyle;
2023-04-12 15:14:01 +02:00
2023-04-12 18:47:46 +02:00
#btnHigher;
#btnLower;
2023-04-12 18:19:01 +02:00
constructor() {
2023-04-12 18:47:46 +02:00
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() {
2023-04-12 18:47:46 +02:00
await this.#model.init();
2023-04-12 18:19:01 +02:00
2023-04-12 18:47:46 +02:00
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:47:46 +02:00
this.#btnHigher = document.getElementById("btnHigher");
this.#btnLower = document.getElementById("btnLower");
this.#btnHigher.onclick = () => this.handleHigherPress();
this.#btnLower.onclick = () => this.handleLowerPress();
2023-04-12 18:19:01 +02:00
this.updateUiFromModel();
2023-04-11 13:00:03 +02:00
}
2023-04-11 12:15:56 +02:00
2023-04-12 18:47:46 +02:00
async handleHigherPress() {
let result = this.#model.postAnswer('higher');
await this.animateTransition(result);
2023-04-12 18:19:01 +02:00
}
2023-04-12 01:25:57 +02:00
2023-04-12 18:47:46 +02:00
async handleLowerPress() {
let result = this.#model.postAnswer('lower');
await this.animateTransition(result);
2023-04-12 18:19:01 +02:00
}
2023-04-12 01:25:57 +02:00
2023-04-12 18:19:01 +02:00
updateUiFromModel() {
2023-04-12 18:47:46 +02:00
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 18:19:01 +02:00
}
2023-04-12 13:29:07 +02:00
2023-04-12 18:19:01 +02:00
async animateTransition(wasCorrect) {
this.updateUiFromModel();
2023-04-12 18:47:46 +02:00
this.#btnHigher.setAttribute("disabled", "");
this.#btnLower.setAttribute("disabled", "");
this.#nextCpuStyle.backgroundColor = wasCorrect ? "lightgreen" : "#FF4444";
2023-04-12 18:19:01 +02:00
const options = {
2023-04-12 18:47:46 +02:00
startVal: this.#model.nextCpu.score / 2,
2023-04-12 18:19:01 +02:00
separator: '.',
decimal: ',',
duration: 2
};
2023-04-12 18:47:46 +02:00
const counter = new CountUp(this.#nextCpuScoreEl, this.#model.nextCpu.score, options);
2023-04-12 18:19:01 +02:00
await new Promise(resolve => counter.start(resolve));
await this.#delay(500);
2023-04-12 18:47:46 +02:00
this.#model.nextRound();
2023-04-12 18:19:01 +02:00
this.updateUiFromModel();
2023-04-12 18:47:46 +02:00
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();
2023-04-12 18:47:46 +02:00
await ui.init()
2023-04-12 18:19:01 +02:00
}