+ filterChallenges: function(type) {
+ return this.challenges.filter(c => c.type == type);
+ },
+ filterGames: function(type) {
+ return this.games.filter(c => c.type == type);
+ },
+ classifyChallenge: function(c) {
+ // Heuristic: should work for most cases... (TODO)
+ return (c.timeControl.indexOf('d') === -1 ? "live" : "corr");
+ },
+ socketMessageListener: function(msg) {
+ // Save and call current st.conn.onmessage if one was already defined
+ // --> also needed in future Game.vue (also in Chat.vue component)
+ this.oldOnmessage(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.players.push({sid:sid, id:0, name:""});
+ // Ask identity, challenges and game(s)
+ this.st.conn.send(JSON.stringify({code:"askidentity", target:sid}));
+ this.st.conn.send(JSON.stringify({code:"askchallenges", target:sid}));
+ this.st.conn.send(JSON.stringify({code:"askgame", 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(
+ {code:"identity", user:this.st.user, target:data.from}));
+ }
+ break;
+ }
+ case "askchallenge":
+ {
+ // Send my current live challenge (if any)
+ const cIdx = this.challenges
+ .findIndex(c => c.from.sid == this.st.user.sid && c.type == "live");
+ if (cIdx >= 0)
+ {
+ const c = this.challenges[cIdx];
+ const myChallenge =
+ {
+ // Minimal challenge informations: (from not required)
+ to: c.to,
+ fen: c.fen,
+ vid: c.vid,
+ timeControl: c.timeControl
+ };
+ this.st.conn.send(JSON.stringify({code:"challenge",
+ challenge:myChallenge, target:data.from}));
+ }
+ break;
+ }
+ case "askgame":
+ {
+ // TODO: Send my current live game (if any): variant, players, movesCount
+ break;
+ }
+ case "identity":
+ {
+ const pIdx = this.players.findIndex(p => p.sid == data.user.sid);
+ this.players[pIdx].id = data.user.id;
+ this.players[pIdx].name = data.user.name;
+ break;
+ }
+ case "challenge":
+ {
+ // Receive challenge from some player (+sid)
+ let newChall = data.chall;
+ newChall.type = classifyChallenge(data.chall);
+ const pIdx = this.players.findIndex(p => p.sid == data.sid);
+ newChall.from = this.players[pIdx]; //may be anonymous
+ newChall.added = Date.now();
+ this.challenges.push(newChall);
+ break;
+ }
+ case "game":
+ {
+ // Receive game from some player (+sid)
+ // TODO: receive game summary (update, count moves)
+ // (just players names, time control, and ID + player ID)
+ // NOTE: it may be correspondance (if newgame while we are connected)
+ break;
+ }
+// * - receive "new game": if live, store locally + redirect to game
+// * If corr: notify "new game has started", give link, but do not redirect
+ case "newgame":
+ {
+ // TODO: new game just started: data contain all informations
+ // (id, players, time control, fenStart ...)
+ // + cid to remove challenge from list
+ break;
+ }
+// * - receive "accept/withdraw/cancel challenge": apply action to challenges list
+ case "acceptchallenge":
+ {
+ // Someone accept an open (or targeted) challenge,
+ // of 3 or more players and empty slots remain.
+ const cIdx = this.challenges.findIndex(c => c.id == data.cid);
+ let players = this.challenges[cIdx].to;
+ const pIdx = this.players.fndIndex(p => p.sid == data.from);
+ for (let i=0; i<players.length; i++)
+ {
+ if (!players[i])
+ {
+ players[i] = this.players[pIdx].name;
+ break;
+ }
+ }
+ break;
+ }
+ case "withdrawchallenge":
+ {
+ const cIdx = this.challenges.findIndex(c => c.id == data.cid);
+ let chall = this.challenges[cIdx]
+ ArrayFun.remove(chall.players, p => p.id == data.uid);
+ chall.players.push({id:0, name:""});
+ break;
+ }
+ case "deletechallenge":
+ {
+ ArrayFun.remove(this.challenges, c => c.id == data.cid);
+ break;
+ }
+ // TODO: distinguish hallConnect and gameConnect ?
+ // Or global variable players
+ // + game variable: "observers"
+ case "connect":
+// * - receive "player connect": send our current challenge (to him or global)
+// * Also send all our games (live - max 1 - and corr) [in web worker ?]
+ {
+ this.players.push({name:"", id:0, sid:data.sid});
+ this.st.conn.send(JSON.stringify({code:"askidentity", target:data.sid}));
+ break;
+ }
+// * - receive "player disconnect": remove from players list
+ case "disconnect":
+ {
+ ArrayFun.remove(this.players, p => p.sid == data.sid);
+ // TODO: also remove all challenges sent by this player,
+ // and all live games where he plays and no other opponent is online
+ break;
+ }
+ }
+ },