Use template's Storage class
This commit is contained in:
parent
36eb43ed8d
commit
0e0238687a
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "minesweeper",
|
||||
"version": "0.82.15",
|
||||
"version": "0.82.16",
|
||||
"description": "Just Minesweeper!",
|
||||
"author": "Florine W. Dekker",
|
||||
"browser": "dist/bundle.js",
|
||||
|
|
|
@ -205,6 +205,7 @@
|
|||
</main>
|
||||
|
||||
<script src="https://static.fwdekker.com/lib/template/2.x.x/template.js"></script>
|
||||
<script src="https://static.fwdekker.com/lib/template/2.x.x/storage.js"></script>
|
||||
<!--suppress HtmlUnknownTarget -->
|
||||
<script src="bundle.js?v=%%VERSION_NUMBER%%"></script>
|
||||
</body>
|
||||
|
|
|
@ -6,8 +6,9 @@ import {findDifficulty} from "./Difficulty";
|
|||
import {HighScores} from "./HighScores";
|
||||
import {Solver} from "./Solver";
|
||||
import {Statistics} from "./Statistics";
|
||||
import {MemoryStorage} from "./Storage";
|
||||
import {Timer} from "./Timer";
|
||||
// @ts-ignore
|
||||
const {MemoryStorage} = window.fwdekker.storage;
|
||||
|
||||
|
||||
/**
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
import {formatTime} from "./Common";
|
||||
import {difficulties, Difficulty} from "./Difficulty";
|
||||
import {LocalStorage, Storage} from "./Storage";
|
||||
// @ts-ignore
|
||||
const {Storage, LocalStorage} = window.fwdekker.storage;
|
||||
|
||||
|
||||
/**
|
||||
|
@ -43,7 +44,7 @@ export class HighScores {
|
|||
addScore(difficulty: Difficulty, score: Score): void {
|
||||
const scores = this.storage.getArray(difficulty.name, []);
|
||||
scores.push(score);
|
||||
scores.sort((a, b) => {
|
||||
scores.sort((a: Score, b: Score) => {
|
||||
return Math.floor(a.time / 1000) !== Math.floor(b.time / 1000)
|
||||
? a.time - b.time
|
||||
: a.deaths - b.deaths;
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
import {BasicIconFont, IconFont} from "./Display";
|
||||
import {LocalStorage, Storage} from "./Storage";
|
||||
// @ts-ignore
|
||||
const {Storage, LocalStorage} = window.fwdekker.storage;
|
||||
|
||||
|
||||
/**
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
import {formatTime} from "./Common";
|
||||
import {LocalStorage, Storage} from "./Storage";
|
||||
// @ts-ignore
|
||||
const {Storage, LocalStorage} = window.fwdekker.storage;
|
||||
|
||||
|
||||
/**
|
||||
|
|
|
@ -1,178 +0,0 @@
|
|||
/**
|
||||
* Stores key-value pairs.
|
||||
*/
|
||||
export interface Storage {
|
||||
/**
|
||||
* Removes the data from storage.
|
||||
*/
|
||||
clear(): void
|
||||
|
||||
/**
|
||||
* Retrieves an array from storage.
|
||||
*
|
||||
* @param name the name of the array to retrieve
|
||||
* @param def the value to return if no array is stored with the given name
|
||||
*/
|
||||
getArray(name: string, def: any[]): any[]
|
||||
|
||||
/**
|
||||
* Stores an array.
|
||||
*
|
||||
* @param name the name of the array to store
|
||||
* @param value the array to store under the given name
|
||||
* @protected
|
||||
*/
|
||||
setArray(name: string, value: any[]): void
|
||||
|
||||
/**
|
||||
* Retrieves a boolean from storage.
|
||||
*
|
||||
* @param name the name of the boolean to retrieve
|
||||
* @param def the value to return if no boolean is stored with the given name
|
||||
* @protected
|
||||
*/
|
||||
getBoolean(name: string, def: boolean): boolean
|
||||
|
||||
/**
|
||||
* Stores a boolean.
|
||||
*
|
||||
* @param name the name of the boolean to store
|
||||
* @param value the boolean to store under the given name
|
||||
* @protected
|
||||
*/
|
||||
setBoolean(name: string, value: boolean): void
|
||||
|
||||
/**
|
||||
* Retrieves a number from storage.
|
||||
*
|
||||
* @param name the name of the number to retrieve
|
||||
* @param def the value to return if no number is stored with the given name
|
||||
* @protected
|
||||
*/
|
||||
getNumber(name: string, def: number): number
|
||||
|
||||
/**
|
||||
* Stores a number.
|
||||
*
|
||||
* @param name the name of the number to store
|
||||
* @param value the number to store under the given name
|
||||
* @protected
|
||||
*/
|
||||
setNumber(name: string, value: number): void
|
||||
}
|
||||
|
||||
/**
|
||||
* Stores key-value pairs in a single entry in `localStorage`.
|
||||
*/
|
||||
export class LocalStorage implements Storage {
|
||||
private readonly key: string;
|
||||
private cache: { [key: string]: string } | null = null;
|
||||
|
||||
|
||||
/**
|
||||
* Constructs a new persistent storage item under the given key.
|
||||
*
|
||||
* @param key the unique identifier to store the data under
|
||||
*/
|
||||
constructor(key: string) {
|
||||
this.key = key;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Reads the object stored in local storage.
|
||||
*
|
||||
* @return the object stored in local storage, or an empty object if there is nothing in the local storage
|
||||
* @private
|
||||
*/
|
||||
private read(): { [key: string]: string } {
|
||||
if (this.cache === null)
|
||||
this.cache = JSON.parse(localStorage.getItem(this.key) ?? "{}");
|
||||
|
||||
return this.cache!;
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes the given object to local storage.
|
||||
*
|
||||
* @param item the object to write to local storage
|
||||
* @private
|
||||
*/
|
||||
private write(item: { [key: string]: string }): void {
|
||||
this.cache = item;
|
||||
localStorage.setItem(this.key, JSON.stringify(item));
|
||||
}
|
||||
|
||||
|
||||
clear(): void {
|
||||
this.cache = null;
|
||||
localStorage.removeItem(this.key);
|
||||
}
|
||||
|
||||
getArray(name: string, def: any[] = []): any[] {
|
||||
const array = this.read()[name];
|
||||
return array === undefined ? def : JSON.parse(array);
|
||||
}
|
||||
|
||||
setArray(name: string, value: any[]): void {
|
||||
const item = this.read();
|
||||
item[name] = JSON.stringify(value);
|
||||
this.write(item);
|
||||
}
|
||||
|
||||
getBoolean(name: string, def: boolean = false): boolean {
|
||||
return (this.read()[name] ?? `${def}`) === "true";
|
||||
}
|
||||
|
||||
setBoolean(name: string, value: boolean): void {
|
||||
const item = this.read();
|
||||
item[name] = "" + value;
|
||||
this.write(item);
|
||||
}
|
||||
|
||||
getNumber(name: string, def: number = 0): number {
|
||||
return +(this.read()[name] ?? def);
|
||||
}
|
||||
|
||||
setNumber(name: string, value: number): void {
|
||||
const item = this.read();
|
||||
item[name] = "" + value;
|
||||
this.write(item);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Stores key-value pairs in an object.
|
||||
*/
|
||||
export class MemoryStorage implements Storage {
|
||||
private storage: { [key: string]: any } = {};
|
||||
|
||||
|
||||
clear(): void {
|
||||
this.storage = {};
|
||||
}
|
||||
|
||||
setArray(name: string, value: any[] = []): void {
|
||||
this.storage[name] = value;
|
||||
}
|
||||
|
||||
getArray(name: string, def: any[]): any[] {
|
||||
return this.storage[name] ?? def;
|
||||
}
|
||||
|
||||
setBoolean(name: string, value: boolean): void {
|
||||
this.storage[name] = value;
|
||||
}
|
||||
|
||||
getBoolean(name: string, def: boolean): boolean {
|
||||
return this.storage[name] ?? def;
|
||||
}
|
||||
|
||||
setNumber(name: string, value: number): void {
|
||||
this.storage[name] = value;
|
||||
}
|
||||
|
||||
getNumber(name: string, def: number): number {
|
||||
return this.storage[name] ?? def;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue