CpuHigherLower/js/statistics.js

42 lines
2.6 KiB
JavaScript
Raw Normal View History

2023-04-21 22:41:22 +02:00
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
if (kind === "m") throw new TypeError("Private method is not writable");
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
};
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
};
var _Stats_score, _Stats_highScore, _Stats_highScoreStorageKey;
2023-04-19 20:03:14 +02:00
export class Stats {
2023-04-19 19:56:51 +02:00
constructor(highScoreStorageKey) {
// used as key for localStorage
2023-04-21 22:41:22 +02:00
var _a;
_Stats_score.set(this, void 0);
_Stats_highScore.set(this, void 0);
_Stats_highScoreStorageKey.set(this, void 0);
__classPrivateFieldSet(this, _Stats_highScoreStorageKey, highScoreStorageKey, "f");
__classPrivateFieldSet(this, _Stats_score, 0, "f");
__classPrivateFieldSet(this, _Stats_highScore, (_a = localStorage.getItem(__classPrivateFieldGet(this, _Stats_highScoreStorageKey, "f"))) !== null && _a !== void 0 ? _a : 0, "f");
2023-04-19 19:56:51 +02:00
}
incrementScore(value = 1) {
2023-04-21 22:41:22 +02:00
__classPrivateFieldSet(this, _Stats_score, __classPrivateFieldGet(this, _Stats_score, "f") + value, "f");
if (__classPrivateFieldGet(this, _Stats_highScore, "f") < __classPrivateFieldGet(this, _Stats_score, "f")) {
__classPrivateFieldSet(this, _Stats_highScore, __classPrivateFieldGet(this, _Stats_score, "f"), "f");
localStorage.setItem(__classPrivateFieldGet(this, _Stats_highScoreStorageKey, "f"), __classPrivateFieldGet(this, _Stats_highScore, "f"));
2023-04-19 19:56:51 +02:00
}
}
2023-04-19 20:48:51 +02:00
resetScore() {
2023-04-21 22:41:22 +02:00
__classPrivateFieldSet(this, _Stats_score, 0, "f");
2023-04-19 20:48:51 +02:00
}
2023-04-19 20:03:14 +02:00
get highScore() {
2023-04-21 22:41:22 +02:00
return __classPrivateFieldGet(this, _Stats_highScore, "f");
2023-04-19 20:03:14 +02:00
}
get score() {
2023-04-21 22:41:22 +02:00
return __classPrivateFieldGet(this, _Stats_score, "f");
2023-04-19 20:03:14 +02:00
}
2023-04-21 22:41:22 +02:00
}
_Stats_score = new WeakMap(), _Stats_highScore = new WeakMap(), _Stats_highScoreStorageKey = new WeakMap();