+ elt.classList.remove("somethingnew"); //in case of
+ for (let t of ["live","corr","import"]) {
+ if (t != type)
+ document.getElementById(t + "Games").classList.remove("active");
+ }
+ },
+ addGameImport(game) {
+ game.type = "import";
+ ImportgameStorage.add(game, (err) => {
+ if (!!err) {
+ if (err.message.indexOf("Key already exists") < 0) {
+ alert(this.st.tr["An error occurred. Try again!"]);
+ return;
+ }
+ // NOTE: since a random new ID is generated for imported games,
+ // this error will not occur.
+ else alert(this.st.tr["The game was already imported"]);
+ }
+ this.$router.push("/game/" + game.id);
+ });
+ },
+ tryShowNewsIndicator: function(type) {
+ if (
+ (type == "live" && this.display != "live") ||
+ (type == "corr" && this.display != "corr")
+ ) {
+ document
+ .getElementById(type + "Games")
+ .classList.add("somethingnew");
+ }
+ },
+ // Called at loading to augment games with myColor + myTurn infos
+ decorate: function(games) {
+ games.forEach(g => {
+ g.myColor =
+ (g.type == "corr" && g.players[0].id == this.st.user.id) ||
+ (g.type == "live" && g.players[0].sid == this.st.user.sid)
+ ? 'w'
+ : 'b';
+ // If game is over, myTurn doesn't exist:
+ if (g.score == "*") {
+ const rem = g.movesCount % 2;
+ if ((rem == 0 && g.myColor == 'w') || (rem == 1 && g.myColor == 'b'))
+ g.myTurn = true;
+ }
+ });
+ },
+ socketMessageListener: function(msg) {
+ if (!this.conn) return;
+ const data = JSON.parse(msg.data);
+ // NOTE: no imported games here
+ let gamesArrays = {
+ "corr": this.corrGames,
+ "live": this.liveGames
+ };
+ switch (data.code) {
+ case "notifyturn":
+ case "notifyscore": {
+ const info = data.data;
+ const type = (!!parseInt(info.gid, 10) ? "corr" : "live");
+ let game = gamesArrays[type].find(g => g.id == info.gid);
+ // "notifything" --> "thing":
+ const thing = data.code.substr(6);
+ game[thing] = info[thing];
+ if (thing == "turn") {
+ game.myTurn = !game.myTurn;
+ if (game.myTurn) this.tryShowNewsIndicator(type);
+ }
+ else game.myTurn = false;
+ // TODO: forcing refresh like that is ugly and wrong.
+ // How to do it cleanly?
+ this.$refs[type + "games"].$forceUpdate();
+ break;
+ }
+ case "notifynewgame": {
+ const gameInfo = data.data;
+ // st.variants might be uninitialized,
+ // if unlucky and newgame right after connect:
+ const v = this.st.variants.find(v => v.id == gameInfo.vid);
+ const vname = !!v ? v.name : "";
+ const type = (gameInfo.cadence.indexOf('d') >= 0 ? "corr": "live");
+ let game = Object.assign(
+ {
+ vname: vname,
+ type: type,
+ score: "*",
+ created: Date.now()
+ },
+ gameInfo
+ );
+ game.myTurn =
+ (type == "corr" && game.players[0].id == this.st.user.id) ||
+ (type == "live" && game.players[0].sid == this.st.user.sid);
+ gamesArrays[type].push(game);
+ if (game.myTurn) this.tryShowNewsIndicator(type);
+ // TODO: cleaner refresh
+ this.$refs[type + "games"].$forceUpdate();
+ break;
+ }
+ }