Add: Route framework

This commit is contained in:
LmeSzinc 2023-09-23 19:19:53 +08:00
parent fd84240d38
commit e26be58a05
3 changed files with 89 additions and 1 deletions

View File

@ -47,7 +47,7 @@ class PositionPredictState:
class Minimap(MapResource):
def init_position(self, position: tuple[int | float, int | float]):
logger.info(f"init_position:{position}")
logger.info(f"init_position: {position}")
self.position = position
def _predict_position(self, image, scale=1.0):

50
tasks/map/route/base.py Normal file
View File

@ -0,0 +1,50 @@
from tasks.map.control.control import MapControl
from tasks.map.control.waypoint import Waypoint
from tasks.map.keywords import MapPlane
class RouteBase(MapControl):
"""
Base class of `Route`
Every `Route` class must implement method `route()`
"""
def route_example(self):
"""
Pages:
in: page_main
out: page_main
Doesn't matter if in/out are not page_main, just be clear what you're doing
"""
self.map_init(
plane=...,
floor=...,
position=...,
)
self.clear_enemy(
Waypoint(...).run_2x(),
Waypoint(...),
)
def map_init(
self,
plane: MapPlane | str,
floor: str = 'F1',
position: tuple[int | float, int | float] = None
):
"""
Args:
plane (MapPlane, str): Such as Jarilo_AdministrativeDistrict
floor (str):
position: Initialize the starter point of minimap tracking
Leaving None will trigger brute-force starter point finding.
"""
try:
if self.device.image is None:
self.device.screenshot()
except AttributeError:
self.device.screenshot()
self.minimap.set_plane(plane, floor=floor)
if position is not None:
self.minimap.init_position(position)

38
tasks/map/route/loader.py Normal file
View File

@ -0,0 +1,38 @@
import importlib
import os
from module.exception import RequestHumanTakeover
from module.logger import logger
from tasks.base.ui import UI
from tasks.map.route.base import RouteBase
class RouteLoader(UI):
route: RouteBase
def route_run(self, route: str):
"""
Args:
route: .py module path such as `daily.forgotten_hall.stage1`
which will load `./route/daily/forgotten_hall/stage1.py`
"""
folder, name = route.rsplit('.', maxsplit=1)
path = f'./route/{route.replace(".", "/")}.py'
try:
module = importlib.import_module(f'route.{folder}.{name}')
except ModuleNotFoundError:
logger.critical(f'Route file not found: {route} ({path})')
if not os.path.exists(path):
logger.critical(f'Route file not exists: {path}')
raise RequestHumanTakeover
# config = copy.deepcopy(self.config).merge(module.Config())
config = self.config
device = self.device
try:
self.route = module.Route(config=config, device=device)
return self.route.route()
except AttributeError as e:
logger.critical(e)
logger.critical(f'Route file {route} ({path}) must define Route.route()')
raise RequestHumanTakeover