- loadGame: async function() {
- this.gameInfo = GameStorage.get(this.gameRef);
-
-console.log(GameStorage.get(this.gameRef));
-
- this.vname = this.gameInfo.vname;
- this.mode = this.gameInfo.mode;
- const vModule = await import("@/variants/" + this.vname + ".js");
- window.V = vModule.VariantRules;
- this.vr = new V(this.gameInfo.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)
-// if (this.gameInfo.players.some(p => p.sid == this.st.user.sid))
-// {
-// this.game.players.forEach(p => {
-// if (p.sid != this.st.user.sid)
-// this.st.conn.send(JSON.stringify({code:"ping", oppid:p.sid}));
-// });
-// }
- },
- oppConnected: function(uid) {
- return this.opponents.some(o => o.id == uid && o.online);
+ loadGame: function(game) {
+ const afterRetrieval = async (game) => {
+ // NOTE: variants array might not be available yet, thus the two next lines
+ const variantCell = this.st.variants.filter(v => v.id == game.vid);
+ const vname = (variantCell.length > 0 ? variantCell[0].name : "");
+ if (!game.fen)
+ game.fen = game.fenStart; //game wasn't started
+ const gtype = (game.timeControl.indexOf('d') >= 0 ? "corr" : "live");
+ if (gtype == "corr")
+ {
+ // corr game: needs to compute the clocks + initime
+ //if (game.players[i].rtime < 0) initime = Date.now(), else compute,
+ //also using move.played fields
+ game.clocks = [-1, -1];
+ game.initime = [0, 0];
+ // TODO: compute clocks + initime
+ }
+ const tc = extractTime(game.timeControl);
+ const myIdx = game.players.findIndex(p => p.sid == this.st.user.sid);
+ if (game.clocks[0] < 0) //game unstarted
+ {
+ game.clocks = [tc.mainTime, tc.mainTime];
+ game.initime[0] = Date.now();
+ if (myIdx >= 0) //I play in this game
+ {
+ GameStorage.update(game.gameId,
+ {
+ clocks: game.clocks,
+ initime: game.initime,
+ });
+ }
+ }
+ const vModule = await import("@/variants/" + vname + ".js");
+ window.V = vModule.VariantRules;
+ this.vr = new V(game.fen);
+ this.game = Object.assign({},
+ game,
+ // NOTE: assign mycolor here, since BaseGame could also bs VS computer
+ {
+ type: gtype,
+ increment: tc.increment,
+ vname: vname,
+ mycolor: [undefined,"w","b"][myIdx+1],
+ // opponent sid not strictly required, but easier
+ oppid: (myIdx < 0 ? undefined : game.players[1-myIdx].sid),
+ }
+ );
+ if (!!this.game.oppid)
+ {
+ // Send ping to server (answer pong if players[s] are connected)
+ this.st.conn.send(JSON.stringify({code:"ping", target:this.game.oppid}));
+ }
+ };
+ if (!!game)
+ return afterRetrival(game);
+ if (!!this.gameRef.rid)
+ {
+ this.st.conn.send(JSON.stringify({code:"askfullgame", target:this.gameRef.rid}));
+ // TODO: just send a game request message to the remote player,
+ // and when receiving answer just call loadGame(received_game)
+ // + remote peer should have registered us as an observer
+ // (send moves updates + resign/abort/draw actions)
+ }
+ else
+ {
+ GameStorage.get(this.gameRef.id, async (game) => {
+ afterRetrieval(game);
+ });
+ }