Simplify 'to' in newchallenge on hall view
[vchess.git] / server / sockets.js
CommitLineData
a29d9d6b 1const url = require('url');
1d184b4c 2
2807f530 3// Node version in Ubuntu 16.04 does not know about URL class
98db2082
BA
4function getJsonFromUrl(url)
5{
6 const query = url.substr(2); //starts with "/?"
7 let result = {};
8 query.split("&").forEach((part) => {
9 const item = part.split("=");
2807f530
BA
10 result[item[0]] = decodeURIComponent(item[1]);
11 });
12 return result;
13}
14
b644ef7f
BA
15// Removal in array of strings (socket IDs)
16function remInArray(arr, item)
17{
18 const idx = arr.indexOf(item);
19 if (idx >= 0)
20 arr.splice(idx, 1);
21}
22
b57dbd12
BA
23// TODO: empêcher multi-log du même user (envoyer le user ID + secret en même temps que name et...)
24// --> si secret ne matche pas celui trouvé en DB, stop
b644ef7f 25// TODO: this file "in the end" would be much simpler, essentially just tracking connect/disconnect
4608eed9 26// (everything else using WebRTC)
b644ef7f
BA
27// TODO: lorsque challenge accepté, seul le dernier joueur à accepter envoi message "please start game"
28// avec les coordonnées des participants. Le serveur renvoit alors les détails de la partie (couleurs, position)
29//TODO: programmatic re-navigation on current game if we receive a move and are not there
4608eed9 30
1d184b4c 31module.exports = function(wss) {
b4d619d1 32 let clients = {}; //associative array client sid --> socket
b644ef7f
BA
33 // No-op function as a callback when sending messages
34 const noop = () => { };
35 wss.on("connection", (socket, req) => {
36 const query = getJsonFromUrl(req.url);
37 const sid = query["sid"];
38 // Ignore duplicate connections (on the same live game that we play):
39 if (!!clients[sid])
40 return socket.send(JSON.stringify({code:"duplicate"}));
b4d619d1
BA
41 clients[sid] = socket;
42 socket.on("message", objtxt => {
43 let obj = JSON.parse(objtxt);
44 if (!!obj.oppid && !clients[oppid])
45 return; //receiver not connected, nothing we can do
46 switch (obj.code)
47 {
48 // Transmit chats and moves to current room
49 // TODO: WebRTC instead in this case (most demanding?)
50 case "newchat":
51 clients[obj.oppid].send(JSON.stringify({code:"newchat",msg:obj.msg}), noop);
52 break;
53 case "newmove":
54 clients[obj.oppid].send(JSON.stringify({code:"newmove",move:obj.move}), noop);
55 break;
56 // TODO: generalize that for several opponents
57 case "ping":
58 socket.send(JSON.stringify({code:"pong",gameId:obj.gameId}));
59 break;
60 case "lastate":
61 const oppId = obj.oppid;
62 obj.oppid = sid; //I'm oppid for my opponent
63 clients[oppId].send(JSON.stringify(obj), noop);
64 break;
65 // TODO: moreover, here, game info should be sent (through challenge; not stored here)
66 case "newgame":
67 clients[oppId].send(
68 JSON.stringify(
69 {code:"newgame",fen:fen,oppid:sid,color:"w",gameid:"TODO"}),
70 noop);
71 break;
72 case "cancelnewgame": //if a user cancel his seek
73 // TODO: just transmit event
74 //delete games[page];
75 break;
76 // TODO: also other challenge events
77 case "resign":
78 clients[obj.oppid].send(JSON.stringify({code:"resign"}), noop);
79 break;
80 // TODO: case "challenge" (get ID) --> send to all, "acceptchallenge" (with ID) --> send to all, "cancelchallenge" --> send to all
81 // also, "sendgame" (give current game info, if any) --> to new connections, "sendchallenges" (same for challenges) --> to new connections
82 case "newchallenge":
83 console.log("challenge received");
84 console.log(obj.sender);
85 console.log(obj);
86 break;
87 }
88 });
89 socket.on("close", () => {
90 delete clients[sid];
91 // Notify every other connected client
92 Object.keys(clients).forEach( k => {
93 clients[k].send(JSON.stringify({code:"disconnect",sid:sid}), noop);
94 });
95 });
1d184b4c
BA
96 });
97}