+ socketMessageListener: function(msg) {
+ const data = JSON.parse(msg.data);
+ switch (data.code)
+ {
+// * - 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 "playergame": a live game by some connected player (NO corr)
+ case "playergame":
+ // TODO: receive live game summary (update, count moves)
+ // (just players names, time control, and ID + player ID)
+ break;
+// * - receive "playerchallenges": list of challenges (sent) by some online player (NO corr)
+ case "playerchallenges":
+ // TODO: receive challenge + challenge updates
+ break;
+ case "newmove": //live or corr
+ // TODO: name conflict ? (game "newmove" event)
+ break;
+// * - receive new challenge: if targeted, replace our name with sender name
+ case "newchallenge":
+ // receive live or corr challenge
+ break;
+// * - receive "accept/withdraw/cancel challenge": apply action to challenges list
+ case "acceptchallenge":
+ if (true) //TODO: if challenge is full
+ this.newGame(data.challenge, data.user); //user.id et user.name
+ 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 "cancelchallenge":
+ ArrayFun.remove(this.challenges, c => c.id == data.cid);
+ break;
+// NOTE: finally only one connect / disconnect couple of events
+// (because on server side we wouldn't know which to choose)
+ case "connect":
+// * - receive "player connect": send all our current challenges (to him or global)
+// * Also send all our games (live - max 1 - and corr) [in web worker ?]
+// * + all our sent challenges.
+ this.players.push({name:data.name, id:data.uid});
+ // TODO: si on est en train de jouer une partie, le notifier au nouveau connecté
+ // envoyer aussi nos défis
+ break;
+// * - receive "player disconnect": remove from players list
+ case "disconnect":
+ ArrayFun.remove(this.players, p => p.id == data.uid);
+ // TODO: also remove all challenges sent by this player,
+ // and all live games where he plays and no other opponent is online
+ break;
+ }
+ },
+ socketCloseListener: function() {
+ // connexion is reinitialized in store.js
+ this.st.conn.addEventListener('message', this.socketMessageListener);
+ this.st.conn.addEventListener('close', this.socketCloseListener);
+ },