This commit is contained in:
hagi 2022-12-30 13:27:37 +01:00
parent bd1e172960
commit 612a639a78
4 changed files with 75 additions and 17 deletions

View file

@ -5,12 +5,16 @@ API_TOKEN = "blunderboard-security-token"
# Set the API endpoint URL # Set the API endpoint URL
url = "http://5.75.138.151:5000/api/get_evaluation" url = "http://5.75.138.151:5000/api/get_evaluation"
wdl_api = "http://5.75.138.151:5000/api/get_wdl"
# Set the chess position and search depth # Set the chess position and search depth
data = {'position': 'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1', 'depth': 20} data = {
"position": "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1",
"depth": 20,
}
# Set the API token in the request header # Set the API token in the request header
headers = {'Authorization': API_TOKEN} headers = {"Authorization": API_TOKEN}
# Send a POST request to the API endpoint # Send a POST request to the API endpoint
response = requests.post(url, json=data, headers=headers) response = requests.post(url, json=data, headers=headers)
@ -20,4 +24,20 @@ if response.status_code == 200:
print(response.json()) print(response.json())
else: else:
print("Error: " + response.json()['error']) print("Error: " + response.json()["error"])
def api_wdl() -> str:
"""
Returns the current wdl from the REST API
:return: str
"""
# Send a POST request to the API endpoint
response2 = requests.post(wdl_api, json=data, headers=headers)
if response2.status_code == 200:
return response2.json()
else:
print("API Error: " + response2.json()["error"])
return "API Error"
print(api_wdl())

View file

@ -27,7 +27,7 @@ default_engine_settings = {
"UCI_LimitStrength": "false", "UCI_LimitStrength": "false",
"UCI_Elo": 1350, "UCI_Elo": 1350,
# "NNUE": "true", # TODO Find out if NNUE can be used with the python wrapper # "NNUE": "true", # TODO Find out if NNUE can be used with the python wrapper
} }
# Create a Stockfish chess engine instance # Create a Stockfish chess engine instance
engine = Stockfish(path="/usr/bin/stockfish") engine = Stockfish(path="/usr/bin/stockfish")
@ -35,19 +35,20 @@ engine = Stockfish(path="/usr/bin/stockfish")
# Set the engine settings # Set the engine settings
engine.update_engine_parameters(default_engine_settings) engine.update_engine_parameters(default_engine_settings)
@app.route('/api/get_evaluation', methods=['POST'])
@app.route("/api/get_evaluation", methods=["POST"])
def get_evaluation(): def get_evaluation():
# Get the API token from the request header # Get the API token from the request header
token = request.headers.get('Authorization') token = request.headers.get("Authorization")
# If the API token is not provided or is invalid, return an error # If the API token is not provided or is invalid, return an error
if token != API_TOKEN: if token != API_TOKEN:
return jsonify({'error': 'Invalid API token'}), 401 return jsonify({"error": "Invalid API token"}), 401
# Get the current chess position and the desired depth from the request body # Get the current chess position and the desired depth from the request body
data = request.get_json() data = request.get_json()
position = data.get('position') position = data.get("position")
depth = data.get('depth') depth = data.get("depth")
# Set the position and search depth in the chess engine # Set the position and search depth in the chess engine
engine.set_fen_position(position) engine.set_fen_position(position)
@ -59,7 +60,6 @@ def get_evaluation():
# Return the evaluation to the client # Return the evaluation to the client
return evaluation return evaluation
if __name__ == '__main__':
if __name__ == "__main__":
app.run(host="") app.run(host="")

View file

@ -22,6 +22,7 @@ install_requires =
pygame pygame
RPi.GPIO RPi.GPIO
stockfish stockfish
requests
[options.packages.find] [options.packages.find]
where = src where = src
@ -61,3 +62,5 @@ max_line_length = 88
[mypy] [mypy]
check_untyped_defs = True check_untyped_defs = True
ignore_missing_imports = True ignore_missing_imports = True
install_types = True
non_interactive = True

View file

@ -4,8 +4,12 @@ from pygame import mixer
import random import random
from stockfish import Stockfish from stockfish import Stockfish
import sys import sys
import requests
API_TOKEN = "blunderboard-security-token"
sound_path = Path(sys.prefix) / "share" / "blunderboard" / "sounds" sound_path = Path(sys.prefix) / "share" / "blunderboard" / "sounds"
api = "http://5.75.138.151:5000/api/get_evaluation"
wdl_api = "http://5.75.138.151:5000/api/get_wdl"
class BlunderEvaluator: class BlunderEvaluator:
@ -37,12 +41,11 @@ class BlunderEvaluator:
self.settings = engine_settings self.settings = engine_settings
self.engine.update_engine_parameters(self.settings) self.engine.update_engine_parameters(self.settings)
self.engine.set_position() self.engine.set_position()
self.current_evaluation = ( self.current_evaluation = self.api_evaluation()
self.engine.get_evaluation()
) # This is not necessary, now that I think about it.
self.evaluations: list[dict] = [] self.evaluations: list[dict] = []
self.current_wdl = self.engine.get_wdl_stats() self.current_wdl = self.engine.get_wdl_stats()
self.wdls: list[tuple[int, int, int]] = [] self.wdls: list = []
self.current_fen = self.engine.get_fen_position()
def reset(self): def reset(self):
self.engine.set_position() self.engine.set_position()
@ -57,7 +60,7 @@ class BlunderEvaluator:
self.engine.make_moves_from_current_position([move]) self.engine.make_moves_from_current_position([move])
self.current_evaluation = self.engine.get_evaluation() self.current_evaluation = self.engine.get_evaluation()
self.evaluations.append(self.current_evaluation) self.evaluations.append(self.current_evaluation)
self.current_wdl = self.engine.get_wdl_stats() self.current_wdl = self.api_wdl()
self.wdls.append(self.current_wdl) self.wdls.append(self.current_wdl)
print(self.current_wdl) print(self.current_wdl)
print(self.current_evaluation) print(self.current_evaluation)
@ -70,6 +73,38 @@ class BlunderEvaluator:
print("Invalid move") print("Invalid move")
self.play_sound("illegal") self.play_sound("illegal")
def api_evaluation(self) -> dict:
"""
Returns the current evaluation from the REST API
:return: str
"""
data = {"position": self.engine.get_fen_position(), "depth": 20}
# Set the API token in the request header
headers = {"Authorization": API_TOKEN}
# Send a POST request to the API endpoint
response = requests.post(api, json=data, headers=headers)
if response.status_code == 200:
return response.json()["value"]
else:
print("API Error: " + response.json()["error"])
return {"NOPE": "PLEASE CRASH"}
def api_wdl(self) -> str:
"""
Returns the current wdl from the REST API
:return: str
"""
data = {"position": self.engine.get_fen_position(), "depth": 20}
# Set the API token in the request header
headers = {"Authorization": API_TOKEN}
# Send a POST request to the API endpoint
response = requests.post(wdl_api, json=data, headers=headers)
if response.status_code == 200:
return response.json()
else:
print("API Error: " + response.json()["error"])
return "API Error"
def move_was_blunder(self) -> bool: def move_was_blunder(self) -> bool:
""" """
Returns true if the last move was a blunder Returns true if the last move was a blunder