Fix email formatting (I think)
[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
a0c41e7e 4// NOTE: url is already transformed, without ?xxx=yyy... parts
98db2082
BA
5function getJsonFromUrl(url)
6{
80ee5d5a
BA
7 const query = url.substr(2); //starts with "/?"
8 let result = {};
9 query.split("&").forEach((part) => {
5bd05dba
BA
10 const item = part.split("=");
11 result[item[0]] = decodeURIComponent(item[1]);
12 });
13 return result;
2807f530
BA
14}
15
1d184b4c 16module.exports = function(wss) {
5bd05dba
BA
17 let clients = {}; //associative array sid --> socket
18 wss.on("connection", (socket, req) => {
19 const query = getJsonFromUrl(req.url);
20 const sid = query["sid"];
5bd05dba 21 if (!!clients[sid])
a0c41e7e
BA
22 {
23 // Dummy messages listener: just send "duplicate" event on anything
24 // ('connect' events for Hall and Game, 'askfullgame' for observers)
25 return socket.on("message", objtxt => {
26 if (["connect","askfullgame"].includes(JSON.parse(objtxt).code))
27 socket.send(JSON.stringify({code:"duplicate"}));
28 });
29 }
92a523d1 30 clients[sid] = {sock: socket, page: query["page"]};
c6788ecf 31 const notifyRoom = (page,code,obj={},excluded=[]) => {
92a523d1 32 Object.keys(clients).forEach(k => {
c6788ecf
BA
33 if (k in excluded)
34 return;
92a523d1 35 if (k != sid && clients[k].page == page)
5c8e044f
BA
36 {
37 clients[k].sock.send(JSON.stringify(Object.assign(
c6788ecf 38 {code:code, from:sid}, obj)));
5c8e044f 39 }
92a523d1
BA
40 });
41 };
41c80bb6
BA
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.
5a3da968 45 socket.on("message", objtxt => {
5bd05dba 46 let obj = JSON.parse(objtxt);
5a3da968 47 if (!!obj.target && !clients[obj.target])
b4d619d1 48 return; //receiver not connected, nothing we can do
5bd05dba
BA
49 switch (obj.code)
50 {
41c80bb6 51 case "connect":
120fe373
BA
52 {
53 const curPage = clients[sid].page;
54 notifyRoom(curPage, "connect"); //Hall or Game
55 if (curPage.indexOf("/game/") >= 0)
ac8f441c 56 notifyRoom("/", "gconnect"); //notify main hall
41c80bb6 57 break;
120fe373 58 }
81d9ce72 59 case "pollclients":
ac8f441c 60 {
f41ce580 61 const curPage = clients[sid].page;
81d9ce72 62 socket.send(JSON.stringify({code:"pollclients",
ac8f441c
BA
63 sockIds: Object.keys(clients).filter(k =>
64 k != sid && clients[k].page == curPage
c6788ecf 65 )}));
92a523d1 66 break;
ac8f441c
BA
67 }
68 case "pollgamers":
ac8f441c
BA
69 socket.send(JSON.stringify({code:"pollgamers",
70 sockIds: Object.keys(clients).filter(k =>
71 k != sid && clients[k].page.indexOf("/game/") >= 0
72 )}));
73 break;
92a523d1 74 case "pagechange":
9335d45b 75 // page change clients[sid].page --> obj.page
a0c41e7e 76 // TODO: some offline rooms don't need to receive disconnect event
c6788ecf 77 notifyRoom(clients[sid].page, "disconnect");
bcaa8c00 78 if (clients[sid].page.indexOf("/game/") >= 0)
ac8f441c 79 notifyRoom("/", "gdisconnect");
92a523d1 80 clients[sid].page = obj.page;
a0c41e7e
BA
81 // No need to notify connection: it's self-sent in .vue file
82 //notifyRoom(obj.page, "connect");
bcaa8c00 83 if (obj.page.indexOf("/game/") >= 0)
ac8f441c 84 notifyRoom("/", "gconnect");
5a3da968
BA
85 break;
86 case "askidentity":
f41ce580
BA
87 clients[obj.target].sock.send(JSON.stringify(
88 {code:"askidentity",from:sid}));
81d9ce72 89 break;
a0c41e7e
BA
90 case "asklastate":
91 clients[obj.target].sock.send(JSON.stringify(
92 {code:"asklastate",from:sid}));
93 break;
dd75774d 94 case "askchallenge":
f41ce580
BA
95 clients[obj.target].sock.send(JSON.stringify(
96 {code:"askchallenge",from:sid}));
81d9ce72 97 break;
cd0d7743 98 case "askgames":
dc284d90 99 {
c6788ecf 100 // Check all clients playing, and send them a "askgame" message
dc284d90
BA
101 let gameSids = {}; //game ID --> [sid1, sid2]
102 const regexpGid = /\/[a-zA-Z0-9]+$/;
c6788ecf
BA
103 Object.keys(clients).forEach(k => {
104 if (k != sid && clients[k].page.indexOf("/game/") >= 0)
105 {
dc284d90
BA
106 const gid = clients[k].page.match(regexpGid)[0];
107 if (!gameSids[gid])
108 gameSids[gid] = [k];
109 else
110 gameSids[gid].push(k);
c6788ecf
BA
111 }
112 });
dc284d90
BA
113 // Request only one client out of 2 (TODO: this is a bit heavy)
114 // Alt: ask game to all, and filter later?
115 Object.keys(gameSids).forEach(gid => {
116 const L = gameSids[gid].length;
117 const idx = L > 1
118 ? Math.floor(Math.random() * Math.floor(L))
119 : 0;
120 const rid = gameSids[gid][idx];
121 clients[rid].sock.send(JSON.stringify(
ab6f48ea 122 {code:"askgame", from: sid}));
dc284d90
BA
123 });
124 break;
125 }
26f3a887
BA
126 case "askgame":
127 clients[obj.target].sock.send(JSON.stringify(
128 {code:"askgame", from:sid}));
129 break;
dc284d90
BA
130 case "askfullgame":
131 clients[obj.target].sock.send(JSON.stringify(
132 {code:"askfullgame", from:sid}));
133 break;
134 case "fullgame":
135 clients[obj.target].sock.send(JSON.stringify(
136 {code:"fullgame", game:obj.game}));
5a3da968
BA
137 break;
138 case "identity":
f41ce580
BA
139 clients[obj.target].sock.send(JSON.stringify(
140 {code:"identity",user:obj.user}));
4d64881e 141 break;
5bd05dba 142 case "refusechallenge":
f41ce580
BA
143 clients[obj.target].sock.send(JSON.stringify(
144 {code:"refusechallenge", cid:obj.cid, from:sid}));
5bd05dba 145 break;
98f48579 146 case "deletechallenge":
f41ce580
BA
147 clients[obj.target].sock.send(JSON.stringify(
148 {code:"deletechallenge", cid:obj.cid, from:sid}));
98f48579 149 break;
a6bddfc6 150 case "newgame":
92a523d1 151 clients[obj.target].sock.send(JSON.stringify(
5bd05dba 152 {code:"newgame", gameInfo:obj.gameInfo, cid:obj.cid}));
a6bddfc6 153 break;
42c15a75 154 case "challenge":
92a523d1 155 clients[obj.target].sock.send(JSON.stringify(
42c15a75
BA
156 {code:"challenge", chall:obj.chall, from:sid}));
157 break;
dd75774d 158 case "game":
c6788ecf
BA
159 if (!!obj.target)
160 {
161 clients[obj.target].sock.send(JSON.stringify(
80ee5d5a 162 {code:"game", game:obj.game, from:sid}));
c6788ecf
BA
163 }
164 else
165 {
166 // Notify all room except opponent and me:
80ee5d5a 167 notifyRoom("/", "game", {game:obj.game}, [obj.oppsid]);
c6788ecf 168 }
4d64881e 169 break;
5bd05dba 170 case "newchat":
ac8f441c 171 notifyRoom(clients[sid].page, "newchat", {chat:obj.chat});
5bd05dba 172 break;
5bd05dba 173 // TODO: WebRTC instead in this case (most demanding?)
26f3a887 174 // --> Or else: at least do a "notifyRoom" (also for draw, resign...)
5bd05dba 175 case "newmove":
f41ce580 176 clients[obj.target].sock.send(JSON.stringify(
c6788ecf 177 {code:"newmove", move:obj.move}));
5bd05dba
BA
178 break;
179 case "lastate":
f41ce580
BA
180 clients[obj.target].sock.send(JSON.stringify(
181 {code:"lastate", state:obj.state}));
5bd05dba 182 break;
5bd05dba 183 case "resign":
f41ce580 184 clients[obj.target].sock.send(JSON.stringify(
3837d4f7 185 {code:"resign", side:obj.side}));
5bd05dba 186 break;
b988c726 187 case "abort":
f41ce580 188 clients[obj.target].sock.send(JSON.stringify(
3837d4f7 189 {code:"abort"}));
b988c726 190 break;
2cc10cdb 191 case "drawoffer":
f41ce580
BA
192 clients[obj.target].sock.send(JSON.stringify(
193 {code:"drawoffer"}));
2cc10cdb
BA
194 break;
195 case "draw":
f41ce580 196 clients[obj.target].sock.send(JSON.stringify(
dcd68c41 197 {code:"draw", message:obj.message}));
2cc10cdb 198 break;
5bd05dba
BA
199 }
200 });
201 socket.on("close", () => {
92a523d1 202 const page = clients[sid].page;
5bd05dba 203 delete clients[sid];
92a523d1 204 notifyRoom(page, "disconnect");
bcaa8c00 205 if (page.indexOf("/game/") >= 0)
ac8f441c 206 notifyRoom("/", "gdisconnect"); //notify main hall
5bd05dba
BA
207 });
208 });
1d184b4c 209}