'use strict';
// ── WebSocket ────────────────────────────────────────────────────────────────
let ws;
let wsReconnectTimer;
function connectWS() {
const proto = location.protocol === 'https:' ? 'wss:' : 'ws:';
ws = new WebSocket(`${proto}//${location.host}`);
ws.addEventListener('open', () => {
setWsStatus('CONNECTÉ', 'ok');
updateInfoWS('CONNECTÉ');
if (wsReconnectTimer) { clearTimeout(wsReconnectTimer); wsReconnectTimer = null; }
});
ws.addEventListener('message', evt => {
let msg;
try { msg = JSON.parse(evt.data); } catch { return; }
if (msg.type === 'alert') {
showAlert(msg.message, msg.html, msg.image);
} else if (msg.type === 'dismiss') {
hideAlert();
} else if (msg.type === 'rootme_update') {
renderRootme(msg.ranking);
} else if (msg.type === 'rootme_flag') {
renderRootme(rootmeCache);
showNotif(`FLAG ! ${msg.login} +${msg.gained} PTS — TOTAL : ${msg.newScore} PTS`, null);
playKillstreak(msg.streak || 1);
} else if (msg.type === 'geo_news') {
showGeoBanner(msg.title, msg.link);
} else if (msg.type === 'anssi_news') {
showNotif(`Nouveau bulletin ANSSI : ${msg.title}`);
}
});
ws.addEventListener('close', () => {
setWsStatus('DÉCONNECTÉ', 'err');
updateInfoWS('DÉCONNECTÉ');
wsReconnectTimer = setTimeout(connectWS, 3000);
});
ws.addEventListener('error', () => {
ws.close();
});
}
function setWsStatus(text, cls) {
const el = document.getElementById('ws-status');
el.textContent = text;
el.className = 'widget-status ' + (cls || '');
}
function updateInfoWS(text) {
document.getElementById('info-ws').textContent = text;
}
// ── Alert overlay ────────────────────────────────────────────────────────────
const overlay = document.getElementById('alert-overlay');
const alertMessageEl = document.getElementById('alert-message');
const alertHtmlEl = document.getElementById('alert-html');
const alertIconEl = document.getElementById('alert-icon');
const alertImageEl = document.getElementById('alert-image');
const infoAlert = document.getElementById('info-alert');
// ── Killstreak sounds ─────────────────────────────────────────────────────────
const killstreakSounds = [
null, // index 0 inutilisé
[new Audio('/sound_effects/first-blood-1.mp3'), new Audio('/sound_effects/first-blood-2.mp3')],
[new Audio('/sound_effects/double-kill-1.mp3'), new Audio('/sound_effects/double-kill-2.mp3')],
[new Audio('/sound_effects/triple-kill-1.mp3'), new Audio('/sound_effects/triple-kill-2.mp3')],
[new Audio('/sound_effects/monsterkill.mp3')],
];
const godlikeAudio = new Audio('/sound_effects/godlike.mp3');
function playKillstreak(streak) {
const pool = streak >= 5 ? [godlikeAudio] : (killstreakSounds[streak] || killstreakSounds[1]);
const snd = pool[Math.floor(Math.random() * pool.length)];
snd.currentTime = 0;
snd.play().catch(err => console.error('killstreak audio:', err));
}
// ── Notification (ANSSI / geo) ────────────────────────────────────────────────
const notifOverlay = document.getElementById('notif-overlay');
const notifMessage = document.getElementById('notif-message');
const notifBarInner = document.getElementById('notif-bar-inner');
const softAlarmAudio = new Audio('/soft_alarm.mp3');
let notifTimer = null;
function showNotif(message, audio = softAlarmAudio, duration = 10_000) {
notifMessage.textContent = message;
// Re-déclencher l'animation de la barre
notifBarInner.style.setProperty('--notif-duration', `${duration / 1000}s`);
notifBarInner.style.animation = 'none';
void notifBarInner.offsetWidth;
notifBarInner.style.animation = '';
notifOverlay.style.animation = 'none';
void notifOverlay.offsetWidth;
notifOverlay.style.animation = '';
notifOverlay.classList.remove('hidden');
if (audio) {
audio.currentTime = 0;
audio.play().catch(err => console.error('notif audio:', err));
}
if (notifTimer) clearTimeout(notifTimer);
notifTimer = setTimeout(() => {
notifOverlay.classList.add('hidden');
notifTimer = null;
}, duration);
}
// ── Geo news bottom banner ────────────────────────────────────────────────────
const geoBanner = document.getElementById('geo-banner');
const geoBannerLink = document.getElementById('geo-banner-link');
let geoBannerTimer = null;
function showGeoBanner(title, link, duration = 15_000) {
geoBannerLink.textContent = title;
geoBannerLink.href = link || '#';
geoBanner.style.animation = 'none';
void geoBanner.offsetWidth;
geoBanner.style.animation = '';
geoBanner.classList.remove('hidden');
if (geoBannerTimer) clearTimeout(geoBannerTimer);
geoBannerTimer = setTimeout(() => {
geoBanner.classList.add('hidden');
geoBannerTimer = null;
}, duration);
}
// ── Alarm sound ───────────────────────────────────────────────────────────────
const alarmAudio = new Audio('/alert.mp3');
function playAlertSound() {
alarmAudio.currentTime = 0;
alarmAudio.play().catch(err => console.error('Audio play failed:', err));
}
function stopAlertSound() {
alarmAudio.pause();
alarmAudio.currentTime = 0;
}
// ── Alert overlay ─────────────────────────────────────────────────────────────
let autoDismissTimer = null;
function showAlert(message, html, image) {
// Image
if (image && image.trim()) {
alertImageEl.innerHTML = ``;
alertIconEl.style.display = 'none';
} else {
alertImageEl.innerHTML = '';
alertIconEl.style.display = '';
}
// Text / HTML
alertMessageEl.textContent = message || '';
alertMessageEl.style.display = (message && !image) ? '' : (message ? '' : 'none');
alertHtmlEl.innerHTML = (html && html.trim()) ? html : '';
// Show overlay
overlay.classList.remove('hidden');
overlay.style.animation = 'none';
void overlay.offsetWidth;
overlay.style.animation = '';
infoAlert.textContent = 'ACTIVE';
infoAlert.style.color = 'var(--red)';
playAlertSound();
if (autoDismissTimer) clearTimeout(autoDismissTimer);
autoDismissTimer = setTimeout(hideAlert, 60_000);
}
function hideAlert() {
if (autoDismissTimer) { clearTimeout(autoDismissTimer); autoDismissTimer = null; }
overlay.classList.add('hidden');
stopAlertSound();
infoAlert.textContent = 'INACTIVE';
infoAlert.style.color = '';
}
// ── Clock ────────────────────────────────────────────────────────────────────
const clockTime = document.getElementById('clock-time');
const clockDate = document.getElementById('clock-date');
const headerClock = document.getElementById('header-clock');
const infoHost = document.getElementById('info-host');
const DAYS = ['Dimanche','Lundi','Mardi','Mercredi','Jeudi','Vendredi','Samedi'];
const MONTHS = ['Jan','Fév','Mar','Avr','Mai','Jun','Jul','Aoû','Sep','Oct','Nov','Déc'];
function pad(n) { return String(n).padStart(2, '0'); }
function updateClock() {
const now = new Date();
const h = pad(now.getHours());
const m = pad(now.getMinutes());
const s = pad(now.getSeconds());
const timeStr = `${h}:${m}:${s}`;
const dateStr = `${DAYS[now.getDay()]} ${pad(now.getDate())} ${MONTHS[now.getMonth()]} ${now.getFullYear()}`;
clockTime.textContent = timeStr;
clockDate.textContent = dateStr;
headerClock.textContent = `${dateStr} ${timeStr}`;
}
// ── ANSSI / CERT-FR feed ─────────────────────────────────────────────────────
const anssiList = document.getElementById('anssi-list');
const anssiStatus = document.getElementById('anssi-status');
async function loadAnssi() {
anssiStatus.textContent = '...';
anssiStatus.className = 'widget-status';
try {
const resp = await fetch('/api/feeds/anssi');
if (!resp.ok) throw new Error(`HTTP ${resp.status}`);
const items = await resp.json();
anssiList.innerHTML = '';
if (!items.length) {
anssiList.innerHTML = '