Add game export
This commit is contained in:
@@ -1,12 +1,16 @@
|
||||
import json
|
||||
|
||||
from hardware.buzzer.buzzer import Buzzer
|
||||
from hardware.led.led import Led
|
||||
from models.exceptions.ServiceException import ServiceException
|
||||
from models.game import Game
|
||||
from services.clock_service import ClockService
|
||||
from services.detection_service import DetectionService
|
||||
|
||||
|
||||
class GameService:
|
||||
|
||||
_game : Game
|
||||
_detection_service : DetectionService
|
||||
_clock_service : ClockService
|
||||
_has_started : bool
|
||||
@@ -19,18 +23,25 @@ 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 :
|
||||
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()
|
||||
try :
|
||||
self._game = Game(white_name, back_name, time_control, increment)
|
||||
self._clock_service.start(time_control, increment)
|
||||
self._clock_service.set_on_terminated(self.stop)
|
||||
self._led.on()
|
||||
self._has_started = True
|
||||
except Exception as e:
|
||||
print(e)
|
||||
raise ServiceException(e)
|
||||
|
||||
def stop(self):
|
||||
self._clock_service.stop()
|
||||
self._detection_service.stop()
|
||||
self._led.off()
|
||||
self._buzzer.beep()
|
||||
self._has_started = False
|
||||
|
||||
@@ -39,7 +50,12 @@ class GameService:
|
||||
if not self._has_started :
|
||||
raise Exception("Game hasn't started yet.")
|
||||
self._clock_service.switch()
|
||||
return self._detection_service.analyze_single_frame()
|
||||
img, fen = self._detection_service.analyze_single_frame()
|
||||
self._game.add_move(fen)
|
||||
return img, fen
|
||||
except Exception as e:
|
||||
print(e)
|
||||
raise ServiceException(e)
|
||||
|
||||
def export_game(self):
|
||||
return json.dumps(self._game)
|
||||
|
||||
Reference in New Issue
Block a user