handle exceptions
This commit is contained in:
@@ -1,28 +1,40 @@
|
||||
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
|
||||
_detection_service : DetectionService
|
||||
_clock_service : ClockService
|
||||
_has_started : bool
|
||||
|
||||
def __init__(self):
|
||||
self.detection_service = DetectionService()
|
||||
self.clock_service = ClockService()
|
||||
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:
|
||||
self.clock_service.start(time_control, increment)
|
||||
self.clock_service.set_on_terminated(self.stop)
|
||||
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._clock_service.stop()
|
||||
self._detection_service.stop()
|
||||
self._has_started = False
|
||||
|
||||
def make_move(self) -> tuple[bytes, str] | None:
|
||||
try :
|
||||
self.clock_service.switch()
|
||||
return self.detection_service.analyze_single_frame()
|
||||
except Exception as e:
|
||||
print(e)
|
||||
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
|
||||
Reference in New Issue
Block a user