Files
Bratonien-Adventskalender/adventskalender/shared/js/lock.js

180 lines
4.9 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// ============================================================
// Bratonien Adventskalender Türsperren / Dev-Konsole
// ============================================================
// 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");
// ===============================
// 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);
// 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();
const isAfter24 =
now.getFullYear() > YEAR ||
(now.getFullYear() === YEAR &&
now.getMonth() === 11 && // Dezember = 11
now.getDate() > 24);
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 ist alles zu
return false;
}
// ===============================
// DEV-Konsole aufbauen (nur bei ?dev)
// ===============================
function createDevConsole() {
// 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;
`;
const title = document.createElement("div");
title.textContent = `DEV ${YEAR}`;
title.style.marginBottom = "6px";
box.appendChild(title);
// Buttons 124
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.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_BASE}_simday`, String(i));
// Anzeige bleibt nur korrekt, wenn wir neu laden
location.reload();
};
grid.appendChild(b);
}
box.appendChild(grid);
// 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();
};
actions.appendChild(unlock);
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(box);
}
}
// ===============================
// 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;
}
// wenn kein sim gesetzt, im dev alles offen
return true;
}
// sonst normale Prüfung
return _checkDoorUnlocked(day);
};
// Dev-Konsole nur anzeigen, wenn ?dev
if (DEV_MODE) {
createDevConsole();
}