+ this.st.conn.send(JSON.stringify({
+ code: "resign",
+ target: this.game.oppid,
+ }));
+ // Next line will trigger a "gameover" event, bubbling up till here
+ this.$refs["basegame"].endGame(
+ this.game.mycolor=="w" ? "0-1" : "1-0", "Resign");
+ },
+ // 3 cases for loading a game:
+ // - from indexedDB (running or completed live game I play)
+ // - from server (one correspondance game I play[ed] or not)
+ // - from remote peer (one live game I don't play, finished or not)
+ 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);
+ // TODO: this is not really beautiful (uid on corr players...)
+ if (gtype == "corr" && game.players[0].color == "b")
+ [ game.players[0], game.players[1] ] = [ game.players[1], game.players[0] ];
+ const myIdx = game.players.findIndex(p => {
+ return p.sid == this.st.user.sid || p.uid == this.st.user.id;
+ });
+ 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}));