+ // Ask server for current corr games (all but mines)
+ ajax(
+ "/observedgames",
+ "GET",
+ {
+ data: {
+ uid: this.st.user.id,
+ cursor: this.cursor
+ },
+ success: (response) => {
+ const L = response.games.length;
+ if (L > 0) {
+ this.cursor = response.games[L - 1].created;
+ if (this.games.length == 0 && this.gdisplay == "live") {
+ document
+ .getElementById("btnGcorr")
+ .classList.add("somethingnew");
+ }
+ }
+ this.games = this.games.concat(
+ response.games.map(g => {
+ const vname = this.getVname(g.vid);
+ return Object.assign(
+ {},
+ g,
+ {
+ type: "corr",
+ vname: vname
+ }
+ );
+ })
+ );
+ }
+ }
+ );
+ // Also ask for corr challenges (open + sent by/to me)
+ ajax(
+ "/challenges",
+ "GET",
+ {
+ data: { uid: this.st.user.id },
+ success: (response) => {
+ if (
+ response.challenges.length > 0 &&
+ this.challenges.length == 0 &&
+ this.cdisplay == "live"
+ ) {
+ document
+ .getElementById("btnCcorr")
+ .classList.add("somethingnew");
+ }
+ // Gather all senders names, and then retrieve full identity:
+ // (TODO [perf]: some might be online...)
+ let names = {};
+ response.challenges.forEach(c => {
+ if (c.uid != this.st.user.id) names[c.uid] = "";
+ else if (!!c.target && c.target != this.st.user.id)
+ names[c.target] = "";
+ });
+ const addChallenges = () => {
+ names[this.st.user.id] = this.st.user.name; //in case of
+ this.challenges = this.challenges.concat(
+ response.challenges.map(c => {
+ const from = { name: names[c.uid], id: c.uid }; //or just name
+ const type = this.classifyObject(c);
+ const vname = this.getVname(c.vid);
+ return Object.assign(
+ {},
+ {
+ type: type,
+ vname: vname,
+ from: from,
+ to: c.target ? names[c.target] : ""
+ },
+ c
+ );
+ })
+ );
+ };
+ if (Object.keys(names).length > 0) {
+ ajax(
+ "/users",
+ "GET",
+ {
+ data: { ids: Object.keys(names).join(",") },
+ success: (response2) => {
+ response2.users.forEach(u => {
+ names[u.id] = u.name;
+ });
+ addChallenges();
+ }
+ }
+ );
+ } else addChallenges();
+ }
+ }
+ );