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"];
103 const gameId
= games
[page
]["gameid"];
105 const mycolor
= (Math
.random() < 0.5 ? 'w' : 'b');
106 socket
.send(JSON
.stringify(
107 {code:"newgame",fen:fen
,oppid:oppId
,color:mycolor
,gameid:gameId
}));
108 if (!!clients
[page
][oppId
])
110 clients
[page
][oppId
].send(
112 {code:"newgame",fen:fen
,oppid:sid
,color:mycolor
=="w"?"b":"w",gameid:gameId
}),
117 games
[page
] = {id:sid
, fen:obj
.fen
, gameid:obj
.gameid
}; //wait for opponent
119 case "cancelnewgame": //if a user cancel his seek
123 if (!!clients
[page
][obj
.oppid
])
124 clients
[page
][obj
.oppid
].send(JSON
.stringify({code:"resign"}), noop
);
129 socket
.on("close", () => {
130 delete clients
[page
][sid
];
131 // Remove potential pending game
132 if (!!games
[page
] && games
[page
]["id"] == sid
)
136 // Send to every client connected on index an update message for counts
137 Object
.keys(clients
["index"]).forEach( k
=> {
138 clients
["index"][k
].send(
139 JSON
.stringify({code:"decrease",vname:page
}), noop
);
142 // Also notify potential opponents:
143 // hit all clients which check if sid corresponds
144 Object
.keys(clients
[page
]).forEach( k
=> {
145 clients
[page
][k
].send(JSON
.stringify({code:"disconnect",id:sid
}), noop
);