/* ============================================================
public/js/buildings/daily.js
/* ── Positionen (% in viewBox 0 0 100 100) ───────────────── */
const STATIONS = [
{ id:1, cx:49.5, cy:86.5 },
{ id:2, cx:37.0, cy:73.0 },
{ id:3, cx:44.5, cy:62.0 },
{ id:4, cx:54.5, cy:53.5 },
{ id:5, cx:49.0, cy:46.0 },
{ id:6, cx:44.5, cy:37.5 },
{ id:7, cx:50.0, cy:22.0 },
];
const R = 4.8; // Kreis-Radius in SVG-Einheiten
let dailyDeckId = null;
let dailyOverlay = null;
/* ── Karten-Pfad öffnen ──────────────────────────────────── */
export async function openDailyMap(deckId) {
dailyDeckId = deckId;
document.getElementById('daily-map-overlay')?.remove();
let completed = [];
try {
const r = await fetch('/api/himmelstor/daily/progress');
if (r.ok) completed = (await r.json()).completed ?? [];
} catch {}
dailyOverlay = document.createElement('div');
dailyOverlay.id = 'daily-map-overlay';
dailyOverlay.innerHTML = `

${buildSvg(completed)}
☀️ TAGESHERAUSFORDERUNG
${completed.length} / 7 Stationen abgeschlossen
${completed.length >= 7 ? allDoneHtml() : ''}
`;
document.body.appendChild(dailyOverlay);
document.getElementById('daily-map-close').addEventListener('click', closeMap);
dailyOverlay.addEventListener('click', e => { if (e.target === dailyOverlay) closeMap(); });
/* Klick-Listener auf SVG-Hitboxen */
dailyOverlay.querySelectorAll('.ds-hit[data-station]').forEach(el => {
el.addEventListener('click', () => startStation(parseInt(el.dataset.station, 10)));
});
}
/* ── SVG aufbauen ────────────────────────────────────────── */
function buildSvg(completed) {
const maxDone = completed.length;
const parts = [];
/* Pfadlinien zwischen Stationen */
for (let i = 0; i < STATIONS.length - 1; i++) {
const a = STATIONS[i];
const b = STATIONS[i + 1];
const done = completed.includes(a.id);
parts.push(``);
}
/* Stationskreise */
STATIONS.forEach(s => {
const isDone = completed.includes(s.id);
const isAvail = !isDone && s.id === maxDone + 1;
const cls = isDone ? 'ds-done' : isAvail ? 'ds-available' : 'ds-locked';
const fSize = R * 0.72;
if (isDone) {
parts.push(`
✓
`);
} else if (isAvail) {
parts.push(`
${s.id}
`);
} else {
parts.push(`
${s.id}
`);
}
});
return ``;
}
/* ── Station starten ─────────────────────────────────────── */
function startStation(station) {
if (!dailyDeckId) { alert('Bitte zuerst ein Deck auswählen.'); return; }
const socket = window._socket;
if (!socket) return;
/* Hitbox deaktivieren während Laden */
dailyOverlay.querySelectorAll(`.ds-hit[data-station="${station}"]`).forEach(el => {
el.style.pointerEvents = 'none';
el.style.opacity = '.4';
});
socket.once('ht_daily_match_found', data => {
closeMap();
openPopup(
`/himmelstor/daily?match=${encodeURIComponent(data.matchId)}`
+ `&slot=${encodeURIComponent(data.mySlot)}`
+ `&deck=${encodeURIComponent(dailyDeckId)}`
+ `&station=${station}`
+ `&opponent=${encodeURIComponent('Wächter ' + station)}`,
'Wächter ' + station, data.matchId
);
});
socket.once('ht_daily_error', data => {
dailyOverlay.querySelectorAll(`.ds-hit[data-station="${station}"]`).forEach(el => {
el.style.pointerEvents = '';
el.style.opacity = '';
});
alert(data.message || 'Fehler beim Starten.');
});
socket.emit('ht_join_daily', { station, deckId: dailyDeckId });
}
/* ── Station als erledigt markieren ─────────────────────── */
export function markDailyStationDone(station) {
if (!dailyOverlay) return;
/* SVG-Gruppe aktualisieren */
const g = dailyOverlay.querySelector(`.ds-station[data-station="${station}"]`);
if (g) {
g.className.baseVal = 'ds-station ds-done';
g.innerHTML = `
✓`;
}
/* Pfadlinie grün */
const line = dailyOverlay.querySelector(`.ds-path-line:nth-of-type(${station})`);
if (line) line.classList.add('done');
/* Nächste Station freischalten */
const nextStation = STATIONS.find(s => s.id === station + 1);
const nextG = nextStation
? dailyOverlay.querySelector(`.ds-station[data-station="${station + 1}"]`)
: null;
if (nextG && nextStation) {
const fSize = R * 0.72;
nextG.className.baseVal = 'ds-station ds-available';
nextG.innerHTML = `
${station+1}
`;
nextG.querySelector('.ds-hit')?.addEventListener('click', () => startStation(station + 1));
}
/* Fortschritt */
const done = dailyOverlay.querySelectorAll('.ds-done').length;
const pct = Math.round(done / 7 * 100);
const fill = document.getElementById('daily-progress-fill');
const text = document.getElementById('daily-progress-text');
if (fill) fill.style.width = pct + '%';
if (text) text.textContent = `${done} / 7 Stationen abgeschlossen`;
if (done >= 7) {
const wrap = document.getElementById('daily-map-wrap');
if (wrap && !document.getElementById('daily-all-done')) {
wrap.insertAdjacentHTML('beforeend', allDoneHtml());
}
}
}
/* ── Popup öffnen ────────────────────────────────────────── */
function openPopup(src, opponent, matchId) {
document.getElementById('ht-daily-backdrop')?.remove();
document.getElementById('ht-daily-popup')?.remove();
const bd = document.createElement('div');
bd.id = 'ht-daily-backdrop';
bd.style.cssText = 'position:fixed;inset:0;background:rgba(0,0,0,.82);backdrop-filter:blur(5px);z-index:9998;';
const pp = document.createElement('div');
pp.id = 'ht-daily-popup';
pp.style.cssText = 'position:fixed;inset:50px;z-index:9999;display:flex;flex-direction:column;border-radius:14px;overflow:hidden;box-shadow:0 0 0 1px rgba(80,140,255,.4),0 30px 90px rgba(0,0,0,.85);';
pp.innerHTML = `
☀️ Daily · vs ${opponent}
${matchId}
`;
document.body.appendChild(bd);
document.body.appendChild(pp);
}
function closeMap() {
document.getElementById('daily-map-overlay')?.remove();
dailyOverlay = null;
}
function allDoneHtml() {
return `
🏆
TAGESQUEST ABGESCHLOSSEN!
Du hast alle 7 Stationen gemeistert.
`;
}