2026-03-29 11:12:13 +02:00
|
|
|
import type { ScreenName, KeyboardLayout } from "./types";
|
|
|
|
|
import { openDB, getProgress, saveProgress } from "./storage/db";
|
|
|
|
|
import { initWelcomeScreen, initLessonScreen } from "./ui/screens";
|
2026-03-29 11:05:59 +02:00
|
|
|
|
|
|
|
|
let db: IDBDatabase;
|
2026-03-29 11:12:13 +02:00
|
|
|
let lessonCleanup: (() => void) | null = null;
|
2026-03-29 11:05:59 +02:00
|
|
|
|
|
|
|
|
export function showScreen(name: ScreenName): void {
|
|
|
|
|
document.querySelectorAll(".screen").forEach((el) => {
|
|
|
|
|
el.classList.remove("screen--active");
|
|
|
|
|
});
|
|
|
|
|
const target = document.getElementById(`screen-${name}`);
|
|
|
|
|
if (target) {
|
|
|
|
|
target.classList.add("screen--active");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function getDB(): IDBDatabase {
|
|
|
|
|
return db;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-29 11:12:13 +02:00
|
|
|
export async function startLesson(
|
|
|
|
|
level: number,
|
|
|
|
|
layout: KeyboardLayout,
|
|
|
|
|
): Promise<void> {
|
|
|
|
|
// Clean up previous lesson if any
|
|
|
|
|
if (lessonCleanup) {
|
|
|
|
|
lessonCleanup();
|
|
|
|
|
lessonCleanup = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
showScreen("lesson");
|
|
|
|
|
|
|
|
|
|
lessonCleanup = initLessonScreen(db, level, layout, async (completedLevel: number) => {
|
|
|
|
|
// Update progress after lesson completion
|
|
|
|
|
const progress = await getProgress(db);
|
|
|
|
|
if (!progress) return;
|
|
|
|
|
|
|
|
|
|
// Add level to completedLevels if not already there
|
|
|
|
|
if (!progress.completedLevels.includes(completedLevel)) {
|
|
|
|
|
progress.completedLevels.push(completedLevel);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Advance to next level if this was the current level (LEVL-02, LEVL-03)
|
|
|
|
|
if (completedLevel === progress.currentLevel) {
|
|
|
|
|
progress.currentLevel = completedLevel + 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Increment session count
|
|
|
|
|
progress.totalSessions++;
|
|
|
|
|
|
|
|
|
|
// Add today's date if not already recorded
|
|
|
|
|
const today = new Date().toISOString().split("T")[0]!;
|
|
|
|
|
if (!progress.sessionDates.includes(today)) {
|
|
|
|
|
progress.sessionDates.push(today);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await saveProgress(db, progress);
|
|
|
|
|
|
|
|
|
|
// Navigate to forest (per D-07: skip reward in Phase 1)
|
|
|
|
|
showScreen("forest");
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-29 11:05:59 +02:00
|
|
|
export async function initApp(): Promise<void> {
|
|
|
|
|
db = await openDB();
|
|
|
|
|
const progress = await getProgress(db);
|
|
|
|
|
if (progress && progress.selectedCharacter) {
|
2026-03-29 11:12:13 +02:00
|
|
|
// Returning user -- go to forest
|
2026-03-29 11:05:59 +02:00
|
|
|
showScreen("forest");
|
|
|
|
|
} else {
|
2026-03-29 11:12:13 +02:00
|
|
|
// First time -- show welcome
|
2026-03-29 11:05:59 +02:00
|
|
|
initWelcomeScreen(db, showScreen);
|
|
|
|
|
showScreen("welcome");
|
|
|
|
|
}
|
|
|
|
|
}
|