@@ -7,6 +7,15 @@ function get_json(url) {
|
||||
return JSON.parse(request.responseText);
|
||||
}
|
||||
|
||||
function post_json(url, json) {
|
||||
var request = new XMLHttpRequest();
|
||||
request.open("post", url, false);
|
||||
request.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
|
||||
request.send(JSON.stringify(json));
|
||||
if (request.status != 200) return null;
|
||||
return JSON.parse(request.responseText);
|
||||
}
|
||||
|
||||
class Backend {
|
||||
constructor() {
|
||||
this.config = new Configuration(
|
||||
@@ -20,8 +29,7 @@ class Backend {
|
||||
this.config.makeMove(source, target);
|
||||
}
|
||||
getAvailableMoves(position) {
|
||||
position;
|
||||
return [];
|
||||
return post_json("http://localhost:3000/get_moves/", position);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -46,6 +54,7 @@ class Chess {
|
||||
let validMoves = this.backend.getAvailableMoves(source);
|
||||
if (validMoves === null) return;
|
||||
this.activeSquares.selectSquare(this.canvas, source);
|
||||
this.activeSquares.setMoveSquares(this.canvas, validMoves);
|
||||
this.moveSource = source;
|
||||
}
|
||||
finalizeMove(target) {
|
||||
@@ -53,6 +62,7 @@ class Chess {
|
||||
this.backend.makeMove(this.moveSource, target);
|
||||
}
|
||||
this.activeSquares.unselectSquare(this.canvas);
|
||||
this.activeSquares.unsetMoveSquares(this.canvas);
|
||||
this.syncBackend();
|
||||
this.moveSource = null;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user