+ // Live game stored remotely: need socket to retrieve it
+ // NOTE: the callback "roomInit" will be lost, so it's not provided.
+ // --> It will be given when receiving "fullgame" socket event.
+ socketInit(() => { this.send("askfullgame"); });
+ });
+ },
+ roomInit: function() {
+ if (!this.roomInitialized) {
+ // Notify the room only now that I connected, because
+ // messages might be lost otherwise (if game loading is slow)
+ this.send("connect");
+ this.send("pollclients");
+ // We may ask fullgame several times if some moves are lost,
+ // but room should be init only once:
+ this.roomInitialized = true;
+ }
+ },
+ send: function(code, obj) {
+ if (!!this.conn && this.conn.readyState == 1)
+ this.conn.send(JSON.stringify(Object.assign({ code: code }, obj)));
+ },
+ isConnected: function(index) {
+ const player = this.game.players[index];
+ // Is it me ? In this case no need to bother with focus
+ if (
+ this.st.user.sid == player.sid ||
+ (!!player.name && this.st.user.id == player.id)
+ ) {
+ // Still have to check for name (because of potential multi-accounts
+ // on same browser, although this should be rare...)
+ return (!this.st.user.name || this.st.user.name == player.name);
+ }
+ // Try to find a match in people:
+ return (
+ (
+ !!player.sid &&
+ Object.keys(this.people).some(sid => {
+ return (
+ sid == player.sid &&
+ Object.values(this.people[sid].tmpIds).some(v => v.focus)
+ );
+ })
+ )
+ ||
+ (
+ player.id > 0 &&
+ Object.values(this.people).some(p => {
+ return (
+ p.id == player.id &&
+ Object.values(p.tmpIds).some(v => v.focus)
+ );
+ })
+ )
+ );
+ },
+ getOppsid: function() {
+ let oppsid = this.game.oppsid;
+ if (!oppsid) {
+ oppsid = Object.keys(this.people).find(
+ sid => this.people[sid].id == this.game.oppid
+ );
+ }
+ // oppsid is useful only if opponent is online:
+ if (!!oppsid && !!this.people[oppsid]) return oppsid;
+ return null;
+ },
+ // NOTE: action if provided is always a closing action
+ toggleChat: function(action) {
+ if (!action && document.getElementById("modalChat").checked)
+ // Entering chat
+ document.getElementById("inputChat").focus();
+ else {
+ document.getElementById("chatBtn").classList.remove("somethingnew");
+ if (!!this.game.mycolor) {
+ // Update "chatRead" variable either on server or locally
+ if (this.game.type == "corr")
+ this.updateCorrGame({ chatRead: this.game.mycolor });
+ else if (this.game.type == "live")
+ GameStorage.update(this.gameRef, { chatRead: true });
+ }
+ }
+ },
+ processChat: function(chat) {
+ this.send("newchat", { data: chat });
+ // NOTE: anonymous chats in corr games are not stored on server (TODO?)
+ if (!!this.game.mycolor) {
+ if (this.game.type == "corr")
+ this.updateCorrGame({ chat: chat });
+ else {
+ // Live game
+ chat.added = Date.now();
+ GameStorage.update(this.gameRef, { chat: chat });
+ }
+ }
+ },
+ clearChat: function() {
+ if (!!this.game.mycolor) {
+ if (this.game.type == "corr") {
+ ajax(
+ "/chats",
+ "DELETE",
+ { data: { gid: this.game.id } }
+ );
+ } else {
+ // Live game
+ GameStorage.update(this.gameRef, { delchat: true });
+ }
+ this.$set(this.game, "chats", []);
+ }
+ },
+ getGameType: function(game) {
+ if (!!game.id.toString().match(/^i/)) return "import";
+ return game.cadence.indexOf("d") >= 0 ? "corr" : "live";
+ },
+ // Notify something after a new move (to opponent and me on MyGames page)
+ notifyMyGames: function(thing, data) {
+ this.send(
+ "notify" + thing,