1 const url
= require('url');
2 const Variants
= require("./variants");
4 module
.exports = function(wss
) {
6 let clients
= { "index": {} };
7 let games
= {}; //pending games (player sid)
8 for (const v
of Variants
)
11 // // Safety counter (TODO: is it necessary ?)
12 // setInterval(() => {
13 // Object.keys(clients).forEach(k => {
14 // Object.keys(clients[k]).forEach(ck => {
15 // if (!clients[k][ck] || clients[k][ck].readyState != 1)
16 // delete clients[k][ck];
19 // }, 60000); //every minute (will be lowered if a lot of users...)
21 // No-op function as a callback when sending messages
22 const noop
= () => { };
24 wss
.on("connection", (socket
, req
) => {
25 const params
= new URL("http://localhost" + req
.url
).searchParams
;
26 const sid
= params
.get("sid");
27 const page
= params
.get("page");
28 clients
[page
][sid
] = socket
;
33 for (const v
of Variants
)
34 countings
[v
.name
] = Object
.keys(clients
[v
.name
]).length
;
35 socket
.send(JSON
.stringify({code:"counts",counts:countings
}));
39 // Send to every client connected on index an update message for counts
40 Object
.keys(clients
["index"]).forEach( k
=> {
41 clients
["index"][k
].send(JSON
.stringify({code:"increase",vname:page
}), noop
);
43 // Also notify potential opponents: hit all clients which check if sid corresponds
44 Object
.keys(clients
[page
]).forEach( k
=> {
45 clients
[page
][k
].send(JSON
.stringify({code:"connect",id:sid
}), noop
);
47 socket
.on("message", objtxt
=> {
48 let obj
= JSON
.parse(objtxt
);
52 if (!!clients
[page
][obj
.oppid
])
54 clients
[page
][obj
.oppid
].send(
55 JSON
.stringify({code:"newmove",move:obj
.move}), noop
);
59 if (!!clients
[page
][obj
.oppid
])
60 socket
.send(JSON
.stringify({code:"pong"}));
63 if (!!clients
[page
][obj
.oppid
])
65 const oppId
= obj
.oppid
;
66 obj
.oppid
= sid
; //I'm oppid for my opponent
67 clients
[page
][oppId
].send(JSON
.stringify(obj
), noop
);
74 const oppId
= games
[page
]["id"];
75 const fen
= games
[page
]["fen"];
77 const mycolor
= Math
.random() < 0.5 ? 'w' : 'b';
79 JSON
.stringify({code:"newgame",fen:fen
,oppid:oppId
,color:mycolor
}));
80 if (!!clients
[page
][oppId
])
82 clients
[page
][oppId
].send(
84 {code:"newgame",fen:fen
,oppid:sid
,color:mycolor
=="w"?"b":"w"}),
89 games
[page
] = {id:sid
, fen:obj
.fen
}; //wait for opponent
91 case "cancelnewgame": //if a user cancel his seek
95 if (!!clients
[page
][obj
.oppid
])
96 clients
[page
][obj
.oppid
].send(JSON
.stringify({code:"resign"}), noop
);
101 socket
.on("close", () => {
102 delete clients
[page
][sid
];
103 // Remove potential pending game
104 if (!!games
[page
] && games
[page
]["id"] == sid
)
108 // Send to every client connected on index an update message for counts
109 Object
.keys(clients
["index"]).forEach( k
=> {
110 clients
["index"][k
].send(JSON
.stringify({code:"decrease",vname:page
}), noop
);
113 // Also notify potential opponents: hit all clients which check if sid corresponds
114 Object
.keys(clients
[page
]).forEach( k
=> {
115 clients
[page
][k
].send(JSON
.stringify({code:"disconnect",id:sid
}), noop
);