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