54 lines
1.1 KiB
Python
54 lines
1.1 KiB
Python
import os
|
|
import subprocess
|
|
|
|
import flask
|
|
from flask_cors import CORS
|
|
|
|
|
|
PIECE_TYPE = [("P", "pawn")]
|
|
COLOR = [
|
|
("W", "white"),
|
|
("B", "black"),
|
|
]
|
|
|
|
HERE = os.path.abspath(os.path.dirname(__file__))
|
|
app = flask.Flask(__name__)
|
|
CORS(app)
|
|
engine = subprocess.Popen(
|
|
[os.path.join(HERE, "../rs/target/debug/schach")],
|
|
stdin=subprocess.PIPE,
|
|
stdout=subprocess.PIPE,
|
|
)
|
|
|
|
|
|
def make_piece(piece_str):
|
|
piece_type, color = piece_str
|
|
return {
|
|
"piece_type": dict(PIECE_TYPE)[piece_type],
|
|
"color": dict(COLOR)[color],
|
|
}
|
|
|
|
|
|
def ask_engine(command):
|
|
engine.stdin.write(f"{command}\n".encode("ascii"))
|
|
engine.stdin.flush()
|
|
reply = engine.stdout.readline().decode("ascii").strip()
|
|
status, result = reply.split(",")
|
|
if status != "ok":
|
|
flask.abort(400)
|
|
return result
|
|
|
|
|
|
@app.route("/get_state/")
|
|
def get_state():
|
|
state_str = ask_engine("get_state")
|
|
state = {
|
|
state_str[i + 2 : i + 4]: make_piece(state_str[i : i + 2])
|
|
for i in range(0, len(state_str), 4)
|
|
}
|
|
return flask.jsonify(state)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
app.run(debug=True, host="127.0.0.1", port=3000)
|