oakmac / chessboardjs

JavaScript chessboard
https://chessboardjs.com
MIT License
2.01k stars 408 forks source link

Temporarily duplicated pieces due to racing updates to currentPosition. #233

Open LMMilewski opened 2 years ago

LMMilewski commented 2 years ago

Consider the "Play Random Computer" example from https://www.chessboardjs.com/examples#5001

First, the following bug can be easily reproduced locally by exaggerating the effect with the following changes:

As a result of the above, you'll see duplicated pieces (for the duration of the animation) whenever the computer makes a move.

This happens because of these racing currentPosition reads/writes:

As a result, if board.position() is called in the 30ms snapSpeed time window after player's move, the player gets to see two versions of the piece moved by the computer:

I attached a screenshot. I played 1.d4 and the computer from your example replied 2...,g4 and now the black g-pawn is duplicated: one is on g4 and the other one is moving from g2 to g4). duplicates

As mentioned at the start of this post, you can easily reproduce it locally by changing makeRandomMove delay to 1 and config.moveSpeed to 1000 in your example at https://www.chessboardjs.com/examples#5001

One workaround is to set config.snapSpeed to 0 and disallowing dragging pieces while computer move is being animated.

JuliaHorokhovatska commented 9 months ago

I have the same problem

JuliaHorokhovatska commented 9 months ago

I added delay whenever the computer makes a move, it is fix duplicated pieces.