21742d1dbb662440cc07bfba4fb6643dee5c9ace
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 // TODO: empêcher multi-log du même user (envoyer le user ID + secret en même temps que name et...)
17 // --> si secret ne matche pas celui trouvé en DB, stop
19 module
.exports = function(wss
) {
20 db
.serialize(function() {
21 db
.all("SELECT * FROM Variants", (err
,variants
) => {
22 let clients
= { "index": {} };
23 let games
= {}; //pending games (player sid)
24 for (const v
of variants
)
26 // No-op function as a callback when sending messages
27 const noop
= () => { };
28 wss
.on("connection", (socket
, req
) => {
29 // const params = new URL("http://localhost" + req.url).searchParams;
30 // const sid = params.get("sid");
31 // const page = params.get("page");
32 var query
= getJsonFromUrl(req
.url
);
33 const sid
= query
["sid"];
34 const page
= query
["page"];
35 // Ignore duplicate connections:
36 if (!!clients
[page
][sid
])
38 socket
.send(JSON
.stringify({code:"duplicate"}));
41 clients
[page
][sid
] = socket
;
46 for (const v
of variants
)
47 countings
[v
.id
] = Object
.keys(clients
[v
.id
]).length
;
48 socket
.send(JSON
.stringify({code:"counts",counts:countings
}));
52 // Send to every client connected on index an update message for counts
53 Object
.keys(clients
["index"]).forEach( k
=> {
54 clients
["index"][k
].send(
55 JSON
.stringify({code:"increase",vid:page
}), noop
);
57 // Also notify potential opponents:
58 // hit all clients which check if sid corresponds
59 Object
.keys(clients
[page
]).forEach( k
=> {
60 clients
[page
][k
].send(JSON
.stringify({code:"connect",id:sid
}), noop
);
62 socket
.on("message", objtxt
=> {
63 let obj
= JSON
.parse(objtxt
);
67 if (!!clients
[page
][obj
.oppid
])
69 clients
[page
][obj
.oppid
].send(
70 JSON
.stringify({code:"newchat",msg:obj
.msg
}), noop
);
74 if (!!clients
[page
][obj
.oppid
])
76 clients
[page
][obj
.oppid
].send(
77 JSON
.stringify({code:"newmove",move:obj
.move}), noop
);
81 if (!!clients
[page
][obj
.oppid
])
82 socket
.send(JSON
.stringify({code:"pong",gameId:obj
.gameId
}));
85 // Reveal my username to opponent
86 if (!!clients
[page
][obj
.oppid
])
88 clients
[page
][obj
.oppid
].send(JSON
.stringify({
89 code:"oppname", name:obj
.name
}));
93 if (!!clients
[page
][obj
.oppid
])
95 const oppId
= obj
.oppid
;
96 obj
.oppid
= sid
; //I'm oppid for my opponent
97 clients
[page
][oppId
].send(JSON
.stringify(obj
), noop
);
104 const oppId
= games
[page
]["id"];
105 const fen
= games
[page
]["fen"];
106 const gameId
= games
[page
]["gameid"];
108 const mycolor
= (Math
.random() < 0.5 ? 'w' : 'b');
109 socket
.send(JSON
.stringify(
110 {code:"newgame",fen:fen
,oppid:oppId
,color:mycolor
,gameid:gameId
}));
111 if (!!clients
[page
][oppId
])
113 clients
[page
][oppId
].send(
115 {code:"newgame",fen:fen
,oppid:sid
,color:mycolor
=="w"?"b":"w",gameid:gameId
}),
120 games
[page
] = {id:sid
, fen:obj
.fen
, gameid:obj
.gameid
}; //wait for opponent
122 case "cancelnewgame": //if a user cancel his seek
126 if (!!clients
[page
][obj
.oppid
])
127 clients
[page
][obj
.oppid
].send(JSON
.stringify({code:"resign"}), noop
);
132 socket
.on("close", () => {
133 delete clients
[page
][sid
];
134 // Remove potential pending game
135 if (!!games
[page
] && games
[page
]["id"] == sid
)
139 // Send to every client connected on index an update message for counts
140 Object
.keys(clients
["index"]).forEach( k
=> {
141 clients
["index"][k
].send(
142 JSON
.stringify({code:"decrease",vid:page
}), noop
);
145 // Also notify potential opponents:
146 // hit all clients which check if sid corresponds
147 Object
.keys(clients
[page
]).forEach( k
=> {
148 clients
[page
][k
].send(JSON
.stringify({code:"disconnect",id:sid
}), noop
);