+ 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"]);
+ break;
+ case "askidentity": {
+ // Request for identification (TODO: anonymous shouldn't need to reply)
+ const me = {
+ // Decompose to avoid revealing email
+ name: this.st.user.name,
+ sid: this.st.user.sid,
+ id: this.st.user.id
+ };
+ this.send("identity", { data: me, target: data.from });
+ break;
+ }
+ case "identity": {
+ const user = data.data;
+ if (user.name) {
+ // If I multi-connect, kill current connexion if no mark (I'm older)
+ if (
+ this.newConnect[user.sid] &&
+ user.id > 0 &&
+ user.id == this.st.user.id &&
+ user.sid != this.st.user.sid
+ ) {
+ if (!this.killed[this.st.user.sid]) {
+ this.send("killme", { sid: this.st.user.sid });
+ this.killed[this.st.user.sid] = true;
+ }
+ }
+ if (user.sid != this.st.user.sid) {
+ //I already know my identity...
+ this.$set(this.people, user.sid, {
+ id: user.id,
+ name: user.name,
+ pages: this.people[user.sid].pages
+ });
+ }