add score and next round

This commit is contained in:
Florian Schmid 2023-04-26 23:34:40 +02:00
parent b4830536a2
commit ae1d9a077a

View File

@ -7,6 +7,9 @@ class UI {
private buttons: HTMLElement[]; private buttons: HTMLElement[];
private cpuName: HTMLElement; private cpuName: HTMLElement;
private score: HTMLElement;
private highScore: HTMLElement;
constructor() { constructor() {
this.model = new ViewModel(); this.model = new ViewModel();
@ -15,17 +18,16 @@ class UI {
let button3 = document.getElementById("btnDate3"); let button3 = document.getElementById("btnDate3");
let button4 = document.getElementById("btnDate4"); let button4 = document.getElementById("btnDate4");
this.buttons = [button1, button2, button3, button4]; this.buttons = [button1, button2, button3, button4];
this.score = document.getElementById("score");
this.highScore = document.getElementById("highScore");
} }
async init() { async init() {
await this.model.init(); await this.model.init();
this.cpuName = document.getElementById("cpuName"); this.cpuName = document.getElementById("cpuName");
this.cpuName.innerText = this.model.currentCpu.name; this.nextRound();
this.model.Dates.forEach((value, index) =>
(this.buttons[index].innerText = value)
);
this.buttons.forEach( this.buttons.forEach(
(btn) => { (btn) => {
@ -35,8 +37,29 @@ class UI {
} }
buttonClick(btn: HTMLElement) { buttonClick(btn: HTMLElement) {
btn.setAttribute("disabled", "");
let result = this.model.processClick(btn.innerText); let result = this.model.processClick(btn.innerText);
btn.style.backgroundColor = result ? "lightgreen" : "#FF4444"; btn.style.backgroundColor = result ? "lightgreen" : "#FF4444";
this.updateScores();
}
nextRound() {
this.buttons.forEach(
(btn) => {btn.removeAttribute("disabled");
}
)
this.cpuName.innerText = this.model.currentCpu.name;
this.model.Dates.forEach((value, index) =>
(this.buttons[index].innerText = value)
);
}
private updateScores() {
this.score.innerText = this.model.score.toString();
this.highScore.innerText = this.model.highScore.toString();
} }
} }
@ -61,8 +84,12 @@ class ViewModel {
this.repo.reset(); this.repo.reset();
} }
incrementScore() { incrementScore(num: number = 1) {
this.stats.incrementScore(); this.stats.incrementScore(num);
}
reduceScore() {
this.stats.incrementScore(-1);
} }
resetScore() { resetScore() {
@ -116,7 +143,13 @@ class ViewModel {
} }
processClick(text: string): boolean { processClick(text: string): boolean {
return text == this.repo.currentCpu.date; let result = text == this.repo.currentCpu.date;
if (result) {
this.incrementScore(2);
} else {
this.reduceScore();
}
return result;
} }
get currentCpu() { get currentCpu() {