Skip to content

Commit

Permalink
Merge branch 'main' into v1.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
qwerty084 authored Dec 10, 2023
2 parents f38d96e + b633e53 commit e015b83
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 6 deletions.
22 changes: 17 additions & 5 deletions src/classes/BoardApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
deepMergeConfig,
getThreats,
isPromotion,
chessJSPieceToLichessPiece,
possibleMoves,
shortToLongColor,
} from '@/helper/Board';
Expand Down Expand Up @@ -33,7 +34,7 @@ import {
} from 'chess.js';
import type { Api } from 'chessground/api';
import { Chessground } from 'chessground/chessground';
import type { Color, Key, MoveMetadata } from 'chessground/types';
import type { Color, Key, MoveMetadata, Role } from 'chessground/types';
import { nextTick } from 'vue';

/**
Expand Down Expand Up @@ -530,11 +531,22 @@ export class BoardApi {
* returns true on success, else false
*/
putPiece(piece: Piece, square: Square): boolean {
const result = this.game.put(piece, square);
if (result) {
this.updateGameState();
// @TODO using putPiece with the same piece and square twice is buggy in movable: false in chess.js state
if (this.board.state.movable.free) {
const current = this.board.state.pieces;
current.set(square, {
color: piece.color === 'w' ? 'white' : 'black',
role: chessJSPieceToLichessPiece[piece.type] as Role,
});
this.board.setPieces(current);
return true;
} else {
const result = this.game.put(piece, square);
if (result) {
this.updateGameState();
}
return result;
}
return result;
}

/**
Expand Down
9 changes: 9 additions & 0 deletions src/helper/Board.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,12 @@ export function deepDiffConfig<T>(oldConfig: T, newConfig: T): DeepPartial<T> {
}
return diff;
}

export const chessJSPieceToLichessPiece = {
p: 'pawn',
n: 'knight',
b: 'bishop',
r: 'rook',
q: 'queen',
k: 'king',
};
6 changes: 5 additions & 1 deletion src/tests/BoardApi.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,11 @@ describe.concurrent('Test the board API', () => {
});

it('adds a pgn header and checks if it is added', () => {
boardApi.setPgnInfo({ White: "Deep Blue", Black: "Kasparov", Date: "1997.05.11" });
boardApi.setPgnInfo({
White: 'Deep Blue',
Black: 'Kasparov',
Date: '1997.05.11',
});
expect(boardApi.getPgn()).toContain('[White "Deep Blue"]');
expect(boardApi.getPgn()).toContain('[Black "Kasparov"]');
expect(boardApi.getPgn()).toContain('[Date "1997.05.11"]');
Expand Down

0 comments on commit e015b83

Please sign in to comment.