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:"newchat",msg:obj
.msg
}), noop
);
57 if (!!clients
[page
][obj
.oppid
])
59 clients
[page
][obj
.oppid
].send(
60 JSON
.stringify({code:"newmove",move:obj
.move}), noop
);
64 if (!!clients
[page
][obj
.oppid
])
65 socket
.send(JSON
.stringify({code:"pong",gameId:obj
.gameId
}));
68 // Reveal my username to opponent
69 if (!!clients
[page
][obj
.oppid
])
71 clients
[page
][obj
.oppid
].send(JSON
.stringify({
72 code:"oppname", name:obj
.name
}));
76 if (!!clients
[page
][obj
.oppid
])
78 const oppId
= obj
.oppid
;
79 obj
.oppid
= sid
; //I'm oppid for my opponent
80 clients
[page
][oppId
].send(JSON
.stringify(obj
), noop
);
87 const oppId
= games
[page
]["id"];
88 const fen
= games
[page
]["fen"];
90 const mycolor
= Math
.random() < 0.5 ? 'w' : 'b';
91 socket
.send(JSON
.stringify(
92 {code:"newgame",fen:fen
,oppid:oppId
,color:mycolor
}));
93 if (!!clients
[page
][oppId
])
95 clients
[page
][oppId
].send(
97 {code:"newgame",fen:fen
,oppid:sid
,color:mycolor
=="w"?"b":"w"}),
102 games
[page
] = {id:sid
, fen:obj
.fen
}; //wait for opponent
104 case "cancelnewgame": //if a user cancel his seek
108 if (!!clients
[page
][obj
.oppid
])
109 clients
[page
][obj
.oppid
].send(JSON
.stringify({code:"resign"}), noop
);
114 socket
.on("close", () => {
115 delete clients
[page
][sid
];
116 // Remove potential pending game
117 if (!!games
[page
] && games
[page
]["id"] == sid
)
121 // Send to every client connected on index an update message for counts
122 Object
.keys(clients
["index"]).forEach( k
=> {
123 clients
["index"][k
].send(
124 JSON
.stringify({code:"decrease",vname:page
}), noop
);
127 // Also notify potential opponents:
128 // hit all clients which check if sid corresponds
129 Object
.keys(clients
[page
]).forEach( k
=> {
130 clients
[page
][k
].send(JSON
.stringify({code:"disconnect",id:sid
}), noop
);