From a1207f2404f898fe46e80688cfdcdba1df5818c5 Mon Sep 17 00:00:00 2001 From: Pavel Lutskov Date: Tue, 14 Dec 2021 21:56:27 +0100 Subject: [PATCH] Implement moving on engine Thus make all state management the engine's responsibility, and remove the state maintaining code from frontend! --- adapter/adapter.py | 7 +++++++ frontend/main.js | 41 ++++++----------------------------------- frontend/visuals.js | 4 ++-- 3 files changed, 15 insertions(+), 37 deletions(-) diff --git a/adapter/adapter.py b/adapter/adapter.py index f48b95f..f4bf1ba 100644 --- a/adapter/adapter.py +++ b/adapter/adapter.py @@ -60,5 +60,12 @@ def get_moves(): return flask.jsonify(moves) +@app.route("/make_move/", methods=["POST"]) +def make_move(): + source, target = flask.request.json + state_str = ask_engine(f"make_move,{source},{target}") + return flask.jsonify(parse_state(state_str)) + + if __name__ == "__main__": app.run(debug=True, host="127.0.0.1", port=3000) diff --git a/frontend/main.js b/frontend/main.js index ccd2c21..82fe807 100644 --- a/frontend/main.js +++ b/frontend/main.js @@ -17,16 +17,13 @@ function post_json(url, json) { } class Backend { - constructor() { - this.config = new Configuration( - new Map(Object.entries(get_json("http://localhost:3000/get_state/"))) + getConfig() { + return new Map( + Object.entries(get_json("http://localhost:3000/get_state/")) ); } - getConfig() { - return this.config; - } makeMove(source, target) { - this.config.makeMove(source, target); + return post_json("http://localhost:3000/make_move/", [source, target]); } getAvailableMoves(position) { return post_json("http://localhost:3000/get_moves/", position); @@ -44,7 +41,7 @@ class Chess { this.canMoveTo = []; document.onclick = (ev) => this.click(ev); } - syncBackend() { + updateState() { let config = this.backend.getConfig(); this.configVis.configuration = config; this.configVis.draw(this.canvas); @@ -63,7 +60,7 @@ class Chess { } this.activeSquares.unselectSquare(this.canvas); this.activeSquares.unsetMoveSquares(this.canvas); - this.syncBackend(); + this.updateState(); this.moveSource = null; } click(ev) { @@ -82,30 +79,4 @@ class Chess { } } -class Configuration { - constructor(board) { - this.board = new Map(board); - } - getAt(position) { - if (!this.board.has(position)) { - return null; - } - return this.board.get(position); - } - setAt(position, piece) { - this.board.set(position, piece); - } - dropAt(position) { - this.board.delete(position); - } - makeMove(source, target) { - const piece = this.getAt(source); - if (piece === null) return; - else { - this.setAt(target, piece); - this.dropAt(source); - } - } -} - new Chess(); diff --git a/frontend/visuals.js b/frontend/visuals.js index ee86dcd..e9e795b 100644 --- a/frontend/visuals.js +++ b/frontend/visuals.js @@ -160,7 +160,7 @@ export class ConfigVis { this.piecesVis = new Map(); } draw(canvas) { - for (let [position, piece] of this.configuration.board) { + for (let [position, piece] of this.configuration) { if (this.piecesVis.has(position)) { if (this.piecesVis.get(position).piece == piece) { continue; @@ -175,7 +175,7 @@ export class ConfigVis { this.piecesVis.set(position, pv); } for (let [position, pv] of this.piecesVis) { - if (!this.configuration.board.has(position)) { + if (!this.configuration.has(position)) { pv.undraw(); this.piecesVis.delete(position); }