Commit | Line | Data |
---|---|---|
1d184b4c BA |
1 | //const url = require('url'); |
2 | const Variants = require("./variants"); | |
3 | ||
4 | function getJsonFromUrl(url) { | |
5 | var query = url.substr(2); //starts with "/?" | |
6 | var result = {}; | |
7 | query.split("&").forEach(function(part) { | |
8 | var item = part.split("="); | |
9 | result[item[0]] = decodeURIComponent(item[1]); | |
10 | }); | |
11 | return result; | |
12 | } | |
13 | ||
14 | module.exports = function(wss) { | |
15 | ||
16 | let clients = { "index": {} }; | |
17 | let games = {}; //pending games (player sid) | |
18 | for (const v of Variants) | |
19 | clients[v.name] = {}; | |
20 | ||
21 | wss.on("connection", (socket, req) => { | |
22 | //const params = new URL("http://localhost" + req.url).searchParams; | |
23 | var query = getJsonFromUrl(req.url); | |
24 | const sid = query["sid"]; //params.get("sid"); | |
25 | const page = query["page"]; //params.get("page"); | |
26 | clients[page][sid] = socket; | |
27 | if (page == "index") | |
28 | { | |
29 | // Send counting info | |
30 | const countings = {}; | |
31 | for (const v of Variants) | |
32 | countings[v.name] = Object.keys(clients[v.name]).length; | |
33 | socket.send(JSON.stringify({code:"counts",counts:countings})); | |
34 | } | |
35 | else | |
36 | { | |
37 | // Send to every client connected on index an update message for counts | |
38 | Object.keys(clients["index"]).forEach( k => { | |
39 | clients["index"][k].send(JSON.stringify({code:"increase",vname:page})); | |
40 | }); | |
41 | // Also notify potential opponents: hit all clients which check if sid corresponds | |
42 | Object.keys(clients[page]).forEach( k => { | |
43 | clients[page][k].send(JSON.stringify({code:"connect",id:sid})); | |
44 | }); | |
45 | socket.on("message", objtxt => { | |
46 | let obj = JSON.parse(objtxt); | |
47 | switch (obj.code) | |
48 | { | |
49 | case "newmove": | |
50 | clients[page][obj.oppid].send(JSON.stringify({code:"newmove",move:obj.move})); | |
51 | break; | |
52 | case "ping": | |
53 | if (!!clients[page][obj.oppid]) | |
54 | socket.send(JSON.stringify({code:"pong"})); | |
55 | break; | |
56 | case "newgame": | |
57 | if (!!games[page]) | |
58 | { | |
59 | // Start a new game | |
60 | const oppId = games[page]["id"]; | |
61 | const fen = games[page]["fen"]; | |
62 | delete games[page]; | |
63 | const mycolor = Math.random() < 0.5 ? 'w' : 'b'; | |
64 | socket.send(JSON.stringify({code:"newgame",fen:fen,oppid:oppId,color:mycolor})); | |
65 | clients[page][oppId].send(JSON.stringify({code:"newgame",fen:fen,oppid:sid,color:mycolor=="w"?"b":"w"})); | |
66 | } | |
67 | else | |
68 | games[page] = {id:sid, fen:obj.fen}; //wait for opponent | |
69 | break; | |
70 | case "resign": | |
71 | clients[page][obj.oppid].send(JSON.stringify({code:"resign"})); | |
72 | break; | |
73 | } | |
74 | }); | |
75 | } | |
76 | socket.on("close", () => { | |
77 | delete clients[page][sid]; | |
78 | // Remove potential pending game | |
79 | if (!!games[page] && games[page]["id"] == sid) | |
80 | delete games[page]; | |
81 | if (page != "index") | |
82 | { | |
83 | // Send to every client connected on index an update message for counts | |
84 | Object.keys(clients["index"]).forEach( k => { | |
85 | clients["index"][k].send(JSON.stringify({code:"decrease",vname:page})); | |
86 | }); | |
87 | } | |
88 | // Also notify potential opponents: hit all clients which check if sid corresponds | |
89 | Object.keys(clients[page]).forEach( k => { | |
90 | clients[page][k].send(JSON.stringify({code:"disconnect",id:sid})); | |
91 | }); | |
92 | }); | |
93 | }); | |
94 | } |