1 // Node version in Ubuntu 16.04 does not know about URL class
2 function getJsonFromUrl(url
) {
3 const query
= url
.substr(2); //starts with "/?"
5 query
.split("&").forEach((part
) => {
6 const item
= part
.split("=");
7 result
[item
[0]] = decodeURIComponent(item
[1]);
12 // Helper to safe-send some message through a (web-)socket:
13 function send(socket
, message
) {
14 if (!!socket
&& socket
.readyState
== 1)
15 socket
.send(JSON
.stringify(message
));
18 module
.exports = function(wss
) {
19 // Associative array page --> sid --> tmpId --> socket
20 // "page" is either "/" for hall or "/game/some_gid" for Game,
21 // or "/mygames" for Mygames page (simpler: no 'people' array).
22 // tmpId is required if a same user (browser) has different tabs
24 // NOTE: only purpose of sidToPages = know when to delete keys in idToSid
27 wss
.on("connection", (socket
, req
) => {
28 const query
= getJsonFromUrl(req
.url
);
29 const sid
= query
["sid"];
30 const id
= query
["id"];
31 const tmpId
= query
["tmpId"];
32 const page
= query
["page"];
33 const notifyRoom
= (page
, code
, obj
={}, except
) => {
34 if (!clients
[page
]) return;
35 except
= except
|| [];
36 Object
.keys(clients
[page
]).forEach(k
=> {
37 if (except
.includes(k
)) return;
38 Object
.keys(clients
[page
][k
]).forEach(x
=> {
39 if (k
== sid
&& x
== tmpId
) return;
41 clients
[page
][k
][x
].socket
,
42 Object
.assign({ code: code
, from: [sid
, tmpId
] }, obj
)
47 const deleteConnexion
= () => {
48 if (!clients
[page
] || !clients
[page
][sid
] || !clients
[page
][sid
][tmpId
])
49 return; //job already done
50 delete clients
[page
][sid
][tmpId
];
51 if (Object
.keys(clients
[page
][sid
]).length
== 0) {
52 delete clients
[page
][sid
];
53 const pgIndex
= sidToPages
[sid
].findIndex(pg
=> pg
== page
);
54 sidToPages
[sid
].splice(pgIndex
, 1);
55 if (Object
.keys(clients
[page
]).length
== 0)
57 // Am I totally offline?
58 if (sidToPages
[sid
].length
== 0) {
59 delete sidToPages
[sid
];
65 const doDisconnect
= () => {
67 // Nothing to notify when disconnecting from MyGames page:
68 if (page
!= "/mygames") {
69 notifyRoom(page
, "disconnect");
70 if (page
.indexOf("/game/") >= 0)
71 notifyRoom("/", "gdisconnect", { page: page
});
74 const messageListener
= (objtxt
) => {
75 let obj
= JSON
.parse(objtxt
);
77 // Wait for "connect" message to notify connection to the room,
78 // because if game loading is slow the message listener might
79 // not be ready too early.
81 notifyRoom(page
, "connect");
82 if (page
.indexOf("/game/") >= 0)
83 notifyRoom("/", "gconnect", { page: page
});
93 Object
.keys(clients
[page
]).forEach(k
=> {
95 Object
.keys(clients
[page
][k
]).forEach(x
=> {
96 // Avoid polling my tmpId: no information to get
97 if (k
!= sid
|| x
!= tmpId
)
98 sockIds
[k
][x
] = { focus: clients
[page
][k
][x
].focus
};
101 send(socket
, { code: "pollclients", sockIds: sockIds
});
104 case "pollclientsandgamers": {
107 Object
.keys(clients
["/"]).forEach(k
=> {
109 Object
.keys(clients
[page
][k
]).forEach(x
=> {
110 // Avoid polling my tmpId: no information to get
111 if (k
!= sid
|| x
!= tmpId
) {
114 focus: clients
[page
][k
][x
].focus
119 // NOTE: a "gamer" could also just be an observer
120 Object
.keys(clients
).forEach(p
=> {
121 if (p
.indexOf("/game/") >= 0) {
122 Object
.keys(clients
[p
]).forEach(k
=> {
123 if (!sockIds
[k
]) sockIds
[k
] = {};
124 Object
.keys(clients
[p
][k
]).forEach(x
=> {
125 if (k
!= sid
|| x
!= tmpId
) {
128 focus: clients
[p
][k
][x
].focus
135 send(socket
, { code: "pollclientsandgamers", sockIds: sockIds
});
139 // Asking something: from is fully identified,
140 // but the requested resource can be from any tmpId (except current!)
143 case "askchallenges":
145 const pg
= obj
.page
|| page
; //required for askidentity and askgame
146 if (!!clients
[pg
] && !!clients
[pg
][obj
.target
]) {
147 let tmpIds
= Object
.keys(clients
[pg
][obj
.target
]);
148 if (obj
.target
== sid
) {
150 const idx_myTmpid
= tmpIds
.findIndex(x
=> x
== tmpId
);
151 if (idx_myTmpid
>= 0) tmpIds
.splice(idx_myTmpid
, 1);
153 if (tmpIds
.length
> 0) {
154 const ttmpId
= tmpIds
[Math
.floor(Math
.random() * tmpIds
.length
)];
156 clients
[pg
][obj
.target
][ttmpId
].socket
,
157 { code: obj
.code
, from: [sid
,tmpId
,page
] }
164 // Special situation of the previous "case":
165 // Full game can be asked to any observer.
166 case "askfullgame": {
167 if (!!clients
[page
]) {
168 let sids
= Object
.keys(clients
[page
]).filter(k
=> k
!= sid
);
169 if (sids
.length
> 0) {
170 // Pick a SID at random in this set, and ask full game:
171 const rid
= sids
[Math
.floor(Math
.random() * sids
.length
)];
172 // ..to a random tmpId:
173 const tmpIds
= Object
.keys(clients
[page
][rid
]);
174 const rtmpId
= tmpIds
[Math
.floor(Math
.random() * tmpIds
.length
)];
176 clients
[page
][rid
][rtmpId
].socket
,
177 { code: "askfullgame", from: [sid
,tmpId
] }
180 // I'm the only person who have the game for the moment:
181 send(socket
, { code: "fullgame", data: { empty: true } });
187 // Some Hall events: target all tmpId's (except mine),
188 case "refusechallenge":
190 Object
.keys(clients
[page
][obj
.target
]).forEach(x
=> {
191 if (obj
.target
!= sid
|| x
!= tmpId
)
193 clients
[page
][obj
.target
][x
].socket
,
194 { code: obj
.code
, data: obj
.data
}
199 // Notify all room: mostly game events
202 case "deletechallenge_s":
209 // "newgame" message can provide a page (corr Game --> Hall)
211 obj
.page
|| page
, obj
.code
, {data: obj
.data
}, obj
.excluded
);
215 // A rematch game started:
216 notifyRoom(page
, "newgame", {data: obj
.data
});
217 // Explicitely notify Hall if gametype == corr.
218 // Live games will be polled from Hall after gconnect event.
219 if (obj
.data
.cadence
.indexOf('d') >= 0)
220 notifyRoom("/", "newgame", {data: obj
.data
});
224 const dataWithFrom
= { from: [sid
,tmpId
], data: obj
.data
};
225 // Special case re-send newmove only to opponent:
226 if (!!obj
.target
&& !!clients
[page
][obj
.target
]) {
227 Object
.keys(clients
[page
][obj
.target
]).forEach(x
=> {
229 clients
[page
][obj
.target
][x
].socket
,
230 Object
.assign({ code: "newmove" }, dataWithFrom
)
234 // NOTE: data.from is useful only to opponent
235 notifyRoom(page
, "newmove", dataWithFrom
);
241 !!clients
[page
][obj
.target
[0]] &&
242 !!clients
[page
][obj
.target
[0]][obj
.target
[1]]
245 clients
[page
][obj
.target
[0]][obj
.target
[1]].socket
,
252 // Special case: notify all, 'transroom': Game --> Hall
253 notifyRoom("/", "result", { gid: obj
.gid
, score: obj
.score
});
257 const gamePg
= "/game/" + obj
.gid
;
258 if (!!clients
[gamePg
] && !!clients
[gamePg
][obj
.target
]) {
259 Object
.keys(clients
[gamePg
][obj
.target
]).forEach(x
=> {
261 clients
[gamePg
][obj
.target
][x
].socket
,
271 case "notifynewgame":
272 if (!!clients
["/mygames"]) {
273 obj
.targets
.forEach(t
=> {
274 const k
= t
.sid
|| idToSid
[t
.id
];
275 if (!!clients
["/mygames"][k
]) {
276 Object
.keys(clients
["/mygames"][k
]).forEach(x
=> {
278 clients
["/mygames"][k
][x
].socket
,
279 { code: obj
.code
, data: obj
.data
}
291 !!clients
[page
][sid
] &&
292 !!clients
[page
][sid
][tmpId
]
294 clients
[page
][sid
][tmpId
].focus
= (obj
.code
== "getfocus");
296 if (page
== "/") notifyRoom("/", obj
.code
, { page: "/" }, [sid
]);
298 // Notify game room + Hall:
299 notifyRoom(page
, obj
.code
, {}, [sid
]);
300 notifyRoom("/", obj
.code
, { page: page
}, [sid
]);
304 // Passing, relaying something: from isn't needed,
305 // but target is fully identified (sid + tmpId)
312 const pg
= obj
.target
[2] || page
; //required for identity and game
313 // NOTE: if in game we ask identity to opponent still in Hall, but
314 // leaving Hall, clients[pg] or clients[pg][target] could be undef.
315 if (!!clients
[pg
] && !!clients
[pg
][obj
.target
[0]]) {
317 clients
[pg
][obj
.target
[0]][obj
.target
[1]].socket
,
318 { code:obj
.code
, data:obj
.data
}
325 const closeListener
= () => {
326 // For browser or tab closing (including page reload):
329 // Update clients object: add new connexion
330 const newElt
= { socket: socket
, focus: true };
332 clients
[page
] = { [sid
]: {[tmpId
]: newElt
} };
333 else if (!clients
[page
][sid
])
334 clients
[page
][sid
] = { [tmpId
]: newElt
};
336 clients
[page
][sid
][tmpId
] = newElt
;
337 // Also update helper correspondances
338 if (!idToSid
[id
]) idToSid
[id
] = sid
;
339 if (!sidToPages
[sid
]) sidToPages
[sid
] = [];
340 const pgIndex
= sidToPages
[sid
].findIndex(pg
=> pg
== page
);
341 if (pgIndex
=== -1) sidToPages
[sid
].push(page
);
342 socket
.on("message", messageListener
);
343 socket
.on("close", closeListener
);