-
Notifications
You must be signed in to change notification settings - Fork 29
Extending Managers
DrInfy edited this page Apr 25, 2020
·
18 revisions
In order to customize managers, override configure_managers
method in KnowledgeBot
.
Following example makes simple implementation for build selection in custom data manager.
class MyDataManager(DataManager):
""" Simple replacement to data manager """
def select_build(self, available_builds: List[str]) -> str:
""" Selects last build if it won and another if it didn't win. """
if len(available_builds) == 1:
return available_builds[0]
assert len(available_builds) > 1
last = self.last_result
if last is not None and last.build_used in available_builds:
if last.result > 0:
return last
available_builds.remove(last)
return available_builds[randint(0, len(available_builds) - 1)]
class MyBot(KnowledgeBot):
data_manager: MyDataManager
def __init__(self):
super().__init__("MyBot")
def configure_managers(self) -> Optional[List[ManagerBase]]:
self.data_manager = MyDataManager() # This is to get proper typing
# Replace default data manager with our extended one
self.knowledge.data_manager = self.data_manager
return None # Do not add overriding managers to additional managers
async def create_plan(self) -> BuildOrder:
builds = ["timing", "macro", "rush"] # Our build selection
# We could use also use self.knowledge.data_manager,
# but that would result in typing warnings
selected_build = self.data_manager.select_build(builds)
self.knowledge.data_manager.set_build(selected_build)
return self.create_build(selected_build)
def create_build(self, selected_build: str) -> BuildOrder:
# Make your own implementation of the available builds
if selected_build == "timing":
return # ...
if selected_build == "macro":
return # ...
if selected_build == "rush":
return # ...
configure_managers
can return a list of managers that are added to Knowledge
to be updated each frame.
class LarvaManager(ManagerBase):
def __init__(self):
super().__init__()
self._larva = 0
@property
def larva_count(self) -> int:
return self._larva
async def update(self):
# This is being run each frame
self._larva = len(self.cache.own(UnitTypeId.LARVA))
async def post_update(self):
# This manager doesn't need to do anything at the end of the frame.
pass
class MyBot(KnowledgeBot):
larva_manager: LarvaManager
def configure_managers(self) -> Optional[List[ManagerBase]]:
"""
returned list of managers are automatically registered as additional managers
"""
return [LarvaManager()]
async def create_plan(self) -> BuildOrder:
return BuildOrder(
# ...
# In order to not get typing warning, we'll pass the larva manager to new act
MyLarvaAct()
# ...
)
class MyLarvaAct(ActBase):
larva_manager: LarvaManager
async def start(self, knowledge: "Knowledge")
await super().start(knowledge)
# Managers can be requested by their type during start phase
self.larva_manager = knowledge.get_manager(LarvaManager)
async def execute(self) -> bool:
# TODO: Do something with self.larva_manager
pass
- Plans and Build Order
- Settings, debug and logging
- Structure and Life Cycle
- Unit Roles
- Unit Cache
- Running Games
- Converting Sharpy bot from before 2.0 version
- Converting Sharpy KnowledgeBot to SkeletonBot
- Converting Python bot to minimal Sharpy bot
- OLD: Extending Your Existing Bot With Sharpy
- Packaging For Ladders
- Extending Sharpy
- Advanced Build Order tricks
- Machine Learning With Sharpy