server for my chess game
store the gamecode inside the ws
| -rw-r--r-- | src/clients.js | 28 | ||||
| -rw-r--r-- | src/game.js | 5 | ||||
| -rw-r--r-- | src/index.js | 36 | ||||
| -rw-r--r-- | src/utils.js | 5 |
4 files changed, 36 insertions, 38 deletions
diff --git a/src/clients.js b/src/clients.js index 3e64702..464beb4 100644 --- a/src/clients.js +++ b/src/clients.js @@ -1,3 +1,5 @@ +import { pick } from "./utils.js"; + function create_data(ws, id, name, country) { return { name: name, @@ -15,13 +17,9 @@ function create_empty_data() { export class Clients { constructor(ws, id, name, country, is_white) { - if (is_white) { - this.w = create_data(ws, id, name, country); - this.b = create_empty_data(); - } else { - this.b = create_data(ws, id, name, country); - this.w = create_empty_data(); - } + this.add(ws, name, id, country, is_white); + if (is_white) this.b = create_empty_data(); + else this.w = create_empty_data(); } get_ws(color) { @@ -29,9 +27,7 @@ export class Clients { } get_info(color) { - return color === "w" - ? { name: this.w.name, country: this.w.country } - : { name: this.b.name, country: this.b.country }; + return pick(color === "w" ? this.w : this.b, "name", "country"); } get client_list() { @@ -71,18 +67,18 @@ export class Clients { // if you check name, country AND id, even if there are duplicate ids (impossible) it wont have problems function check(on) { // if on doesnt have a name property, this will not error, because js - return on.name == nameorws && on.country == country && on.id == id; + return on.name === nameorws && on.country === country && on.id === id; } if (check(this.w)) return "w"; else if (check(this.b)) return "b"; - } else if (typeof nameorws == "object") { - if (this.w.ws == nameorws) return "w"; - if (this.b.ws == nameorws) return "b"; + } else if (typeof nameorws === "object") { + if (this.w.ws === nameorws) return "w"; + if (this.b.ws === nameorws) return "b"; } } erase(client) { - if (this.w.ws == client) this.w.ws = undefined; - else if (this.b.ws == client) this.b.ws = undefined; + if (this.w.ws === client) this.w.ws = undefined; + else if (this.b.ws === client) this.b.ws = undefined; } } diff --git a/src/game.js b/src/game.js index 1ef79fe..0c34957 100644 --- a/src/game.js +++ b/src/game.js @@ -6,6 +6,7 @@ export class Game { constructor(data, ws, wss) { this.wss = wss; this.gamecode = data.gamecode; + ws.gamecode = this.gamecode; this.clients = new Clients(ws, data.id, data.name, data.country, data.team); this.spectators = []; this.game = new Chess(); @@ -53,6 +54,7 @@ export class Game { add_client(ws, data, is_white = this.clients.w.empty) { this.clients.add(ws, data.name, data.id, data.country, is_white); + ws.gamecode = this.gamecode; return is_white ? 0 : 1; } @@ -94,13 +96,12 @@ export class Game { let us = this.color_of(ws); if (us !== undefined) { let sendto = this.get_ws(flip_color(us)); - delete data.gamecode; // dont send the gamecode to the other player: waste of bytes if (sendto) { sendto.send_packet(data, header); send_group_packet(data, header, this.spectators); // give it to the specs return true; } - } else console.log(`could not find client in game ${data.gamecode}`); + } else console.log(`could not find client in game ${this.gamecode}`); return false; } diff --git a/src/index.js b/src/index.js index 020f23d..a673f31 100644 --- a/src/index.js +++ b/src/index.js @@ -234,31 +234,27 @@ function handle_hostrequest(data, ws) { } function handle_move(data, ws) { - const gc = data.gamecode; if ( - games.hasOwnProperty(data.gamecode) && - games[gc].validate_move(data.move) && + games.hasOwnProperty(ws.gamecode) && + games[ws.gamecode].validate_move(data.move) && signal_other(data, ws, HEADERS.move) - ) - console.log("made move", data.move, "on", gc); - games[gc].move(data.move); + ) { + games[ws.gamecode].move(data.move); + console.log(`made move ${data.move} on ${ws.gamecode}`); + } } function handle_undo(data, ws) { - const gc = data.gamecode; - const sent = signal_other(data, ws, HEADERS.undo); - if (sent && data.accepted === true) { - games[gc].undo(); - if (data.two === true) games[gc].undo(); + if (signal_other(data, ws, HEADERS.undo) && data.accepted === true) { + games[ws.gamecode].undo(); + if (data.two === true) games[ws.gamecode].undo(); // do it again } } function handle_rematch(data, ws) { - const gc = data.gamecode; - if (games.hasOwnProperty(gc)) { - signal_other(data, ws, HEADERS.rematch); + if (signal_other(data, ws, HEADERS.rematch)) { // check if its a request, and if the request is accepted - if (data.accepted === true) games[gc].reset_game(); // reset if it is + if (data.accepted === true) games[ws.gamecode].reset_game(); // reset if it is } } @@ -278,16 +274,16 @@ function handle_spectate(data, ws) { // relays to both clients function dual_relay(data, ws, header = HEADERS.relay) { - if (games.hasOwnProperty(data.gamecode)) { - games[data.gamecode].send_group_packet(data, header); + if (games.hasOwnProperty(ws.gamecode)) { + games[ws.gamecode].send_group_packet(data, header); return true; - } else console.log(`dual relay: game ${data.gamecode} does not exist`); + } else console.log(`dual relay: game ${ws.gamecode} does not exist`); return false; } // relays to the other client function signal_other(data, ws, header = HEADERS.signal) { - if (games.hasOwnProperty(data.gamecode)) - return games[data.gamecode].send_signal_packet(data, ws, header); + if (games.hasOwnProperty(ws.gamecode)) + return games[ws.gamecode].send_signal_packet(data, ws, header); return false; } diff --git a/src/utils.js b/src/utils.js index fe76518..f5a6688 100644 --- a/src/utils.js +++ b/src/utils.js @@ -11,3 +11,8 @@ export function send_group_packet(data, header, clients) { if (client) client.send_packet(data, header); }); } + +export function pick(o, ...props) { + if (o === undefined) return undefined; + return Object.assign({}, ...props.map((prop) => ({ [prop]: o[prop] }))); +} |