11f7d8ec50a348364c73502709e45fa8e9da262b
[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 // NOTE: url is already transformed, without ?xxx=yyy... parts
5 function getJsonFromUrl(url)
6 {
7 const query = url.substr(2); //starts with "/?"
8 let result = {};
9 query.split("&").forEach((part) => {
10 const item = part.split("=");
11 result[item[0]] = decodeURIComponent(item[1]);
12 });
13 return result;
14 }
15
16 module.exports = function(wss) {
17 // Associative array sid --> tmpId --> {socket, page},
18 // "page" is either "/" for hall or "/game/some_gid" for Game,
19 // tmpId is required if a same user (browser) has different tabs
20 let clients = {};
21 wss.on("connection", (socket, req) => {
22 const query = getJsonFromUrl(req.url);
23 const sid = query["sid"];
24 const tmpId = query["tmpId"];
25 const notifyRoom = (page,code,obj={},excluded=[]) => {
26 Object.keys(clients).forEach(k => {
27 if (k in excluded)
28 return;
29 if (k != sid && clients[k].page == page)
30 {
31 clients[k].sock.send(JSON.stringify(Object.assign(
32 {code:code, from:[sid,tmpId]}, obj)));
33 }
34 });
35 };
36 const messageListener = (objtxt) => {
37 let obj = JSON.parse(objtxt);
38 if (!!obj.target && !clients[obj.target])
39 return; //receiver not connected, nothing we can do
40 switch (obj.code)
41 {
42 // Wait for "connect" message to notify connection to the room,
43 // because if game loading is slow the message listener might
44 // not be ready too early.
45 case "connect":
46 {
47 const curPage = clients[sid][tmpId].page;
48 notifyRoom(curPage, "connect"); //Hall or Game
49 if (curPage.indexOf("/game/") >= 0)
50 notifyRoom("/", "gconnect"); //notify main hall
51 break;
52 }
53 case "disconnect":
54 {
55 const oldPage = obj.page;
56 notifyRoom(oldPage, "disconnect"); //Hall or Game
57 if (oldPage.indexOf("/game/") >= 0)
58 notifyRoom("/", "gdisconnect"); //notify main hall
59 break;
60 }
61 case "pollclients":
62 {
63 const curPage = clients[sid][tmpId].page;
64 let sockIds = {}; //result, object sid ==> [tmpIds]
65 Object.keys(clients).forEach(k => {
66 Object.keys(clients[k]).forEach(x => {
67 if ((k != sid || x != tmpId)
68 && clients[k][x].page == curPage)
69 {
70 if (!sockIds[k])
71 sockIds[k] = [x];
72 else
73 sockIds[k].push(x);
74 }
75 });
76 });
77 socket.send(JSON.stringify({code:"pollclients", sockIds:sockIds}));
78 break;
79 }
80 case "pollgamers":
81 {
82 let sockIds = {};
83 Object.keys(clients).forEach(k => {
84 Object.keys(clients[k]).forEach(x => {
85 if ((k != sid || x != tmpId)
86 && clients[k][x].page.indexOf("/game/") >= 0)
87 {
88 if (!sockIds[k])
89 sockIds[k] = [x];
90 else
91 sockIds[k].push(x);
92 }
93 });
94 });
95 socket.send(JSON.stringify({code:"pollgamers", sockIds:sockIds}));
96 break;
97 }
98 case "askidentity":
99 {
100 // Identity only depends on sid, so select a tmpId at random
101 const tmpIds = Object.keys(clients[obj.target]);
102 const tmpId_idx = Math.floor(Math.random() * tmpIds.length);
103 clients[obj.target][tmpIds[tmpId_idx]].sock.send(JSON.stringify(
104 {code:"askidentity",from:[sid,tmpId]}));
105 break;
106 }
107 case "asklastate":
108 clients[obj.target[0]][obj.target[1]].sock.send(JSON.stringify(
109 {code:"asklastate",from:[sid,tmpId]}));
110 break;
111 case "askchallenge":
112 clients[obj.target[0]][obj.target[1]].sock.send(JSON.stringify(
113 {code:"askchallenge",from:[sid,tmpId]}));
114 break;
115 case "askgames":
116 {
117 // Check all clients playing, and send them a "askgame" message
118 // game ID --> [ sid1 --> array of tmpIds, sid2 --> array of tmpIds]
119 let gameSids = {};
120 const regexpGid = /\/[a-zA-Z0-9]+$/;
121 Object.keys(clients).forEach(k => {
122 Object.keys(clients[k]).forEach(x => {
123 if ((k != sid || x != tmpId)
124 && clients[k][x].page.indexOf("/game/") >= 0)
125 {
126 const gid = clients[k][x].page.match(regexpGid)[0];
127 if (!gameSids[gid])
128 gameSids[gid] = [{k: [x]}];
129 else if (k == Object.keys(gameSids[gid][0])[0])
130 gameSids[gid][0][k].push(x);
131 else if (gameSids[gid].length == 1)
132 gameSids[gid].push({k: [x]});
133 else
134 Object.values(gameSids[gid][1]).push(x);
135 }
136 });
137 // Request only one client out of 2 (TODO: this is a bit heavy)
138 // Alt: ask game to all, and filter later?
139 Object.keys(gameSids).forEach(gid => {
140 const L = gameSids[gid].length;
141 const sidIdx = L > 1
142 ? Math.floor(Math.random() * Math.floor(L))
143 : 0;
144 const tmpIdx = Object.values(gameSids[gid][sidIdx]
145 const rid = gameSids[gid][sidIdx][tmpIdx];
146 clients[sidIdx][tmpIdx].sock.send(JSON.stringify(
147 {code:"askgame", from: [sid,tmpId]}));
148 });
149 break;
150 }
151 case "askgame":
152 clients[obj.target].sock.send(JSON.stringify(
153 {code:"askgame", from:sid}));
154 break;
155 case "askfullgame":
156 clients[obj.target].sock.send(JSON.stringify(
157 {code:"askfullgame", from:sid}));
158 break;
159 case "fullgame":
160 clients[obj.target].sock.send(JSON.stringify(
161 {code:"fullgame", game:obj.game}));
162 break;
163 case "identity":
164 clients[obj.target].sock.send(JSON.stringify(
165 {code:"identity",user:obj.user}));
166 break;
167 case "refusechallenge":
168 clients[obj.target].sock.send(JSON.stringify(
169 {code:"refusechallenge", cid:obj.cid, from:sid}));
170 break;
171 case "deletechallenge":
172 clients[obj.target].sock.send(JSON.stringify(
173 {code:"deletechallenge", cid:obj.cid, from:sid}));
174 break;
175 case "newgame":
176 clients[obj.target].sock.send(JSON.stringify(
177 {code:"newgame", gameInfo:obj.gameInfo, cid:obj.cid}));
178 break;
179 case "challenge":
180 clients[obj.target].sock.send(JSON.stringify(
181 {code:"challenge", chall:obj.chall, from:sid}));
182 break;
183 case "game":
184 if (!!obj.target)
185 {
186 clients[obj.target].sock.send(JSON.stringify(
187 {code:"game", game:obj.game, from:sid}));
188 }
189 else
190 {
191 // Notify all room except opponent and me:
192 notifyRoom("/", "game", {game:obj.game}, [obj.oppsid]);
193 }
194 break;
195 case "newchat":
196 notifyRoom(clients[sid].page, "newchat", {chat:obj.chat});
197 break;
198 // TODO: WebRTC instead in this case (most demanding?)
199 // --> Or else: at least do a "notifyRoom" (also for draw, resign...)
200 case "newmove":
201 clients[obj.target].sock.send(JSON.stringify(
202 {code:"newmove", move:obj.move}));
203 break;
204 case "lastate":
205 clients[obj.target].sock.send(JSON.stringify(
206 {code:"lastate", state:obj.state}));
207 break;
208 case "resign":
209 clients[obj.target].sock.send(JSON.stringify(
210 {code:"resign", side:obj.side}));
211 break;
212 case "abort":
213 clients[obj.target].sock.send(JSON.stringify(
214 {code:"abort"}));
215 break;
216 case "drawoffer":
217 clients[obj.target].sock.send(JSON.stringify(
218 {code:"drawoffer"}));
219 break;
220 case "draw":
221 clients[obj.target].sock.send(JSON.stringify(
222 {code:"draw", message:obj.message}));
223 break;
224 }
225 };
226 const closeListener = () => {
227 delete clients[sid];
228 };
229 if (!!clients[sid])
230 {
231 // Turn off old sock through current client:
232 clients[sid].sock.send(JSON.stringify({code:"duplicate"}));
233 }
234 // Potentially replace current connection:
235 clients[sid] = {sock: socket, page: query["page"]};
236 socket.on("message", messageListener);
237 socket.on("close", closeListener);
238 });
239 }