Fixes around game observing. TODO: abort/resign/draw + corr
[vchess.git] / server / sockets.js
1 const url = require('url');
2
3 // Node version in Ubuntu 16.04 does not know about URL class
4 function getJsonFromUrl(url)
5 {
6 const query = url.substr(2); //starts with "/?"
7 let result = {};
8 query.split("&").forEach((part) => {
9 const item = part.split("=");
10 result[item[0]] = decodeURIComponent(item[1]);
11 });
12 return result;
13 }
14
15 module.exports = function(wss) {
16 let clients = {}; //associative array sid --> socket
17 wss.on("connection", (socket, req) => {
18 const query = getJsonFromUrl(req.url);
19 const sid = query["sid"];
20 // TODO: later, allow duplicate connections (shouldn't be much more complicated)
21 if (!!clients[sid])
22 return socket.send(JSON.stringify({code:"duplicate"}));
23 clients[sid] = {sock: socket, page: query["page"]};
24 const notifyRoom = (page,code,obj={},excluded=[]) => {
25 Object.keys(clients).forEach(k => {
26 if (k in excluded)
27 return;
28 if (k != sid && clients[k].page == page)
29 {
30 clients[k].sock.send(JSON.stringify(Object.assign(
31 {code:code, from:sid}, obj)));
32 }
33 });
34 };
35 notifyRoom(query["page"], "connect"); //Hall or Game
36 if (query["page"].indexOf("/game/") >= 0)
37 notifyRoom("/", "connect"); //notify main hall
38 socket.on("message", objtxt => {
39 let obj = JSON.parse(objtxt);
40 if (!!obj.target && !clients[obj.target])
41 return; //receiver not connected, nothing we can do
42 switch (obj.code)
43 {
44 case "pollclients":
45 const curPage = clients[sid].page;
46 socket.send(JSON.stringify({code:"pollclients",
47 sockIds: Object.keys(clients).filter(k => k != sid &&
48 (clients[k].page == curPage ||
49 // Consider that people playing are in Hall too:
50 (curPage == "/" && clients[k].page.indexOf("/game/") >= 0))
51 )}));
52 break;
53 case "pagechange":
54 notifyRoom(clients[sid].page, "disconnect");
55 if (clients[sid].page.indexOf("/game/") >= 0)
56 notifyRoom("/", "disconnect");
57 clients[sid].page = obj.page;
58 notifyRoom(obj.page, "connect");
59 if (obj.page.indexOf("/game/") >= 0)
60 notifyRoom("/", "connect");
61 break;
62 case "askidentity":
63 clients[obj.target].sock.send(JSON.stringify(
64 {code:"askidentity",from:sid}));
65 break;
66 case "askchallenge":
67 clients[obj.target].sock.send(JSON.stringify(
68 {code:"askchallenge",from:sid}));
69 break;
70 case "askgames":
71 {
72 // Check all clients playing, and send them a "askgame" message
73 let gameSids = {}; //game ID --> [sid1, sid2]
74 const regexpGid = /\/[a-zA-Z0-9]+$/;
75 Object.keys(clients).forEach(k => {
76 if (k != sid && clients[k].page.indexOf("/game/") >= 0)
77 {
78 const gid = clients[k].page.match(regexpGid)[0];
79 if (!gameSids[gid])
80 gameSids[gid] = [k];
81 else
82 gameSids[gid].push(k);
83 }
84 });
85 // Request only one client out of 2 (TODO: this is a bit heavy)
86 // Alt: ask game to all, and filter later?
87 Object.keys(gameSids).forEach(gid => {
88 const L = gameSids[gid].length;
89 const idx = L > 1
90 ? Math.floor(Math.random() * Math.floor(L))
91 : 0;
92 const rid = gameSids[gid][idx];
93 clients[rid].sock.send(JSON.stringify(
94 {code:"askgame", from: sid}));
95 });
96 break;
97 }
98 case "askfullgame":
99 clients[obj.target].sock.send(JSON.stringify(
100 {code:"askfullgame", from:sid}));
101 break;
102 case "fullgame":
103 clients[obj.target].sock.send(JSON.stringify(
104 {code:"fullgame", game:obj.game}));
105 break;
106 case "identity":
107 clients[obj.target].sock.send(JSON.stringify(
108 {code:"identity",user:obj.user}));
109 break;
110 case "refusechallenge":
111 clients[obj.target].sock.send(JSON.stringify(
112 {code:"refusechallenge", cid:obj.cid, from:sid}));
113 break;
114 case "deletechallenge":
115 clients[obj.target].sock.send(JSON.stringify(
116 {code:"deletechallenge", cid:obj.cid, from:sid}));
117 break;
118 case "newgame":
119 clients[obj.target].sock.send(JSON.stringify(
120 {code:"newgame", gameInfo:obj.gameInfo, cid:obj.cid}));
121 break;
122 case "challenge":
123 clients[obj.target].sock.send(JSON.stringify(
124 {code:"challenge", chall:obj.chall, from:sid}));
125 break;
126 case "game":
127 if (!!obj.target)
128 {
129 clients[obj.target].sock.send(JSON.stringify(
130 {code:"game", game:obj.game, from:sid}));
131 }
132 else
133 {
134 // Notify all room except opponent and me:
135 notifyRoom("/", "game", {game:obj.game}, [obj.oppsid]);
136 }
137 break;
138 case "newchat":
139 // WARNING: do not use query["page"], because the page may change
140 notifyRoom(clients[sid].page, "newchat",
141 {msg: obj.msg, name: obj.name});
142 break;
143 // TODO: WebRTC instead in this case (most demanding?)
144 case "newmove":
145 clients[obj.target].sock.send(JSON.stringify(
146 {code:"newmove", move:obj.move}));
147 break;
148 case "lastate":
149 clients[obj.target].sock.send(JSON.stringify(
150 {code:"lastate", state:obj.state}));
151 break;
152 case "resign":
153 clients[obj.target].sock.send(JSON.stringify(
154 {code:"resign"}));
155 break;
156 case "abort":
157 clients[obj.target].sock.send(JSON.stringify(
158 {code:"abort",msg:obj.msg}));
159 break;
160 case "drawoffer":
161 clients[obj.target].sock.send(JSON.stringify(
162 {code:"drawoffer"}));
163 break;
164 case "draw":
165 clients[obj.target].sock.send(JSON.stringify(
166 {code:"draw"}));
167 break;
168 }
169 });
170 socket.on("close", () => {
171 const page = clients[sid].page;
172 delete clients[sid];
173 notifyRoom(page, "disconnect");
174 if (page.indexOf("/game/") >= 0)
175 notifyRoom("/", "disconnect"); //notify main hall
176 });
177 });
178 }