'update'
[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: lorsque challenge accepté, seul le dernier joueur à accepter envoi message "please start game"
26 // avec les coordonnées des participants. Le serveur renvoit alors les détails de la partie (couleurs, position)
27 //TODO: programmatic re-navigation on current game if we receive a move and are not there
28
29 module.exports = function(wss) {
30 let clients = {}; //associative array sid --> socket
31 wss.on("connection", (socket, req) => {
32 const query = getJsonFromUrl(req.url);
33 const sid = query["sid"];
34 // Ignore duplicate connections (on the same live game that we play):
35 if (!!clients[sid])
36 return socket.send(JSON.stringify({code:"duplicate"}));
37 clients[sid] = socket;
38 // Notify room:
39 Object.keys(clients).forEach(k => {
40 if (k != sid)
41 clients[k].send(JSON.stringify({code:"connect",sid:sid}));
42 });
43 socket.on("message", objtxt => {
44 let obj = JSON.parse(objtxt);
45 if (!!obj.target && !clients[obj.target])
46 return; //receiver not connected, nothing we can do
47 //console.log(obj.code);
48 switch (obj.code)
49 {
50 case "pollclients":
51 socket.send(JSON.stringify({code:"pollclients",
52 sockIds:Object.keys(clients).filter(k => k != sid)}));
53 break;
54 case "askidentity":
55 clients[obj.target].send(
56 JSON.stringify({code:"askidentity",from:sid}));
57 break;
58 case "askchallenge":
59 clients[obj.target].send(
60 JSON.stringify({code:"askchallenge",from:sid}));
61 break;
62 case "askgame":
63 clients[obj.target].send(
64 JSON.stringify({code:"askgame",from:sid}));
65 break;
66 case "identity":
67 clients[obj.target].send(
68 JSON.stringify({code:"identity",user:obj.user}));
69 break;
70 case "challenge":
71 clients[obj.target].send(
72 JSON.stringify({code:"challenge", chall:obj.chall, from:sid}));
73 break;
74 case "acceptchallenge":
75 clients[obj.target].send(
76 JSON.stringify({code:"acceptchallenge", cid:obj.cid, from:sid}));
77 break;
78 case "withdrawchallenge":
79 clients[obj.target].send(
80 JSON.stringify({code:"withdrawchallenge", cid:obj.cid, from:sid}));
81 break;
82 case "refusechallenge":
83 clients[obj.target].send(
84 JSON.stringify({code:"refusechallenge", cid:obj.cid, from:sid}));
85 break;
86 case "deletechallenge":
87 clients[obj.target].send(
88 JSON.stringify({code:"deletechallenge", cid:obj.cid, from:sid}));
89 break;
90 case "newgame":
91 clients[obj.target].send(JSON.stringify(
92 {code:"newgame", gameInfo:obj.gameInfo, cid:obj.cid}));
93 break;
94 case "game":
95 // TODO: relay (live) game to other player
96 break;
97 case "newchat":
98 clients[obj.target].send(JSON.stringify({code:"newchat",msg:obj.msg}));
99 break;
100 // Transmit chats and moves to current room
101 // TODO: WebRTC instead in this case (most demanding?)
102 case "newmove":
103 clients[obj.target].send(JSON.stringify({code:"newmove",move:obj.move}));
104 break;
105 // TODO: generalize that for several opponents
106 case "ping":
107 socket.send(JSON.stringify({code:"pong",gameId:obj.gameId}));
108 break;
109 case "lastate":
110 const oppId = obj.target;
111 obj.oppid = sid; //I'm the opponent of my opponent(s)
112 clients[oppId].send(JSON.stringify(obj));
113 break;
114 case "resign":
115 clients[obj.target].send(JSON.stringify({code:"resign"}));
116 break;
117 }
118 });
119 socket.on("close", () => {
120 delete clients[sid];
121 // Notify every other connected client
122 Object.keys(clients).forEach( k => {
123 clients[k].send(JSON.stringify({code:"disconnect",sid:sid}));
124 });
125 });
126 });
127 }