PGN-Parser verbessert: robustes Tokenizing mit Kommentar- und %clk-Erkennung; app.js: manuelle Partieauswahl bleibt nach Klick erhalten
This commit is contained in:
12
app.js
12
app.js
@@ -16,6 +16,7 @@ let countdown = 0;
|
|||||||
let serverLastFetch = null;
|
let serverLastFetch = null;
|
||||||
let laraColor = null;
|
let laraColor = null;
|
||||||
let currentMoveIndex = -1;
|
let currentMoveIndex = -1;
|
||||||
|
let userSelectedGame = false;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Lädt die PGN-Datei und aktualisiert die Anzeige
|
* Lädt die PGN-Datei und aktualisiert die Anzeige
|
||||||
@@ -46,11 +47,11 @@ async function loadPGN() {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Finde die aktuelle/live Partie
|
// Nur automatisch wechseln, wenn der Benutzer keine andere Partie ausgewählt hat
|
||||||
const liveGame = getLiveGame(allLaraGames);
|
if (!userSelectedGame) {
|
||||||
const targetGame = liveGame || getLatestGame(allLaraGames);
|
const liveGame = getLiveGame(allLaraGames);
|
||||||
|
currentGame = liveGame || getLatestGame(allLaraGames);
|
||||||
currentGame = targetGame;
|
}
|
||||||
updateBoard();
|
updateBoard();
|
||||||
updatePlayerInfo();
|
updatePlayerInfo();
|
||||||
updateMovesList();
|
updateMovesList();
|
||||||
@@ -306,6 +307,7 @@ function updateAllGamesList() {
|
|||||||
`;
|
`;
|
||||||
|
|
||||||
entry.addEventListener('click', () => {
|
entry.addEventListener('click', () => {
|
||||||
|
userSelectedGame = true;
|
||||||
currentGame = game;
|
currentGame = game;
|
||||||
updateBoard();
|
updateBoard();
|
||||||
updatePlayerInfo();
|
updatePlayerInfo();
|
||||||
|
|||||||
@@ -66,21 +66,43 @@ function parseGameBlock(block) {
|
|||||||
function parseMoves(movesText) {
|
function parseMoves(movesText) {
|
||||||
const moves = [];
|
const moves = [];
|
||||||
|
|
||||||
// Remove comments in curly braces
|
let clockWhite = null;
|
||||||
movesText = movesText.replace(/\{[^}]*\}/g, '');
|
let clockBlack = null;
|
||||||
|
let color = 'w';
|
||||||
|
|
||||||
// Remove move numbers
|
const tokenRegex = /\d+\.\s*|\{[^}]*\}|\S+/g;
|
||||||
movesText = movesText.replace(/\d+\.\s*/g, '');
|
const tokens = movesText.match(tokenRegex) || [];
|
||||||
|
|
||||||
// Split into individual moves
|
for (let i = 0; i < tokens.length; i++) {
|
||||||
const tokens = movesText.split(/\s+/).filter(t => t.trim());
|
let token = tokens[i].trim();
|
||||||
|
|
||||||
for (const token of tokens) {
|
|
||||||
if (['1-0', '0-1', '1/2-1/2', '*'].includes(token)) {
|
if (['1-0', '0-1', '1/2-1/2', '*'].includes(token)) {
|
||||||
moves.push({ san: token, isResult: true });
|
moves.push({ san: token, isResult: true, whiteClock: clockWhite, blackClock: clockBlack });
|
||||||
} else if (token.length > 0) {
|
continue;
|
||||||
moves.push({ san: token, isResult: false });
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (/^\d+\.$/.test(token)) continue;
|
||||||
|
|
||||||
|
if (token.startsWith('{')) {
|
||||||
|
const clkMatch = token.match(/\[%clk\s+([\d:]+)\]/);
|
||||||
|
if (clkMatch && moves.length > 0) {
|
||||||
|
const lastMove = moves[moves.length - 1];
|
||||||
|
if (!lastMove.isResult && lastMove.color) {
|
||||||
|
if (lastMove.color === 'w') {
|
||||||
|
clockWhite = clkMatch[1];
|
||||||
|
} else {
|
||||||
|
clockBlack = clkMatch[1];
|
||||||
|
}
|
||||||
|
lastMove.whiteClock = clockWhite;
|
||||||
|
lastMove.blackClock = clockBlack;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
const move = { san: token, isResult: false, whiteClock: clockWhite, blackClock: clockBlack, color };
|
||||||
|
moves.push(move);
|
||||||
|
color = color === 'w' ? 'b' : 'w';
|
||||||
}
|
}
|
||||||
|
|
||||||
return moves;
|
return moves;
|
||||||
|
|||||||
Reference in New Issue
Block a user