Trigger game stop

This commit is contained in:
2026-01-05 11:55:08 +01:00
parent e2679901f5
commit 6a47d83ada
2 changed files with 23 additions and 10 deletions

View File

@@ -18,6 +18,7 @@ class GameController:
def __init__(self, app : Flask, broker_service : MQTTService):
self._game_service = GameService()
self._game_service.set_on_terminated(self._stop)
self._broker_service = broker_service
self._register_routes(app)
self._auth_token = "0eed89e8-7625-4f8d-bf2a-0872aede0efb"
@@ -49,12 +50,9 @@ class GameController:
def stop_game(self):
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()
except Exception as ex:
print(ex)
except Exception as e:
print(e)
def make_move(self):
try:
@@ -90,3 +88,11 @@ class GameController:
print(response.status_code)
except Exception as 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)

View File

@@ -1,4 +1,5 @@
import json
from typing import Callable
from hardware.buzzer.buzzer import Buzzer
from hardware.led.led import Led
@@ -16,6 +17,7 @@ class GameService:
_has_started : bool
_led : Led
_buzzer : Buzzer
_on_terminated : Callable[[str], None]
def __init__(self):
self._detection_service = DetectionService()
@@ -23,7 +25,6 @@ class GameService:
self._has_started = False
self._led = Led(7)
self._buzzer = Buzzer(8)
self.game = None
def start(self, white_name, back_name, time_control : int, increment : int ) -> None:
if self._has_started :
@@ -42,7 +43,7 @@ class GameService:
self._clock_service.stop()
self._detection_service.stop()
self._led.off()
self._buzzer.beep()
self._notify()
self._has_started = False
def make_move(self) -> tuple[bytes, str] | None:
@@ -57,5 +58,11 @@ class GameService:
print(e)
raise ServiceException(e)
def export_game(self):
return json.dumps(self._game)
def set_on_terminated(self, callback: Callable[[str], None]):
self._on_terminated = callback
def _notify(self):
game_data = json.dumps(self._game)
self._on_terminated(game_data)