29 lines
925 B
TypeScript
29 lines
925 B
TypeScript
import { writable } from 'svelte/store';
|
|
import {
|
|
emptyProgress,
|
|
loadProgress,
|
|
recordRun,
|
|
saveProgress,
|
|
type Progress,
|
|
} from '../game/persistence';
|
|
import type { Target } from '../game/tasks';
|
|
import type { Stage } from '../game/stages';
|
|
|
|
const initial: Progress = typeof window === 'undefined' ? emptyProgress() : loadProgress();
|
|
|
|
export const progress = writable<Progress>(initial);
|
|
|
|
// Letzter abgeschlossener Lauf — damit der ResultScreen genau diesen Versuch
|
|
// hervorheben kann, auch wenn er nicht der beste ist.
|
|
export const lastRun = writable<{ target: Target; stage: Stage; date: string } | null>(null);
|
|
|
|
progress.subscribe((p) => {
|
|
if (typeof window !== 'undefined') saveProgress(p);
|
|
});
|
|
|
|
export function recordResult(target: Target, stage: Stage): void {
|
|
const date = new Date().toISOString();
|
|
progress.update((p) => recordRun(p, target, stage, date));
|
|
lastRun.set({ target, stage, date });
|
|
}
|