+ // Helpers:
+ send: function(code, obj) {
+ if (!!this.conn)
+ {
+ this.conn.send(JSON.stringify(
+ Object.assign(
+ {code: code},
+ obj,
+ )
+ ));
+ }
+ },
+ 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 : "");
+ },
+ 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
+ return (o.cadence.indexOf('d') === -1 ? "live" : "corr");
+ },
+ setDisplay: function(letter, type, e) {
+ this[letter + "display"] = type;
+ let elt = !!e
+ ? e.target
+ : document.getElementById("btn" + letter.toUpperCase() + type);
+ elt.classList.add("active");
+ if (!!elt.previousElementSibling)
+ elt.previousElementSibling.classList.remove("active");
+ else
+ elt.nextElementSibling.classList.remove("active");
+ },
+ isGamer: function(sid) {
+ return this.people[sid].pages.some(p => p.indexOf("/game/") >= 0);
+ },
+ showPlayerActionBtn: function(sid) {
+ // Do not show action btn if I'm anonymous and target isn't playing,
+ // or target is anonymous and not playing,
+ // or target is me and I don't play anywhere.
+ const targetIsGamer = this.isGamer(sid);
+ if ((!this.st.user.name && !targetIsGamer) ||
+ (!this.people[sid].name && !this.isGamer(this.st.user.sid)) ||
+ (sid == this.st.user.sid && !targetIsGamer))
+ {
+ return false;
+ }
+ return true;
+ },
+ getActionLabel: function(sid) {
+ return this.people[sid].pages.some(p => p == "/")
+ ? "Challenge"
+ : "Observe";
+ },
+ challOrWatch: function(sid) {
+ if (this.people[sid].pages.some(p => p == "/"))
+ {
+ // Available, in Hall
+ this.newchallenge.to = this.people[sid].name;
+ doClick("modalNewgame");
+ }
+ else
+ {
+ // In some game, maybe playing maybe not: show a random one
+ let gids = [];
+ this.people[sid].pages.forEach(p => {
+ const matchGid = p.match(/[a-zA-Z0-9]+$/);
+ if (!!matchGid)
+ gids.push(matchGid[0]);
+ });
+ const gid = gids[Math.floor(Math.random() * gids.length)];
+ this.showGame(this.games.find(g => g.id == gid));
+ }
+ },
+ 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.rids[Math.floor(Math.random() * g.rids.length)];
+ this.$router.push(url);
+ },
+ processChat: function(chat) {
+ this.send("newchat", {data:chat});
+ },
+ // Messaging center: