node-chess icon indicating copy to clipboard operation
node-chess copied to clipboard

dump / restore game state to / from json.

Open macdja38 opened this issue 7 years ago • 7 comments

This is a feature request

It would be nice to see a simple api to dump game state to json and restore it. Something like

let json = game.dump()
game.restore(json)

This could be used to save games in a database or create save/restore points.

macdja38 avatar Jun 07 '17 04:06 macdja38

@macdja38 - thank you for this issue report!

I did something similar to this for a stdlib hosted micro service (https://stdlib.com/brozeph/chess), but I didn't use JSON as the format.

The code implementation is here: https://github.com/brozeph/stdlib-chess/blob/master/f/game/index.js

Effectively, I maintained state for the moves using a base64 encoded string... but I like the idea of being able to quickly and easily serialize/deserialize as JSON. I'll spend some time on this as time allows - it may take me a hot minute to get to it, though.

brozeph avatar Jun 07 '17 12:06 brozeph

https://github.com/Piterden/chessbot/blob/master/src/handlers/start/index.js#L12-L31

https://github.com/Piterden/chessbot/tree/master/migrations

Piterden avatar Apr 18 '18 22:04 Piterden

@Piterden - thank you for sharing! It looks like you're saving each move into a table and then restoring the board by re-applying each move individually. This is effectively what I did in the stdlib implementation.

An alternative, I recently noticed, which I forgot about in my original reply on this issue thread, that there is a way to load a game from a move history: https://github.com/brozeph/node-chess/blob/master/src/game.js#L86. One possibility might be to serialize the move history as JSON (JSON.stringify(gameClient.game.moveHistory)) and then use that when creating a game subsequently for reload... if this sounds interesting or worthwhile, I could implement a load method from the game clients that does just this (as an alternative to the create method).

brozeph avatar Apr 18 '18 23:04 brozeph

Unfortunately, for my case the load method wouldn't be implementable, cause I would need to use the DB for store, anyway. Btw, I also calculate which turn is now from those data.

Piterden avatar Apr 24 '18 05:04 Piterden

@macdja38 #41 not json, but FEN

Piterden avatar Nov 12 '20 04:11 Piterden

@macdja38 #41 not json, but FEN

Nice, thanks!

macdja38 avatar Nov 12 '20 04:11 macdja38

Should we keep this issue open, or does the FEN load address this need?

brozeph avatar Mar 01 '21 19:03 brozeph