Bug fix
[vchess.git] / server / sockets.js
CommitLineData
a29d9d6b 1const url = require('url');
1d184b4c 2
2807f530 3// Node version in Ubuntu 16.04 does not know about URL class
98db2082
BA
4function getJsonFromUrl(url)
5{
80ee5d5a
BA
6 const query = url.substr(2); //starts with "/?"
7 let result = {};
8 query.split("&").forEach((part) => {
5bd05dba
BA
9 const item = part.split("=");
10 result[item[0]] = decodeURIComponent(item[1]);
11 });
12 return result;
2807f530
BA
13}
14
1d184b4c 15module.exports = function(wss) {
5bd05dba
BA
16 let clients = {}; //associative array sid --> socket
17 wss.on("connection", (socket, req) => {
18 const query = getJsonFromUrl(req.url);
19 const sid = query["sid"];
5bd05dba
BA
20 if (!!clients[sid])
21 return socket.send(JSON.stringify({code:"duplicate"}));
92a523d1 22 clients[sid] = {sock: socket, page: query["page"]};
c6788ecf 23 const notifyRoom = (page,code,obj={},excluded=[]) => {
92a523d1 24 Object.keys(clients).forEach(k => {
c6788ecf
BA
25 if (k in excluded)
26 return;
92a523d1 27 if (k != sid && clients[k].page == page)
5c8e044f
BA
28 {
29 clients[k].sock.send(JSON.stringify(Object.assign(
c6788ecf 30 {code:code, from:sid}, obj)));
5c8e044f 31 }
92a523d1
BA
32 });
33 };
41c80bb6
BA
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.
5a3da968 37 socket.on("message", objtxt => {
5bd05dba 38 let obj = JSON.parse(objtxt);
5a3da968 39 if (!!obj.target && !clients[obj.target])
b4d619d1 40 return; //receiver not connected, nothing we can do
5bd05dba
BA
41 switch (obj.code)
42 {
41c80bb6 43 case "connect":
120fe373
BA
44 {
45 const curPage = clients[sid].page;
46 notifyRoom(curPage, "connect"); //Hall or Game
47 if (curPage.indexOf("/game/") >= 0)
ac8f441c 48 notifyRoom("/", "gconnect"); //notify main hall
41c80bb6 49 break;
120fe373 50 }
81d9ce72 51 case "pollclients":
ac8f441c 52 {
f41ce580 53 const curPage = clients[sid].page;
81d9ce72 54 socket.send(JSON.stringify({code:"pollclients",
ac8f441c
BA
55 sockIds: Object.keys(clients).filter(k =>
56 k != sid && clients[k].page == curPage
c6788ecf 57 )}));
92a523d1 58 break;
ac8f441c
BA
59 }
60 case "pollgamers":
ac8f441c
BA
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;
92a523d1 66 case "pagechange":
9335d45b 67 // page change clients[sid].page --> obj.page
c6788ecf 68 notifyRoom(clients[sid].page, "disconnect");
bcaa8c00 69 if (clients[sid].page.indexOf("/game/") >= 0)
ac8f441c 70 notifyRoom("/", "gdisconnect");
92a523d1 71 clients[sid].page = obj.page;
c6788ecf 72 notifyRoom(obj.page, "connect");
bcaa8c00 73 if (obj.page.indexOf("/game/") >= 0)
ac8f441c 74 notifyRoom("/", "gconnect");
5a3da968
BA
75 break;
76 case "askidentity":
f41ce580
BA
77 clients[obj.target].sock.send(JSON.stringify(
78 {code:"askidentity",from:sid}));
81d9ce72 79 break;
dd75774d 80 case "askchallenge":
f41ce580
BA
81 clients[obj.target].sock.send(JSON.stringify(
82 {code:"askchallenge",from:sid}));
81d9ce72 83 break;
cd0d7743 84 case "askgames":
dc284d90 85 {
c6788ecf 86 // Check all clients playing, and send them a "askgame" message
dc284d90
BA
87 let gameSids = {}; //game ID --> [sid1, sid2]
88 const regexpGid = /\/[a-zA-Z0-9]+$/;
c6788ecf
BA
89 Object.keys(clients).forEach(k => {
90 if (k != sid && clients[k].page.indexOf("/game/") >= 0)
91 {
dc284d90
BA
92 const gid = clients[k].page.match(regexpGid)[0];
93 if (!gameSids[gid])
94 gameSids[gid] = [k];
95 else
96 gameSids[gid].push(k);
c6788ecf
BA
97 }
98 });
dc284d90
BA
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(
ab6f48ea 108 {code:"askgame", from: sid}));
dc284d90
BA
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}));
5a3da968
BA
119 break;
120 case "identity":
f41ce580
BA
121 clients[obj.target].sock.send(JSON.stringify(
122 {code:"identity",user:obj.user}));
4d64881e 123 break;
5bd05dba 124 case "refusechallenge":
f41ce580
BA
125 clients[obj.target].sock.send(JSON.stringify(
126 {code:"refusechallenge", cid:obj.cid, from:sid}));
5bd05dba 127 break;
98f48579 128 case "deletechallenge":
f41ce580
BA
129 clients[obj.target].sock.send(JSON.stringify(
130 {code:"deletechallenge", cid:obj.cid, from:sid}));
98f48579 131 break;
a6bddfc6 132 case "newgame":
92a523d1 133 clients[obj.target].sock.send(JSON.stringify(
5bd05dba 134 {code:"newgame", gameInfo:obj.gameInfo, cid:obj.cid}));
a6bddfc6 135 break;
42c15a75 136 case "challenge":
92a523d1 137 clients[obj.target].sock.send(JSON.stringify(
42c15a75
BA
138 {code:"challenge", chall:obj.chall, from:sid}));
139 break;
dd75774d 140 case "game":
c6788ecf
BA
141 if (!!obj.target)
142 {
143 clients[obj.target].sock.send(JSON.stringify(
80ee5d5a 144 {code:"game", game:obj.game, from:sid}));
c6788ecf
BA
145 }
146 else
147 {
148 // Notify all room except opponent and me:
80ee5d5a 149 notifyRoom("/", "game", {game:obj.game}, [obj.oppsid]);
c6788ecf 150 }
4d64881e 151 break;
5bd05dba 152 case "newchat":
ac8f441c 153 notifyRoom(clients[sid].page, "newchat", {chat:obj.chat});
5bd05dba 154 break;
5bd05dba 155 // TODO: WebRTC instead in this case (most demanding?)
ac8f441c 156 // --> At least do a "notifyRoom"
5bd05dba 157 case "newmove":
f41ce580 158 clients[obj.target].sock.send(JSON.stringify(
c6788ecf 159 {code:"newmove", move:obj.move}));
5bd05dba
BA
160 break;
161 case "lastate":
f41ce580
BA
162 clients[obj.target].sock.send(JSON.stringify(
163 {code:"lastate", state:obj.state}));
5bd05dba 164 break;
5bd05dba 165 case "resign":
f41ce580 166 clients[obj.target].sock.send(JSON.stringify(
3837d4f7 167 {code:"resign", side:obj.side}));
5bd05dba 168 break;
b988c726 169 case "abort":
f41ce580 170 clients[obj.target].sock.send(JSON.stringify(
3837d4f7 171 {code:"abort"}));
b988c726 172 break;
2cc10cdb 173 case "drawoffer":
f41ce580
BA
174 clients[obj.target].sock.send(JSON.stringify(
175 {code:"drawoffer"}));
2cc10cdb
BA
176 break;
177 case "draw":
f41ce580 178 clients[obj.target].sock.send(JSON.stringify(
dcd68c41 179 {code:"draw", message:obj.message}));
2cc10cdb 180 break;
5bd05dba
BA
181 }
182 });
183 socket.on("close", () => {
92a523d1 184 const page = clients[sid].page;
5bd05dba 185 delete clients[sid];
92a523d1 186 notifyRoom(page, "disconnect");
bcaa8c00 187 if (page.indexOf("/game/") >= 0)
ac8f441c 188 notifyRoom("/", "gdisconnect"); //notify main hall
5bd05dba
BA
189 });
190 });
1d184b4c 191}