adventskalender/shared/js/lock.js aktualisiert
This commit is contained in:
@@ -2,71 +2,54 @@
|
|||||||
// Bratonien Adventskalender – Türsperren / Dev-Konsole
|
// 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 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");
|
||||||
|
|
||||||
// ===============================
|
// ============================================================
|
||||||
// Hilfen
|
// Hilfsfunktionen
|
||||||
// ===============================
|
// ============================================================
|
||||||
|
|
||||||
// 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);
|
||||||
|
|
||||||
// wenn User explizit entsperrt hat
|
// Manuell entsperrt
|
||||||
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. des jeweiligen Jahres: alles offen
|
// Nach dem 24.12. immer offen
|
||||||
const now = new Date();
|
const now = new Date();
|
||||||
const isAfter24 =
|
const after24 =
|
||||||
now.getFullYear() > YEAR ||
|
now.getFullYear() > YEAR ||
|
||||||
(now.getFullYear() === YEAR &&
|
(now.getFullYear() === YEAR && now.getMonth() === 11 && now.getDate() > 24);
|
||||||
now.getMonth() === 11 && // Dezember = 11
|
if (after24) return true;
|
||||||
now.getDate() > 24);
|
|
||||||
|
|
||||||
if (isAfter24) return true;
|
// Dezember -> bis aktueller Tag
|
||||||
|
|
||||||
// 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 ist alles zu
|
// Vor Dezember alles zu
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// ===============================
|
// ============================================================
|
||||||
// DEV-Konsole aufbauen (nur bei ?dev)
|
// Dev-Konsole
|
||||||
// ===============================
|
// ============================================================
|
||||||
|
|
||||||
function createDevConsole() {
|
function createDevConsole() {
|
||||||
// gemeinsames Panel holen oder anlegen
|
const panel = document.getElementById("dev-panel");
|
||||||
let panel = document.getElementById("dev-panel");
|
|
||||||
if (!panel) {
|
if (!panel) {
|
||||||
panel = document.createElement("div");
|
console.warn("⚠️ Kein #dev-panel gefunden. save-progress.js muss zuerst laden.");
|
||||||
panel.id = "dev-panel";
|
return;
|
||||||
panel.style.cssText = `
|
|
||||||
position: fixed;
|
|
||||||
bottom: 10px;
|
|
||||||
right: 10px;
|
|
||||||
display: flex;
|
|
||||||
flex-direction: column;
|
|
||||||
gap: 10px;
|
|
||||||
z-index: 9999;
|
|
||||||
`;
|
|
||||||
document.body.appendChild(panel);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const box = document.createElement("div");
|
const box = document.createElement("div");
|
||||||
@@ -74,9 +57,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;
|
||||||
`;
|
`;
|
||||||
@@ -86,7 +69,7 @@ function createDevConsole() {
|
|||||||
title.style.marginBottom = "6px";
|
title.style.marginBottom = "6px";
|
||||||
box.appendChild(title);
|
box.appendChild(title);
|
||||||
|
|
||||||
// Buttons 1–24
|
// Tag-Buttons
|
||||||
const grid = document.createElement("div");
|
const grid = document.createElement("div");
|
||||||
grid.style.cssText = `
|
grid.style.cssText = `
|
||||||
display: grid;
|
display: grid;
|
||||||
@@ -114,10 +97,9 @@ function createDevConsole() {
|
|||||||
}
|
}
|
||||||
box.appendChild(grid);
|
box.appendChild(grid);
|
||||||
|
|
||||||
// Reihe für Sperre aufheben / alle sperren
|
// Steuer-Buttons
|
||||||
const actions = document.createElement("div");
|
const actions = document.createElement("div");
|
||||||
actions.style.display = "flex";
|
actions.style.cssText = "display:flex;gap:6px;";
|
||||||
actions.style.gap = "6px";
|
|
||||||
|
|
||||||
const unlock = document.createElement("button");
|
const unlock = document.createElement("button");
|
||||||
unlock.textContent = "🚪 Sperre aufheben";
|
unlock.textContent = "🚪 Sperre aufheben";
|
||||||
@@ -134,7 +116,6 @@ 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";
|
||||||
@@ -152,33 +133,25 @@ function createDevConsole() {
|
|||||||
localStorage.removeItem(`${STORAGE_BASE}_simday`);
|
localStorage.removeItem(`${STORAGE_BASE}_simday`);
|
||||||
location.reload();
|
location.reload();
|
||||||
};
|
};
|
||||||
actions.appendChild(relock);
|
|
||||||
|
|
||||||
|
actions.appendChild(unlock);
|
||||||
|
actions.appendChild(relock);
|
||||||
box.appendChild(actions);
|
box.appendChild(actions);
|
||||||
|
|
||||||
// endgültig ins Panel
|
|
||||||
panel.appendChild(box);
|
panel.appendChild(box);
|
||||||
}
|
}
|
||||||
|
|
||||||
// ===============================
|
// ============================================================
|
||||||
// GLOBALER Export für door-open.js
|
// Export + Init
|
||||||
// ===============================
|
// ============================================================
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
// wenn kein sim gesetzt, im dev alles offen
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// sonst normale Prüfung
|
|
||||||
return _checkDoorUnlocked(day);
|
return _checkDoorUnlocked(day);
|
||||||
};
|
};
|
||||||
|
|
||||||
// Dev-Konsole nur anzeigen, wenn ?dev
|
if (DEV_MODE) createDevConsole();
|
||||||
if (DEV_MODE) {
|
|
||||||
createDevConsole();
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user