Files
board-mate/rpi/services/game_service.py
2026-01-04 20:42:52 +01:00

46 lines
1.5 KiB
Python

from hardware.buzzer.buzzer import Buzzer
from hardware.led.led import Led
from models.exceptions.ServiceException import ServiceException
from services.clock_service import ClockService
from services.detection_service import DetectionService
class GameService:
_detection_service : DetectionService
_clock_service : ClockService
_has_started : bool
_led : Led
_buzzer : Buzzer
def __init__(self):
self._detection_service = DetectionService()
self._clock_service = ClockService()
self._has_started = False
self._led = Led(7)
self._buzzer = Buzzer(6)
def start(self, white_name, back_name, time_control : int, increment : int ) -> None:
if self._has_started :
raise ServiceException("Game has already started.")
self._clock_service.start(time_control, increment)
self._clock_service.set_on_terminated(self.stop)
self._has_started = True
self._led.on()
def stop(self):
self._clock_service.stop()
self._detection_service.stop()
self._buzzer.beep()
self._has_started = False
def make_move(self) -> tuple[bytes, str] | None:
try :
if not self._has_started :
raise Exception("Game hasn't started yet.")
self._clock_service.switch()
return self._detection_service.analyze_single_frame()
except Exception as e:
print(e)
raise ServiceException(e)