Skip to content

Commit

Permalink
Notify OBS that SC2 is showing loading screen
Browse files Browse the repository at this point in the history
  • Loading branch information
manuelseeger committed Jul 22, 2024
1 parent 3b32122 commit f07e57c
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 3 deletions.
3 changes: 2 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
AICOACH_MONGO_DSN=
AICOACH_ASSISTANT_ID=
AICOACH_OPENAI_API_KEY=
AICOACH_OPENAI_ORG_ID=
AICOACH_OPENAI_ORG_ID=
AICOACH_OBS_WS_PW=
1 change: 1 addition & 0 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ class Config(BaseSettings):
sc2_client_url: str = "http://127.0.0.1:6119"
screenshot: str
tessdata_dir: str
obs_ws_pw: str | None

season: int

Expand Down
1 change: 1 addition & 0 deletions environment-cp311.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,4 @@ dependencies:
- pyodmongo
#- RealtimeTTS # install manually as it downgrades openai
- keyboard
- obsws-python
37 changes: 37 additions & 0 deletions obs_client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import obsws_python as obs
import click

from time import sleep

from obs_tools.sc2client import sc2client
from config import config
from obs_tools.types import UIInfo, Screen
from rich import print

@click.command()
def main():
with obs.ReqClient(host='localhost', port=4455, password=config.obs_ws_pw, timeout=3) as cl:
resp = cl.get_version()
print(f"OBS Version: {resp.obs_version}")

while True:
ui = sc2client.get_screens()

if len(ui.activeScreens) and Screen.loading in ui.activeScreens:
print(Screen.loading)

data = {
"message": Screen.loading
}

cl.call_vendor_request(vendor_name="AdvancedSceneSwitcher", request_type="AdvancedSceneSwitcherMessage", request_data=data)
sleep(5)
elif len(ui.activeScreens) == 0:
print("In game")
sleep(10)
else:
sleep(0.25)


if __name__ == '__main__':
main()
15 changes: 14 additions & 1 deletion obs_tools/sc2client.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from config import config

from .types import GameInfo, Result, ScanResult
from .types import GameInfo, Result, ScanResult, UIInfo, Screen

log = logging.getLogger(f"{config.name}.{__name__}")

Expand Down Expand Up @@ -43,6 +43,19 @@ def get_opponent_name(self, gameinfo=None) -> str:
if player.name != config.student.name:
return player.name
return None

def get_screens(self) -> UIInfo:
try:
response = requests.get(config.sc2_client_url + "/ui")
if response.status_code == 200:
try:
ui = UIInfo.model_validate_json(response.text)
return ui
except ValidationError as e:
log.warn(f"Invalid UI data: {e}")
except ConnectionError as e:
log.warn("Could not connect to SC2 game client, is SC2 running?")
return None

def wait_for_gameinfo(
self, timeout: int = 20, delay: float = 0.5, ongoing=False
Expand Down
23 changes: 22 additions & 1 deletion obs_tools/types.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,26 @@
from enum import Enum
from typing import List

from enum import Enum
from pydantic import BaseModel


class Screen(str, Enum):
loading = "ScreenLoading/ScreenLoading"
score = "ScreenScore/ScreenScore"
home = "ScreenHome/ScreenHome"
background = "ScreenBackgroundSC2/ScreenBackgroundSC2"
foreground = "ScreenForegroundSC2/ScreenForegroundSC2"
navigation = "ScreenNavigationSC2/ScreenNavigationSC2"
userprofile = "ScreenUserProfile/ScreenUserProfile"
multiplayer = "ScreenMultiplayer/ScreenMultiplayer"
single = "ScreenSingle/ScreenSingle"
collection = "ScreenCollection/ScreenCollection"
coopcampaign = "ScreenCoopCampaign/ScreenCoopCampaign"
custom = "ScreenCustom/ScreenCustom"
replay = "ScreenReplay/ScreenReplay"
battlelobby = "ScreenBattleLobby/ScreenBattleLobby"


class ScanResult(BaseModel):
mapname: str
opponent: str
Expand Down Expand Up @@ -82,3 +99,7 @@ def is_decided(self) -> bool:
and len(self.players) > 0
and all(player.result != Result.undecided for player in self.players)
)


class UIInfo(BaseModel):
activeScreens: List[Screen]

0 comments on commit f07e57c

Please sign in to comment.