forked from BurnySc2/python-sc2
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontroller.py
More file actions
Latest commit
79 lines (64 loc) · 2.98 KB
/
Copy pathcontroller.py
File metadata and controls
79 lines (64 loc) · 2.98 KB
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
froms2clientprotocolimportsc2api_pb2assc_pb
from .playerimportComputer
from .protocolimportProtocol
frompathlibimportPath
importplatform
fromloguruimportlogger
classController(Protocol):
def__init__(self, ws, process):
super().__init__(ws)
self._process=process
@property
defrunning(self):
returnself._process._processisnotNone
asyncdefcreate_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
)
ifrandom_seedisnotNone:
req.random_seed=random_seed
forplayerinplayers:
p=req.player_setup.add()
p.type=player.type.value
ifisinstance(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) forpinplayers)}")
result=awaitself._execute(create_game=req)
returnresult
asyncdefrequest_available_maps(self):
req=sc_pb.RequestAvailableMaps()
result=awaitself._execute(available_maps=req)
returnresult
asyncdefrequest_save_map(self, download_path: str):
""" Not working on linux. """
req=sc_pb.RequestSaveMap(map_path=download_path)
result=awaitself._execute(save_map=req)
returnresult
asyncdefrequest_replay_info(self, replay_path: str):
""" Not working on linux. """
req=sc_pb.RequestReplayInfo(replay_path=replay_path, download_data=False)
result=awaitself._execute(replay_info=req)
returnresult
asyncdefstart_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
)
ifplatform.system() =="Linux":
replay_name=Path(replay_path).name
home_replay_folder=Path.home() /"Documents"/"StarCraft II"/"Replays"
ifstr(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}"
)
raiseFileNotFoundError
replay_path=replay_name
req=sc_pb.RequestStartReplay(
replay_path=replay_path, observed_player_id=observed_id, realtime=realtime, options=ifopts
)
result=awaitself._execute(start_replay=req)
assertresult.status==4, f"{result.start_replay.error} - {result.start_replay.error_details}"
returnresult