Trigger game stop
This commit is contained in:
@@ -18,6 +18,7 @@ class GameController:
|
|||||||
|
|
||||||
def __init__(self, app : Flask, broker_service : MQTTService):
|
def __init__(self, app : Flask, broker_service : MQTTService):
|
||||||
self._game_service = GameService()
|
self._game_service = GameService()
|
||||||
|
self._game_service.set_on_terminated(self._stop)
|
||||||
self._broker_service = broker_service
|
self._broker_service = broker_service
|
||||||
self._register_routes(app)
|
self._register_routes(app)
|
||||||
self._auth_token = "0eed89e8-7625-4f8d-bf2a-0872aede0efb"
|
self._auth_token = "0eed89e8-7625-4f8d-bf2a-0872aede0efb"
|
||||||
@@ -49,12 +50,9 @@ class GameController:
|
|||||||
|
|
||||||
def stop_game(self):
|
def stop_game(self):
|
||||||
try :
|
try :
|
||||||
game_data = self._game_service.export_game()
|
|
||||||
print(f"Exporting game data : {game_data}")
|
|
||||||
self._broker_service.publish("/customer/game/data", game_data, 2)
|
|
||||||
self._game_service.stop()
|
self._game_service.stop()
|
||||||
except Exception as ex:
|
except Exception as e:
|
||||||
print(ex)
|
print(e)
|
||||||
|
|
||||||
def make_move(self):
|
def make_move(self):
|
||||||
try:
|
try:
|
||||||
@@ -89,4 +87,12 @@ class GameController:
|
|||||||
response = requests.post(url, json=payload, verify=False)
|
response = requests.post(url, json=payload, verify=False)
|
||||||
print(response.status_code)
|
print(response.status_code)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(e)
|
print(e)
|
||||||
|
|
||||||
|
def _stop(self, game_data : str):
|
||||||
|
try :
|
||||||
|
print(f"Exporting game data : {game_data}")
|
||||||
|
self._broker_service.publish("/customer/game/data", game_data, 2)
|
||||||
|
self._game_service.stop()
|
||||||
|
except Exception as ex:
|
||||||
|
print(ex)
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import json
|
import json
|
||||||
|
from typing import Callable
|
||||||
|
|
||||||
from hardware.buzzer.buzzer import Buzzer
|
from hardware.buzzer.buzzer import Buzzer
|
||||||
from hardware.led.led import Led
|
from hardware.led.led import Led
|
||||||
@@ -16,6 +17,7 @@ class GameService:
|
|||||||
_has_started : bool
|
_has_started : bool
|
||||||
_led : Led
|
_led : Led
|
||||||
_buzzer : Buzzer
|
_buzzer : Buzzer
|
||||||
|
_on_terminated : Callable[[str], None]
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self._detection_service = DetectionService()
|
self._detection_service = DetectionService()
|
||||||
@@ -23,7 +25,6 @@ class GameService:
|
|||||||
self._has_started = False
|
self._has_started = False
|
||||||
self._led = Led(7)
|
self._led = Led(7)
|
||||||
self._buzzer = Buzzer(8)
|
self._buzzer = Buzzer(8)
|
||||||
self.game = None
|
|
||||||
|
|
||||||
def start(self, white_name, back_name, time_control : int, increment : int ) -> None:
|
def start(self, white_name, back_name, time_control : int, increment : int ) -> None:
|
||||||
if self._has_started :
|
if self._has_started :
|
||||||
@@ -42,7 +43,7 @@ class GameService:
|
|||||||
self._clock_service.stop()
|
self._clock_service.stop()
|
||||||
self._detection_service.stop()
|
self._detection_service.stop()
|
||||||
self._led.off()
|
self._led.off()
|
||||||
self._buzzer.beep()
|
self._notify()
|
||||||
self._has_started = False
|
self._has_started = False
|
||||||
|
|
||||||
def make_move(self) -> tuple[bytes, str] | None:
|
def make_move(self) -> tuple[bytes, str] | None:
|
||||||
@@ -57,5 +58,11 @@ class GameService:
|
|||||||
print(e)
|
print(e)
|
||||||
raise ServiceException(e)
|
raise ServiceException(e)
|
||||||
|
|
||||||
def export_game(self):
|
def set_on_terminated(self, callback: Callable[[str], None]):
|
||||||
return json.dumps(self._game)
|
self._on_terminated = callback
|
||||||
|
|
||||||
|
def _notify(self):
|
||||||
|
game_data = json.dumps(self._game)
|
||||||
|
self._on_terminated(game_data)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user