adventskalender/shared/js/lock.js aktualisiert

This commit is contained in:
2025-11-05 21:02:52 +00:00
parent 87370875be
commit 4d2e02aac5

View File

@@ -2,66 +2,68 @@
// Bratonien Adventskalender Türsperren / Dev-Konsole // Bratonien Adventskalender Türsperren / Dev-Konsole
// ============================================================ // ============================================================
// Jahr aus Pfad ableiten (z. B. /2025/) // Jahr aus URL holen (…/2025/…), sonst aktuelles Jahr nehmen
const pathYear = window.location.pathname.match(/(\d{4})/); const pathYear = window.location.pathname.match(/(\d{4})/);
const YEAR = pathYear ? parseInt(pathYear[1], 10) : new Date().getFullYear(); 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 STORAGE_BASE = `bratonien_${YEAR}_lock`;
const DEV_MODE = window.location.search.includes("dev"); const DEV_MODE = window.location.search.includes("dev");
// ============================================================ // ===============================
// Hilfsfunktionen // Hilfen
// ============================================================ // ===============================
// simulierten Tag aus dem Storage holen (nur im Dev-Modus sinnvoll)
function getSimulatedDay() { function getSimulatedDay() {
if (!DEV_MODE) return null; if (!DEV_MODE) return null;
const val = localStorage.getItem(`${STORAGE_BASE}_simday`); const val = localStorage.getItem(`${STORAGE_BASE}_simday`);
return val ? parseInt(val, 10) : null; return val ? parseInt(val, 10) : null;
} }
// eigentliche Sperrlogik (INTERN) NICHT global machen!
function _checkDoorUnlocked(day) { function _checkDoorUnlocked(day) {
day = parseInt(day, 10); day = parseInt(day, 10);
// Manuell entsperrt? // wenn User explizit entsperrt hat
const forceUnlock = localStorage.getItem(`${STORAGE_BASE}_unlocked`) === "true"; const forceUnlock = localStorage.getItem(`${STORAGE_BASE}_unlocked`) === "true";
if (forceUnlock) return true; if (forceUnlock) return true;
// Nach dem 24.12. dieses Jahres alles offen // nach dem 24.12. des jeweiligen Jahres: alles offen
const now = new Date(); const now = new Date();
const after24 = const isAfter24 =
now.getFullYear() > YEAR || now.getFullYear() > YEAR ||
(now.getFullYear() === YEAR && (now.getFullYear() === YEAR &&
now.getMonth() === 11 && now.getMonth() === 11 && // Dezember = 11
now.getDate() > 24); now.getDate() > 24);
if (after24) return true;
// Im Dezember: bis heutigem Tag if (isAfter24) return true;
// normaler Modus: nur bis heutigem Tag offen (im Dezember)
if (now.getFullYear() === YEAR && now.getMonth() === 11) { if (now.getFullYear() === YEAR && now.getMonth() === 11) {
return day <= now.getDate(); return day <= now.getDate();
} }
// Vor Dezember: alles zu // vor Dezember ist alles zu
return false; return false;
} }
// ============================================================ // ===============================
// Dev-Konsole bauen // DEV-Konsole aufbauen (nur bei ?dev)
// ============================================================ // ===============================
function createDevConsole() { function createDevConsole() {
// versuchen, vorhandenes Panel zu nehmen // gemeinsames Panel holen oder anlegen
let panel = document.getElementById("dev-panel"); let panel = document.getElementById("dev-panel");
// falls save-progress.js noch keins gebaut hat, hier wirklich EINS bauen
if (!panel) { if (!panel) {
panel = document.createElement("div"); panel = document.createElement("div");
panel.id = "dev-panel"; panel.id = "dev-panel";
// WICHTIG: höher setzen, damit es nicht mit dem anderen (save-progress) Button kollidiert
panel.style.cssText = ` panel.style.cssText = `
position: fixed; position: fixed;
bottom: 10px; bottom: 70px; /* <- hier der Abstand nach oben */
right: 10px; right: 10px;
display: flex; display: flex;
flex-direction: column; flex-direction: column;
align-items: flex-end;
gap: 10px; gap: 10px;
z-index: 9999; z-index: 9999;
`; `;
@@ -73,9 +75,9 @@ function createDevConsole() {
box.style.cssText = ` box.style.cssText = `
background: rgba(0,0,0,0.8); background: rgba(0,0,0,0.8);
color: #f1e1a6; color: #f1e1a6;
padding: 10px;
border: 1px solid #c8aa49; border: 1px solid #c8aa49;
border-radius: 8px; border-radius: 8px;
padding: 10px;
max-width: 340px; max-width: 340px;
font-family: monospace; font-family: monospace;
`; `;
@@ -85,7 +87,7 @@ function createDevConsole() {
title.style.marginBottom = "6px"; title.style.marginBottom = "6px";
box.appendChild(title); box.appendChild(title);
// Grid 124 // Buttons 124
const grid = document.createElement("div"); const grid = document.createElement("div");
grid.style.cssText = ` grid.style.cssText = `
display: grid; display: grid;
@@ -113,9 +115,10 @@ function createDevConsole() {
} }
box.appendChild(grid); box.appendChild(grid);
// Steuerbuttons // Reihe für Sperre aufheben / alle sperren
const actions = document.createElement("div"); const actions = document.createElement("div");
actions.style.cssText = `display:flex;gap:6px;`; actions.style.display = "flex";
actions.style.gap = "6px";
const unlock = document.createElement("button"); const unlock = document.createElement("button");
unlock.textContent = "🚪 Sperre aufheben"; unlock.textContent = "🚪 Sperre aufheben";
@@ -132,6 +135,7 @@ function createDevConsole() {
localStorage.setItem(`${STORAGE_BASE}_unlocked`, "true"); localStorage.setItem(`${STORAGE_BASE}_unlocked`, "true");
location.reload(); location.reload();
}; };
actions.appendChild(unlock);
const relock = document.createElement("button"); const relock = document.createElement("button");
relock.textContent = "🔒 Alle sperren"; relock.textContent = "🔒 Alle sperren";
@@ -149,36 +153,33 @@ function createDevConsole() {
localStorage.removeItem(`${STORAGE_BASE}_simday`); localStorage.removeItem(`${STORAGE_BASE}_simday`);
location.reload(); location.reload();
}; };
actions.appendChild(unlock);
actions.appendChild(relock); actions.appendChild(relock);
box.appendChild(actions); box.appendChild(actions);
// endgültig ins Panel
panel.appendChild(box); panel.appendChild(box);
} }
// ============================================================ // ===============================
// Export + Init // GLOBALER Export für door-open.js
// ============================================================ // ===============================
// von door-open.js aufrufbar
window.isDoorUnlocked = function (day) { window.isDoorUnlocked = function (day) {
// wenn wir im dev sind UND es einen simulierten Tag gibt → den nehmen
if (DEV_MODE) { if (DEV_MODE) {
const sim = getSimulatedDay(); const sim = getSimulatedDay();
if (sim !== null) { if (sim !== null) {
return parseInt(day, 10) <= sim; return parseInt(day, 10) <= sim;
} }
// dev ohne sim: alles offen // wenn kein sim gesetzt, im dev alles offen
return true; return true;
} }
// sonst normale Prüfung
return _checkDoorUnlocked(day); return _checkDoorUnlocked(day);
}; };
// Dev-Konsole erst bauen, wenn DOM fertig ist // Dev-Konsole nur anzeigen, wenn ?dev
if (DEV_MODE) { if (DEV_MODE) {
if (document.readyState === "loading") { createDevConsole();
document.addEventListener("DOMContentLoaded", createDevConsole);
} else {
createDevConsole();
}
} }