- // TODO: ask directly to people (webRTC)
- break;
- case "newchat":
- clients[obj.oppid].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.oppid].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.oppid;
- obj.oppid = sid; //I'm oppid for my opponent
- clients[oppId].send(JSON.stringify(obj));
- break;
- // TODO: moreover, here, game info should be sent (through challenge; not stored here)
- case "newgame":
- clients[oppId].send(
- JSON.stringify(
- {code:"newgame",fen:fen,oppid:sid,color:"w",gameid:"TODO"}),
- noop);
- 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.oppid].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
- case "newchallenge":
- console.log("challenge received");
- console.log(obj.sender);
- console.log(obj);
- break;
- }
- });
- 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}));
- });
- });
- });
+ {
+ // Check all clients playing, and send them a "askgame" message
+ let gameSids = {}; //game ID --> [sid1, sid2]
+ const regexpGid = /\/[a-zA-Z0-9]+$/;
+ Object.keys(clients).forEach(k => {
+ if (k != sid && clients[k].page.indexOf("/game/") >= 0)
+ {
+ const gid = clients[k].page.match(regexpGid)[0];
+ if (!gameSids[gid])
+ gameSids[gid] = [k];
+ else
+ gameSids[gid].push(k);
+ }
+ });
+ // Request only one client out of 2 (TODO: this is a bit heavy)
+ // Alt: ask game to all, and filter later?
+ Object.keys(gameSids).forEach(gid => {
+ const L = gameSids[gid].length;
+ const idx = L > 1
+ ? Math.floor(Math.random() * Math.floor(L))
+ : 0;
+ const rid = gameSids[gid][idx];
+ clients[rid].sock.send(JSON.stringify(
+ {code:"askgame", from: sid}));
+ });
+ break;
+ }
+ case "askgame":
+ clients[obj.target].sock.send(JSON.stringify(
+ {code:"askgame", from:sid}));
+ break;
+ case "askfullgame":
+ clients[obj.target].sock.send(JSON.stringify(
+ {code:"askfullgame", from:sid}));
+ break;
+ case "fullgame":
+ clients[obj.target].sock.send(JSON.stringify(
+ {code:"fullgame", game:obj.game}));
+ break;
+ case "identity":
+ clients[obj.target].sock.send(JSON.stringify(
+ {code:"identity",user:obj.user}));
+ break;
+ case "refusechallenge":
+ clients[obj.target].sock.send(JSON.stringify(
+ {code:"refusechallenge", cid:obj.cid, from:sid}));
+ break;
+ case "deletechallenge":
+ clients[obj.target].sock.send(JSON.stringify(
+ {code:"deletechallenge", cid:obj.cid, from:sid}));
+ break;
+ case "newgame":
+ clients[obj.target].sock.send(JSON.stringify(
+ {code:"newgame", gameInfo:obj.gameInfo, cid:obj.cid}));
+ break;
+ case "challenge":
+ clients[obj.target].sock.send(JSON.stringify(
+ {code:"challenge", chall:obj.chall, from:sid}));
+ break;
+ case "game":
+ 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":
+ notifyRoom(clients[sid].page, "newchat", {chat:obj.chat});
+ break;
+ // TODO: WebRTC instead in this case (most demanding?)
+ // --> Or else: at least do a "notifyRoom" (also for draw, resign...)
+ 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", side:obj.side}));
+ break;
+ case "abort":
+ clients[obj.target].sock.send(JSON.stringify(
+ {code:"abort"}));
+ break;
+ case "drawoffer":
+ clients[obj.target].sock.send(JSON.stringify(
+ {code:"drawoffer"}));
+ break;
+ case "draw":
+ clients[obj.target].sock.send(JSON.stringify(
+ {code:"draw", message:obj.message}));
+ break;
+ }
+ };
+ const closeListener = () => {
+ const page = clients[sid].page;
+ delete clients[sid];
+ notifyRoom(page, "disconnect");
+ if (page.indexOf("/game/") >= 0)
+ notifyRoom("/", "gdisconnect"); //notify main hall
+ };
+ if (!!clients[sid])
+ {
+ // Turn off old sock through current client:
+ clients[sid].sock.send(JSON.stringify({code:"duplicate"}));
+ }
+ // Potentially replace current connection:
+ clients[sid] = {sock: socket, page: query["page"]};
+ socket.on("message", messageListener);
+ socket.on("close", closeListener);
+ });