forked from BurnySc2/python-sc2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontroller.py
79 lines (64 loc) · 2.98 KB
/
controller.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
from s2clientprotocol import sc2api_pb2 as sc_pb
from .player import Computer
from .protocol import Protocol
from pathlib import Path
import platform
from loguru import logger
class Controller(Protocol):
def __init__(self, ws, process):
super().__init__(ws)
self._process = process
@property
def running(self):
return self._process._process is not None
async def create_game(self, game_map, players, realtime: bool, random_seed=None, disable_fog=None):
req = sc_pb.RequestCreateGame(
local_map=sc_pb.LocalMap(map_path=str(game_map.relative_path)), realtime=realtime, disable_fog=disable_fog
)
if random_seed is not None:
req.random_seed = random_seed
for player in players:
p = req.player_setup.add()
p.type = player.type.value
if isinstance(player, Computer):
p.race = player.race.value
p.difficulty = player.difficulty.value
p.ai_build = player.ai_build.value
logger.info("Creating new game")
logger.info(f"Map: {game_map.name}")
logger.info(f"Players: {', '.join(str(p) for p in players)}")
result = await self._execute(create_game=req)
return result
async def request_available_maps(self):
req = sc_pb.RequestAvailableMaps()
result = await self._execute(available_maps=req)
return result
async def request_save_map(self, download_path: str):
""" Not working on linux. """
req = sc_pb.RequestSaveMap(map_path=download_path)
result = await self._execute(save_map=req)
return result
async def request_replay_info(self, replay_path: str):
""" Not working on linux. """
req = sc_pb.RequestReplayInfo(replay_path=replay_path, download_data=False)
result = await self._execute(replay_info=req)
return result
async def start_replay(self, replay_path: str, realtime: bool, observed_id: int = 0):
ifopts = sc_pb.InterfaceOptions(
raw=True, score=True, show_cloaked=True, raw_affects_selection=True, raw_crop_to_playable_area=False
)
if platform.system() == "Linux":
replay_name = Path(replay_path).name
home_replay_folder = Path.home() / "Documents" / "StarCraft II" / "Replays"
if str(home_replay_folder / replay_name) != replay_path:
logger.warning(
f"Linux detected, please put your replay in your home directory at {home_replay_folder}. It was detected at {replay_path}"
)
raise FileNotFoundError
replay_path = replay_name
req = sc_pb.RequestStartReplay(
replay_path=replay_path, observed_player_id=observed_id, realtime=realtime, options=ifopts
)
result = await self._execute(start_replay=req)
assert result.status == 4, f"{result.start_replay.error} - {result.start_replay.error_details}"
return result