Bug fix
[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 module.exports = function(wss) {
16 let clients = {}; //associative array sid --> socket
17 wss.on("connection", (socket, req) => {
18 const query = getJsonFromUrl(req.url);
19 const sid = query["sid"];
20 if (!!clients[sid])
21 return socket.send(JSON.stringify({code:"duplicate"}));
22 clients[sid] = {sock: socket, page: query["page"]};
23 const notifyRoom = (page,code,obj={},excluded=[]) => {
24 Object.keys(clients).forEach(k => {
25 if (k in excluded)
26 return;
27 if (k != sid && clients[k].page == page)
28 {
29 clients[k].sock.send(JSON.stringify(Object.assign(
30 {code:code, from:sid}, obj)));
31 }
32 });
33 };
34 // Wait for "connect" message to notify connection to the room,
35 // because if game loading is slow the message listener might
36 // not be ready too early.
37 socket.on("message", objtxt => {
38 let obj = JSON.parse(objtxt);
39 if (!!obj.target && !clients[obj.target])
40 return; //receiver not connected, nothing we can do
41 switch (obj.code)
42 {
43 case "connect":
44 {
45 const curPage = clients[sid].page;
46 notifyRoom(curPage, "connect"); //Hall or Game
47 if (curPage.indexOf("/game/") >= 0)
48 notifyRoom("/", "gconnect"); //notify main hall
49 break;
50 }
51 case "pollclients":
52 {
53 const curPage = clients[sid].page;
54 socket.send(JSON.stringify({code:"pollclients",
55 sockIds: Object.keys(clients).filter(k =>
56 k != sid && clients[k].page == curPage
57 )}));
58 break;
59 }
60 case "pollgamers":
61 socket.send(JSON.stringify({code:"pollgamers",
62 sockIds: Object.keys(clients).filter(k =>
63 k != sid && clients[k].page.indexOf("/game/") >= 0
64 )}));
65 break;
66 case "pagechange":
67 // page change clients[sid].page --> obj.page
68 notifyRoom(clients[sid].page, "disconnect");
69 if (clients[sid].page.indexOf("/game/") >= 0)
70 notifyRoom("/", "gdisconnect");
71 clients[sid].page = obj.page;
72 notifyRoom(obj.page, "connect");
73 if (obj.page.indexOf("/game/") >= 0)
74 notifyRoom("/", "gconnect");
75 break;
76 case "askidentity":
77 clients[obj.target].sock.send(JSON.stringify(
78 {code:"askidentity",from:sid}));
79 break;
80 case "askchallenge":
81 clients[obj.target].sock.send(JSON.stringify(
82 {code:"askchallenge",from:sid}));
83 break;
84 case "askgames":
85 {
86 // Check all clients playing, and send them a "askgame" message
87 let gameSids = {}; //game ID --> [sid1, sid2]
88 const regexpGid = /\/[a-zA-Z0-9]+$/;
89 Object.keys(clients).forEach(k => {
90 if (k != sid && clients[k].page.indexOf("/game/") >= 0)
91 {
92 const gid = clients[k].page.match(regexpGid)[0];
93 if (!gameSids[gid])
94 gameSids[gid] = [k];
95 else
96 gameSids[gid].push(k);
97 }
98 });
99 // Request only one client out of 2 (TODO: this is a bit heavy)
100 // Alt: ask game to all, and filter later?
101 Object.keys(gameSids).forEach(gid => {
102 const L = gameSids[gid].length;
103 const idx = L > 1
104 ? Math.floor(Math.random() * Math.floor(L))
105 : 0;
106 const rid = gameSids[gid][idx];
107 clients[rid].sock.send(JSON.stringify(
108 {code:"askgame", from: sid}));
109 });
110 break;
111 }
112 case "askfullgame":
113 clients[obj.target].sock.send(JSON.stringify(
114 {code:"askfullgame", from:sid}));
115 break;
116 case "fullgame":
117 clients[obj.target].sock.send(JSON.stringify(
118 {code:"fullgame", game:obj.game}));
119 break;
120 case "identity":
121 clients[obj.target].sock.send(JSON.stringify(
122 {code:"identity",user:obj.user}));
123 break;
124 case "refusechallenge":
125 clients[obj.target].sock.send(JSON.stringify(
126 {code:"refusechallenge", cid:obj.cid, from:sid}));
127 break;
128 case "deletechallenge":
129 clients[obj.target].sock.send(JSON.stringify(
130 {code:"deletechallenge", cid:obj.cid, from:sid}));
131 break;
132 case "newgame":
133 clients[obj.target].sock.send(JSON.stringify(
134 {code:"newgame", gameInfo:obj.gameInfo, cid:obj.cid}));
135 break;
136 case "challenge":
137 clients[obj.target].sock.send(JSON.stringify(
138 {code:"challenge", chall:obj.chall, from:sid}));
139 break;
140 case "game":
141 if (!!obj.target)
142 {
143 clients[obj.target].sock.send(JSON.stringify(
144 {code:"game", game:obj.game, from:sid}));
145 }
146 else
147 {
148 // Notify all room except opponent and me:
149 notifyRoom("/", "game", {game:obj.game}, [obj.oppsid]);
150 }
151 break;
152 case "newchat":
153 notifyRoom(clients[sid].page, "newchat", {chat:obj.chat});
154 break;
155 // TODO: WebRTC instead in this case (most demanding?)
156 // --> At least do a "notifyRoom"
157 case "newmove":
158 clients[obj.target].sock.send(JSON.stringify(
159 {code:"newmove", move:obj.move}));
160 break;
161 case "lastate":
162 clients[obj.target].sock.send(JSON.stringify(
163 {code:"lastate", state:obj.state}));
164 break;
165 case "resign":
166 clients[obj.target].sock.send(JSON.stringify(
167 {code:"resign", side:obj.side}));
168 break;
169 case "abort":
170 clients[obj.target].sock.send(JSON.stringify(
171 {code:"abort"}));
172 break;
173 case "drawoffer":
174 clients[obj.target].sock.send(JSON.stringify(
175 {code:"drawoffer"}));
176 break;
177 case "draw":
178 clients[obj.target].sock.send(JSON.stringify(
179 {code:"draw", message:obj.message}));
180 break;
181 }
182 });
183 socket.on("close", () => {
184 const page = clients[sid].page;
185 delete clients[sid];
186 notifyRoom(page, "disconnect");
187 if (page.indexOf("/game/") >= 0)
188 notifyRoom("/", "gdisconnect"); //notify main hall
189 });
190 });
191 }