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
// ============================================================
// 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 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");
// ============================================================
// Hilfsfunktionen
// ============================================================
// ===============================
// Hilfen
// ===============================
// 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);
// Manuell entsperrt?
// wenn User explizit entsperrt hat
const forceUnlock = localStorage.getItem(`${STORAGE_BASE}_unlocked`) === "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 after24 =
const isAfter24 =
now.getFullYear() > YEAR ||
(now.getFullYear() === YEAR &&
now.getMonth() === 11 &&
now.getMonth() === 11 && // Dezember = 11
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) {
return day <= now.getDate();
}
// Vor Dezember: alles zu
// vor Dezember ist alles zu
return false;
}
// ============================================================
// Dev-Konsole bauen
// ============================================================
// ===============================
// DEV-Konsole aufbauen (nur bei ?dev)
// ===============================
function createDevConsole() {
// versuchen, vorhandenes Panel zu nehmen
// gemeinsames Panel holen oder anlegen
let panel = document.getElementById("dev-panel");
// falls save-progress.js noch keins gebaut hat, hier wirklich EINS bauen
if (!panel) {
panel = document.createElement("div");
panel.id = "dev-panel";
// WICHTIG: höher setzen, damit es nicht mit dem anderen (save-progress) Button kollidiert
panel.style.cssText = `
position: fixed;
bottom: 10px;
bottom: 70px; /* <- hier der Abstand nach oben */
right: 10px;
display: flex;
flex-direction: column;
align-items: flex-end;
gap: 10px;
z-index: 9999;
`;
@@ -73,9 +75,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;
`;
@@ -85,7 +87,7 @@ function createDevConsole() {
title.style.marginBottom = "6px";
box.appendChild(title);
// Grid 124
// Buttons 124
const grid = document.createElement("div");
grid.style.cssText = `
display: grid;
@@ -113,9 +115,10 @@ function createDevConsole() {
}
box.appendChild(grid);
// Steuerbuttons
// Reihe für Sperre aufheben / alle sperren
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");
unlock.textContent = "🚪 Sperre aufheben";
@@ -132,6 +135,7 @@ function createDevConsole() {
localStorage.setItem(`${STORAGE_BASE}_unlocked`, "true");
location.reload();
};
actions.appendChild(unlock);
const relock = document.createElement("button");
relock.textContent = "🔒 Alle sperren";
@@ -149,36 +153,33 @@ function createDevConsole() {
localStorage.removeItem(`${STORAGE_BASE}_simday`);
location.reload();
};
actions.appendChild(unlock);
actions.appendChild(relock);
box.appendChild(actions);
// endgültig ins Panel
panel.appendChild(box);
}
// ============================================================
// Export + Init
// ============================================================
// von door-open.js aufrufbar
// ===============================
// GLOBALER Export für door-open.js
// ===============================
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;
}
// dev ohne sim: alles offen
// wenn kein sim gesetzt, im dev alles offen
return true;
}
// sonst normale Prüfung
return _checkDoorUnlocked(day);
};
// Dev-Konsole erst bauen, wenn DOM fertig ist
// Dev-Konsole nur anzeigen, wenn ?dev
if (DEV_MODE) {
if (document.readyState === "loading") {
document.addEventListener("DOMContentLoaded", createDevConsole);
} else {
createDevConsole();
}
}