adventskalender/shared/js/lock.js aktualisiert
This commit is contained in:
@@ -1,135 +1,109 @@
|
|||||||
// ============================================================
|
// ============================================================
|
||||||
// Bratonien Adventskalender – Türchen-Sperrlogik & Dev-Overlay
|
// Bratonien Adventskalender – Türsperren / Dev-Konsole
|
||||||
// ============================================================
|
// ============================================================
|
||||||
// Autor: Thomas Dannenberg & GPT-5
|
|
||||||
// Pfad: shared/js/lock.js
|
|
||||||
// Zweck: Sperrt Türchen nach aktuellem Tag, mit Dev-Modus (?dev)
|
|
||||||
|
|
||||||
(() => {
|
// Jahr automatisch aus Ordnernamen ableiten
|
||||||
const pathMatch = window.location.pathname.match(/\/(\d{4})\//);
|
const YEAR = window.location.pathname.match(/(\d{4})/)?.[1] || "0000";
|
||||||
const YEAR = pathMatch ? pathMatch[1] : new Date().getFullYear();
|
const STORAGE_KEY = `bratonien_lock_${YEAR}`;
|
||||||
const PARAMS = new URLSearchParams(window.location.search);
|
let devMode = window.location.search.includes("?dev");
|
||||||
const DEV_MODE = PARAMS.has("dev");
|
|
||||||
|
|
||||||
// Lokale Speicherkeys
|
// ===============================
|
||||||
const KEY_SIM_DAY = `bratonien_${YEAR}_sim_day`;
|
// Sperrlogik
|
||||||
const KEY_UNLOCKED = `bratonien_${YEAR}_dev_unlock`;
|
// ===============================
|
||||||
|
function isDoorUnlocked(day) {
|
||||||
// === Aktuellen Tag bestimmen ===
|
// Dev-Modus immer alles offen
|
||||||
function getCurrentDay() {
|
if (devMode) return true;
|
||||||
if (DEV_MODE) {
|
|
||||||
const simDay = parseInt(localStorage.getItem(KEY_SIM_DAY), 10) || 1;
|
|
||||||
return simDay;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
// Nach 24.12. immer alles offen
|
||||||
const now = new Date();
|
const now = new Date();
|
||||||
const month = now.getMonth() + 1; // Dez = 12
|
if (now.getFullYear() > YEAR || (now.getFullYear() == YEAR && now.getMonth() > 11 && now.getDate() > 24))
|
||||||
const day = now.getDate();
|
return true;
|
||||||
|
|
||||||
// Nur im Dezember relevant
|
// Falls der Benutzer alles entsperrt hat
|
||||||
if (month !== 12) return 0;
|
if (localStorage.getItem(`${STORAGE_KEY}_unlocked`) === "true")
|
||||||
if (day >= 25) return 25; // Nach dem 24.12. alles offen
|
return true;
|
||||||
return day;
|
|
||||||
}
|
|
||||||
|
|
||||||
// === Türchen-Freigabe prüfen ===
|
// Normale Regel: Türchen <= aktueller Tag freigegeben
|
||||||
window.isDoorUnlocked = function (day) {
|
const today = now.getDate();
|
||||||
const current = getCurrentDay();
|
return parseInt(day) <= today;
|
||||||
const unlocked = localStorage.getItem(KEY_UNLOCKED) === "true";
|
}
|
||||||
|
|
||||||
if (unlocked) return true;
|
// ===============================
|
||||||
if (current >= 25) return true; // Nach 24. alles frei
|
// Dev-Konsole (nur bei ?dev)
|
||||||
return parseInt(day, 10) <= current;
|
// ===============================
|
||||||
};
|
function createDevConsole() {
|
||||||
|
// Prüfen, ob Reset-Button existiert (von save-progress.js)
|
||||||
// === Dev-Overlay aufbauen ===
|
let resetBtn = document.getElementById("dev-reset");
|
||||||
if (DEV_MODE) {
|
let devBox = document.createElement("div");
|
||||||
const overlay = document.createElement("div");
|
devBox.className = "dev-console";
|
||||||
overlay.id = "dev-overlay";
|
devBox.style.cssText = `
|
||||||
overlay.style.cssText = `
|
display:flex; flex-wrap:wrap; gap:6px;
|
||||||
position: fixed;
|
margin:10px 0 0 0; padding:8px;
|
||||||
bottom: 10px;
|
background:#111; border:1px solid #c8aa49;
|
||||||
right: 10px;
|
border-radius:8px; color:#c8aa49; font-family:monospace;
|
||||||
background: rgba(0,0,0,0.7);
|
justify-content:flex-start;
|
||||||
color: #fff;
|
|
||||||
font-family: monospace;
|
|
||||||
font-size: 13px;
|
|
||||||
padding: 10px;
|
|
||||||
border-radius: 8px;
|
|
||||||
z-index: 9999;
|
|
||||||
display: flex;
|
|
||||||
flex-direction: column;
|
|
||||||
gap: 6px;
|
|
||||||
align-items: center;
|
|
||||||
`;
|
`;
|
||||||
|
|
||||||
// Infozeile
|
// Sperrbutton
|
||||||
const info = document.createElement("div");
|
const lockBtn = document.createElement("button");
|
||||||
info.textContent = `DEV-MODUS: Simuliertag ${getCurrentDay()}`;
|
lockBtn.textContent = "🔒 Alle sperren";
|
||||||
overlay.appendChild(info);
|
lockBtn.onclick = () => {
|
||||||
|
localStorage.removeItem(`${STORAGE_KEY}_unlocked`);
|
||||||
// Buttonleiste
|
localStorage.removeItem(`bratonien_opened_${YEAR}`);
|
||||||
const grid = document.createElement("div");
|
console.log("Alle Türchen gesperrt.");
|
||||||
grid.style.cssText = `
|
|
||||||
display: grid;
|
|
||||||
grid-template-columns: repeat(8, 1fr);
|
|
||||||
gap: 4px;
|
|
||||||
`;
|
|
||||||
|
|
||||||
for (let i = 1; i <= 24; i++) {
|
|
||||||
const btn = document.createElement("button");
|
|
||||||
btn.textContent = i;
|
|
||||||
btn.style.cssText = `
|
|
||||||
padding: 3px 6px;
|
|
||||||
background: #444;
|
|
||||||
color: #fff;
|
|
||||||
border: 1px solid #777;
|
|
||||||
border-radius: 4px;
|
|
||||||
cursor: pointer;
|
|
||||||
`;
|
|
||||||
btn.onclick = () => {
|
|
||||||
localStorage.setItem(KEY_SIM_DAY, i);
|
|
||||||
info.textContent = `DEV-MODUS: Simuliertag ${i}`;
|
|
||||||
};
|
|
||||||
grid.appendChild(btn);
|
|
||||||
}
|
|
||||||
overlay.appendChild(grid);
|
|
||||||
|
|
||||||
// Unlock & Reset
|
|
||||||
const actions = document.createElement("div");
|
|
||||||
actions.style.cssText = "display:flex;gap:6px;margin-top:6px;";
|
|
||||||
|
|
||||||
const unlockBtn = document.createElement("button");
|
|
||||||
unlockBtn.textContent = "🔓 Sperre aufheben";
|
|
||||||
unlockBtn.style.cssText = `
|
|
||||||
background:#2c6e49;color:white;border:none;
|
|
||||||
padding:5px 10px;border-radius:4px;cursor:pointer;
|
|
||||||
`;
|
|
||||||
unlockBtn.onclick = () => {
|
|
||||||
localStorage.setItem(KEY_UNLOCKED, "true");
|
|
||||||
alert("Alle Türchen entsperrt.");
|
|
||||||
};
|
|
||||||
|
|
||||||
const resetBtn = document.createElement("button");
|
|
||||||
resetBtn.textContent = "⟳ Reset";
|
|
||||||
resetBtn.style.cssText = `
|
|
||||||
background:#8b0000;color:white;border:none;
|
|
||||||
padding:5px 10px;border-radius:4px;cursor:pointer;
|
|
||||||
`;
|
|
||||||
resetBtn.onclick = () => {
|
|
||||||
localStorage.removeItem(KEY_SIM_DAY);
|
|
||||||
localStorage.removeItem(KEY_UNLOCKED);
|
|
||||||
localStorage.removeItem(`bratonien_${YEAR}_progress`);
|
|
||||||
alert("Entwicklungseinstellungen & Fortschritt zurückgesetzt.");
|
|
||||||
location.reload();
|
location.reload();
|
||||||
};
|
};
|
||||||
|
devBox.appendChild(lockBtn);
|
||||||
|
|
||||||
actions.appendChild(unlockBtn);
|
// Tag-Buttons 1–24
|
||||||
actions.appendChild(resetBtn);
|
for (let i = 1; i <= 24; i++) {
|
||||||
overlay.appendChild(actions);
|
const b = document.createElement("button");
|
||||||
|
b.textContent = i;
|
||||||
document.body.appendChild(overlay);
|
b.style.minWidth = "32px";
|
||||||
|
b.onclick = () => {
|
||||||
|
localStorage.setItem(`${STORAGE_KEY}_simulateDay`, i);
|
||||||
|
console.log(`Simuliere Tag ${i}`);
|
||||||
|
location.reload();
|
||||||
|
};
|
||||||
|
devBox.appendChild(b);
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log(`[Lock.js] Initialisiert – aktueller Tag: ${getCurrentDay()} (Dev=${DEV_MODE})`);
|
// Sperre aufheben
|
||||||
})();
|
const unlockBtn = document.createElement("button");
|
||||||
|
unlockBtn.textContent = "🚪 Sperre aufheben";
|
||||||
|
unlockBtn.onclick = () => {
|
||||||
|
localStorage.setItem(`${STORAGE_KEY}_unlocked`, "true");
|
||||||
|
console.log("Alle Türchen entsperrt.");
|
||||||
|
location.reload();
|
||||||
|
};
|
||||||
|
devBox.appendChild(unlockBtn);
|
||||||
|
|
||||||
|
// Einfügen unterhalb des Reset-Buttons (falls vorhanden)
|
||||||
|
if (resetBtn) {
|
||||||
|
resetBtn.insertAdjacentElement("afterend", devBox);
|
||||||
|
} else {
|
||||||
|
document.body.appendChild(devBox);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ===============================
|
||||||
|
// Simulationsfunktion (nur Dev)
|
||||||
|
// ===============================
|
||||||
|
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) {
|
||||||
|
// Simulation berücksichtigen
|
||||||
|
const simDay = getSimulatedDay();
|
||||||
|
if (devMode && simDay) return parseInt(day) <= simDay;
|
||||||
|
return isDoorUnlocked(day);
|
||||||
|
};
|
||||||
|
|
||||||
|
// Dev-Konsole aktivieren
|
||||||
|
if (devMode) createDevConsole();
|
||||||
Reference in New Issue
Block a user