X-Git-Url: https://git.auder.net/?p=vchess.git;a=blobdiff_plain;f=client%2Fsrc%2Futils%2FgameStorage.js;h=0284763a882d24a39e02618ab7022a1f508680d5;hp=2ccb494f9b9e3a96947afab7e1d892915d6f3701;hb=3d55deea9a2011c38d8d0067bd57fc889958bec2;hpb=dce792f64ab0a311d348a6eb05c440dd1b170bd3 diff --git a/client/src/utils/gameStorage.js b/client/src/utils/gameStorage.js index 2ccb494f..0284763a 100644 --- a/client/src/utils/gameStorage.js +++ b/client/src/utils/gameStorage.js @@ -67,22 +67,43 @@ export const GameStorage = }, // TODO: also option to takeback a move ? - // NOTE: for live games only (all on server for corr) update: function(gameId, obj) //move, fen, clocks, score, initime, ... { - dbOperation((db) => { - let objectStore = db.transaction("games", "readwrite").objectStore("games"); - objectStore.get(gameId).onsuccess = function(event) { - const game = event.target.result; - Object.keys(obj).forEach(k => { - if (k == "move") - game.moves.push(obj[k]); - else - game[k] = obj[k]; - }); - objectStore.put(game); //save updated data - } - }); + if (Number.isInteger(gameId) || !isNaN(parseInt(gameId))) + { + // corr: only move, fen and score + ajax( + "/games", + "PUT", + { + gid: gameId, + newObj: + { + // TODO: I think stringify isn't requuired here (see ajax() ) + move: JSON.stringify(obj.move), //may be undefined... + fen: obj.fen, + score: obj.score, + } + } + ); + } + else + { + // live + dbOperation((db) => { + let objectStore = db.transaction("games", "readwrite").objectStore("games"); + objectStore.get(gameId).onsuccess = function(event) { + const game = event.target.result; + Object.keys(obj).forEach(k => { + if (k == "move") + game.moves.push(obj[k]); + else + game[k] = obj[k]; + }); + objectStore.put(game); //save updated data + } + }); + } }, // Retrieve all local games (running, completed, imported...)