Commit | Line | Data |
---|---|---|
a29d9d6b | 1 | const url = require('url'); |
1d184b4c | 2 | |
2807f530 | 3 | // Node version in Ubuntu 16.04 does not know about URL class |
98db2082 BA |
4 | function getJsonFromUrl(url) |
5 | { | |
6 | const query = url.substr(2); //starts with "/?" | |
7 | let result = {}; | |
8 | query.split("&").forEach((part) => { | |
9 | const item = part.split("="); | |
2807f530 BA |
10 | result[item[0]] = decodeURIComponent(item[1]); |
11 | }); | |
12 | return result; | |
13 | } | |
14 | ||
b644ef7f BA |
15 | // Removal in array of strings (socket IDs) |
16 | function remInArray(arr, item) | |
17 | { | |
18 | const idx = arr.indexOf(item); | |
19 | if (idx >= 0) | |
20 | arr.splice(idx, 1); | |
21 | } | |
22 | ||
b57dbd12 BA |
23 | // TODO: empêcher multi-log du même user (envoyer le user ID + secret en même temps que name et...) |
24 | // --> si secret ne matche pas celui trouvé en DB, stop | |
b644ef7f | 25 | // TODO: this file "in the end" would be much simpler, essentially just tracking connect/disconnect |
4608eed9 | 26 | // (everything else using WebRTC) |
b644ef7f BA |
27 | // TODO: lorsque challenge accepté, seul le dernier joueur à accepter envoi message "please start game" |
28 | // avec les coordonnées des participants. Le serveur renvoit alors les détails de la partie (couleurs, position) | |
29 | //TODO: programmatic re-navigation on current game if we receive a move and are not there | |
4608eed9 | 30 | |
1d184b4c | 31 | module.exports = function(wss) { |
4d64881e | 32 | let clients = {}; //associative array sid --> socket |
b644ef7f BA |
33 | wss.on("connection", (socket, req) => { |
34 | const query = getJsonFromUrl(req.url); | |
35 | const sid = query["sid"]; | |
052d17ea | 36 | // Ignore duplicate connections (on the same live game that we play): |
b644ef7f BA |
37 | if (!!clients[sid]) |
38 | return socket.send(JSON.stringify({code:"duplicate"})); | |
b4d619d1 | 39 | clients[sid] = socket; |
5a3da968 BA |
40 | // Notify room: |
41 | Object.keys(clients).forEach(k => { | |
42 | if (k != sid) | |
43 | clients[k].send(JSON.stringify({code:"connect",sid:sid})); | |
44 | }); | |
45 | socket.on("message", objtxt => { | |
b4d619d1 | 46 | let obj = JSON.parse(objtxt); |
5a3da968 | 47 | if (!!obj.target && !clients[obj.target]) |
b4d619d1 | 48 | return; //receiver not connected, nothing we can do |
5a3da968 | 49 | //console.log(obj.code); |
b4d619d1 BA |
50 | switch (obj.code) |
51 | { | |
81d9ce72 BA |
52 | case "pollclients": |
53 | socket.send(JSON.stringify({code:"pollclients", | |
54 | sockIds:Object.keys(clients).filter(k => k != sid)})); | |
5a3da968 BA |
55 | break; |
56 | case "askidentity": | |
81d9ce72 BA |
57 | clients[obj.target].send( |
58 | JSON.stringify({code:"askidentity",from:sid})); | |
59 | break; | |
dd75774d | 60 | case "askchallenge": |
81d9ce72 | 61 | clients[obj.target].send( |
dd75774d | 62 | JSON.stringify({code:"askchallenge",from:sid})); |
81d9ce72 BA |
63 | break; |
64 | case "askgame": | |
65 | clients[obj.target].send( | |
66 | JSON.stringify({code:"askgame",from:sid})); | |
5a3da968 BA |
67 | break; |
68 | case "identity": | |
81d9ce72 BA |
69 | clients[obj.target].send( |
70 | JSON.stringify({code:"identity",user:obj.user})); | |
4d64881e | 71 | break; |
dd75774d BA |
72 | case "challenge": |
73 | // Relay challenge to other player | |
6855163c | 74 | clients[obj.target].send( |
bb7dd7db BA |
75 | JSON.stringify({code:"challenge", chall:obj.chall, from:sid})); |
76 | break; | |
77 | case "acceptchallenge": | |
78 | clients[obj.target].send( | |
79 | JSON.stringify({code:"acceptchallenge", cid:obj.cid, from:sid})); | |
dd75774d | 80 | break; |
a6bddfc6 BA |
81 | case "withdrawchallenge": |
82 | clients[obj.target].send( | |
83 | JSON.stringify({code:"withdrawchallenge", cid:obj.cid, from:sid})); | |
84 | break; | |
85 | case "newgame": | |
86 | clients[obj.target].send(JSON.stringify( | |
87 | {code:"newgame", gameInfo:obj.gameInfo})); | |
88 | break; | |
dd75774d | 89 | case "game": |
bb7dd7db | 90 | // TODO: relay (live) game to other player |
4d64881e | 91 | break; |
b4d619d1 | 92 | case "newchat": |
5a3da968 | 93 | clients[obj.target].send(JSON.stringify({code:"newchat",msg:obj.msg})); |
b4d619d1 | 94 | break; |
4d64881e BA |
95 | // Transmit chats and moves to current room |
96 | // TODO: WebRTC instead in this case (most demanding?) | |
b4d619d1 | 97 | case "newmove": |
5a3da968 | 98 | clients[obj.target].send(JSON.stringify({code:"newmove",move:obj.move})); |
b4d619d1 BA |
99 | break; |
100 | // TODO: generalize that for several opponents | |
101 | case "ping": | |
102 | socket.send(JSON.stringify({code:"pong",gameId:obj.gameId})); | |
103 | break; | |
104 | case "lastate": | |
5a3da968 BA |
105 | const oppId = obj.target; |
106 | obj.oppid = sid; //I'm the opponent of my opponent(s) | |
4d64881e | 107 | clients[oppId].send(JSON.stringify(obj)); |
b4d619d1 BA |
108 | break; |
109 | // TODO: moreover, here, game info should be sent (through challenge; not stored here) | |
b4d619d1 BA |
110 | // TODO: also other challenge events |
111 | case "resign": | |
5a3da968 | 112 | clients[obj.target].send(JSON.stringify({code:"resign"})); |
b4d619d1 BA |
113 | break; |
114 | // TODO: case "challenge" (get ID) --> send to all, "acceptchallenge" (with ID) --> send to all, "cancelchallenge" --> send to all | |
115 | // also, "sendgame" (give current game info, if any) --> to new connections, "sendchallenges" (same for challenges) --> to new connections | |
b4d619d1 BA |
116 | } |
117 | }); | |
118 | socket.on("close", () => { | |
119 | delete clients[sid]; | |
120 | // Notify every other connected client | |
121 | Object.keys(clients).forEach( k => { | |
4d64881e | 122 | clients[k].send(JSON.stringify({code:"disconnect",sid:sid})); |
b4d619d1 BA |
123 | }); |
124 | }); | |
1d184b4c BA |
125 | }); |
126 | } |