add statistic class

This commit is contained in:
Florian Schmid 2023-04-19 19:56:51 +02:00
parent 69a4772b76
commit 1fb4e3f8e3

23
js/statistics.js Normal file
View File

@ -0,0 +1,23 @@
class Stats {
#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);
}
}
}