Almost finished problems logic. TODO: showProblem() part
[vchess.git] / sockets.js
CommitLineData
a29d9d6b 1const url = require('url');
da06a6eb
BA
2const sqlite3 = require('sqlite3');
3const db = new sqlite3.Database('db/vchess.sqlite');
1d184b4c 4
1d184b4c 5module.exports = function(wss) {
da06a6eb
BA
6 db.serialize(function() {
7 db.all("SELECT * FROM Variants", (err,variants) => {
8 let clients = { "index": {} };
9 let games = {}; //pending games (player sid)
10 for (const v of variants)
11 clients[v.name] = {};
12 // No-op function as a callback when sending messages
13 const noop = () => { };
14 wss.on("connection", (socket, req) => {
15 const params = new URL("http://localhost" + req.url).searchParams;
16 const sid = params.get("sid");
17 const page = params.get("page");
18 // Ignore duplicate connections:
19 if (!!clients[page][sid])
1d184b4c 20 {
da06a6eb
BA
21 socket.send(JSON.stringify({code:"duplicate"}));
22 return;
23 }
24 clients[page][sid] = socket;
25 if (page == "index")
26 {
27 // Send counting info
28 const countings = {};
29 for (const v of variants)
30 countings[v.name] = Object.keys(clients[v.name]).length;
31 socket.send(JSON.stringify({code:"counts",counts:countings}));
32 }
33 else
34 {
35 // Send to every client connected on index an update message for counts
36 Object.keys(clients["index"]).forEach( k => {
37 clients["index"][k].send(
38 JSON.stringify({code:"increase",vname:page}), noop);
39 });
40 // Also notify potential opponents:
41 // 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}), noop);
44 });
45 socket.on("message", objtxt => {
46 let obj = JSON.parse(objtxt);
47 switch (obj.code)
1d184b4c 48 {
da06a6eb
BA
49 case "newmove":
50 if (!!clients[page][obj.oppid])
51 {
52 clients[page][obj.oppid].send(
53 JSON.stringify({code:"newmove",move:obj.move}), noop);
54 }
55 break;
56 case "ping":
57 if (!!clients[page][obj.oppid])
58 socket.send(JSON.stringify({code:"pong"}));
59 break;
60 case "lastate":
61 if (!!clients[page][obj.oppid])
62 {
63 const oppId = obj.oppid;
64 obj.oppid = sid; //I'm oppid for my opponent
65 clients[page][oppId].send(JSON.stringify(obj), noop);
66 }
67 break;
68 case "newgame":
69 if (!!games[page])
70 {
71 // Start a new game
72 const oppId = games[page]["id"];
73 const fen = games[page]["fen"];
74 delete games[page];
75 const mycolor = Math.random() < 0.5 ? 'w' : 'b';
76 socket.send(JSON.stringify(
77 {code:"newgame",fen:fen,oppid:oppId,color:mycolor}));
78 if (!!clients[page][oppId])
79 {
80 clients[page][oppId].send(
81 JSON.stringify(
82 {code:"newgame",fen:fen,oppid:sid,color:mycolor=="w"?"b":"w"}),
83 noop);
84 }
85 }
86 else
87 games[page] = {id:sid, fen:obj.fen}; //wait for opponent
88 break;
89 case "cancelnewgame": //if a user cancel his seek
90 delete games[page];
91 break;
92 case "resign":
93 if (!!clients[page][obj.oppid])
94 clients[page][obj.oppid].send(JSON.stringify({code:"resign"}), noop);
95 break;
1d184b4c 96 }
da06a6eb 97 });
1d184b4c 98 }
da06a6eb
BA
99 socket.on("close", () => {
100 delete clients[page][sid];
101 // Remove potential pending game
102 if (!!games[page] && games[page]["id"] == sid)
103 delete games[page];
104 if (page != "index")
105 {
106 // Send to every client connected on index an update message for counts
107 Object.keys(clients["index"]).forEach( k => {
108 clients["index"][k].send(
109 JSON.stringify({code:"decrease",vname:page}), noop);
110 });
111 }
112 // Also notify potential opponents:
113 // 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);
116 });
1d184b4c 117 });
1d184b4c
BA
118 });
119 });
120 });
121}