+ // Socket not ready yet (initial loading)
+ // NOTE: first arg is Websocket object, unused here:
+ this.conn.onopen = () => callback();
+ };
+ this.fetchGame((game) => {
+ if (!!game)
+ this.loadVariantThenGame(game, () => socketInit(this.roomInit));
+ else
+ // Live game stored remotely: need socket to retrieve it
+ // NOTE: the callback "roomInit" will be lost, so we don't provide it.
+ // --> It will be given when receiving "fullgame" socket event.
+ socketInit(() => { this.send("askfullgame"); });
+ });
+ },
+ cleanBeforeDestroy: function() {
+ if (!!this.askLastate)
+ clearInterval(this.askLastate);
+ if (!!this.retrySendmove)
+ clearInterval(this.retrySendmove);
+ if (!!this.clockUpdate)
+ clearInterval(this.clockUpdate);
+ this.send("disconnect");
+ },
+ 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.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 || 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 =>
+ sid == player.sid && this.people[sid].focus)
+ )
+ ||
+ (
+ !!player.id &&
+ Object.values(this.people).some(p =>
+ p.id == player.id && p.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;
+ },
+ toggleChat: function() {
+ if (document.getElementById("modalChat").checked)
+ // Entering chat
+ document.getElementById("inputChat").focus();
+ // TODO: next line is only required when exiting chat,
+ // but the event for now isn't well detected.
+ document.getElementById("chatBtn").classList.remove("somethingnew");
+ },
+ processChat: function(chat) {
+ this.send("newchat", { data: chat });
+ // NOTE: anonymous chats in corr games are not stored on server (TODO?)
+ if (this.game.type == "corr" && this.st.user.id > 0)
+ this.updateCorrGame({ chat: chat });
+ },
+ clearChat: function() {
+ // Nothing more to do if game is live (chats not recorded)
+ if (this.game.type == "corr") {
+ if (!!this.game.mycolor) {
+ ajax(
+ "/chats",
+ "DELETE",
+ { data: { gid: this.game.id } }
+ );
+ }
+ this.$set(this.game, "chats", []);
+ }
+ },
+ getGameType: function(game) {
+ 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,