Small fixes
[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
BA
43 case "connect":
44 notifyRoom(query["page"], "connect"); //Hall or Game
45 if (query["page"].indexOf("/game/") >= 0)
46 notifyRoom("/", "connect"); //notify main hall
47 break;
81d9ce72 48 case "pollclients":
f41ce580 49 const curPage = clients[sid].page;
81d9ce72 50 socket.send(JSON.stringify({code:"pollclients",
c6788ecf
BA
51 sockIds: Object.keys(clients).filter(k => k != sid &&
52 (clients[k].page == curPage ||
53 // Consider that people playing are in Hall too:
54 (curPage == "/" && clients[k].page.indexOf("/game/") >= 0))
55 )}));
92a523d1
BA
56 break;
57 case "pagechange":
c6788ecf 58 notifyRoom(clients[sid].page, "disconnect");
bcaa8c00
BA
59 if (clients[sid].page.indexOf("/game/") >= 0)
60 notifyRoom("/", "disconnect");
92a523d1 61 clients[sid].page = obj.page;
c6788ecf 62 notifyRoom(obj.page, "connect");
bcaa8c00
BA
63 if (obj.page.indexOf("/game/") >= 0)
64 notifyRoom("/", "connect");
5a3da968
BA
65 break;
66 case "askidentity":
f41ce580
BA
67 clients[obj.target].sock.send(JSON.stringify(
68 {code:"askidentity",from:sid}));
81d9ce72 69 break;
dd75774d 70 case "askchallenge":
f41ce580
BA
71 clients[obj.target].sock.send(JSON.stringify(
72 {code:"askchallenge",from:sid}));
81d9ce72 73 break;
cd0d7743 74 case "askgames":
dc284d90 75 {
c6788ecf 76 // Check all clients playing, and send them a "askgame" message
dc284d90
BA
77 let gameSids = {}; //game ID --> [sid1, sid2]
78 const regexpGid = /\/[a-zA-Z0-9]+$/;
c6788ecf
BA
79 Object.keys(clients).forEach(k => {
80 if (k != sid && clients[k].page.indexOf("/game/") >= 0)
81 {
dc284d90
BA
82 const gid = clients[k].page.match(regexpGid)[0];
83 if (!gameSids[gid])
84 gameSids[gid] = [k];
85 else
86 gameSids[gid].push(k);
c6788ecf
BA
87 }
88 });
dc284d90
BA
89 // Request only one client out of 2 (TODO: this is a bit heavy)
90 // Alt: ask game to all, and filter later?
91 Object.keys(gameSids).forEach(gid => {
92 const L = gameSids[gid].length;
93 const idx = L > 1
94 ? Math.floor(Math.random() * Math.floor(L))
95 : 0;
96 const rid = gameSids[gid][idx];
97 clients[rid].sock.send(JSON.stringify(
ab6f48ea 98 {code:"askgame", from: sid}));
dc284d90
BA
99 });
100 break;
101 }
102 case "askfullgame":
103 clients[obj.target].sock.send(JSON.stringify(
104 {code:"askfullgame", from:sid}));
105 break;
106 case "fullgame":
107 clients[obj.target].sock.send(JSON.stringify(
108 {code:"fullgame", game:obj.game}));
5a3da968
BA
109 break;
110 case "identity":
f41ce580
BA
111 clients[obj.target].sock.send(JSON.stringify(
112 {code:"identity",user:obj.user}));
4d64881e 113 break;
5bd05dba 114 case "refusechallenge":
f41ce580
BA
115 clients[obj.target].sock.send(JSON.stringify(
116 {code:"refusechallenge", cid:obj.cid, from:sid}));
5bd05dba 117 break;
98f48579 118 case "deletechallenge":
f41ce580
BA
119 clients[obj.target].sock.send(JSON.stringify(
120 {code:"deletechallenge", cid:obj.cid, from:sid}));
98f48579 121 break;
a6bddfc6 122 case "newgame":
92a523d1 123 clients[obj.target].sock.send(JSON.stringify(
5bd05dba 124 {code:"newgame", gameInfo:obj.gameInfo, cid:obj.cid}));
a6bddfc6 125 break;
42c15a75 126 case "challenge":
92a523d1 127 clients[obj.target].sock.send(JSON.stringify(
42c15a75
BA
128 {code:"challenge", chall:obj.chall, from:sid}));
129 break;
dd75774d 130 case "game":
c6788ecf
BA
131 if (!!obj.target)
132 {
133 clients[obj.target].sock.send(JSON.stringify(
80ee5d5a 134 {code:"game", game:obj.game, from:sid}));
c6788ecf
BA
135 }
136 else
137 {
138 // Notify all room except opponent and me:
80ee5d5a 139 notifyRoom("/", "game", {game:obj.game}, [obj.oppsid]);
c6788ecf 140 }
4d64881e 141 break;
5bd05dba 142 case "newchat":
c97830ea
BA
143 // WARNING: do not use query["page"], because the page may change
144 notifyRoom(clients[sid].page, "newchat",
145 {msg: obj.msg, name: obj.name});
5bd05dba 146 break;
5bd05dba
BA
147 // TODO: WebRTC instead in this case (most demanding?)
148 case "newmove":
f41ce580 149 clients[obj.target].sock.send(JSON.stringify(
c6788ecf 150 {code:"newmove", move:obj.move}));
5bd05dba
BA
151 break;
152 case "lastate":
f41ce580
BA
153 clients[obj.target].sock.send(JSON.stringify(
154 {code:"lastate", state:obj.state}));
5bd05dba 155 break;
5bd05dba 156 case "resign":
f41ce580 157 clients[obj.target].sock.send(JSON.stringify(
3837d4f7 158 {code:"resign", side:obj.side}));
5bd05dba 159 break;
b988c726 160 case "abort":
f41ce580 161 clients[obj.target].sock.send(JSON.stringify(
3837d4f7 162 {code:"abort"}));
b988c726 163 break;
2cc10cdb 164 case "drawoffer":
f41ce580
BA
165 clients[obj.target].sock.send(JSON.stringify(
166 {code:"drawoffer"}));
2cc10cdb
BA
167 break;
168 case "draw":
f41ce580 169 clients[obj.target].sock.send(JSON.stringify(
dcd68c41 170 {code:"draw", message:obj.message}));
2cc10cdb 171 break;
5bd05dba
BA
172 }
173 });
174 socket.on("close", () => {
92a523d1 175 const page = clients[sid].page;
5bd05dba 176 delete clients[sid];
92a523d1 177 notifyRoom(page, "disconnect");
bcaa8c00
BA
178 if (page.indexOf("/game/") >= 0)
179 notifyRoom("/", "disconnect"); //notify main hall
5bd05dba
BA
180 });
181 });
1d184b4c 182}