Can create, send and accept challenge. Next step: targetted, launch game
[vchess.git] / server / sockets.js
1 const url = require('url');
2
3 // Node version in Ubuntu 16.04 does not know about URL class
4 function getJsonFromUrl(url)
5 {
6 const query = url.substr(2); //starts with "/?"
7 let result = {};
8 query.split("&").forEach((part) => {
9 const item = part.split("=");
10 result[item[0]] = decodeURIComponent(item[1]);
11 });
12 return result;
13 }
14
15 // Removal in array of strings (socket IDs)
16 function remInArray(arr, item)
17 {
18 const idx = arr.indexOf(item);
19 if (idx >= 0)
20 arr.splice(idx, 1);
21 }
22
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
25 // TODO: this file "in the end" would be much simpler, essentially just tracking connect/disconnect
26 // (everything else using WebRTC)
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
30
31 module.exports = function(wss) {
32 let clients = {}; //associative array sid --> socket
33 wss.on("connection", (socket, req) => {
34 const query = getJsonFromUrl(req.url);
35 const sid = query["sid"];
36 // Ignore duplicate connections (on the same live game that we play):
37 if (!!clients[sid])
38 return socket.send(JSON.stringify({code:"duplicate"}));
39 clients[sid] = socket;
40 // Notify room:
41 Object.keys(clients).forEach(k => {
42 if (k != sid)
43 clients[k].send(JSON.stringify({code:"connect",sid:sid}));
44 });
45 socket.on("message", objtxt => {
46 let obj = JSON.parse(objtxt);
47 if (!!obj.target && !clients[obj.target])
48 return; //receiver not connected, nothing we can do
49 //console.log(obj.code);
50 switch (obj.code)
51 {
52 case "pollclients":
53 socket.send(JSON.stringify({code:"pollclients",
54 sockIds:Object.keys(clients).filter(k => k != sid)}));
55 break;
56 case "askidentity":
57 clients[obj.target].send(
58 JSON.stringify({code:"askidentity",from:sid}));
59 break;
60 case "askchallenge":
61 clients[obj.target].send(
62 JSON.stringify({code:"askchallenge",from:sid}));
63 break;
64 case "askgame":
65 clients[obj.target].send(
66 JSON.stringify({code:"askgame",from:sid}));
67 break;
68 case "identity":
69 clients[obj.target].send(
70 JSON.stringify({code:"identity",user:obj.user}));
71 break;
72 case "challenge":
73 // Relay challenge to other player
74 clients[obj.target].send(
75 JSON.stringify({code:"challenge", chall:obj.chall, from:sid}));
76 break;
77 case "acceptchallenge":
78 clients[obj.target].send(
79 JSON.stringify({code:"acceptchallenge", cid:obj.cid, from:sid}));
80 break;
81 case "game":
82 // TODO: relay (live) game to other player
83 break;
84 case "newchat":
85 clients[obj.target].send(JSON.stringify({code:"newchat",msg:obj.msg}));
86 break;
87 // Transmit chats and moves to current room
88 // TODO: WebRTC instead in this case (most demanding?)
89 case "newmove":
90 clients[obj.target].send(JSON.stringify({code:"newmove",move:obj.move}));
91 break;
92 // TODO: generalize that for several opponents
93 case "ping":
94 socket.send(JSON.stringify({code:"pong",gameId:obj.gameId}));
95 break;
96 case "lastate":
97 const oppId = obj.target;
98 obj.oppid = sid; //I'm the opponent of my opponent(s)
99 clients[oppId].send(JSON.stringify(obj));
100 break;
101 // TODO: moreover, here, game info should be sent (through challenge; not stored here)
102 case "newgame":
103 clients[obj.target].send(JSON.stringify({code:"newgame", game:obj.game}));
104 break;
105 case "cancelnewgame": //if a user cancel his seek
106 // TODO: just transmit event
107 //delete games[page];
108 break;
109 // TODO: also other challenge events
110 case "resign":
111 clients[obj.target].send(JSON.stringify({code:"resign"}));
112 break;
113 // TODO: case "challenge" (get ID) --> send to all, "acceptchallenge" (with ID) --> send to all, "cancelchallenge" --> send to all
114 // also, "sendgame" (give current game info, if any) --> to new connections, "sendchallenges" (same for challenges) --> to new connections
115 }
116 });
117 socket.on("close", () => {
118 delete clients[sid];
119 // Notify every other connected client
120 Object.keys(clients).forEach( k => {
121 clients[k].send(JSON.stringify({code:"disconnect",sid:sid}));
122 });
123 });
124 });
125 }