- // TODO: how to know who is observing ? Send message to everyone with game ID ?
- // and then just listen to (dis)connect events
-
-
- // server always send "connect on " + URL ; then add to observers if game...
- // detect multiple tabs connected (when connect ask server if my SID is already in use)
-// router when access a game page tell to server I joined + game ID (no need rid)
-// and ask server for current joined (= observers)
-// when send to chat (or a move), reach only this group (send gid along)
-
- // --> doivent être enregistrés comme observers au niveau du serveur...
- // non: poll users + events startObserving / stopObserving
- // (à faire au niveau du routeur ?)
-
-
- // TODO: also handle "draw accepted" (use opponents array?)
- // --> must give this info also when sending lastState...
- // and, if all players agree then OK draw (end game ...etc)
- const socketMessageListener = msg => {
+ },
+ // NOTE: some redundant code with Hall.vue (mostly related to people array)
+ created: function() {
+ // Always add myself to players' list
+ const my = this.st.user;
+ this.$set(this.people, my.sid, { id: my.id, name: my.name });
+ this.gameRef.id = this.$route.params["id"];
+ this.gameRef.rid = this.$route.query["rid"]; //may be undefined
+ // Initialize connection
+ this.connexionString =
+ params.socketUrl +
+ "/?sid=" +
+ this.st.user.sid +
+ "&tmpId=" +
+ getRandString() +
+ "&page=" +
+ encodeURIComponent(this.$route.path);
+ this.conn = new WebSocket(this.connexionString);
+ this.conn.onmessage = this.socketMessageListener;
+ this.conn.onclose = this.socketCloseListener;
+ // Socket init required before loading remote game:
+ const socketInit = callback => {
+ if (!!this.conn && this.conn.readyState == 1)
+ // 1 == OPEN state
+ callback();
+ else
+ // Socket not ready yet (initial loading)
+ // NOTE: it's important to call callback without arguments,
+ // otherwise first arg is Websocket object and loadGame fails.
+ this.conn.onopen = () => callback();
+ };
+ if (!this.gameRef.rid)
+ // Game stored locally or on server
+ this.loadGame(null, () => socketInit(this.roomInit));
+ else
+ // 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.loadGame);
+ },
+ mounted: function() {
+ document
+ .getElementById("chatWrap")
+ .addEventListener("click", processModalClick);
+ },
+ beforeDestroy: function() {
+ this.send("disconnect");
+ },
+ methods: {
+ roomInit: function() {
+ // 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");
+ },
+ 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 ?
+ if (this.st.user.sid == player.sid || this.st.user.id == player.uid)
+ return true;
+ // Try to find a match in people:
+ return (
+ (
+ player.sid &&
+ Object.keys(this.people).some(sid => sid == player.sid)
+ )
+ ||
+ (
+ player.uid &&
+ Object.values(this.people).some(p => p.id == player.uid)
+ )
+ );
+ },
+ resetChatColor: function() {
+ // TODO: this is called twice, once on opening an once on closing
+ 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)
+ GameStorage.update(this.gameRef.id, { 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", {gid: this.game.id});
+ this.game.chats = [];
+ }
+ },
+ // Notify turn after a new move (to opponent and me on MyGames page)
+ notifyTurn: function(sid) {
+ const player = this.people[sid];
+ const colorIdx = this.game.players.findIndex(
+ p => p.sid == sid || p.id == player.id);
+ const color = ["w","b"][colorIdx];
+ const yourTurn =
+ (
+ color == "w" &&
+ this.game.movesCount % 2 == 0
+ )
+ ||
+ (
+ color == "b" &&
+ this.game.movesCount % 2 == 1
+ );
+ this.send("turnchange", { target: sid, yourTurn: yourTurn });
+ },
+ socketMessageListener: function(msg) {
+ if (!this.conn) return;