Skip to content

Commit

Permalink
implement pause/resume
Browse files Browse the repository at this point in the history
  • Loading branch information
JarbasAl committed Dec 18, 2024
1 parent 6065645 commit acf3f03
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
6 changes: 6 additions & 0 deletions ovos_workshop/skills/common_play.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ def __init__(self, supported_media: List[MediaType] = None,
self.__resume_handler = None
self._stop_event = Event()
self._playing = Event()
self._paused = Event()
# TODO new default icon
self.skill_icon = skill_icon or ""

Expand Down Expand Up @@ -391,11 +392,13 @@ def __handle_ocp_play(self, message):
self.bus.emit(Message("ovos.common_play.player.state",
{"state": PlayerState.PLAYING}))
self._playing.set()
self._paused.clear()
else:
LOG.error(f"Playback requested but {self.skill_id} handler not "
"implemented")

def __handle_ocp_pause(self, message):
self._paused.set()
if self.__pause_handler:
if self.__pause_handler(message):
self.bus.emit(Message("ovos.common_play.player.state",
Expand All @@ -405,6 +408,7 @@ def __handle_ocp_pause(self, message):
"implemented")

def __handle_ocp_resume(self, message):
self._paused.clear()
if self.__resume_handler:
if self.__resume_handler(message):
self.bus.emit(Message("ovos.common_play.player.state",
Expand All @@ -430,6 +434,7 @@ def __handle_ocp_prev(self, message):
def __handle_ocp_stop(self, message):
# for skills managing their own playback
if self._playing.is_set():
self._paused.clear()
self.stop()
self.gui.release()
self.bus.emit(Message("ovos.common_play.player.state",
Expand Down Expand Up @@ -545,6 +550,7 @@ def __handle_ocp_featured(self, message):

def _handle_stop(self, message):
self._playing.clear()
self._paused.clear()
super()._handle_stop(message)

def default_shutdown(self):
Expand Down
21 changes: 18 additions & 3 deletions ovos_workshop/skills/game_skill.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ def _ocp_search(self, phrase: str, media_type: MediaType) -> Iterable[MediaEntry
def is_playing(self) -> bool:
return self._playing.is_set()

@property
def is_paused(self) -> bool:
return self._paused.is_set()

@abc.abstractmethod
def on_play_game(self):
"""called by ocp_pipeline when 'play XXX' matches the game"""
Expand Down Expand Up @@ -138,13 +142,17 @@ class ConversationalGameSkill(OVOSGameSkill):
def on_play_game(self):
"""called by ocp_pipeline when 'play XXX' matches the game"""

@abc.abstractmethod
def on_pause_game(self):
"""called by ocp_pipeline on 'pause' if game is being played"""
# TODO - default dialog/sound
self._paused.set()
self.acknowledge()

@abc.abstractmethod
def on_resume_game(self):
"""called by ocp_pipeline on 'resume/unpause' if game is being played and paused"""
# TODO - default dialog/sound
self._paused.clear()
self.acknowledge()

@abc.abstractmethod
def on_stop_game(self):
Expand Down Expand Up @@ -193,6 +201,10 @@ def skill_will_trigger(self, utterance: str, lang: str, skill_id: Optional[str]

def converse(self, message: Message):
try:
if self.is_paused:
# let ocp_pipeline unpause as appropriate
return False

utterance = message.data["utterances"][0]
lang = get_message_lang(message)
# let the user implemented intents do the job if they can handle the utterance
Expand All @@ -214,7 +226,10 @@ def handle_deactivate(self, message: Message):
means the user didn't interact with the game for a long time and intent parser will be released
"""
try:
if self.is_playing:
if self.is_paused:
self.log.info("Game is paused, keeping it active")
self.activate() # keep the game in active skills list so it can still converse
elif self.is_playing:
self.log.info("Game abandoned due to inactivity")
self.on_abandon_game()
self.on_stop_game()
Expand Down

0 comments on commit acf3f03

Please sign in to comment.