40 lines
1.3 KiB
Python
40 lines
1.3 KiB
Python
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
|
|
|
|
def __init__(self):
|
|
self._detection_service = DetectionService()
|
|
self._clock_service = ClockService()
|
|
self._has_started = False
|
|
|
|
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
|
|
|
|
def stop(self):
|
|
self._clock_service.stop()
|
|
self._detection_service.stop()
|
|
self._has_started = False
|
|
|
|
def make_move(self) -> tuple[bytes, str] | None:
|
|
try :
|
|
if not self._has_started :
|
|
raise ServiceException("Game hasn't started yet.")
|
|
self._clock_service.switch()
|
|
return self._detection_service.analyze_single_frame()
|
|
except ServiceException as se:
|
|
print(se)
|
|
return None
|
|
except Exception as ex:
|
|
print(ex)
|
|
return None |