- getOppSid: function() {
- if (!!this.game.oppsid)
- return this.game.oppsid;
- const opponent = this.people.find(p => p.id == this.game.oppid);
- return (!!opponent ? opponent.sid : null);
- },
- socketMessageListener: function(msg) {
- const data = JSON.parse(msg.data);
- switch (data.code)
- {
- // 0.2] Receive clients list (just socket IDs)
- case "pollclients":
- {
- data.sockIds.forEach(sid => {
- this.people.push({sid:sid, id:0, name:""});
- // Ask only identity
- this.st.conn.send(JSON.stringify({code:"askidentity", target:sid}));
- });
- break;
- }
- case "askidentity":
- {
- // Request for identification: reply if I'm not anonymous
- if (this.st.user.id > 0)
- {
- this.st.conn.send(JSON.stringify(
- // people[0] instead of st.user to avoid sending email
- {code:"identity", user:this.people[0], target:data.from}));
- }
- break;
- }
- case "identity":
- {
- let player = this.people.find(p => p.sid == data.user.sid);
- // NOTE: sometimes player.id fails because player is undefined...
- // Probably because the event was meant for Hall?
- if (!player)
- return;
- player.id = data.user.id;
- player.name = data.user.name;
- // Sending last state only for live games: corr games are complete
- if (this.game.type == "live" && this.game.oppsid == player.sid)
- {
- // Send our "last state" informations to opponent
- const L = this.game.moves.length;
- this.st.conn.send(JSON.stringify({
- code: "lastate",
- target: player.sid,
- state:
- {
- lastMove: (L>0 ? this.game.moves[L-1] : undefined),
- score: this.game.score,
- movesCount: L,
- drawOffer: this.drawOffer,
- clocks: this.game.clocks,
- }
- }));
- }
- break;
- }
- case "askgame":
- // Send current (live) game
- const myGame =
- {
- // Minimal game informations:
- id: this.game.id,
- players: this.game.players.map(p => { return {name:p.name}; }),
- vid: this.game.vid,
- timeControl: this.game.timeControl,
- };
- this.st.conn.send(JSON.stringify({code:"game",
- game:myGame, target:data.from}));
- break;
- case "newmove":
- // NOTE: this call to play() will trigger processMove()
- this.$refs["basegame"].play(data.move,
- "receive", this.game.vname!="Dark" ? "animate" : null);
- break;
- case "lastate": //got opponent infos about last move
- {
- const L = this.game.moves.length;
- if (data.movesCount > L)
- {
- // Just got last move from him
- this.$refs["basegame"].play(data.lastMove,
- "receive", this.game.vname!="Dark" ? "animate" : null);
- if (data.score != "*" && this.game.score == "*")
- {
- // Opponent resigned or aborted game, or accepted draw offer
- // (this is not a stalemate or checkmate)
- this.$refs["basegame"].endGame(data.score, "Opponent action");
- }
- this.game.clocks = data.clocks; //TODO: check this?
- this.drawOffer = data.drawOffer; //does opponent offer draw?
- }
- break;
- }
- case "resign":
- this.$refs["basegame"].endGame(
- this.game.mycolor=="w" ? "1-0" : "0-1", "Resign");
- break;
- case "abort":
- this.$refs["basegame"].endGame("?", "Abort: " + data.msg);
- break;
- case "draw":
- this.$refs["basegame"].endGame("1/2", "Mutual agreement");
- break;
- case "drawoffer":
- this.drawOffer = "received";
- break;
- case "askfullgame":
- // TODO: use data.id to retrieve game in indexedDB (but for now only one running game so OK)
- this.st.conn.send(JSON.stringify({code:"fullgame", game:this.game, target:data.from}));
- break;
- case "fullgame":
- this.loadGame(data.game);
- break;
- // TODO: drawaccepted (click draw button before sending move
- // ==> draw offer in move)
- // ==> on "newmove", check "drawOffer" field
- case "connect":
- {
- this.people.push({name:"", id:0, sid:data.from});
- this.st.conn.send(JSON.stringify({code:"askidentity", target:data.from}));
- break;
- }
- case "disconnect":
- ArrayFun.remove(this.people, p => p.sid == data.from);
- break;
- }
- },
- offerDraw: function() {
- // TODO: also for corr games
- if (this.drawOffer == "received")
- {
- if (!confirm("Accept draw?"))
- return;
- const oppsid = this.getOppSid();
- if (!!oppsid)
- this.st.conn.send(JSON.stringify({code:"draw", target:oppsid}));
- this.$refs["basegame"].endGame("1/2", "Mutual agreement");
- }
- else if (this.drawOffer == "sent")
- this.drawOffer = "";
- else
- {
- if (!confirm("Offer draw?"))
- return;
- const oppsid = this.getOppSid();
- if (!!oppsid)
- this.st.conn.send(JSON.stringify({code:"drawoffer", target:oppsid}));
- }
- },
- // + 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(event) {
- let modalBox = document.getElementById("modalAbort");
- if (!event)
- {
- // First call show options:
- modalBox.checked = true;
- }
- else
- {
- modalBox.checked = false; //decision made: box disappear
- const message = event.target.innerText;
- // Next line will trigger a "gameover" event, bubbling up till here
- this.$refs["basegame"].endGame("?", "Abort: " + message);
- const oppsid = this.getOppSid();
- if (!!oppsid)
- {
- this.st.conn.send(JSON.stringify({
- code: "abort",
- msg: message,
- target: oppsid,
- }));
- }
- }
- },
- resign: function(e) {
- if (!confirm("Resign the game?"))
- return;
- const oppsid = this.getOppSid();
- if (!!oppsid)
- {
- this.st.conn.send(JSON.stringify({
- code: "resign",
- target: oppsid,
- }));
- }
- // Next line will trigger a "gameover" event, bubbling up till here
- this.$refs["basegame"].endGame(
- this.game.mycolor=="w" ? "0-1" : "1-0", "Resign");