2023-04-19 20:03:14 +02:00
|
|
|
export class Stats {
|
2023-04-19 19:56:51 +02:00
|
|
|
#score;
|
|
|
|
#highScore;
|
|
|
|
#highScoreStorageKey;
|
|
|
|
|
|
|
|
constructor(highScoreStorageKey) {
|
|
|
|
// used as key for localStorage
|
|
|
|
|
|
|
|
this.#highScoreStorageKey = highScoreStorageKey;
|
|
|
|
|
|
|
|
this.#score = 0;
|
|
|
|
this.#highScore = localStorage.getItem(this.#highScoreStorageKey) ?? 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
incrementScore(value = 1) {
|
|
|
|
this.#score += value;
|
|
|
|
|
|
|
|
if (this.#highScore < this.#score) {
|
|
|
|
this.#highScore = this.#score;
|
|
|
|
localStorage.setItem(this.#highScoreStorageKey, this.#highScore);
|
|
|
|
}
|
|
|
|
}
|
2023-04-19 20:03:14 +02:00
|
|
|
|
|
|
|
get highScore() {
|
|
|
|
return this.#highScore;
|
|
|
|
}
|
|
|
|
|
|
|
|
get score() {
|
|
|
|
return this.#score;
|
|
|
|
}
|
2023-04-19 19:56:51 +02:00
|
|
|
}
|