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