server for my chess game
Diffstat (limited to 'src/game.js')
-rw-r--r--src/game.js78
1 files changed, 72 insertions, 6 deletions
diff --git a/src/game.js b/src/game.js
index 47d61bd..f6fdae3 100644
--- a/src/game.js
+++ b/src/game.js
@@ -27,38 +27,80 @@ export class Game {
this.move = this.game.move;
this.undo = this.game.undo;
}
-
+ /**
+ * Gets the games pgn
+ *
+ * @returns {String} the games PGN
+ */
get pgn() {
return this.game.pgn();
}
+ /**
+ * Gets the number of players
+ * players !== number of clients
+ *
+ * @returns {Number} the number of players
+ */
get players() {
return this.clients.players;
}
+ /**
+ * Gets the number of clients
+ * clients !== number of players
+ *
+ * @returns {Number} the number of clients
+ */
get client_count() {
return this.clients.length;
}
+ /**
+ * Gets the list of clients
+ *
+ * @returns {WebSocket[]} the list of clients
+ */
get client_list() {
return this.clients.client_list;
}
- // true for valid
+ /**
+ * Checks if a move is valid
+ *
+ * @param {String} move the move
+ * @returns {Boolean} valid
+ */
validate_move(move) {
const res = this.game.move(move);
if (str_obj(res) == "{}") return false;
this.game.undo();
return true;
}
-
+ /**
+ * Adds a client
+ *
+ * @param {WebSocket} ws The websocket of the client
+ * @param {Object} data The packet the client sent
+ * @param {Boolean} is_white=has_empty_slot Is the client white
+ */
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;
}
+ /**
+ * Cleans dead clients
+ *
+ * @param {} set_is_alive=true To set `websocket.is_alive` = false or not
+ */
clean_clients(set_is_alive = true) {
+ /**
+ * @param {WebSocket} c client
+ * @param {WebSocketServer} wss websocketserver
+ * @param {CallableFunction} removal_func
+ */
function remove_client(c, wss, removal_func) {
if (!c) return;
if (c.is_alive === true && set_is_alive) {
@@ -78,21 +120,42 @@ export class Game {
remove_client(client, this.wss, this.remove_client.bind(this));
});
}
-
+ /**
+ * Checks if the game is dead: no clients & no spectators
+ *
+ * @returns {Boolean} is dead
+ */
get dead() {
return this.client_count === 0 && this.spectators.length == 0;
}
+ /**
+ * Removes a spectator websocket
+ *
+ * @param {WebSocket} ws The websocket to remove
+ * @callback
+ */
remove_spectator(ws) {
this.spectators.slice(this.spectators.indexOf(ws));
}
-
+ /**
+ * Sends a packet to the clients and spectators
+ *
+ * @param {Object} packet The packet to send
+ * @param {String} header The header to use
+ */
send_group_packet(packet, header) {
delete packet.gamecode;
send_group_packet(packet, header, this.clients.client_list);
send_group_packet(packet, header, this.spectators);
}
-
+ /**
+ * Sends a packet to the opponent, and spectators
+ *
+ * @param {Object} data The packet to send
+ * @param {WebSocket} ws The thing thats sending it
+ * @param {String} header The header to use
+ */
send_signal_packet(data, ws, header) {
let us = this.color_of(ws);
if (us === undefined) return false;
@@ -103,6 +166,9 @@ export class Game {
return true;
}
+ /**
+ * Reset the internal game
+ */
reset_game() {
this.game = new Chess();
}