Skip to content

Commit

Permalink
feat: Leave stream running for a grace period when halted
Browse files Browse the repository at this point in the history
  • Loading branch information
cetteup committed Feb 8, 2024
1 parent e3d46a9 commit 0a05894
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
17 changes: 11 additions & 6 deletions BF2AutoSpectator/game/instance_state.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from datetime import datetime, timedelta
from typing import Tuple
from typing import Tuple, Optional


class GameInstanceState:
Expand Down Expand Up @@ -39,7 +39,7 @@ class GameInstanceState:
# Error details
__error_unresponsive_count = 0
__error_restart_required: bool = False
__halted: bool = False
__halted_since: Optional[datetime] = None

# Global getter/setter functions
def set_spectator_on_server(self, spectator_on_server: bool):
Expand Down Expand Up @@ -187,10 +187,15 @@ def error_restart_required(self) -> bool:
return self.__error_restart_required

def set_halted(self, halted: bool):
self.__halted = halted
# Don't overwrite any existing timestamp
if self.__halted_since is not None:
return
self.__halted_since = datetime.now() if halted else None

def halted(self) -> bool:
return self.__halted
def halted(self, grace_period: float = 0.0) -> bool:
if self.__halted_since is None:
return False
return datetime.now() >= self.__halted_since + timedelta(seconds=grace_period)

# Reset relevant fields after map rotation
def map_rotation_reset(self):
Expand Down Expand Up @@ -242,4 +247,4 @@ def restart_reset(self):
self.__iterations_on_spawn_menu = 0
self.__error_unresponsive_count = 0
self.__error_restart_required = False
self.__halted = False
self.__halted_since = None
4 changes: 3 additions & 1 deletion BF2AutoSpectator/spectate.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,9 @@ def run():
logger.error('Failed to check OBS stream status')
logger.error(str(e))

if streaming is True and (stopped or gis.halted()):
# When halted, only stop stream after a 180-second grace period to make the reason (error message)
# visible for viewers and/or allow controller to initiate a server switch, resolving the halted state
if streaming is True and (stopped or gis.halted(grace_period=180)):
# Stop stream when stopped or game instance is halted
logger.info('Stopping OBS stream')
try:
Expand Down

0 comments on commit 0a05894

Please sign in to comment.