document.addEventListener('keydown', function (e) { // Mac = Option, Windows/Linux = Alt const isOptionOrAlt = e.altKey || e.metaKey; if (isOptionOrAlt && e.shiftKey && e.key.toLowerCase() === 'f') { toggleFullscreen(); } if (e.key === 'ArrowUp' || e.key === 'ArrowDown') { e.preventDefault(); } }); function toggleFullscreen() { // If not in fullscreen → enter fullscreen if (!document.fullscreenElement) { document.documentElement.requestFullscreen() .catch(err => console.error("Fullscreen error:", err)); } // If already fullscreen → exit fullscreen else { document.exitFullscreen(); } } let messagePosArray = []; const csrf_token = window.CSDR_TOKEN; const text = document.getElementById('wsInfo'); const rect = document.getElementById('wsInfoRectangle'); let ws; let firstConnect = true; let wsOpen = false; const RETRY_DELAY = 2000; // ms const WSaccesstype = "kampfrichter"; const WSaccess = window.FREIGABE; const WSaccessPermission = "W"; const urlAjaxNewWSToken = '/intern/scripts/ajax-create-ws-token.php'; async function fetchNewWSToken(accesstype, access, accessPermission) { try { const response = await fetch(urlAjaxNewWSToken, { method: "POST", headers: { 'Content-Type': 'application/x-www-form-urlencoded', }, body: new URLSearchParams({ csrf_token, accesstype, access, accessPermission }) }); if (response.status === 403) { console.warn("Please Re-Autenithicate. Reloading page..."); location.reload(); return null; } if (!response.ok) return null; const data = await response.json(); return data.success ? data.token : null; } catch (error) { console.error("Token fetch failed:", error); return null; } } async function startWebSocket() { console.log("Attempting WebSocket connection..."); let token; if (firstConnect) { token = window.WS_ACCESS_TOKEN; } else { token = await fetchNewWSToken(WSaccesstype, WSaccess, WSaccessPermission); } if (!token) { console.error("No valid token available. Retrying..."); scheduleRetry(); return; } try { ws = new WebSocket(`wss://${window.location.hostname}/ws/?access=token&token=${token}`); } catch (err) { console.error("Malformed WebSocket URL", err); scheduleRetry(); return; } ws.onopen = () => { console.log("WebSocket connected!"); if (!firstConnect) { displayMsg(1, "Live Syncronisation wieder verfügbar"); } firstConnect = true; wsOpen = true; rect.innerHTML = ''; requestAnimationFrame(() => { text.style.opacity = 1; }); }; ws.onerror = (event) => { console.error("WebSocket error observed." + JSON.stringify(event)); }; ws.onclose = (event) => { if (firstConnect) { displayMsg(0, "Live Syncronisation verloren"); console.log(event); } firstConnect = false; wsOpen = false; rect.innerHTML = ''; requestAnimationFrame(() => { text.style.opacity = 1; }); scheduleRetry(); }; } function scheduleRetry() { console.log(`Retrying in ${RETRY_DELAY}ms...`); setTimeout(startWebSocket, RETRY_DELAY); } // Start the initial connection attempt safely startWebSocket(); function updateRunButtons(targetCount, personId, $container) { if (targetCount === 0) { return; } const geraetId = $container.find('.submit-display-result').first().data('geraet-id') || ""; const currentCount = $container.find('.submit-display-result').length; $container.find('.submit-display-result').removeClass('hidden'); if (targetCount > currentCount) { for (let i = currentCount + 1; i <= targetCount; i++) { const buttonHtml = ` `; $container.append(buttonHtml); } $container.find('.submit-display-result[data-run="1"]').val('Ergebnis anzeigen (Run 1)'); } else if (targetCount < currentCount) { for (let i = currentCount; i > targetCount; i--) { $container.find(`.submit-display-result[data-run="${i}"]`).remove(); } if (targetCount === 1 && $container.find('.submit-display-result').length === 1) { $container.find('.submit-display-result').val('Ergebnis anzeigen'); } } $container.find('.submit-display-result').each(function() { $(this).attr('data-person-id', personId); }); } $.fn.updateCurrentEdit = function() { return this.each(function() { const $input = $(this); const url = '/intern/scripts/kampfrichter/ajax/ajax-kampfrichter_currentedit.php'; if ($input.attr('data-person-id') === "0"){ $('