- // TODO: relay (live) game to other player
- break;
- case "newchat":
- clients[obj.target].send(JSON.stringify({code:"newchat",msg:obj.msg}));
- break;
- // Transmit chats and moves to current room
- // TODO: WebRTC instead in this case (most demanding?)
- case "newmove":
- clients[obj.target].send(JSON.stringify({code:"newmove",move:obj.move}));
- break;
- // TODO: generalize that for several opponents
- case "ping":
- socket.send(JSON.stringify({code:"pong",gameId:obj.gameId}));
- break;
- case "lastate":
- const oppId = obj.target;
- obj.oppid = sid; //I'm the opponent of my opponent(s)
- clients[oppId].send(JSON.stringify(obj));
- break;
- // TODO: moreover, here, game info should be sent (through challenge; not stored here)
- case "newgame":
- clients[obj.target].send(JSON.stringify({code:"newgame", game:obj.game}));
- break;
- case "cancelnewgame": //if a user cancel his seek
- // TODO: just transmit event
- //delete games[page];
- break;
- // TODO: also other challenge events
- case "resign":
- clients[obj.target].send(JSON.stringify({code:"resign"}));
- break;
- // TODO: case "challenge" (get ID) --> send to all, "acceptchallenge" (with ID) --> send to all, "cancelchallenge" --> send to all
- // also, "sendgame" (give current game info, if any) --> to new connections, "sendchallenges" (same for challenges) --> to new connections
- }
- });
- socket.on("close", () => {
- delete clients[sid];
- // Notify every other connected client
- Object.keys(clients).forEach( k => {
- clients[k].send(JSON.stringify({code:"disconnect",sid:sid}));
- });
- });
- });
+ if (!!obj.target)
+ {
+ clients[obj.target].sock.send(JSON.stringify(
+ {code:"game", game:obj.game, from:sid}));
+ }
+ else
+ {
+ // Notify all room except opponent and me:
+ notifyRoom("/", "game", {game:obj.game}, [obj.oppsid]);
+ }
+ break;
+ case "newchat":
+ // WARNING: do not use query["page"], because the page may change
+ notifyRoom(clients[sid].page, "newchat",
+ {msg: obj.msg, name: obj.name});
+ break;
+ // TODO: WebRTC instead in this case (most demanding?)
+ case "newmove":
+ clients[obj.target].sock.send(JSON.stringify(
+ {code:"newmove", move:obj.move}));
+ break;
+ case "lastate":
+ clients[obj.target].sock.send(JSON.stringify(
+ {code:"lastate", state:obj.state}));
+ break;
+ case "resign":
+ clients[obj.target].sock.send(JSON.stringify(
+ {code:"resign"}));
+ break;
+ case "abort":
+ clients[obj.target].sock.send(JSON.stringify(
+ {code:"abort",msg:obj.msg}));
+ break;
+ case "drawoffer":
+ clients[obj.target].sock.send(JSON.stringify(
+ {code:"drawoffer"}));
+ break;
+ case "draw":
+ clients[obj.target].sock.send(JSON.stringify(
+ {code:"draw"}));
+ break;
+ }
+ });
+ socket.on("close", () => {
+ const page = clients[sid].page;
+ delete clients[sid];
+ notifyRoom(page, "disconnect");
+ if (page.indexOf("/game/") >= 0)
+ notifyRoom("/", "disconnect"); //notify main hall
+ });
+ });