Repository navigation
Expand file tree
/
Copy pathstate.js
More file actions
149 lines (138 loc) · 6.9 KB
/
Copy pathstate.js
File metadata and controls
149 lines (138 loc) · 6.9 KB
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
"use strict";
(function exposeLernsteinState(root) {
const VERSION = 4;
const categories = ["names", "hardness", "properties"];
const modes = ["type", "flashcard", "choice", "matching", "review", "timed"];
const properties = [
"chemical_formula", "color", "other_colors", "streak", "luster", "cleavage",
"density", "fracture", "transparency", "refractive_index", "crystal_system",
"locations", "formation",
];
function freshState() {
return {
schemaVersion: VERSION,
language: "de",
theme: "light",
category: "names",
mode: "type",
property: "hardness",
matchingVariant: "image",
soundEnabled: true,
photoVarietyEnabled: false,
progress: {},
markedStones: [],
daily: {},
goal: { scope: "starter20", group: "", size: 20 },
gamification: {
enabled: true,
fundserie: { days: 0, lastDay: null, lastGraceWeek: null },
dailyExpedition: { day: "", correct: 0 },
weeklyExpedition: { week: "", masteredStones: [] },
seals: [],
ownedStones: [],
groupRounds: [],
},
};
}
function objectOrEmpty(value) {
return value && typeof value === "object" && !Array.isArray(value) ? value : {};
}
function uniqueStrings(value) {
return [...new Set(Array.isArray(value) ? value.filter((item) => typeof item === "string") : [])];
}
function normalizeState(saved) {
if (!saved || typeof saved !== "object" || Array.isArray(saved)) {
throw new TypeError("Lernstein-Daten müssen ein JSON-Objekt sein.");
}
const defaults = freshState();
const state = { ...defaults, ...saved, schemaVersion: VERSION };
if (!["de", "en"].includes(state.language)) state.language = defaults.language;
if (!["light", "dark"].includes(state.theme)) state.theme = defaults.theme;
if (!categories.includes(state.category)) state.category = defaults.category;
if (!modes.includes(state.mode)) state.mode = defaults.mode;
if (state.mode === "timed") state.category = "names";
if (!["image", "language"].includes(state.matchingVariant)) state.matchingVariant = defaults.matchingVariant;
state.soundEnabled = saved.soundEnabled === undefined ? defaults.soundEnabled : saved.soundEnabled === true;
state.photoVarietyEnabled = state.photoVarietyEnabled === true;
if (!properties.includes(state.property)) state.property = defaults.property;
state.progress = objectOrEmpty(state.progress);
state.daily = objectOrEmpty(state.daily);
state.markedStones = uniqueStrings(state.markedStones);
const goal = { ...defaults.goal, ...objectOrEmpty(saved.goal) };
if (!["starter20", "favorites", "family", "all"].includes(goal.scope)) goal.scope = defaults.goal.scope;
goal.group = typeof goal.group === "string" ? goal.group : "";
goal.size = Number.isFinite(Number(goal.size)) ? Math.max(1, Math.min(281, Math.round(Number(goal.size)))) : 20;
state.goal = goal;
const gamification = { ...defaults.gamification, ...objectOrEmpty(saved.gamification) };
gamification.enabled = gamification.enabled !== false;
gamification.fundserie = { ...defaults.gamification.fundserie, ...objectOrEmpty(gamification.fundserie) };
gamification.dailyExpedition = { ...defaults.gamification.dailyExpedition, ...objectOrEmpty(gamification.dailyExpedition) };
gamification.weeklyExpedition = { ...defaults.gamification.weeklyExpedition, ...objectOrEmpty(gamification.weeklyExpedition) };
delete gamification.glanz;
delete gamification.correctStreak;
delete gamification.discoveredStones;
delete gamification.dailyExpedition.rounds;
delete gamification.dailyExpedition.claimed;
delete gamification.dailyExpedition.correctStones;
delete gamification.weeklyExpedition.newStones;
delete gamification.weeklyExpedition.crystalSystems;
gamification.fundserie.days = Number.isFinite(Number(gamification.fundserie.days))
? Math.max(0, Math.floor(Number(gamification.fundserie.days)))
: 0;
if (typeof gamification.fundserie.lastDay !== "string" || !/^\d{4}-\d{2}-\d{2}$/.test(gamification.fundserie.lastDay)) {
gamification.fundserie.lastDay = null;
}
if (typeof gamification.fundserie.lastGraceWeek !== "string") gamification.fundserie.lastGraceWeek = null;
gamification.dailyExpedition.day = typeof gamification.dailyExpedition.day === "string" ? gamification.dailyExpedition.day : "";
gamification.dailyExpedition.correct = Number.isFinite(Number(gamification.dailyExpedition.correct))
? Math.max(0, Math.floor(Number(gamification.dailyExpedition.correct)))
: 0;
gamification.weeklyExpedition.week = typeof gamification.weeklyExpedition.week === "string" ? gamification.weeklyExpedition.week : "";
for (const key of ["seals", "ownedStones", "groupRounds"]) {
gamification[key] = uniqueStrings(gamification[key]);
}
gamification.weeklyExpedition.masteredStones = uniqueStrings(gamification.weeklyExpedition.masteredStones);
if (!saved.gamification?.fundserie) {
const practiced = Object.keys(state.daily)
.filter((day) => state.daily[day]?.completed > 0 && /^\d{4}-\d{2}-\d{2}$/.test(day))
.sort()
.reverse();
const latest = practiced[0];
const now = new Date();
const today = `${now.getFullYear()}-${String(now.getMonth() + 1).padStart(2, "0")}-${String(now.getDate()).padStart(2, "0")}`;
const yesterdayDate = new Date(now.getFullYear(), now.getMonth(), now.getDate() - 1);
const yesterday = `${yesterdayDate.getFullYear()}-${String(yesterdayDate.getMonth() + 1).padStart(2, "0")}-${String(yesterdayDate.getDate()).padStart(2, "0")}`;
if (latest === today || latest === yesterday) {
let days = 0;
let cursor = new Date(`${latest}T12:00:00`);
for (const day of practiced) {
const expected = `${cursor.getFullYear()}-${String(cursor.getMonth() + 1).padStart(2, "0")}-${String(cursor.getDate()).padStart(2, "0")}`;
if (day !== expected) break;
days += 1;
cursor.setDate(cursor.getDate() - 1);
}
gamification.fundserie.days = days;
gamification.fundserie.lastDay = latest;
}
}
state.gamification = gamification;
return state;
}
function createBackup(state, exportedAt = new Date().toISOString()) {
return {
format: "lernstein-backup",
version: VERSION,
exportedAt,
state: normalizeState(state),
};
}
function parseBackup(text) {
const backup = JSON.parse(text);
if (!backup || backup.format !== "lernstein-backup" || !Number.isInteger(backup.version) || backup.version > VERSION) {
throw new TypeError("Diese Sicherungsdatei wird nicht unterstützt.");
}
return normalizeState(backup.state);
}
root.LernsteinState = { VERSION, freshState, normalizeState, createBackup, parseBackup };
if (typeof module !== "undefined" && module.exports) module.exports = root.LernsteinState;
})(globalThis);