Skip to content

Commit e4f7670

Browse files
committed
Fix types for example folder
1 parent a1801a5 commit e4f7670

20 files changed

Lines changed: 193 additions & 170 deletions

examples/bot_vs_bot.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616

1717
def main_old():
18-
result: list[Result] = run_game(
18+
result: Result | list[Result | None] = run_game(
1919
maps.get("AcropolisLE"),
2020
[
2121
Bot(Race.Protoss, WarpGateBot()),

examples/competitive/__init__.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
import aiohttp
55
from loguru import logger
66

7-
import sc2
87
from sc2.client import Client
8+
from sc2.main import _play_game
9+
from sc2.portconfig import Portconfig
910
from sc2.protocol import ConnectionAlreadyClosedError
1011

1112

@@ -41,7 +42,7 @@ def run_ladder_game(bot):
4142
else:
4243
ports = [lan_port + p for p in range(1, 6)]
4344

44-
portconfig = sc2.portconfig.Portconfig()
45+
portconfig = Portconfig()
4546
portconfig.server = [ports[1], ports[2]]
4647
portconfig.players = [[ports[3], ports[4]]]
4748

@@ -56,10 +57,11 @@ def run_ladder_game(bot):
5657
# Modified version of sc2.main._join_game to allow custom host and port, and to not spawn an additional sc2process (thanks to alkurbatov for fix)
5758
async def join_ladder_game(host, port, players, realtime, portconfig, save_replay_as=None, game_time_limit=None):
5859
ws_url = f"ws://{host}:{port}/sc2api"
60+
# pyrefly: ignore
5961
ws_connection = await aiohttp.ClientSession().ws_connect(ws_url, timeout=120)
6062
client = Client(ws_connection)
6163
try:
62-
result = await sc2.main._play_game(players[0], client, realtime, portconfig, game_time_limit)
64+
result = await _play_game(players[0], client, realtime, portconfig, game_time_limit)
6365
if save_replay_as is not None:
6466
await client.save_replay(save_replay_as)
6567
# await client.leave()
@@ -68,6 +70,6 @@ async def join_ladder_game(host, port, players, realtime, portconfig, save_repla
6870
logger.error("Connection was closed before the game ended")
6971
return None
7072
finally:
71-
ws_connection.close()
73+
await ws_connection.close()
7274

7375
return result

examples/competitive/run.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import sys
22

3-
from __init__ import run_ladder_game
3+
from examples.competitive.__init__ import run_ladder_game
44

55
# Load bot
6-
from bot import CompetitiveBot
6+
from examples.competitive.bot import CompetitiveBot
77

88
from sc2 import maps
99
from sc2.data import Difficulty, Race

examples/fastreload.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ def main():
2020
input("Press enter to reload ")
2121

2222
reload(zerg_rush)
23-
player_config[0].ai = zerg_rush.ZergRushBot()
23+
if isinstance(player_config[0], Bot):
24+
player_config[0].ai = zerg_rush.ZergRushBot()
2425
gen.send(player_config)
2526

2627

examples/host_external_norestart.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ def main():
1010
portconfig: Portconfig = Portconfig()
1111
print(portconfig.as_json)
1212

13+
# pyrefly: ignore
1314
player_config = [Bot(Race.Zerg, ZergRushBot()), Bot(Race.Zerg, None)]
1415

1516
for g in _host_game_iter(maps.get("Abyssal Reef LE"), player_config, realtime=False, portconfig=portconfig):

examples/protoss/find_adept_shades.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def __init__(self):
1818
async def on_start(self):
1919
self.client.game_step = 2
2020
await self.client.debug_create_unit(
21-
[[UnitTypeId.ADEPT, 10, self.townhalls[0].position.towards(self.game_info.map_center, 5), 1]]
21+
[(UnitTypeId.ADEPT, 10, self.townhalls[0].position.towards(self.game_info.map_center, 5), 1)]
2222
)
2323

2424
async def on_step(self, iteration: int):

examples/protoss/threebase_voidray.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -94,16 +94,12 @@ async def on_step(self, iteration: int):
9494
for nexus in self.townhalls.ready:
9595
vgs = self.vespene_geyser.closer_than(15, nexus)
9696
for vg in vgs:
97-
if not self.can_afford(UnitTypeId.ASSIMILATOR):
98-
break
99-
100-
worker = self.select_build_worker(vg.position)
101-
if worker is None:
102-
break
103-
104-
if not self.gas_buildings or not self.gas_buildings.closer_than(1, vg):
105-
worker.build_gas(vg)
106-
worker.stop(queue=True)
97+
if self.can_afford(UnitTypeId.ASSIMILATOR):
98+
worker = self.select_build_worker(vg.position)
99+
if worker is not None:
100+
if not self.gas_buildings or not self.gas_buildings.closer_than(1, vg):
101+
worker.build_gas(vg)
102+
worker.stop(queue=True)
107103

108104
# If we have less than 3 but at least 3 nexuses, build stargate
109105
if self.structures(UnitTypeId.PYLON).ready and self.structures(UnitTypeId.CYBERNETICSCORE).ready:

examples/protoss/warpgate_push.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,12 @@ async def on_step(self, iteration: int):
7979
for nexus in self.townhalls.ready:
8080
vgs = self.vespene_geyser.closer_than(15, nexus)
8181
for vg in vgs:
82-
if not self.can_afford(UnitTypeId.ASSIMILATOR):
83-
break
84-
worker = self.select_build_worker(vg.position)
85-
if worker is None:
86-
break
87-
if not self.gas_buildings or not self.gas_buildings.closer_than(1, vg):
88-
worker.build_gas(vg)
89-
worker.stop(queue=True)
82+
if self.can_afford(UnitTypeId.ASSIMILATOR):
83+
worker = self.select_build_worker(vg.position)
84+
if worker is not None:
85+
if not self.gas_buildings or not self.gas_buildings.closer_than(1, vg):
86+
worker.build_gas(vg)
87+
worker.stop(queue=True)
9088

9189
# Research warp gate if cybercore is completed
9290
if (

examples/simulate_fight_scenario.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,19 +42,21 @@ async def on_step(self, iteration: int):
4242
return
4343

4444
async def reset_arena(self):
45+
if self.enemy_location is None:
46+
return
4547
await self.client.debug_kill_unit(self.all_units)
4648

4749
await self.client.debug_create_unit(
4850
[
49-
[UnitTypeId.SUPPLYDEPOT, 1, self.enemy_location, OPPONENT_PLAYER_ID],
50-
[UnitTypeId.MARINE, 4, self.enemy_location.towards(self.start_location, 8), OPPONENT_PLAYER_ID],
51+
(UnitTypeId.SUPPLYDEPOT, 1, self.enemy_location, OPPONENT_PLAYER_ID),
52+
(UnitTypeId.MARINE, 4, self.enemy_location.towards(self.start_location, 8), OPPONENT_PLAYER_ID),
5153
]
5254
)
5355

5456
await self.client.debug_create_unit(
5557
[
56-
[UnitTypeId.SUPPLYDEPOT, 1, self.start_location, MY_PLAYER_ID],
57-
[UnitTypeId.MARINE, 4, self.start_location.towards(self.enemy_location, 8), MY_PLAYER_ID],
58+
(UnitTypeId.SUPPLYDEPOT, 1, self.start_location, MY_PLAYER_ID),
59+
(UnitTypeId.MARINE, 4, self.start_location.towards(self.enemy_location, 8), MY_PLAYER_ID),
5860
]
5961
)
6062

@@ -63,6 +65,8 @@ async def manage_enemy_units(self):
6365
unit.attack(self.start_location)
6466

6567
async def manage_own_units(self):
68+
if self.enemy_location is None:
69+
return
6670
for unit in self.units(UnitTypeId.MARINE):
6771
unit.attack(self.enemy_location)
6872
# TODO: implement your fight logic here

examples/terran/cyclone_push.py

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -78,31 +78,29 @@ async def on_step(self, iteration: int):
7878
# Near same command as above with the depot
7979
await self.build(UnitTypeId.BARRACKS, near=cc.position.towards(self.game_info.map_center, 8))
8080

81-
# If we have a barracks (complete or under construction) and less than 2 gas structures (here: refineries)
82-
elif self.structures(UnitTypeId.BARRACKS) and self.gas_buildings.amount < 2:
83-
if self.can_afford(UnitTypeId.REFINERY):
84-
# All the vespene geysirs nearby, including ones with a refinery on top of it
85-
vgs = self.vespene_geyser.closer_than(10, cc)
86-
for vg in vgs:
87-
if self.gas_buildings.filter(lambda unit: unit.distance_to(vg) < 1):
88-
continue
89-
# Select a worker closest to the vespene geysir
90-
worker: Unit | None = self.select_build_worker(vg)
91-
# Worker can be none in cases where all workers are dead
92-
# or 'select_build_worker' function only selects from workers which carry no minerals
93-
if worker is None:
94-
continue
81+
# If we have a barracks (complete or under construction) and less than 2 gas structures (here: refineries)
82+
if self.structures(UnitTypeId.BARRACKS) and self.gas_buildings.amount < 2:
83+
if self.can_afford(UnitTypeId.REFINERY):
84+
# All the vespene geysirs nearby, including ones with a refinery on top of it
85+
vgs = self.vespene_geyser.closer_than(10, cc)
86+
for vg in vgs:
87+
has_refinery = self.gas_buildings.filter(lambda unit: unit.distance_to(vg) < 1)
88+
if has_refinery:
89+
continue
90+
# Select a worker closest to the vespene geysir
91+
worker: Unit | None = self.select_build_worker(vg)
92+
# Worker can be none in cases where all workers are dead
93+
# or 'select_build_worker' function only selects from workers which carry no minerals
94+
if worker is not None:
9595
# Issue the build command to the worker, important: vg has to be a Unit, not a position
9696
worker.build_gas(vg)
97-
# Only issue one build geysir command per frame
98-
break
99-
100-
# If we have at least one barracks that is compelted, build factory
101-
if self.structures(UnitTypeId.BARRACKS).ready:
102-
if self.structures(UnitTypeId.FACTORY).amount < 3 and not self.already_pending(UnitTypeId.FACTORY):
103-
if self.can_afford(UnitTypeId.FACTORY):
104-
position: Point2 = cc.position.towards_with_random_angle(self.game_info.map_center, 16)
105-
await self.build(UnitTypeId.FACTORY, near=position)
97+
98+
# If we have at least one barracks that is completed, build factory
99+
if self.structures(UnitTypeId.BARRACKS).ready:
100+
if self.structures(UnitTypeId.FACTORY).amount < 3 and not self.already_pending(UnitTypeId.FACTORY):
101+
if self.can_afford(UnitTypeId.FACTORY):
102+
position: Point2 = cc.position.towards_with_random_angle(self.game_info.map_center, 16)
103+
await self.build(UnitTypeId.FACTORY, near=position)
106104

107105
for factory in self.structures(UnitTypeId.FACTORY).ready.idle:
108106
# Reactor allows us to build two at a time

0 commit comments

Comments
 (0)