+ // Helpers:
+ filterChallenges: function(type) {
+ return this.challenges.filter(c => c.type == type);
+ },
+ filterGames: function(type) {
+ return this.games.filter(g => g.type == type);
+ },
+ classifyObject: function(o) { //challenge or game
+ // Heuristic: should work for most cases... (TODO)
+ return (o.timeControl.indexOf('d') === -1 ? "live" : "corr");
+ },
+ showGame: function(g) {
+ // NOTE: we are an observer, since only games I don't play are shown here
+ // ==> Moves sent by connected remote player(s) if live game
+ let url = "/game/" + g.id;
+ if (g.type == "live")
+ url += "?rid=" + g.rid;
+ this.$router.push(url);
+ },
+ getVname: function(vid) {
+ const variant = this.st.variants.find(v => v.id == vid);
+ // this.st.variants might be uninitialized (variant == null)
+ return (!!variant ? variant.name : "");
+ },
+ whatPlayerDoes: function(p) {
+ if (this.games.some(g => g.type == "live"
+ && g.players.some(pl => pl.sid == p.sid)))
+ {
+ return "Playing";
+ }
+ return "Challenge"; //player is available
+ },
+ sendSomethingTo: function(to, code, obj, warnDisconnected) {
+ const doSend = (code, obj, sid) => {
+ this.st.conn.send(JSON.stringify(Object.assign(
+ {},
+ {code: code},
+ obj,
+ {target: sid}
+ )));
+ };
+ if (!!to)
+ {
+ // Challenge with targeted players
+ const targetSid =
+ Object.keys(this.people).find(sid => this.people[sid].name == to);
+ if (!targetSid)
+ {
+ if (!!warnDisconnected)
+ alert("Warning: " + pname + " is not connected");
+ }
+ else
+ doSend(code, obj, targetSid);
+ }
+ else
+ {
+ // Open challenge: send to all connected players (except us)
+ Object.keys(this.people).forEach(sid => {
+ if (sid != this.st.user.sid)
+ doSend(code, obj, sid);
+ });
+ }
+ },
+ // Messaging center: