-
- let clients = { "index": {} };
- let games = {}; //pending games (player sid)
- for (const v of Variants)
- clients[v.name] = {};
-
- // (resign, newgame, newmove). See https://github.com/websockets/ws/blob/master/lib/websocket.js around line 313
- // TODO: awaiting newmove, resign, (+newgame?) :: in memory structure (use Redis ?)
- let newmoves = {};
- let newresign = {};
- for (const v of Variants)
- {
- newmoves[v.name] = {};
- newresign[v.name] = {};
- }
-
- wss.on("connection", (socket, req) => {
- //const params = new URL("http://localhost" + req.url).searchParams;
- var query = getJsonFromUrl(req.url);
- const sid = query["sid"]; //params.get("sid");
- const page = query["page"]; //params.get("page");
- clients[page][sid] = socket;
- if (page == "index")
- {
- // Send counting info
- const countings = {};
- for (const v of Variants)
- countings[v.name] = Object.keys(clients[v.name]).length;
- socket.send(JSON.stringify({code:"counts",counts:countings}));
- }
- else
- {
- // Send to every client connected on index an update message for counts
- Object.keys(clients["index"]).forEach( k => {
- clients["index"][k].send(JSON.stringify({code:"increase",vname:page}));
- });
- // Also notify potential opponents: hit all clients which check if sid corresponds
- Object.keys(clients[page]).forEach( k => {
- clients[page][k].send(JSON.stringify({code:"connect",id:sid}));
- });
- if (!!newmoves[page][sid])
- {
- socket.send(JSON.stringify({code:"newmove",move:newmoves[page][sid]}));
- delete newmoves[page][sid];
- }
- if (!!newresign[page][sid])
- {
- socket.send(JSON.stringify({code:"resign"}));
- delete newresign[page][sid];
- }
- socket.on("message", objtxt => {
- let obj = JSON.parse(objtxt);
- switch (obj.code)
+ db.serialize(function() {
+ db.all("SELECT * FROM Variants", (err,variants) => {
+ let clients = { "index": {} };
+ let games = {}; //pending games (player sid)
+ for (const v of variants)
+ clients[v.name] = {};
+ // No-op function as a callback when sending messages
+ const noop = () => { };
+ wss.on("connection", (socket, req) => {
+// const params = new URL("http://localhost" + req.url).searchParams;
+// const sid = params.get("sid");
+// const page = params.get("page");
+ var query = getJsonFromUrl(req.url);
+ const sid = query["sid"];
+ const page = query["page"];
+ // Ignore duplicate connections:
+ if (!!clients[page][sid])