Files
lara-schach-live/js/board.js

97 lines
3.0 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* 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();
}