adventskalender/shared/js/lock.js aktualisiert

This commit is contained in:
2025-11-05 20:44:28 +00:00
parent 12219faa28
commit fa31584aca

View File

@@ -2,71 +2,54 @@
// Bratonien Adventskalender Türsperren / Dev-Konsole
// ============================================================
// Jahr aus URL holen (…/2025/…), sonst aktuelles Jahr nehmen
// Jahr aus Pfad ableiten (z. B. /2025/)
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");
// ===============================
// Hilfen
// ===============================
// ============================================================
// Hilfsfunktionen
// ============================================================
// 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
// Manuell entsperrt
const forceUnlock = localStorage.getItem(`${STORAGE_BASE}_unlocked`) === "true";
if (forceUnlock) return true;
// nach dem 24.12. des jeweiligen Jahres: alles offen
// Nach dem 24.12. immer offen
const now = new Date();
const isAfter24 =
const after24 =
now.getFullYear() > YEAR ||
(now.getFullYear() === YEAR &&
now.getMonth() === 11 && // Dezember = 11
now.getDate() > 24);
(now.getFullYear() === YEAR && now.getMonth() === 11 && now.getDate() > 24);
if (after24) return true;
if (isAfter24) return true;
// normaler Modus: nur bis heutigem Tag offen (im Dezember)
// Dezember -> bis aktueller Tag
if (now.getFullYear() === YEAR && now.getMonth() === 11) {
return day <= now.getDate();
}
// vor Dezember ist alles zu
// Vor Dezember alles zu
return false;
}
// ===============================
// DEV-Konsole aufbauen (nur bei ?dev)
// ===============================
// ============================================================
// Dev-Konsole
// ============================================================
function createDevConsole() {
// gemeinsames Panel holen oder anlegen
let panel = document.getElementById("dev-panel");
const panel = document.getElementById("dev-panel");
if (!panel) {
panel = document.createElement("div");
panel.id = "dev-panel";
panel.style.cssText = `
position: fixed;
bottom: 10px;
right: 10px;
display: flex;
flex-direction: column;
gap: 10px;
z-index: 9999;
`;
document.body.appendChild(panel);
console.warn("⚠️ Kein #dev-panel gefunden. save-progress.js muss zuerst laden.");
return;
}
const box = document.createElement("div");
@@ -74,9 +57,9 @@ function createDevConsole() {
box.style.cssText = `
background: rgba(0,0,0,0.8);
color: #f1e1a6;
padding: 10px;
border: 1px solid #c8aa49;
border-radius: 8px;
padding: 10px;
max-width: 340px;
font-family: monospace;
`;
@@ -86,7 +69,7 @@ function createDevConsole() {
title.style.marginBottom = "6px";
box.appendChild(title);
// Buttons 124
// Tag-Buttons
const grid = document.createElement("div");
grid.style.cssText = `
display: grid;
@@ -114,10 +97,9 @@ function createDevConsole() {
}
box.appendChild(grid);
// Reihe für Sperre aufheben / alle sperren
// Steuer-Buttons
const actions = document.createElement("div");
actions.style.display = "flex";
actions.style.gap = "6px";
actions.style.cssText = "display:flex;gap:6px;";
const unlock = document.createElement("button");
unlock.textContent = "🚪 Sperre aufheben";
@@ -134,7 +116,6 @@ function createDevConsole() {
localStorage.setItem(`${STORAGE_BASE}_unlocked`, "true");
location.reload();
};
actions.appendChild(unlock);
const relock = document.createElement("button");
relock.textContent = "🔒 Alle sperren";
@@ -152,33 +133,25 @@ function createDevConsole() {
localStorage.removeItem(`${STORAGE_BASE}_simday`);
location.reload();
};
actions.appendChild(relock);
actions.appendChild(unlock);
actions.appendChild(relock);
box.appendChild(actions);
// endgültig ins Panel
panel.appendChild(box);
}
// ===============================
// GLOBALER Export für door-open.js
// ===============================
// ============================================================
// Export + Init
// ============================================================
window.isDoorUnlocked = function (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
if (sim !== null) return parseInt(day, 10) <= sim;
return true;
}
// sonst normale Prüfung
return _checkDoorUnlocked(day);
};
// Dev-Konsole nur anzeigen, wenn ?dev
if (DEV_MODE) {
createDevConsole();
}
if (DEV_MODE) createDevConsole();