From c7adc2697c888bfd864521dabececeaf7e7ad2f5 Mon Sep 17 00:00:00 2001 From: Thomas Dannenberg Date: Wed, 5 Nov 2025 20:27:25 +0000 Subject: [PATCH] adventskalender/shared/js/lock.js aktualisiert --- adventskalender/shared/js/lock.js | 211 ++++++++++++++++++++---------- 1 file changed, 141 insertions(+), 70 deletions(-) diff --git a/adventskalender/shared/js/lock.js b/adventskalender/shared/js/lock.js index 72aa76b..33003f8 100644 --- a/adventskalender/shared/js/lock.js +++ b/adventskalender/shared/js/lock.js @@ -2,108 +2,179 @@ // Bratonien Adventskalender – Türsperren / Dev-Konsole // ============================================================ -// Jahr automatisch aus Ordnernamen ableiten -const YEAR = window.location.pathname.match(/(\d{4})/)?.[1] || "0000"; -const STORAGE_KEY = `bratonien_lock_${YEAR}`; -let devMode = window.location.search.includes("?dev"); +// Jahr aus URL holen (…/2025/…), sonst aktuelles Jahr nehmen +const pathYear = window.location.pathname.match(/(\d{4})/); +const YEAR = pathYear ? parseInt(pathYear[1], 10) : new Date().getFullYear(); + +// alles, was wir speichern, bekommt das Jahr dran +const STORAGE_BASE = `bratonien_${YEAR}_lock`; +const DEV_MODE = window.location.search.includes("dev"); // =============================== -// Sperrlogik +// Hilfen // =============================== -function isDoorUnlocked(day) { - // Dev-Modus immer alles offen - if (devMode) return true; - // Nach 24.12. immer alles offen +// simulierten Tag aus dem Storage holen (nur im Dev-Modus sinnvoll) +function getSimulatedDay() { + if (!DEV_MODE) return null; + const val = localStorage.getItem(`${STORAGE_BASE}_simday`); + return val ? parseInt(val, 10) : null; +} + +// eigentliche Sperrlogik (INTERN) – NICHT global machen! +function _checkDoorUnlocked(day) { + day = parseInt(day, 10); + + // wenn User explizit entsperrt hat + const forceUnlock = localStorage.getItem(`${STORAGE_BASE}_unlocked`) === "true"; + if (forceUnlock) return true; + + // nach dem 24.12. des jeweiligen Jahres: alles offen const now = new Date(); - if (now.getFullYear() > YEAR || (now.getFullYear() == YEAR && now.getMonth() > 11 && now.getDate() > 24)) - return true; + const isAfter24 = + now.getFullYear() > YEAR || + (now.getFullYear() === YEAR && + now.getMonth() === 11 && // Dezember = 11 + now.getDate() > 24); - // Falls der Benutzer alles entsperrt hat - if (localStorage.getItem(`${STORAGE_KEY}_unlocked`) === "true") - return true; + if (isAfter24) return true; - // Normale Regel: Türchen <= aktueller Tag freigegeben - const today = now.getDate(); - return parseInt(day) <= today; + // normaler Modus: nur bis heutigem Tag offen (im Dezember) + if (now.getFullYear() === YEAR && now.getMonth() === 11) { + return day <= now.getDate(); + } + + // vor Dezember ist alles zu + return false; } // =============================== -// Dev-Konsole (nur bei ?dev) +// DEV-Konsole aufbauen (nur bei ?dev) // =============================== function createDevConsole() { - // Prüfen, ob Reset-Button existiert (von save-progress.js) - let resetBtn = document.getElementById("dev-reset"); - let devBox = document.createElement("div"); - devBox.className = "dev-console"; - devBox.style.cssText = ` - display:flex; flex-wrap:wrap; gap:6px; - margin:10px 0 0 0; padding:8px; - background:#111; border:1px solid #c8aa49; - border-radius:8px; color:#c8aa49; font-family:monospace; - justify-content:flex-start; + // schauen, ob schon ein Reset-Button existiert (von save-progress.js) + const existingReset = document.getElementById("dev-reset"); + + const box = document.createElement("div"); + box.className = "dev-console"; + box.style.cssText = ` + background: rgba(0,0,0,0.8); + color: #f1e1a6; + position: fixed; + bottom: 10px; + right: 10px; + z-index: 9999; + padding: 10px; + border: 1px solid #c8aa49; + border-radius: 8px; + max-width: 340px; + font-family: monospace; `; - // Sperrbutton - const lockBtn = document.createElement("button"); - lockBtn.textContent = "🔒 Alle sperren"; - lockBtn.onclick = () => { - localStorage.removeItem(`${STORAGE_KEY}_unlocked`); - localStorage.removeItem(`bratonien_opened_${YEAR}`); - console.log("Alle Türchen gesperrt."); - location.reload(); - }; - devBox.appendChild(lockBtn); + const title = document.createElement("div"); + title.textContent = `DEV ${YEAR}`; + title.style.marginBottom = "6px"; + box.appendChild(title); - // Tag-Buttons 1–24 + // Buttons 1–24 + const grid = document.createElement("div"); + grid.style.cssText = ` + display: grid; + grid-template-columns: repeat(8, minmax(28px, 1fr)); + gap: 4px; + margin-bottom: 8px; + `; for (let i = 1; i <= 24; i++) { const b = document.createElement("button"); b.textContent = i; - b.style.minWidth = "32px"; + b.style.cssText = ` + background: #333; + color: #fff; + border: 1px solid #666; + border-radius: 4px; + cursor: pointer; + font-size: 11px; + padding: 3px 0; + `; b.onclick = () => { - localStorage.setItem(`${STORAGE_KEY}_simulateDay`, i); - console.log(`Simuliere Tag ${i}`); + localStorage.setItem(`${STORAGE_BASE}_simday`, String(i)); + // Anzeige bleibt nur korrekt, wenn wir neu laden location.reload(); }; - devBox.appendChild(b); + grid.appendChild(b); } + box.appendChild(grid); - // Sperre aufheben - const unlockBtn = document.createElement("button"); - unlockBtn.textContent = "🚪 Sperre aufheben"; - unlockBtn.onclick = () => { - localStorage.setItem(`${STORAGE_KEY}_unlocked`, "true"); - console.log("Alle Türchen entsperrt."); + // Reihe für Sperre aufheben / alle sperren + const actions = document.createElement("div"); + actions.style.display = "flex"; + actions.style.gap = "6px"; + + const unlock = document.createElement("button"); + unlock.textContent = "🚪 Sperre aufheben"; + unlock.style.cssText = ` + background: #2e6f40; + color: #fff; + border: none; + padding: 4px 8px; + border-radius: 4px; + cursor: pointer; + font-size: 11px; + `; + unlock.onclick = () => { + localStorage.setItem(`${STORAGE_BASE}_unlocked`, "true"); location.reload(); }; - devBox.appendChild(unlockBtn); + actions.appendChild(unlock); - // Einfügen unterhalb des Reset-Buttons (falls vorhanden) - if (resetBtn) { - resetBtn.insertAdjacentElement("afterend", devBox); + const relock = document.createElement("button"); + relock.textContent = "🔒 Alle sperren"; + relock.style.cssText = ` + background: #8b0000; + color: #fff; + border: none; + padding: 4px 8px; + border-radius: 4px; + cursor: pointer; + font-size: 11px; + `; + relock.onclick = () => { + localStorage.removeItem(`${STORAGE_BASE}_unlocked`); + localStorage.removeItem(`${STORAGE_BASE}_simday`); + location.reload(); + }; + actions.appendChild(relock); + + box.appendChild(actions); + + // An passender Stelle einfügen + if (existingReset) { + // direkt danach + existingReset.insertAdjacentElement("afterend", box); } else { - document.body.appendChild(devBox); + document.body.appendChild(box); } } // =============================== -// Simulationsfunktion (nur Dev) -// =============================== -function getSimulatedDay() { - if (!devMode) return null; - const simDay = parseInt(localStorage.getItem(`${STORAGE_KEY}_simulateDay`)); - return simDay || null; -} - -// =============================== -// Export (global für andere Skripte) +// GLOBALER Export für door-open.js // =============================== window.isDoorUnlocked = function (day) { - // Simulation berücksichtigen - const simDay = getSimulatedDay(); - if (devMode && simDay) return parseInt(day) <= simDay; - return isDoorUnlocked(day); + // wenn wir im dev sind UND es einen simulierten Tag gibt → den nehmen + if (DEV_MODE) { + const sim = getSimulatedDay(); + if (sim !== null) { + return parseInt(day, 10) <= sim; + } + // wenn kein sim gesetzt, im dev alles offen + return true; + } + + // sonst normale Prüfung + return _checkDoorUnlocked(day); }; -// Dev-Konsole aktivieren -if (devMode) createDevConsole(); \ No newline at end of file +// Dev-Konsole nur anzeigen, wenn ?dev +if (DEV_MODE) { + createDevConsole(); +} \ No newline at end of file