Handle starting command

This commit is contained in:
2025-12-31 16:04:06 +01:00
parent aeb4bd74d9
commit 6e781b464a
4 changed files with 102 additions and 56 deletions

View File

@@ -0,0 +1,53 @@
from flask import jsonify, request
from services.game_service import GameService
class GameController:
_game_service : GameService
_has_started : bool
_auth_token : str
def __init__(self, app):
self._game_service = GameService()
self._register_routes(app)
self.auth_token = "0eed89e8-7625-4f8d-bf2a-0872aede0efb"
self._has_started = False
def _register_routes(self, app):
app.add_url_rule("/command/party/start", view_func=self.start_party, methods=['POST'])
app.add_url_rule("/command/party/play", view_func=self.make_move, methods=['POST'])
def start_party(self):
try:
data = request.get_json()
if data is None:
raise Exception("Data must be provided")
white_name = data["white_name"]
black_name = data["black_name"]
time_control = int(data["time_control"])
increment = int(data["increment"])
self._game_service.start(white_name, black_name, time_control, increment)
return jsonify({"status": "ok", "message": "Game started"}), 200
except Exception as ex:
print(ex)
return jsonify({"status": "error", "message": f"An error occurred : {ex}"}), 500
def make_move(self):
try:
if not self._has_started:
jsonify({"status": "error", "message": "Game hasn't started yet"}), 400
auth_token = request.headers.get("Authorization")
if auth_token != self.auth_token:
return jsonify({"status": "error", "message": "Invalid authorization token"}), 401
self._game_service.make_move()
return jsonify({"status": "ok"}), 200
except Exception as ex:
print(ex)
return jsonify({"status": "error", "message": f"An error occurred : {ex}"}), 500