// Turn "wb" into "B" (for FEN)
static board2fen(b)
{
- return b[0]=='w' ? b[1].toUpperCase() : b[1];
+ return (b[0]=='w' ? b[1].toUpperCase() : b[1]);
}
// Turn "p" into "bp" (for board)
static fen2board(f)
{
- return f.charCodeAt()<=90 ? "w"+f.toLowerCase() : "b"+f;
+ return (f.charCodeAt()<=90 ? "w"+f.toLowerCase() : "b"+f);
}
// Check if FEN describe a position
},
// localStorage:
- // TODO: also option to takeback a move ? Is fen included in move ?
+ // TODO: also option to takeback a move ?
// NOTE: for live games only (all on server for corr)
- update: function(fen, move, clocks, started, score)
+ update: function(o) //move, clock, initime, score, colorIdx
{
+ // TODO: finish this --> colorIdx must be computed before entering the function
let gameState = JSON.parse(localStorage.getItem("gameState"));
- if (!!fen)
+ if (!!o.move)
{
- gameState.moves.push(move);
- gameState.fen = fen;
- gameState.clocks = clocks;
+ // https://stackoverflow.com/a/38750895
+ const allowed = ['appear', 'vanish', 'start', 'end'];
+ const filtered_move = Object.keys(o.move)
+ .filter(key => allowed.includes(key))
+ .reduce((obj, key) => {
+ obj[key] = raw[key];
+ return obj;
+ }, {});
+ gameState.moves.push(filtered_move);
+ gameState.fen = o.move.fen;
+ const colorIdx = ["w","b","g","r"][o.move.color];
+ gameState.clocks[colorIdx] = o.move.clock;
}
- if (!!started)
- gameState.started = started;
- if (!!score)
- gameState.score = score;
+ if (!!o.initime) //just a flag (true)
+ gameState.initime = Date.now();
+ if (!!o.score)
+ gameState.score = o.score;
localStorage.setItem("gameState", JSON.stringify(gameState));
- if (!!score && score != "*")
+ if (!!o.score && o.score != "*")
transferToDb(); //game is over
},
const vModule = await import("@/variants/" + game.vname + ".js");
window.V = vModule.VariantRules;
this.vr = new V(game.fen);
+ // Post-processing: decorate each move with current FEN:
+ // (to be able to jump to any position quickly)
+ game.moves.forEach(move => {
+ vr.play(move); //side-effect: set move.fen
+ });
+ this.vr.re_init(game.fen);
});
// // Poll all players except me (if I'm playing) to know online status.
// // --> Send ping to server (answer pong if players[s] are connected)
},
};
</script>
+
+// TODO: utiliser "started" (renommer) pour se souvenir du timestamp où un user a commencé à réfléchir à un coup. Le sauvegarder dans update aussi...
+
+// variable initime à récupérer de game aussi (et pas "started")
+// si initime à -1 ou undefined alors pas commencé. Abort possible à tout moment avec message
+// Sorry I have to go / Game seems over / Game is not interesting
+
+move.clock à màj avec (current) clock - temps de réflexion (now() - initime) + increment
+après chaque coup dans une partie live ou corr non terminée.
+--> donc à Game.update on passe directement clock
+
+code "T" pour score "perte au temps" ?