-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathstart_bot.py
73 lines (61 loc) · 2.54 KB
/
start_bot.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
#!python3
import sys
import asyncio
import logging
import argparse
import sc2
from sc2 import Race
from sc2.player import Bot
from bot import MyBot
def main(is_master, map_name, races, portconfig, replay_path, log_path, step_time_limit=None, game_time_limit=None):
portconfig = sc2.portconfig.Portconfig.from_json(portconfig)
i = 0 if is_master else 1
player_config = [Bot(Race[r], None) for r in races]
player_config[i].ai = MyBot()
if log_path is not None:
logger = logging.getLogger("sc2")
logger.setLevel(logging.DEBUG)
fh = logging.FileHandler(log_path)
fh.setLevel(logging.DEBUG)
logger.addHandler(fh)
if is_master:
g = sc2.main._host_game(
sc2.maps.get(map_name),
player_config,
realtime=False,
save_replay_as=replay_path,
portconfig=portconfig,
step_time_limit=step_time_limit,
game_time_limit=game_time_limit
)
else:
g = sc2.main._join_game(
player_config,
realtime=False,
save_replay_as=replay_path,
portconfig=portconfig,
step_time_limit=step_time_limit,
game_time_limit=game_time_limit
)
result = asyncio.get_event_loop().run_until_complete(g)
print(result)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Run a game against external server')
parser.add_argument('--master', action='store_true', help='this is the master (creates the game)')
parser.add_argument('--replay-path', nargs=1, default='replay.SC2Replay')
parser.add_argument('--log-path', nargs=1, default='-')
parser.add_argument('--step-time-limit', nargs=1, default=None, help="Max seconds per step (realtime seconds)")
parser.add_argument('--game-time-limit', nargs=1, default=None, help="Max seconds per game (gametime seconds)")
parser.add_argument('map_name', help='name of the game map')
parser.add_argument('races', help='player races as comma-separated list')
parser.add_argument('portconfig', help='port configuration as json')
args = parser.parse_args()
races = args.races.split(",")
assert all(r in ["Random", "Zerg", "Protoss", "Terran"] for r in races)
main(
args.master, args.map_name, races, args.portconfig,
args.replay_path[0],
args.log_path[0] if args.log_path[0] != "-" else None,
float(args.step_time_limit[0]) if args.step_time_limit else None,
float(args.game_time_limit[0]) if args.game_time_limit else None,
)