Save state (nothing really achieved)
[vchess.git] / server / sockets.js
... / ...
CommitLineData
1const url = require('url');
2
3// Node version in Ubuntu 16.04 does not know about URL class
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("=");
10 result[item[0]] = decodeURIComponent(item[1]);
11 });
12 return result;
13}
14
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
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
31module.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 socket.on("message", objtxt => {
41 let obj = JSON.parse(objtxt);
42 if (!!obj.oppid && !clients[oppid])
43 return; //receiver not connected, nothing we can do
44 switch (obj.code)
45 {
46 case "askplayers":
47 socket.send(JSON.stringify({code:"room", players:clients}));
48 break;
49 case "askchallenges":
50 // TODO: ask directly to people (webRTC)
51 break;
52 case "askgames":
53 // TODO: ask directly to people (webRTC)
54 break;
55 case "newchat":
56 clients[obj.oppid].send(JSON.stringify({code:"newchat",msg:obj.msg}));
57 break;
58 // Transmit chats and moves to current room
59 // TODO: WebRTC instead in this case (most demanding?)
60 case "newmove":
61 clients[obj.oppid].send(JSON.stringify({code:"newmove",move:obj.move}));
62 break;
63 // TODO: generalize that for several opponents
64 case "ping":
65 socket.send(JSON.stringify({code:"pong",gameId:obj.gameId}));
66 break;
67 case "lastate":
68 const oppId = obj.oppid;
69 obj.oppid = sid; //I'm oppid for my opponent
70 clients[oppId].send(JSON.stringify(obj));
71 break;
72 // TODO: moreover, here, game info should be sent (through challenge; not stored here)
73 case "newgame":
74 clients[oppId].send(
75 JSON.stringify(
76 {code:"newgame",fen:fen,oppid:sid,color:"w",gameid:"TODO"}),
77 noop);
78 break;
79 case "cancelnewgame": //if a user cancel his seek
80 // TODO: just transmit event
81 //delete games[page];
82 break;
83 // TODO: also other challenge events
84 case "resign":
85 clients[obj.oppid].send(JSON.stringify({code:"resign"}));
86 break;
87 // TODO: case "challenge" (get ID) --> send to all, "acceptchallenge" (with ID) --> send to all, "cancelchallenge" --> send to all
88 // also, "sendgame" (give current game info, if any) --> to new connections, "sendchallenges" (same for challenges) --> to new connections
89 case "newchallenge":
90 console.log("challenge received");
91 console.log(obj.sender);
92 console.log(obj);
93 break;
94 }
95 });
96 socket.on("close", () => {
97 delete clients[sid];
98 // Notify every other connected client
99 Object.keys(clients).forEach( k => {
100 clients[k].send(JSON.stringify({code:"disconnect",sid:sid}));
101 });
102 });
103 });
104}