+ case "duplicate":
+ alert(this.st.tr["Warning: multi-tabs not supported"]);
+ break;
+ // 0.2] Receive clients list (just socket IDs)
+ case "pollclients":
+ data.sockIds.forEach(sid => {
+ this.$set(this.people, sid, {id:0, name:""});
+ // Ask identity and challenges
+ this.st.conn.send(JSON.stringify({code:"askidentity", target:sid}));
+ this.st.conn.send(JSON.stringify({code:"askchallenge", target:sid}));
+ });
+ break;
+ case "pollgamers":
+ // NOTE: we could make a difference between people in hall
+ // and gamers, but is it necessary?
+ data.sockIds.forEach(sid => {
+ this.$set(this.people, sid, {id:0, name:"", gamer:true});
+ this.st.conn.send(JSON.stringify({code:"askidentity", target:sid}));
+ });
+ // Also ask current games to all playing peers (TODO: some design issue)
+ this.st.conn.send(JSON.stringify({code:"askgames"}));
+ 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: {
+ // NOTE: decompose to avoid revealing email
+ name: this.st.user.name,
+ sid: this.st.user.sid,
+ id: this.st.user.id,
+ },
+ target:data.from}));
+ }
+ break;
+ }
+ case "identity":
+ {
+ this.$set(this.people, data.user.sid,
+ {
+ id: data.user.id,
+ name: data.user.name,
+ gamer: this.people[data.user.sid].gamer,
+ });
+ 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];
+ if (!!c.to)
+ {
+ // Only share targeted challenges to the targets:
+ const toSid = Object.keys(this.people).find(k =>
+ this.people[k].name == c.to);
+ if (toSid != data.from)
+ return;
+ }
+ const myChallenge =
+ {
+ // Minimal challenge informations: (from not required)
+ id: c.id,
+ to: c.to,
+ fen: c.fen,
+ vid: c.vid,
+ timeControl: c.timeControl,
+ };
+ this.st.conn.send(JSON.stringify({code:"challenge",
+ chall:myChallenge, target:data.from}));
+ }
+ break;
+ }
+ case "challenge":
+ {
+ // Receive challenge from some player (+sid)
+ let newChall = data.chall;
+ newChall.type = this.classifyObject(data.chall);
+ newChall.from =
+ Object.assign({sid:data.from}, this.people[data.from]);
+ newChall.added = Date.now(); //TODO: this is reception timestamp, not creation
+ newChall.vname = this.getVname(newChall.vid);
+ this.challenges.push(newChall);
+ break;
+ }
+ case "game":
+ {
+ // Receive game from some player (+sid)
+ // NOTE: it may be correspondance (if newgame while we are connected)
+ // If duplicate found: select rid (remote ID) at random
+ let game = this.games.find(g => g.id == data.game.id);
+ if (!!game && Math.random() < 0.5)
+ game.rid = data.from;
+ else
+ {
+ let newGame = data.game;
+ newGame.type = this.classifyObject(data.game);
+ newGame.vname = this.getVname(data.game.vid);
+ newGame.rid = data.from;
+ newGame.score = "*";
+ this.games.push(newGame);
+ }
+ break;
+ }