clean click event

This commit is contained in:
Didictateur 2025-10-18 22:10:56 +02:00
parent 758c4f4675
commit 1571094ed5
3 changed files with 7 additions and 38 deletions

View file

@ -90,26 +90,9 @@ function renderBoardFromState(state, asWhite = null) {
cell.appendChild(piece); cell.appendChild(piece);
boardEl.appendChild(cell); boardEl.appendChild(cell);
// click to select/move // clicks on cells should do absolutely nothing (display-only mode)
cell.addEventListener('click', () => { cell.addEventListener('click', () => {
// if no game/socket, ignore // intentionally empty - no selection, no move, no UI changes
if (!currentGameId) return;
const x = parseInt(cell.dataset.x, 10);
const y = parseInt(cell.dataset.y, 10);
if (!selected) {
selected = { x, y };
cell.classList.add('selected');
setMeta('Selected ' + x + ',' + y);
} else {
// send move
const from = selected;
const to = { x, y };
selected = null;
// clear previously selected class
document.querySelectorAll('.cell.selected').forEach(el => el.classList.remove('selected'));
setMeta('Sending move ' + from.x + ',' + from.y + ' -> ' + to.x + ',' + to.y);
if (socket && myPlayerId && currentGameId) socket.emit('move', { gameId: currentGameId, playerId: myPlayerId, from, to });
}
}); });
} }
} }

View file

@ -60,26 +60,9 @@ function renderBoardFromState(state) {
cell.appendChild(piece); cell.appendChild(piece);
boardEl.appendChild(cell); boardEl.appendChild(cell);
// click to select/move // clicks on cells should do absolutely nothing (display-only mode)
cell.addEventListener('click', () => { cell.addEventListener('click', () => {
// if no game/socket, ignore // intentionally empty — do not select, do not emit moves, do not change UI
if (!currentGameId) return;
const x = parseInt(cell.dataset.x, 10);
const y = parseInt(cell.dataset.y, 10);
if (!selected) {
selected = { x, y };
cell.classList.add('selected');
setMeta('Selected ' + x + ',' + y);
} else {
// send move
const from = selected;
const to = { x, y };
selected = null;
// clear previously selected class
document.querySelectorAll('.cell.selected').forEach(el => el.classList.remove('selected'));
setMeta('Sending move ' + from.x + ',' + from.y + ' -> ' + to.x + ',' + to.y);
if (socket && myPlayerId && currentGameId) socket.emit('move', { gameId: currentGameId, playerId: myPlayerId, from, to });
}
}); });
} }
} }

View file

@ -19,3 +19,6 @@ main{padding:16px}
.piece{width:48px;height:48px} .piece{width:48px;height:48px}
.game-header{display:flex;justify-content:space-between;align-items:center;margin-bottom:8px} .game-header{display:flex;justify-content:space-between;align-items:center;margin-bottom:8px}
.controls button{margin-left:8px} .controls button{margin-left:8px}
/* Ensure selected cells have no visual effect (display-only mode) */
.cell.selected{outline: none !important; box-shadow: none !important; border-color: inherit !important; background: inherit !important}