node-chess
node-chess copied to clipboard
dump / restore game state to / from json.
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 - 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.
https://github.com/Piterden/chessbot/blob/master/src/handlers/start/index.js#L12-L31
https://github.com/Piterden/chessbot/tree/master/migrations
@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).
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.
@macdja38 #41 not json, but FEN
@macdja38 #41 not json, but FEN
Nice, thanks!
Should we keep this issue open, or does the FEN load address this need?