1 const url
= require('url');
2 const sqlite3
= require('sqlite3');
3 const db
= new sqlite3
.Database('db/vchess.sqlite');
5 module
.exports = function(wss
) {
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
)
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
])
21 socket
.send(JSON
.stringify({code:"duplicate"}));
24 clients
[page
][sid
] = socket
;
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
}));
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
);
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
);
45 socket
.on("message", objtxt
=> {
46 let obj
= JSON
.parse(objtxt
);
50 if (!!clients
[page
][obj
.oppid
])
52 clients
[page
][obj
.oppid
].send(
53 JSON
.stringify({code:"newmove",move:obj
.move}), noop
);
57 if (!!clients
[page
][obj
.oppid
])
58 socket
.send(JSON
.stringify({code:"pong"}));
61 if (!!clients
[page
][obj
.oppid
])
63 const oppId
= obj
.oppid
;
64 obj
.oppid
= sid
; //I'm oppid for my opponent
65 clients
[page
][oppId
].send(JSON
.stringify(obj
), noop
);
72 const oppId
= games
[page
]["id"];
73 const fen
= games
[page
]["fen"];
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
])
80 clients
[page
][oppId
].send(
82 {code:"newgame",fen:fen
,oppid:sid
,color:mycolor
=="w"?"b":"w"}),
87 games
[page
] = {id:sid
, fen:obj
.fen
}; //wait for opponent
89 case "cancelnewgame": //if a user cancel his seek
93 if (!!clients
[page
][obj
.oppid
])
94 clients
[page
][obj
.oppid
].send(JSON
.stringify({code:"resign"}), noop
);
99 socket
.on("close", () => {
100 delete clients
[page
][sid
];
101 // Remove potential pending game
102 if (!!games
[page
] && games
[page
]["id"] == sid
)
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
);
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
);