Skip to content

Commit

Permalink
Merge pull request #183 from qwerty084/fix/always-emit-move-event
Browse files Browse the repository at this point in the history
emit move event on check and checkmate
  • Loading branch information
Luca Hendrik Helms authored May 26, 2023
2 parents c2579b0 + b437455 commit 98d0ede
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
18 changes: 9 additions & 9 deletions src/helper/EmitEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,30 @@ import type { Chess } from 'chess.js';
import type { Api } from 'chessground/api';

export function emitBoardEvents(game: Chess, board: Api, emit: Emit): void {
const lastMove = game.history({ verbose: true }).pop();
if (typeof lastMove !== 'undefined') {
emit('move', lastMove);
}

if (game.inCheck()) {
const pieces = board.state.pieces;
for (const [key, piece] of pieces) {
if (piece.role === 'king' && piece.color === board?.state.turnColor) {
board.state.check = key;
emit('check', board.state.turnColor);
if (game.isCheckmate()) {
emit('checkmate', board.state.turnColor);
} else {
emit('check', board.state.turnColor);
}
return;
}
}
}

if (game.isDraw()) {
emit('draw', true);
emit('draw');
}

if (game.isStalemate()) {
emit('stalemate', true);
}

const lastMove = game.history({ verbose: true }).pop();
if (typeof lastMove !== 'undefined') {
emit('move', lastMove);
emit('stalemate');
}
}
4 changes: 2 additions & 2 deletions src/typings/Chessboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ export type PieceColor = 'white' | 'black';
export interface Emit {
(e: 'boardCreated', boardApi: BoardApi): void;
(e: 'checkmate', isMated: PieceColor): void;
(e: 'stalemate', isStalemate: boolean): void;
(e: 'draw', isDraw: boolean): void;
(e: 'stalemate'): void;
(e: 'draw'): void;
(e: 'check', isInCheck: PieceColor): void;
(e: 'promotion', promotion: PromotionEvent): void;
(e: 'move', move: MoveEvent): void;
Expand Down

0 comments on commit 98d0ede

Please sign in to comment.