Split app.js into modular components (state, evaluation, ui, board, data)

This commit is contained in:
2026-05-27 22:55:49 +02:00
parent 8d971dbef9
commit 8758441f65
9 changed files with 667 additions and 695 deletions

97
js/board.js Normal file
View File

@@ -0,0 +1,97 @@
/**
* Lara Kiesewetter Live Schachturnier
* Board rendering and move navigation
*/
/* global $, Chess, Chessboard */
function handleMoveClick(e) {
if (!e.target.classList.contains('move') || !currentGame) return;
goToMove(parseInt(e.target.dataset.index));
}
function goToMove(index) {
if (!currentGame) return;
const nonResultMoves = currentGame.moves.filter(m => !m.isResult);
if (index < -1) index = -1;
if (index >= nonResultMoves.length) index = nonResultMoves.length - 1;
currentMoveIndex = index;
chess = new Chess();
for (let i = 0; i <= index; i++) {
const san = nonResultMoves[i].san;
let result = chess.move(san);
if (!result) {
const cleanSan = san.replace(/^([NBRQK])([a-h])?([1-8])?([a-h][1-8].*)$/, '$1$4');
result = chess.move(cleanSan);
}
if (!result) break;
}
board.position(chess.fen(), true);
highlightActivePlayer();
highlightLastMove();
updateClocks(index);
syncEvalBarHeight();
updateEvaluation();
document.querySelectorAll('#moves-list .move').forEach(el => el.classList.remove('current'));
if (index >= 0) {
const moveEl = document.querySelector(`#moves-list [data-index="${index}"]`);
if (moveEl) moveEl.classList.add('current');
}
}
function updateBoard() {
if (!currentGame) return;
const laraIsBlack = currentGame.black.toLowerCase().includes('kiesewetter');
const orientation = laraIsBlack ? 'black' : 'white';
const nonResultMoves = currentGame.moves.filter(m => !m.isResult);
if (currentMoveIndex >= nonResultMoves.length || currentMoveIndex < -1) {
currentMoveIndex = nonResultMoves.length - 1;
} else if (previousMoveCount >= 0 && currentMoveIndex === previousMoveCount - 1 && nonResultMoves.length > previousMoveCount) {
currentMoveIndex = nonResultMoves.length - 1;
}
previousMoveCount = nonResultMoves.length;
chess = new Chess();
if (currentMoveIndex >= 0) {
for (let i = 0; i <= currentMoveIndex && i < nonResultMoves.length; i++) {
const san = nonResultMoves[i].san;
let result = chess.move(san);
if (!result) {
const cleanSan = san.replace(/^([NBRQK])([a-h])?([1-8])?([a-h][1-8].*)$/, '$1$4');
result = chess.move(cleanSan);
}
if (!result) break;
}
}
if (board) {
board.position(chess.fen(), true);
board.orientation(orientation);
board.resize();
highlightLastMove();
} else {
board = Chessboard('board', {
position: chess.fen(),
orientation: orientation,
pieceTheme: 'https://chessboardjs.com/img/chesspieces/wikipedia/{piece}.png',
draggable: false,
spawnMoveError: false
});
document.getElementById('moves-list').addEventListener('click', handleMoveClick);
}
highlightActivePlayer();
syncEvalBarHeight();
updateEvaluation();
}