+ const my = this.st.user;
+ this.people.push({sid:my.sid, id:my.id, name:my.name});
+ // Retrieve live challenge (not older than 30 minute) if any:
+ const chall = JSON.parse(localStorage.getItem("challenge") || "false");
+ if (!!chall)
+ {
+ if ((Date.now() - chall.added)/1000 <= 30*60)
+ this.challenges.push(chall);
+ else
+ localStorage.removeItem("challenge");
+ }
+ if (this.st.user.id > 0)
+ {
+ // Ask server for current corr games (all but mines)
+ ajax(
+ "/games",
+ "GET",
+ {uid: this.st.user.id, excluded: true},
+ response => {
+ this.games = this.games.concat(response.games.map(g => {
+ const type = this.classifyObject(g);
+ const vname = this.getVname(g.vid);
+ return Object.assign({}, g, {type: type, vname: vname});
+ }));
+ }
+ );
+ // Also ask for corr challenges (open + sent to me)
+ ajax(
+ "/challenges",
+ "GET",
+ {uid: this.st.user.id},
+ response => {
+ // Gather all senders names, and then retrieve full identity:
+ // (TODO [perf]: some might be online...)
+ const uids = response.challenges.map(c => { return c.uid });
+ ajax("/users",
+ "GET",
+ { ids: uids.join(",") },
+ response2 => {
+ let names = {};
+ response2.users.forEach(u => {names[u.id] = u.name});
+ this.challenges = this.challenges.concat(
+ response.challenges.map(c => {
+ // (just players names in fact)
+ const from = {name: names[c.uid], id: c.uid};
+ const type = this.classifyObject(c);
+ const vname = this.getVname(c.vid);
+ return Object.assign({}, c, {type: type, vname: vname, from: from});
+ })
+ )
+ }
+ );
+ }
+ );
+ // TODO: I don't like this code below; improvement?
+ let retryForVnames = setInterval(() => {
+ if (this.st.variants.length > 0) //variants array is loaded
+ {
+ if (this.games.length > 0 && this.games[0].vname == "")
+ {
+ // Fix games' vnames:
+ this.games.forEach(g => { g.vname = this.getVname(g.vid); });
+ }
+ if (this.challenges.length > 0 && this.challenges[0].vname == "")
+ {
+ // Fix challenges' vnames:
+ this.challenges.forEach(c => { c.vname = this.getVname(c.vid); });
+ }
+ clearInterval(retryForVnames);
+ }
+ }, 50);
+ }
+ // 0.1] Ask server for room composition:
+ const funcPollClients = () => {