+ offerDraw: function() {
+ if (!confirm("Offer draw?"))
+ return;
+ // Stay in "draw offer sent" state until next move is played
+ this.drawOfferSent = true;
+ if (this.subMode == "corr")
+ {
+ // TODO: set drawOffer on in game (how ?)
+ }
+ else //live game
+ {
+ this.opponents.forEach(o => {
+ if (!!o.online)
+ {
+ try {
+ this.conn.send(JSON.stringify({code: "draw", oppid: o.id}));
+ } catch (INVALID_STATE_ERR) {
+ return;
+ }
+ }
+ });
+ }
+ },
+ // + conn handling: "draw" message ==> agree for draw (if we have "drawOffered" at true)
+ receiveDrawOffer: function() {
+ //if (...)
+ // TODO: ignore if preventDrawOffer is set; otherwise show modal box with option "prevent future offers"
+ // if accept: send message "draw"
+ },
+ abortGame: function() {
+ if (!confirm("Abort the game?"))
+ return;
+ //+ bouton "abort" avec score == "?" + demander confirmation pour toutes ces actions,
+ //send message: "gameOver" avec score "?"
+ },
+ resign: function(e) {
+ if (!confirm("Resign the game?"))
+ return;
+ if (this.mode == "human" && this.oppConnected(this.oppid))
+ {
+ try {
+ this.conn.send(JSON.stringify({code: "resign", oppid: this.oppid}));
+ } catch (INVALID_STATE_ERR) {
+ return;
+ }
+ }
+ this.endGame(this.mycolor=="w"?"0-1":"1-0");
+ },
+ translate: translate,
+ newGameFromFen: function(fen) {
+ this.vr = new VariantRules(fen);
+ this.moves = [];
+ this.cursor = -1;
+ this.fenStart = newFen;
+ this.score = "*";
+ if (this.mode == "analyze")
+ {
+ this.mycolor = V.ParseFen(newFen).turn;
+ this.orientation = this.mycolor;
+ }
+ else if (this.mode == "computer") //only other alternative (HH with gameId)
+ {
+ this.mycolor = (Math.random() < 0.5 ? "w" : "b");
+ this.orientation = this.mycolor;
+ this.compWorker.postMessage(["init",newFen]);
+ if (this.mycolor != "w" || this.subMode == "auto")
+ this.playComputerMove();
+ }
+ },
+ loadGame: function() {
+ // TODO: ask game to remote peer if this.remoteId is set
+ // (or just if game not found locally)
+ // NOTE: if it's a corr game, ask it from server
+ const game = getGameFromStorage(this.gameId);
+ this.opponent.id = game.oppid; //opponent ID in case of running HH game
+ this.opponent.name = game.oppname; //maye be blank (if anonymous)
+ this.score = game.score;
+ this.mycolor = game.mycolor;
+ this.fenStart = game.fenStart;
+ this.moves = game.moves;
+ this.cursor = game.moves.length-1;
+ this.lastMove = (game.moves.length > 0 ? game.moves[this.cursor] : null);
+ },
+ setEndgameMessage: function(score) {
+ let eogMessage = "Undefined";
+ switch (score)
+ {
+ case "1-0":
+ eogMessage = translations["White win"];
+ break;
+ case "0-1":
+ eogMessage = translations["Black win"];
+ break;
+ case "1/2":
+ eogMessage = translations["Draw"];
+ break;
+ case "?":
+ eogMessage = "Unfinished";
+ break;
+ }
+ this.endgameMessage = eogMessage;
+ },