adventskalender/shared/js/lock.js aktualisiert
This commit is contained in:
@@ -2,108 +2,179 @@
|
|||||||
// Bratonien Adventskalender – Türsperren / Dev-Konsole
|
// Bratonien Adventskalender – Türsperren / Dev-Konsole
|
||||||
// ============================================================
|
// ============================================================
|
||||||
|
|
||||||
// Jahr automatisch aus Ordnernamen ableiten
|
// Jahr aus URL holen (…/2025/…), sonst aktuelles Jahr nehmen
|
||||||
const YEAR = window.location.pathname.match(/(\d{4})/)?.[1] || "0000";
|
const pathYear = window.location.pathname.match(/(\d{4})/);
|
||||||
const STORAGE_KEY = `bratonien_lock_${YEAR}`;
|
const YEAR = pathYear ? parseInt(pathYear[1], 10) : new Date().getFullYear();
|
||||||
let devMode = window.location.search.includes("?dev");
|
|
||||||
|
// alles, was wir speichern, bekommt das Jahr dran
|
||||||
|
const STORAGE_BASE = `bratonien_${YEAR}_lock`;
|
||||||
|
const DEV_MODE = window.location.search.includes("dev");
|
||||||
|
|
||||||
// ===============================
|
// ===============================
|
||||||
// Sperrlogik
|
// Hilfen
|
||||||
// ===============================
|
// ===============================
|
||||||
function isDoorUnlocked(day) {
|
|
||||||
// Dev-Modus immer alles offen
|
|
||||||
if (devMode) return true;
|
|
||||||
|
|
||||||
// Nach 24.12. immer alles offen
|
// 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 now = new Date();
|
||||||
if (now.getFullYear() > YEAR || (now.getFullYear() == YEAR && now.getMonth() > 11 && now.getDate() > 24))
|
const isAfter24 =
|
||||||
return true;
|
now.getFullYear() > YEAR ||
|
||||||
|
(now.getFullYear() === YEAR &&
|
||||||
|
now.getMonth() === 11 && // Dezember = 11
|
||||||
|
now.getDate() > 24);
|
||||||
|
|
||||||
// Falls der Benutzer alles entsperrt hat
|
if (isAfter24) return true;
|
||||||
if (localStorage.getItem(`${STORAGE_KEY}_unlocked`) === "true")
|
|
||||||
return true;
|
|
||||||
|
|
||||||
// Normale Regel: Türchen <= aktueller Tag freigegeben
|
// normaler Modus: nur bis heutigem Tag offen (im Dezember)
|
||||||
const today = now.getDate();
|
if (now.getFullYear() === YEAR && now.getMonth() === 11) {
|
||||||
return parseInt(day) <= today;
|
return day <= now.getDate();
|
||||||
|
}
|
||||||
|
|
||||||
|
// vor Dezember ist alles zu
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// ===============================
|
// ===============================
|
||||||
// Dev-Konsole (nur bei ?dev)
|
// DEV-Konsole aufbauen (nur bei ?dev)
|
||||||
// ===============================
|
// ===============================
|
||||||
function createDevConsole() {
|
function createDevConsole() {
|
||||||
// Prüfen, ob Reset-Button existiert (von save-progress.js)
|
// schauen, ob schon ein Reset-Button existiert (von save-progress.js)
|
||||||
let resetBtn = document.getElementById("dev-reset");
|
const existingReset = document.getElementById("dev-reset");
|
||||||
let devBox = document.createElement("div");
|
|
||||||
devBox.className = "dev-console";
|
const box = document.createElement("div");
|
||||||
devBox.style.cssText = `
|
box.className = "dev-console";
|
||||||
display:flex; flex-wrap:wrap; gap:6px;
|
box.style.cssText = `
|
||||||
margin:10px 0 0 0; padding:8px;
|
background: rgba(0,0,0,0.8);
|
||||||
background:#111; border:1px solid #c8aa49;
|
color: #f1e1a6;
|
||||||
border-radius:8px; color:#c8aa49; font-family:monospace;
|
position: fixed;
|
||||||
justify-content:flex-start;
|
bottom: 10px;
|
||||||
|
right: 10px;
|
||||||
|
z-index: 9999;
|
||||||
|
padding: 10px;
|
||||||
|
border: 1px solid #c8aa49;
|
||||||
|
border-radius: 8px;
|
||||||
|
max-width: 340px;
|
||||||
|
font-family: monospace;
|
||||||
`;
|
`;
|
||||||
|
|
||||||
// Sperrbutton
|
const title = document.createElement("div");
|
||||||
const lockBtn = document.createElement("button");
|
title.textContent = `DEV ${YEAR}`;
|
||||||
lockBtn.textContent = "🔒 Alle sperren";
|
title.style.marginBottom = "6px";
|
||||||
lockBtn.onclick = () => {
|
box.appendChild(title);
|
||||||
localStorage.removeItem(`${STORAGE_KEY}_unlocked`);
|
|
||||||
localStorage.removeItem(`bratonien_opened_${YEAR}`);
|
|
||||||
console.log("Alle Türchen gesperrt.");
|
|
||||||
location.reload();
|
|
||||||
};
|
|
||||||
devBox.appendChild(lockBtn);
|
|
||||||
|
|
||||||
// Tag-Buttons 1–24
|
// Buttons 1–24
|
||||||
|
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++) {
|
for (let i = 1; i <= 24; i++) {
|
||||||
const b = document.createElement("button");
|
const b = document.createElement("button");
|
||||||
b.textContent = i;
|
b.textContent = i;
|
||||||
b.style.minWidth = "32px";
|
b.style.cssText = `
|
||||||
|
background: #333;
|
||||||
|
color: #fff;
|
||||||
|
border: 1px solid #666;
|
||||||
|
border-radius: 4px;
|
||||||
|
cursor: pointer;
|
||||||
|
font-size: 11px;
|
||||||
|
padding: 3px 0;
|
||||||
|
`;
|
||||||
b.onclick = () => {
|
b.onclick = () => {
|
||||||
localStorage.setItem(`${STORAGE_KEY}_simulateDay`, i);
|
localStorage.setItem(`${STORAGE_BASE}_simday`, String(i));
|
||||||
console.log(`Simuliere Tag ${i}`);
|
// Anzeige bleibt nur korrekt, wenn wir neu laden
|
||||||
location.reload();
|
location.reload();
|
||||||
};
|
};
|
||||||
devBox.appendChild(b);
|
grid.appendChild(b);
|
||||||
}
|
}
|
||||||
|
box.appendChild(grid);
|
||||||
|
|
||||||
// Sperre aufheben
|
// Reihe für Sperre aufheben / alle sperren
|
||||||
const unlockBtn = document.createElement("button");
|
const actions = document.createElement("div");
|
||||||
unlockBtn.textContent = "🚪 Sperre aufheben";
|
actions.style.display = "flex";
|
||||||
unlockBtn.onclick = () => {
|
actions.style.gap = "6px";
|
||||||
localStorage.setItem(`${STORAGE_KEY}_unlocked`, "true");
|
|
||||||
console.log("Alle Türchen entsperrt.");
|
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();
|
location.reload();
|
||||||
};
|
};
|
||||||
devBox.appendChild(unlockBtn);
|
actions.appendChild(unlock);
|
||||||
|
|
||||||
// Einfügen unterhalb des Reset-Buttons (falls vorhanden)
|
const relock = document.createElement("button");
|
||||||
if (resetBtn) {
|
relock.textContent = "🔒 Alle sperren";
|
||||||
resetBtn.insertAdjacentElement("afterend", devBox);
|
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 {
|
} else {
|
||||||
document.body.appendChild(devBox);
|
document.body.appendChild(box);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ===============================
|
// ===============================
|
||||||
// Simulationsfunktion (nur Dev)
|
// GLOBALER Export für door-open.js
|
||||||
// ===============================
|
|
||||||
function getSimulatedDay() {
|
|
||||||
if (!devMode) return null;
|
|
||||||
const simDay = parseInt(localStorage.getItem(`${STORAGE_KEY}_simulateDay`));
|
|
||||||
return simDay || null;
|
|
||||||
}
|
|
||||||
|
|
||||||
// ===============================
|
|
||||||
// Export (global für andere Skripte)
|
|
||||||
// ===============================
|
// ===============================
|
||||||
window.isDoorUnlocked = function (day) {
|
window.isDoorUnlocked = function (day) {
|
||||||
// Simulation berücksichtigen
|
// wenn wir im dev sind UND es einen simulierten Tag gibt → den nehmen
|
||||||
const simDay = getSimulatedDay();
|
if (DEV_MODE) {
|
||||||
if (devMode && simDay) return parseInt(day) <= simDay;
|
const sim = getSimulatedDay();
|
||||||
return isDoorUnlocked(day);
|
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 aktivieren
|
// Dev-Konsole nur anzeigen, wenn ?dev
|
||||||
if (devMode) createDevConsole();
|
if (DEV_MODE) {
|
||||||
|
createDevConsole();
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user