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

157 lines
4.2 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 Pfad ableiten (z. B. /2025/)
const pathYear = window.location.pathname.match(/(\d{4})/);
const YEAR = pathYear ? parseInt(pathYear[1], 10) : new Date().getFullYear();
const STORAGE_BASE = `bratonien_${YEAR}_lock`;
const DEV_MODE = window.location.search.includes("dev");
// ============================================================
// Hilfsfunktionen
// ============================================================
function getSimulatedDay() {
if (!DEV_MODE) return null;
const val = localStorage.getItem(`${STORAGE_BASE}_simday`);
return val ? parseInt(val, 10) : null;
}
function _checkDoorUnlocked(day) {
day = parseInt(day, 10);
// Manuell entsperrt
const forceUnlock = localStorage.getItem(`${STORAGE_BASE}_unlocked`) === "true";
if (forceUnlock) return true;
// Nach dem 24.12. immer offen
const now = new Date();
const after24 =
now.getFullYear() > YEAR ||
(now.getFullYear() === YEAR && now.getMonth() === 11 && now.getDate() > 24);
if (after24) return true;
// Dezember -> bis aktueller Tag
if (now.getFullYear() === YEAR && now.getMonth() === 11) {
return day <= now.getDate();
}
// Vor Dezember alles zu
return false;
}
// ============================================================
// Dev-Konsole
// ============================================================
function createDevConsole() {
const panel = document.getElementById("dev-panel");
if (!panel) {
console.warn("⚠️ Kein #dev-panel gefunden. save-progress.js muss zuerst laden.");
return;
}
const box = document.createElement("div");
box.className = "dev-console";
box.style.cssText = `
background: rgba(0,0,0,0.8);
color: #f1e1a6;
border: 1px solid #c8aa49;
border-radius: 8px;
padding: 10px;
max-width: 340px;
font-family: monospace;
`;
const title = document.createElement("div");
title.textContent = `DEV ${YEAR}`;
title.style.marginBottom = "6px";
box.appendChild(title);
// Tag-Buttons
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));
location.reload();
};
grid.appendChild(b);
}
box.appendChild(grid);
// Steuer-Buttons
const actions = document.createElement("div");
actions.style.cssText = "display:flex;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();
};
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(unlock);
actions.appendChild(relock);
box.appendChild(actions);
panel.appendChild(box);
}
// ============================================================
// Export + Init
// ============================================================
window.isDoorUnlocked = function (day) {
if (DEV_MODE) {
const sim = getSimulatedDay();
if (sim !== null) return parseInt(day, 10) <= sim;
return true;
}
return _checkDoorUnlocked(day);
};
if (DEV_MODE) createDevConsole();