Skip to content

Commit 1b7d151

Browse files
committed
refactor: Return game message enum from instance manager
1 parent c8f049a commit 1b7d151

3 files changed

Lines changed: 51 additions & 18 deletions

File tree

BF2AutoSpectator/game/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from .instance_manager import GameInstanceManager
1+
from .instance_manager import GameInstanceManager, GameMessage
22
from .instance_state import GameInstanceState
33

4-
__all__ = ['GameInstanceManager', 'GameInstanceState']
4+
__all__ = ['GameInstanceManager', 'GameMessage', 'GameInstanceState']

BF2AutoSpectator/game/instance_manager.py

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import re
44
import subprocess
55
import time
6+
from enum import Enum
67
from typing import Tuple, Optional
78

89
import numpy as np
@@ -31,6 +32,19 @@
3132
MAP_NAME_REGEX_MULTI = re.compile(r'[-]{2,}')
3233

3334

35+
class GameMessage(str, Enum):
36+
ServerFull = 'server-full'
37+
Kicked = 'kicked'
38+
Banned = 'banned'
39+
ConnectionLost = 'connection-lost'
40+
ConnectionFailed = 'connection-failed'
41+
ModifiedContent = 'modified-content'
42+
InvalidIP = 'invalid-ip'
43+
ReadError = 'read-error'
44+
ConnectionRefused = 'connection-refused'
45+
Unknown = 'unknown'
46+
47+
3448
class GameInstanceManager:
3549
game_path: str
3650
player_name: str
@@ -194,15 +208,36 @@ def is_game_message_visible(self) -> bool:
194208
image_ops=[(ImageOperation.invert, None)]
195209
)
196210

197-
def ocr_game_message(self) -> str:
211+
def get_game_message(self) -> Tuple[GameMessage, str]:
198212
# Get ocr result of game message content region
199-
return ocr_screenshot_game_window_region(
213+
game_message = ocr_screenshot_game_window_region(
200214
self.game_window,
201215
self.resolution,
202216
'game-message-text',
203217
image_ops=[(ImageOperation.invert, None)]
204218
)
205219

220+
if 'full' in game_message:
221+
return GameMessage.ServerFull, game_message
222+
elif 'kicked' in game_message:
223+
return GameMessage.Kicked, game_message
224+
elif 'banned' in game_message:
225+
return GameMessage.Banned, game_message
226+
elif 'connection' in game_message and 'lost' in game_message:
227+
return GameMessage.ConnectionLost, game_message
228+
elif 'failed to connect' in game_message:
229+
return GameMessage.ConnectionFailed, game_message
230+
elif 'modified content' in game_message:
231+
return GameMessage.ModifiedContent, game_message
232+
elif 'invalid ip address' in game_message:
233+
return GameMessage.InvalidIP, game_message
234+
elif 'error reading from the server' in game_message:
235+
return GameMessage.ReadError, game_message
236+
elif 'server has refused the connection' in game_message:
237+
return GameMessage.ConnectionRefused, game_message
238+
239+
return GameMessage.Unknown, game_message
240+
206241
def is_in_menu(self) -> bool:
207242
# Get ocr result of quit menu item area
208243
return 'quit' in ocr_screenshot_game_window_region(

BF2AutoSpectator/spectate.py

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from BF2AutoSpectator.common.exceptions import SpawnCoordinatesNotAvailableException
1313
from BF2AutoSpectator.common.logger import logger
1414
from BF2AutoSpectator.common.utility import is_responding_pid, find_window_by_title, taskkill_pid, init_pytesseract
15-
from BF2AutoSpectator.game import GameInstanceManager
15+
from BF2AutoSpectator.game import GameInstanceManager, GameMessage
1616
from BF2AutoSpectator.remote import ControllerClient, GamePhase, OBSClient
1717
from BF2AutoSpectator.global_state import GlobalState
1818

@@ -367,41 +367,39 @@ def run():
367367

368368
continue
369369

370-
# Make sure we are still in the game
371370
if gim.is_game_message_visible():
372371
logger.debug('Game message present, ocr-ing message')
373-
game_message = gim.ocr_game_message()
372+
game_message, raw_message = gim.get_game_message()
374373

375-
if 'full' in game_message:
374+
if game_message is GameMessage.ServerFull:
376375
logger.warning('Server full, trying to rejoin in 20 seconds')
377376
gis.set_spectator_on_server(False)
378377
time.sleep(20)
379-
elif 'kicked' in game_message:
378+
elif game_message is GameMessage.Kicked:
380379
logger.warning('Got kicked, trying to rejoin')
381380
gis.set_spectator_on_server(False)
382-
elif 'banned' in game_message and not (gis.halted() and not gs.halted()):
381+
elif game_message is GameMessage.Banned and not (gis.halted() and not gs.halted()):
383382
logger.critical('Got banned, contact server admin')
384383
gis.set_spectator_on_server(False)
385384
gis.set_halted(True)
386385
gs.set_halted(True)
387-
elif 'connection' in game_message and 'lost' in game_message or \
388-
'failed to connect' in game_message:
389-
logger.error('Connection lost, trying to reconnect')
386+
elif game_message is GameMessage.ConnectionLost or game_message is GameMessage.ConnectionFailed:
387+
logger.error('Connection lost/failed, trying to reconnect')
390388
gis.set_spectator_on_server(False)
391-
elif 'modified content' in game_message:
389+
elif game_message is GameMessage.ModifiedContent:
392390
logger.warning('Got kicked for modified content, trying to rejoin')
393391
gis.set_spectator_on_server(False)
394-
elif 'invalid ip address' in game_message:
392+
elif game_message is GameMessage.InvalidIP:
395393
logger.error('Join by ip dialogue bugged, restart required')
396394
gis.set_error_restart_required(True)
397-
elif 'error reading from the server' in game_message:
395+
elif game_message is GameMessage.ReadError:
398396
logger.error('Error reading from GameSpy-ish backend, restart required')
399397
gis.set_error_restart_required(True)
400-
elif 'server has refused the connection' in game_message:
398+
elif game_message is GameMessage.ConnectionRefused:
401399
logger.error('Failed to connect to GameSpy-ish backend, restart required')
402400
gis.set_error_restart_required(True)
403401
elif not (gis.halted() and not gs.halted()):
404-
logger.critical(f'Unhandled game message: {game_message}')
402+
logger.critical(f'Unhandled game message: {raw_message}')
405403
gis.set_spectator_on_server(False)
406404
gis.set_halted(True)
407405
gs.set_halted(True)

0 commit comments

Comments
 (0)