server for my chess game
move validation whoo (`chess.js`) & convert to module
| -rw-r--r-- | package-lock.json | 11 | ||||
| -rw-r--r-- | package.json | 2 | ||||
| -rw-r--r-- | src/index.js | 43 | ||||
| -rw-r--r-- | src/pg.js | 9 | ||||
| -rw-r--r-- | src/ping.js | 10 |
5 files changed, 43 insertions, 32 deletions
diff --git a/package-lock.json b/package-lock.json index 4e1419a..d35eafc 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,6 +10,7 @@ "license": "MIT", "dependencies": { "@gd-com/utils": "^4.1.0", + "chess.js": "^0.13.3", "pg": "^8.7.3", "ws": "^7.4.6" }, @@ -33,6 +34,11 @@ "node": ">=4" } }, + "node_modules/chess.js": { + "version": "0.13.3", + "resolved": "https://registry.npmjs.org/chess.js/-/chess.js-0.13.3.tgz", + "integrity": "sha512-qQnWyoMYvi9nzF7RgtKgqMyS+OhlbCcc2KbWKs1Vo1/r0aKvJfqeFhRUukp7CD3yZhtttt5k0pPjkkbi9ZXxhg==" + }, "node_modules/long": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/long/-/long-4.0.0.tgz", @@ -203,6 +209,11 @@ "resolved": "https://registry.npmjs.org/buffer-writer/-/buffer-writer-2.0.0.tgz", "integrity": "sha512-a7ZpuTZU1TRtnwyCNW3I5dc0wWNC3VR9S++Ewyk2HHZdrO3CQJqSpd+95Us590V6AL7JqUAH2IwZ/398PmNFgw==" }, + "chess.js": { + "version": "0.13.3", + "resolved": "https://registry.npmjs.org/chess.js/-/chess.js-0.13.3.tgz", + "integrity": "sha512-qQnWyoMYvi9nzF7RgtKgqMyS+OhlbCcc2KbWKs1Vo1/r0aKvJfqeFhRUukp7CD3yZhtttt5k0pPjkkbi9ZXxhg==" + }, "long": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/long/-/long-4.0.0.tgz", diff --git a/package.json b/package.json index d1424ba..0880505 100644 --- a/package.json +++ b/package.json @@ -1,4 +1,5 @@ { + "type": "module", "name": "gd-com-websocket-basic", "version": "1.0.0", "description": "", @@ -11,6 +12,7 @@ "license": "MIT", "dependencies": { "@gd-com/utils": "^4.1.0", + "chess.js": "^0.13.3", "pg": "^8.7.3", "ws": "^7.4.6" }, diff --git a/src/index.js b/src/index.js index 73f5896..642fdae 100644 --- a/src/index.js +++ b/src/index.js @@ -1,11 +1,14 @@ -const { Server } = require("ws"); +import ws from "ws"; +const { Server } = ws; const PORT = process.env.PORT || 3000; const wss = new Server({ port: PORT }); // init server asap -const { putVar, getVar } = require("@gd-com/utils"); -const { command } = require("./pg"); -const { self_ping } = require("./ping"); +import utils from "@gd-com/utils"; +const { putVar, getVar } = utils; +import { command } from "./pg.js"; +import { self_ping } from "./ping.js"; +import { Chess } from "chess.js"; const HEADERS = { relay: "R", @@ -240,25 +243,21 @@ class Game { }, }; this.spectators = []; - this.moves = []; - this.fullmoves = 1; - this.turn = true; - if (data.moves) this.load_move_array(data.moves); + this.game = new Chess(); + if (data.hasOwnProperty("moves")) this.game.load_pgn(data.moves.join(" ")); } get pgn() { - return this.moves.join(" "); + return this.game.pgn(); } - load_move_array(move_array) { - move_array.forEach((item) => { - this.add_turn(item); - }); + //true for valid + validate_move(move) { + const res = this.game.move(move); + if (str_obj(res) == "{}") return false; + this.game.undo(); + return true; } - add_turn(move) { - this.turn = !this.turn; - if (this.turn) { - this.moves.push(`${move}`); - this.fullmoves++; - } else this.moves.push(`${this.fullmoves}. ${move}`); + add_move(move) { + this.game.move(move); } pop_move() { this.moves.pop(); @@ -320,7 +319,11 @@ function handle_stop(data, ws) { function handle_move(data, ws) { const gc = data.gamecode; - if (signal_other(data, ws, HEADERS.move)) games[gc].add_turn(data.move); + if ( + games[gc].validate_move(data.move) && + signal_other(data, ws, HEADERS.move) + ) + games[gc].add_move(data.move); } function handle_undo(data, ws) { @@ -1,4 +1,5 @@ -const { Pool } = require("pg"); +import pg from "pg"; +const { Pool } = pg; const pool = new Pool({ connectionString: process.env.DATABASE_URL, @@ -9,7 +10,7 @@ pool.on("error", (err, client) => { console.error("Error:", err); }); -async function command(command) { +export async function command(command) { let client; try { client = await pool.connect(); @@ -21,7 +22,3 @@ async function command(command) { client.release(); } } - -module.exports = { - command: command, -}; diff --git a/src/ping.js b/src/ping.js index 6a531a2..15cfdfa 100644 --- a/src/ping.js +++ b/src/ping.js @@ -1,4 +1,4 @@ -const https = require("https"); +import { request } from "https"; const options = { hostname: "gd-chess-server.herokuapp.com", @@ -6,9 +6,7 @@ const options = { method: "GET", }; -module.exports = { - self_ping: function ping() { - const ping = https.request(options); - ping.end(); - }, +export const self_ping = function ping() { + const ping = request(options); + ping.end(); }; |