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