35 lines
794 B
TypeScript
35 lines
794 B
TypeScript
import { PREF } from "./const";
|
|
|
|
export const MAX_POINTS = 10000;
|
|
export const MIN_POINTS = 0;
|
|
|
|
function setPoints(points: string) {
|
|
localStorage.setItem(PREF.points, points);
|
|
window.dispatchEvent(new Event('storage'));
|
|
}
|
|
|
|
function getPoints(): number {
|
|
const points = localStorage.getItem(PREF.points);
|
|
if (points === null) {
|
|
return 0;
|
|
}
|
|
return Number.parseInt(points);
|
|
}
|
|
|
|
function clampPoints(points: number) {
|
|
return Math.max(MIN_POINTS, Math.min(MAX_POINTS, points));
|
|
}
|
|
|
|
export function addPoints(points: number) {
|
|
let p = getPoints();
|
|
p += points;
|
|
p = clampPoints(p);
|
|
setPoints(p.toString());
|
|
}
|
|
|
|
export function takePoints(points: number) {
|
|
let p = getPoints();
|
|
p -= points;
|
|
p = clampPoints(p);
|
|
setPoints(p.toString());
|
|
} |