From e68a15a63eaa0ea2e6a5a5d989312204e0fccf0f Mon Sep 17 00:00:00 2001 From: Terry Bolt Date: Thu, 7 May 2015 19:20:20 +0100 Subject: [PATCH 1/3] Added game_depth and engine_colour to api.models.Game. Closes #3 and closes #4. --- api/models.py | 2 ++ api/views.py | 33 +++++++++++++++++++++------------ 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/api/models.py b/api/models.py index e91e527..20298fb 100644 --- a/api/models.py +++ b/api/models.py @@ -12,3 +12,5 @@ class Game(models.Model): exit_reason = models.IntegerField(default=-1) plies_moved = models.IntegerField(default=0) engine_scores = models.TextField() + engine_colour = models.IntegerField(default=-1) # 0=White, 1=Black + game_depth = models.IntegerField(default=-1) diff --git a/api/views.py b/api/views.py index c0732f0..a8dc338 100644 --- a/api/views.py +++ b/api/views.py @@ -25,10 +25,18 @@ def version(request): def init(request): if request.method == "GET": elo = int(request.GET.get("elo", -1)) + depth = int(request.GET.get("depth", -1)) + colour = int(request.GET.get("engine_colour", -1)) if elo < 0: return JsonResponse({"status": "error", - "error_desc": "Elo missing or invalid in request."}) - game = Game(player_elo=elo) + "error_desc": "Elo missing or invalid in request."}) + if depth < 0: + return JsonResponse({"status": "error", + "error_desc": "Depth missing or invalid in request."}) + if colour < 0: + return JsonResponse({"status": "error", + "error_desc": "Engine colour missing or invalid in request."}) + game = Game(player_elo=elo, engine_colour=colour, game_depth=depth) game.save() return JsonResponse({"status": "ok", "id": game.id}) @@ -37,21 +45,22 @@ def move(request): game_id = int(request.GET.get("id", -1)) if game_id < 0: return JsonResponse({"status": "error", - "error_desc": "ID missing or invalid in request."}) + "error_desc": "ID missing or invalid in request."}) position = str(request.GET.get("position", "")) if position == "": return JsonResponse({"status": "error", - "error_desc": "Position missing in request."}) + "error_desc": "Position missing in request."}) try: chess.Board(fen=position) except ValueError: return JsonResponse({"status": "error", - "error_desc": "Invalid position in request."}) + "error_desc": "Invalid position in request."}) depth = int(request.GET.get("depth", 7)) - if depth > 7: - depth = 7 + if depth > 7 or depth < 1: + return JsonResponse({"status": "error", + "error_desc": "Depth missing or invalid in request."}) try: game = Game.objects.get(id=game_id) @@ -78,26 +87,26 @@ def quit(request): game_id = int(request.GET.get("id", -1)) if game_id < 0: return JsonResponse({"status": "error", - "error_desc": "ID missing or invalid in request."}) + "error_desc": "ID missing or invalid in request."}) exit_reason = int(request.GET.get("reason", -1)) if exit_reason < 0 or exit_reason > 7: return JsonResponse({"status": "error", - "error_desc": "Reason missing or invalid in request."}) + "error_desc": "Reason missing or invalid in request."}) plies_moved = int(request.GET.get("plies", -1)) if plies_moved < 0: return JsonResponse({"status": "error", - "error_desc": "Number of plies missing or invalid in request."}) + "error_desc": "Number of plies missing or invalid in request."}) try: game = Game.objects.get(id=game_id) if game.game_over: return JsonResponse({"status": "error", - "error_desc": "A game with that ID does not exist."}) + "error_desc": "A game with that ID does not exist."}) except Game.DoesNotExist: return JsonResponse({"status": "error", - "error_desc": "A game with that ID does not exist."}) + "error_desc": "A game with that ID does not exist."}) game.exit_reason = exit_reason game.plies_moved = plies_moved From 1eebd4ee160e823ce948e9336a6c08b25b1c05ca Mon Sep 17 00:00:00 2001 From: Terry Bolt Date: Thu, 7 May 2015 19:29:17 +0100 Subject: [PATCH 2/3] Updated version to 0.2.0 and moved version info from config.json to api/__init__.py. --- api/__init__.py | 4 +++- api/views.py | 8 ++++++-- config.json | 4 ---- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/api/__init__.py b/api/__init__.py index 8b13789..4a1a4cd 100644 --- a/api/__init__.py +++ b/api/__init__.py @@ -1 +1,3 @@ - +VERSION_MAJOR = 0 +VERSION_MINOR = 2 +VERSION_HOTFIX = 0 diff --git a/api/views.py b/api/views.py index a8dc338..47e4942 100644 --- a/api/views.py +++ b/api/views.py @@ -13,14 +13,18 @@ from .models import Game from api.chess_engine_pool import Move +from api import VERSION_MAJOR, VERSION_MINOR, VERSION_HOTFIX rpyc.core.protocol.DEFAULT_CONFIG["allow_pickle"] = True rpyc.core.protocol.DEFAULT_CONFIG["allow_public_attrs"] = True def version(request): if request.method == "GET": - config = json.load(open(os.getcwd() + "/config.json")) - return JsonResponse(config["version"]) + return JsonResponse({ + "major": VERSION_MAJOR, + "minor": VERSION_MINOR, + "hotfix": VERSION_HOTFIX + }) def init(request): if request.method == "GET": diff --git a/config.json b/config.json index c9186ea..50fbaea 100644 --- a/config.json +++ b/config.json @@ -1,7 +1,3 @@ { - "version": { - "major": 0, - "minor": 1 - }, "engine_filename": "./Saruman" } From 95843b1c9182700096d36c318fba8ce6453e1cb7 Mon Sep 17 00:00:00 2001 From: Terry Bolt Date: Thu, 7 May 2015 19:36:25 +0100 Subject: [PATCH 3/3] Added stricter FEN validation. --- api/views.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/api/views.py b/api/views.py index 47e4942..88344b8 100644 --- a/api/views.py +++ b/api/views.py @@ -56,7 +56,9 @@ def move(request): return JsonResponse({"status": "error", "error_desc": "Position missing in request."}) try: - chess.Board(fen=position) + temp_board = chess.Board(fen=position) + if temp_board.status() != chess.STATUS_VALID: + raise ValueError("Invalid FEN.") except ValueError: return JsonResponse({"status": "error", "error_desc": "Invalid position in request."})