-
Notifications
You must be signed in to change notification settings - Fork 3
/
GameStorage.ts
39 lines (29 loc) · 1.11 KB
/
GameStorage.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
export class GameStorage {
static get highScore(): number {
const value = GameStorage.getCookie("highscore");
return value === "" ? 0 : value;
}
static set highScore(hs: number) {
GameStorage.setCookie("highscore", hs.toString(), 1000);
}
private static setCookie (cname: string, cvalue: string, exdays: number) {
const date = new Date();
date.setTime(date.getTime() + (exdays * 24 * 60 * 60 * 1000));
const expires = `expires=${date.toUTCString()}`;
document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
}
private static getCookie(cname: string): any {
const name = cname + "=";
const items = document.cookie.split(";");
for (let i = 0; i < items.length; i++) {
let c = items[i];
while (c.charAt(0) === " ") {
c = c.substring(1);
}
if (c.indexOf(name) === 0) {
return c.substring(name.length, c.length);
}
}
return "";
}
}