1 const url
= require('url');
2 const sqlite3
= require('sqlite3');
3 const db
= new sqlite3
.Database(__dirname
+ "/db/vchess.sqlite");
5 // Node version in Ubuntu 16.04 does not know about URL class
6 function getJsonFromUrl(url
) {
7 var query
= url
.substr(2); //starts with "/?"
9 query
.split("&").forEach(function(part
) {
10 var item
= part
.split("=");
11 result
[item
[0]] = decodeURIComponent(item
[1]);
16 module
.exports = function(wss
) {
17 db
.serialize(function() {
18 db
.all("SELECT * FROM Variants", (err
,variants
) => {
19 let clients
= { "index": {} };
20 let games
= {}; //pending games (player sid)
21 for (const v
of variants
)
23 // No-op function as a callback when sending messages
24 const noop
= () => { };
25 wss
.on("connection", (socket
, req
) => {
26 // const params = new URL("http://localhost" + req.url).searchParams;
27 // const sid = params.get("sid");
28 // const page = params.get("page");
29 var query
= getJsonFromUrl(req
.url
);
30 const sid
= query
["sid"];
31 const page
= query
["page"];
32 // Ignore duplicate connections:
33 if (!!clients
[page
][sid
])
35 socket
.send(JSON
.stringify({code:"duplicate"}));
38 clients
[page
][sid
] = socket
;
43 for (const v
of variants
)
44 countings
[v
.name
] = Object
.keys(clients
[v
.name
]).length
;
45 socket
.send(JSON
.stringify({code:"counts",counts:countings
}));
49 // Send to every client connected on index an update message for counts
50 Object
.keys(clients
["index"]).forEach( k
=> {
51 clients
["index"][k
].send(
52 JSON
.stringify({code:"increase",vname:page
}), noop
);
54 // Also notify potential opponents:
55 // hit all clients which check if sid corresponds
56 Object
.keys(clients
[page
]).forEach( k
=> {
57 clients
[page
][k
].send(JSON
.stringify({code:"connect",id:sid
}), noop
);
59 socket
.on("message", objtxt
=> {
60 let obj
= JSON
.parse(objtxt
);
64 if (!!clients
[page
][obj
.oppid
])
66 clients
[page
][obj
.oppid
].send(
67 JSON
.stringify({code:"newchat",msg:obj
.msg
}), noop
);
71 if (!!clients
[page
][obj
.oppid
])
73 clients
[page
][obj
.oppid
].send(
74 JSON
.stringify({code:"newmove",move:obj
.move}), noop
);
78 if (!!clients
[page
][obj
.oppid
])
79 socket
.send(JSON
.stringify({code:"pong",gameId:obj
.gameId
}));
82 // Reveal my username to opponent
83 if (!!clients
[page
][obj
.oppid
])
85 clients
[page
][obj
.oppid
].send(JSON
.stringify({
86 code:"oppname", name:obj
.name
}));
90 if (!!clients
[page
][obj
.oppid
])
92 const oppId
= obj
.oppid
;
93 obj
.oppid
= sid
; //I'm oppid for my opponent
94 clients
[page
][oppId
].send(JSON
.stringify(obj
), noop
);
101 const oppId
= games
[page
]["id"];
102 const fen
= games
[page
]["fen"];
104 const mycolor
= Math
.random() < 0.5 ? 'w' : 'b';
105 socket
.send(JSON
.stringify(
106 {code:"newgame",fen:fen
,oppid:oppId
,color:mycolor
}));
107 if (!!clients
[page
][oppId
])
109 clients
[page
][oppId
].send(
111 {code:"newgame",fen:fen
,oppid:sid
,color:mycolor
=="w"?"b":"w"}),
116 games
[page
] = {id:sid
, fen:obj
.fen
}; //wait for opponent
118 case "cancelnewgame": //if a user cancel his seek
122 if (!!clients
[page
][obj
.oppid
])
123 clients
[page
][obj
.oppid
].send(JSON
.stringify({code:"resign"}), noop
);
128 socket
.on("close", () => {
129 delete clients
[page
][sid
];
130 // Remove potential pending game
131 if (!!games
[page
] && games
[page
]["id"] == sid
)
135 // Send to every client connected on index an update message for counts
136 Object
.keys(clients
["index"]).forEach( k
=> {
137 clients
["index"][k
].send(
138 JSON
.stringify({code:"decrease",vname:page
}), noop
);
141 // Also notify potential opponents:
142 // hit all clients which check if sid corresponds
143 Object
.keys(clients
[page
]).forEach( k
=> {
144 clients
[page
][k
].send(JSON
.stringify({code:"disconnect",id:sid
}), noop
);