+ // Always add myself to players' list
+ const my = this.st.user;
+ this.tempId = getRandString();
+ this.$set(this.people, my.sid, {id:my.id, name:my.name, tmpId: [this.tempId]});
+ // 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 by/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...)
+ let names = {};
+ response.challenges.forEach(c => {
+ if (c.uid != this.st.user.id)
+ names[c.uid] = ""; //unknwon for now
+ else if (!!c.target && c.target != this.st.user.id)
+ names[c.target] = "";
+ });
+ const addChallenges = (newChalls) => {
+ 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 (names !== {})
+ {
+ ajax("/users",
+ "GET",
+ { ids: Object.keys(names).join(",") },
+ response2 => {
+ response2.users.forEach(u => {names[u.id] = u.name});
+ addChallenges();
+ }
+ );
+ }
+ else
+ addChallenges();
+ }
+ );
+ // 0.1] Ask server for room composition:
+ const funcPollClients = () => {
+ this.conn.send(JSON.stringify({code:"connect"}));
+ this.conn.send(JSON.stringify({code:"pollclients"}));
+ this.conn.send(JSON.stringify({code:"pollgamers"}));
+ };
+ // Initialize connection
+ this.page = this.$route.path;
+ const connexionString = params.socketUrl +
+ "/?sid=" + this.st.user.sid +
+ "&tmpId=" + this.tempId +
+ "&page=" + encodeURIComponent(this.page);
+ this.conn = new WebSocket(connexionString);
+ this.conn.onopen = funcPollClients;
+ this.conn.onmessage = this.socketMessageListener;
+ const socketCloseListener = () => {
+ this.conn = new WebSocket(connexionString);
+ this.conn.addEventListener('message', this.socketMessageListener);
+ this.conn.addEventListener('close', socketCloseListener);
+ };
+ this.conn.onclose = socketCloseListener;
+ },
+ mounted: function() {
+ [document.getElementById("infoDiv"),document.getElementById("newgameDiv")]
+ .forEach(elt => elt.addEventListener("click", processModalClick));
+ document.querySelectorAll("#predefinedTimeControls > button").forEach(
+ (b) => { b.addEventListener("click",
+ () => { this.newchallenge.timeControl = b.innerHTML; }
+ )}
+ );
+ },
+ beforeDestroy: function() {
+ this.conn.send(JSON.stringify({code:"disconnect",page:this.page}));