+
+ case "newmove": {
+ const dataWithFrom = {from: [sid,tmpId], data: obj.data};
+ // Special case re-send newmove only to opponent:
+ if (!!obj.target && !!clients[page][obj.target]) {
+ Object.keys(clients[page][obj.target]).forEach(x => {
+ send(
+ clients[page][obj.target][x],
+ Object.assign({code: "newmove"}, dataWithFrom)
+ );
+ });
+ } else {
+ // NOTE: data.from is useful only to opponent
+ notifyRoom(page, "newmove", dataWithFrom);
+ }
+ break;
+ }
+ case "gotmove":
+ if (
+ !!clients[page][obj.target[0]] &&
+ !!clients[page][obj.target[0]][obj.target[1]]
+ ) {
+ send(
+ clients[page][obj.target[0]][obj.target[1]],
+ {code: "gotmove", data: obj.data}
+ );
+ }
+ break;
+
+ case "result":
+ // Special case: notify all, 'transroom': Game --> Hall
+ notifyRoom("/", "result", {gid: obj.gid, score: obj.score});
+ break;
+
+ case "mconnect":
+ // Special case: notify some game rooms that
+ // I'm watching game state from MyGames
+ // TODO: this code is ignored for now
+ obj.gids.forEach(gid => {
+ const pg = "/game/" + gid;
+ Object.keys(clients[pg]).forEach(s => {
+ Object.keys(clients[pg][s]).forEach(x => {
+ send(
+ clients[pg][s][x],
+ {code: "mconnect", data: obj.data}
+ );
+ });
+ });
+ });
+ break;
+ case "mdisconnect":
+ // TODO
+ // Also TODO: pass newgame to MyGames, and gameover (result)
+ break;
+
+ // Passing, relaying something: from isn't needed,
+ // but target is fully identified (sid + tmpId)
+ case "challenge":
+ case "fullgame":
+ case "game":
+ case "identity":
+ case "lastate":
+ {
+ const pg = obj.target[2] || page; //required for identity and game
+ // NOTE: if in game we ask identity to opponent still in Hall,
+ // but leaving Hall, clients[pg] or clients[pg][target] could be undefined
+ if (!!clients[pg] && !!clients[pg][obj.target[0]])
+ send(clients[pg][obj.target[0]][obj.target[1]], {code:obj.code, data:obj.data});
+ break;
+ }
+ }
+ };
+ const closeListener = () => {
+ // For browser or tab closing (including page reload):
+ doDisconnect();
+ };
+ // Update clients object: add new connexion
+ if (!clients[page])
+ clients[page] = {[sid]: {[tmpId]: socket}};
+ else if (!clients[page][sid])
+ clients[page][sid] = {[tmpId]: socket};
+ else
+ clients[page][sid][tmpId] = socket;
+ socket.on("message", messageListener);
+ socket.on("close", closeListener);
+ });