Fix caching issue - load fresh PGN for current round
This commit is contained in:
83
app.js
83
app.js
@@ -14,44 +14,59 @@ let laraColor = null;
|
||||
let currentMoveIndex = -1;
|
||||
let userSelectedGame = false;
|
||||
let userScrolledMoves = false;
|
||||
let lastMtime = 0;
|
||||
let currentRound = 0;
|
||||
let roundPgns = {};
|
||||
let pollId = 0;
|
||||
|
||||
/**
|
||||
* Lädt die PGN-Datei und aktualisiert die Anzeige
|
||||
*/
|
||||
async function fetchRoundPGN(round) {
|
||||
const res = await fetch(`http://localhost:8111/pgn/${round}`);
|
||||
if (!res.ok) return null;
|
||||
return await res.text();
|
||||
}
|
||||
|
||||
async function loadPGN(showOverlay = true) {
|
||||
const currentPollId = ++pollId;
|
||||
const currentPollId = pollId;
|
||||
if (showOverlay) showLoading(true);
|
||||
hideError();
|
||||
|
||||
try {
|
||||
// Zuerst Tabelle abrufen, um die aktuelle Runde zu ermitteln
|
||||
await updateStandings();
|
||||
|
||||
if (currentRound === 0) {
|
||||
if (showOverlay) showLoading(false);
|
||||
return;
|
||||
}
|
||||
const [pgnResponse, statusResponse] = await Promise.all([
|
||||
fetch(`http://localhost:8111/pgn/${currentRound}?since=${lastMtime}`),
|
||||
fetch('http://localhost:8111/status').catch(() => null)
|
||||
]);
|
||||
|
||||
|
||||
// Fehlende vergangene Runden einmalig nachladen
|
||||
for (let r = 1; r < currentRound; r++) {
|
||||
if (roundPgns[r] === undefined) {
|
||||
const text = await fetchRoundPGN(r);
|
||||
if (text !== null) roundPgns[r] = text;
|
||||
}
|
||||
}
|
||||
|
||||
const statusResponse = await fetch('http://localhost:8111/status').catch(() => null);
|
||||
|
||||
// Aktuelle Runde immer frisch holen
|
||||
const pgnText = await fetchRoundPGN(currentRound);
|
||||
if (currentPollId !== pollId) return;
|
||||
if (!pgnResponse.ok) throw new Error(`HTTP ${pgnResponse.status}`);
|
||||
|
||||
const mtimeHeader = pgnResponse.headers.get('X-Cache-Mtime');
|
||||
if (mtimeHeader) lastMtime = parseFloat(mtimeHeader);
|
||||
|
||||
if (pgnText !== null) roundPgns[currentRound] = pgnText;
|
||||
|
||||
// Nächste Runde prüfen (sobald verfügbar, einmalig holen)
|
||||
const nextRound = currentRound + 1;
|
||||
if (roundPgns[nextRound] === undefined) {
|
||||
const text = await fetchRoundPGN(nextRound).catch(() => null);
|
||||
if (text) roundPgns[nextRound] = text;
|
||||
}
|
||||
|
||||
if (statusResponse && statusResponse.ok) {
|
||||
const status = await statusResponse.json();
|
||||
serverLastFetch = status.last_fetch ? status.last_fetch * 1000 : null;
|
||||
}
|
||||
|
||||
const pgnText = await pgnResponse.text();
|
||||
const allGames = parsePGN(pgnText);
|
||||
// Alle PGNs kombinieren
|
||||
const combinedPgn = Object.values(roundPgns).join('\n\n');
|
||||
const allGames = parsePGN(combinedPgn);
|
||||
allLaraGames = filterLaraGames(allGames);
|
||||
|
||||
if (allLaraGames.length === 0) {
|
||||
@@ -59,7 +74,6 @@ async function loadPGN(showOverlay = true) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Nur automatisch wechseln, wenn der Benutzer keine andere Partie ausgewählt hat
|
||||
if (!userSelectedGame) {
|
||||
const liveGame = getLiveGame(allLaraGames);
|
||||
const newGame = liveGame || getLatestGame(allLaraGames);
|
||||
@@ -439,18 +453,29 @@ function updateTimestamp() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Start long-polling: nach jeder Antwort sofort die nächste Anfrage stellen
|
||||
* Startet Auto-Refresh alle 30 Sekunden
|
||||
*/
|
||||
function startAutoRefresh() {
|
||||
document.getElementById('refresh-timer').textContent = '● Live';
|
||||
document.getElementById('refresh-timer').style.color = '#4ade80';
|
||||
|
||||
const myId = ++pollId;
|
||||
(async function poll() {
|
||||
while (pollId === myId) {
|
||||
await loadPGN(false);
|
||||
}
|
||||
})();
|
||||
let lastUpdate = Date.now();
|
||||
let pollInterval, timer;
|
||||
|
||||
document.getElementById('refresh-timer').textContent = '0s';
|
||||
document.getElementById('refresh-timer').style.color = '#4ade80';
|
||||
|
||||
loadPGN(true);
|
||||
|
||||
pollInterval = setInterval(() => {
|
||||
if (pollId !== myId) { clearInterval(pollInterval); clearInterval(timer); return; }
|
||||
loadPGN(false);
|
||||
lastUpdate = Date.now();
|
||||
}, 30000);
|
||||
|
||||
timer = setInterval(() => {
|
||||
if (pollId !== myId) { clearInterval(pollInterval); clearInterval(timer); return; }
|
||||
const s = Math.floor((Date.now() - lastUpdate) / 1000);
|
||||
document.getElementById('refresh-timer').textContent = `${s}s`;
|
||||
}, 1000);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -474,7 +499,7 @@ function hideError() {
|
||||
*/
|
||||
document.getElementById('refresh-btn').addEventListener('click', () => {
|
||||
pollId++;
|
||||
loadPGN(true).then(() => startAutoRefresh());
|
||||
startAutoRefresh();
|
||||
});
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user