Save state: draft of askgame logic but some bugs for now
[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 // url: /game/XYZ/?sid=XYZ
7 const queryParts = url.split("?");
8 let result = {page: queryParts[0]};
9 queryParts[1].split("&").forEach((part) => {
10 const item = part.split("=");
11 result[item[0]] = decodeURIComponent(item[1]);
12 });
13 return result;
14 }
15
16 module.exports = function(wss) {
17 let clients = {}; //associative array sid --> socket
18 wss.on("connection", (socket, req) => {
19 const query = getJsonFromUrl(req.url);
20 const sid = query["sid"];
21 // TODO: later, allow duplicate connections (shouldn't be much more complicated)
22 if (!!clients[sid])
23 return socket.send(JSON.stringify({code:"duplicate"}));
24 clients[sid] = {sock: socket, page: query["page"]};
25 const notifyRoom = (page,code,obj={},excluded=[]) => {
26 Object.keys(clients).forEach(k => {
27 if (k in excluded)
28 return;
29 if (k != sid && clients[k].page == page)
30 {
31 clients[k].sock.send(JSON.stringify(Object.assign(
32 {code:code, from:sid}, obj)));
33 }
34 });
35 };
36 notifyRoom(query["page"], "connect"); //Hall or Game
37 socket.on("message", objtxt => {
38 let obj = JSON.parse(objtxt);
39 if (!!obj.target && !clients[obj.target])
40 return; //receiver not connected, nothing we can do
41
42 console.log(obj.code);
43 console.log(clients);
44
45 switch (obj.code)
46 {
47 case "pollclients":
48 const curPage = clients[sid].page;
49 socket.send(JSON.stringify({code:"pollclients",
50 sockIds: Object.keys(clients).filter(k => k != sid &&
51 (clients[k].page == curPage ||
52 // Consider that people playing are in Hall too:
53 (curPage == "/" && clients[k].page.indexOf("/game/") >= 0))
54 )}));
55 break;
56 case "pagechange":
57 notifyRoom(clients[sid].page, "disconnect");
58 clients[sid].page = obj.page;
59 notifyRoom(obj.page, "connect");
60 break;
61 case "askidentity":
62 clients[obj.target].sock.send(JSON.stringify(
63 {code:"askidentity",from:sid}));
64 break;
65 case "askchallenge":
66 clients[obj.target].sock.send(JSON.stringify(
67 {code:"askchallenge",from:sid}));
68 break;
69 case "askgame":
70 // Check all clients playing, and send them a "askgame" message
71 Object.keys(clients).forEach(k => {
72 if (k != sid && clients[k].page.indexOf("/game/") >= 0)
73 {
74 clients[k].sock.send(JSON.stringify(
75 {code:"askgame", from: sid}));
76 }
77 });
78 clients[obj.target].sock.send(JSON.stringify(
79 {code:"askgame",from:sid}));
80 break;
81 case "identity":
82 clients[obj.target].sock.send(JSON.stringify(
83 {code:"identity",user:obj.user}));
84 break;
85 case "refusechallenge":
86 clients[obj.target].sock.send(JSON.stringify(
87 {code:"refusechallenge", cid:obj.cid, from:sid}));
88 break;
89 case "deletechallenge":
90 clients[obj.target].sock.send(JSON.stringify(
91 {code:"deletechallenge", cid:obj.cid, from:sid}));
92 break;
93 case "newgame":
94 clients[obj.target].sock.send(JSON.stringify(
95 {code:"newgame", gameInfo:obj.gameInfo, cid:obj.cid}));
96 break;
97 case "challenge":
98 clients[obj.target].sock.send(JSON.stringify(
99 {code:"challenge", chall:obj.chall, from:sid}));
100 break;
101 case "game":
102 if (!!obj.target)
103 {
104 clients[obj.target].sock.send(JSON.stringify(
105 {code:"game", game:data.game, from:sid}));
106 }
107 else
108 {
109 // Notify all room except opponent and me:
110 notifyRoom("/", "game", {game:data.game}, [data.oppsid]);
111 }
112 break;
113 case "newchat":
114 notifyRoom(query["page"], "newchat", {msg:obj.msg, name:obj.name});
115 break;
116 // TODO: WebRTC instead in this case (most demanding?)
117 case "newmove":
118 clients[obj.target].sock.send(JSON.stringify(
119 {code:"newmove", move:obj.move}));
120 break;
121 case "lastate":
122 clients[obj.target].sock.send(JSON.stringify(
123 {code:"lastate", state:obj.state}));
124 break;
125 case "resign":
126 clients[obj.target].sock.send(JSON.stringify(
127 {code:"resign"}));
128 break;
129 case "abort":
130 clients[obj.target].sock.send(JSON.stringify(
131 {code:"abort",msg:obj.msg}));
132 break;
133 case "drawoffer":
134 clients[obj.target].sock.send(JSON.stringify(
135 {code:"drawoffer"}));
136 break;
137 case "draw":
138 clients[obj.target].sock.send(JSON.stringify(
139 {code:"draw"}));
140 break;
141 }
142 });
143 socket.on("close", () => {
144 const page = clients[sid].page;
145 delete clients[sid];
146 notifyRoom(page, "disconnect");
147 });
148 });
149 }