-
Notifications
You must be signed in to change notification settings - Fork 15
/
localStorage.js
83 lines (68 loc) · 2.31 KB
/
localStorage.js
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
function updateStreakAndFastestTimes(time, scoreString) {
const currentDate = getCurrentDate();
let now = `${
currentDate.getMonth() + 1
}/${currentDate.getDate()}/${currentDate.getFullYear()}`;
const lastPlayedDate = localStorage.getItem("lastPlayedDate");
if (lastPlayedDate !== now) {
const lastDate = new Date(lastPlayedDate);
const lastTime = lastDate.getTime();
const currentTime = currentDate.getTime();
const dayDifference = Math.floor(
(currentTime - lastTime) / (1000 * 60 * 60 * 24)
);
if (dayDifference === 1) {
let currentStreak = parseInt(localStorage.getItem("currentStreak")) || 0;
currentStreak++;
localStorage.setItem("currentStreak", currentStreak);
} else if (dayDifference > 1) {
localStorage.setItem("currentStreak", "1");
}
}
if (time === null) {
localStorage.setItem("currentStreak", "0");
}
localStorage.setItem("lastPlayedDate", now);
let currentStreak = parseInt(localStorage.getItem("currentStreak"));
let longestStreak = parseInt(localStorage.getItem("longestStreak")) || 0;
if (currentStreak > longestStreak) {
localStorage.setItem("longestStreak", currentStreak);
}
let totalDaysPlayed = parseInt(localStorage.getItem("totalDaysPlayed")) || 0;
totalDaysPlayed++;
localStorage.setItem("totalDaysPlayed", totalDaysPlayed);
if (time !== null) {
let fastestTimes = JSON.parse(localStorage.getItem("fastestTimes")) || [];
fastestTimes.push({ time, date: now });
fastestTimes.sort((a, b) => a.time - b.time);
if (fastestTimes.length > 3) {
fastestTimes.pop();
}
localStorage.setItem("fastestTimes", JSON.stringify(fastestTimes));
}
localStorage.setItem("latestScoreString", scoreString);
localStorage.setItem("latestTimeString", convertMillisecondsToTime(time));
}
function hasPlayedToday() {
let today = getCurrentDate();
let now = `${today.getMonth() + 1}/${today.getDate()}/${today.getFullYear()}`;
let lastPlayedDate = localStorage.getItem("lastPlayedDate");
return lastPlayedDate === now;
}
function autosave(game) {
if (game === null) {
clearAutosave();
}
localStorage.setItem("autosave", JSON.stringify(game));
}
function loadAutosave() {
let autosave = JSON.parse(localStorage.getItem("autosave"));
if (autosave) {
return autosave;
} else {
return null;
}
}
function clearAutosave() {
localStorage.removeItem("autosave");
}