+ switch (data.code) {
+ case "pollclientsandgamers": {
+ // Since people can be both in Hall and Game,
+ // need to track "askIdentity" requests:
+ let identityAsked = {};
+ data.sockIds.forEach(s => {
+ const page = s.page || "/";
+ if (s.sid != this.st.user.sid && !identityAsked[s.sid]) {
+ identityAsked[s.sid] = true;
+ this.send("askidentity", { target: s.sid, page: page });
+ }
+ if (!this.people[s.sid])
+ this.$set(this.people, s.sid, { id: 0, name: "", pages: [page] });
+ else if (this.people[s.sid].pages.indexOf(page) < 0)
+ this.people[s.sid].pages.push(page);
+ if (!s.page)
+ //peer is in Hall
+ this.send("askchallenge", { target: s.sid });
+ //peer is in Game
+ else this.send("askgame", { target: s.sid, page: page });
+ });
+ break;
+ }
+ case "connect":
+ case "gconnect": {
+ const page = data.page || "/";
+ // NOTE: player could have been polled earlier, but might have logged in then
+ // So it's a good idea to ask identity if he was anonymous.
+ // But only ask game / challenge if currently disconnected.
+ if (!this.people[data.from]) {
+ this.$set(this.people, data.from, {
+ name: "",
+ id: 0,
+ pages: [page]
+ });
+ if (data.code == "connect")
+ this.send("askchallenge", { target: data.from });
+ else this.send("askgame", { target: data.from, page: page });
+ } else {
+ // append page if not already in list
+ if (this.people[data.from].pages.indexOf(page) < 0)
+ this.people[data.from].pages.push(page);
+ }
+ if (this.people[data.from].id == 0) {
+ this.newConnect[data.from] = true; //for self multi-connects tests
+ this.send("askidentity", { target: data.from, page: page });
+ }
+ break;
+ }
+ case "disconnect":
+ case "gdisconnect": {
+ // If the user reloads the page twice very quickly (experienced with Firefox),
+ // the first reload won't have time to connect but will trigger a "close" event anyway.
+ // ==> Next check is required.
+ if (!this.people[data.from]) return;
+ // Disconnect means no more tmpIds:
+ if (data.code == "disconnect") {
+ // Remove the live challenge sent by this player:
+ ArrayFun.remove(this.challenges, c => c.from.sid == data.from);
+ } else {
+ // Remove the matching live game if now unreachable
+ const gid = data.page.match(/[a-zA-Z0-9]+$/)[0];
+ const gidx = this.games.findIndex(g => g.id == gid);
+ if (gidx >= 0) {
+ const game = this.games[gidx];
+ if (
+ game.type == "live" &&
+ game.rids.length == 1 &&
+ game.rids[0] == data.from
+ ) {
+ this.games.splice(gidx, 1);
+ }
+ }
+ }
+ const page = data.page || "/";
+ ArrayFun.remove(this.people[data.from].pages, p => p == page);
+ if (this.people[data.from].pages.length == 0)
+ this.$delete(this.people, data.from);
+ break;
+ }
+ case "killed":
+ // I logged in elsewhere:
+ this.conn = null;
+ alert(this.st.tr["New connexion detected: tab now offline"]);