server for my chess game
| -rw-r--r-- | src/game.js | 9 | ||||
| -rw-r--r-- | src/index.js | 6 | ||||
| -rw-r--r-- | src/pg.js | 4 |
3 files changed, 14 insertions, 5 deletions
diff --git a/src/game.js b/src/game.js index f6fdae3..f093f9f 100644 --- a/src/game.js +++ b/src/game.js @@ -1,8 +1,17 @@ import { Chess } from "chess.js"; +import { WebSocketServer } from "ws"; import { Clients } from "./clients.js"; import { str_obj, send_group_packet, flip_color } from "./utils.js"; export class Game { + /** + * Creates an instance of `Game`. + * @param {Object} data The packet of id name country team, etc + * @param {WebSocket} ws The websocket that hosted + * @param {WebSocketServer} wss The wss, will be stored for later erasure of clients + * @memberof Game + * @constructor + */ constructor(data, ws, wss) { this.wss = wss; this.gamecode = data.gamecode; diff --git a/src/index.js b/src/index.js index 26cb527..81a1973 100644 --- a/src/index.js +++ b/src/index.js @@ -1,6 +1,6 @@ import { WebSocket, WebSocketServer } from "ws"; -const PORT = process.env.PORT || 3000; +const PORT = Number(process.env.PORT || 3000); const wss = new WebSocketServer({ port: PORT }); // init server asap import utils from "@gd-com/utils"; @@ -91,7 +91,7 @@ wss.on("connection", (ws, req) => { ws.on("pong", ws.heartbeat); // on message recieved ws.on("message", (message) => { - let recieve = getVar(Buffer.from(message)).value; + let recieve = getVar(Buffer.from(String(message))).value; let data = recieve.data; let header = recieve.header; if (header) { @@ -169,7 +169,7 @@ async function signin(data, ws) { */ async function signup(data, ws) { const res = await get_propertys(data.name); - if (fail(res, ws, "ALREADY_EXISTS", HEADERS.create_user)) return; // if existing, fail + if (fail(res !== undefined, ws, "ALREADY_EXISTS", HEADERS.create_user)) return; // if existing, fail /** * Creates the user @@ -14,7 +14,7 @@ pool.on("error", (err) => { * Runs a SQL command on the database * * @param {String} command The command to run - * @returns {(undefined|Object)} result + * @returns {Promise<(undefined|Object)>} result */ export async function command(command) { let client; @@ -25,6 +25,6 @@ export async function command(command) { } catch (err) { console.error("database: err:", err.stack); } finally { - client.release(); + if (client) client.release(); } } |