Skip to content
This repository has been archived by the owner on Dec 27, 2024. It is now read-only.

Commit

Permalink
feat(plugin): allow run code on race result
Browse files Browse the repository at this point in the history
subtask of #170
  • Loading branch information
NateScarlet committed Aug 28, 2021
1 parent baae540 commit d11d2cf
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 13 deletions.
2 changes: 2 additions & 0 deletions auto_derby/_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ class config:

on_single_mode_live = sc.g.on_winning_live
on_single_mode_command = sc.g.on_command
on_single_mode_race_result = sc.g.on_race_result
on_single_mode_crane_game: Callable[
[single_mode.Context], None
] = _default_on_single_mode_crane_game
Expand Down Expand Up @@ -129,6 +130,7 @@ def apply(cls) -> None:
sc.g.pause_if_race_order_gt = cls.pause_if_race_order_gt
sc.g.on_winning_live = cls.on_single_mode_live
sc.g.on_command = cls.on_single_mode_command
sc.g.on_race_result = cls.on_single_mode_race_result
template.g.last_screenshot_save_path = cls.last_screenshot_save_path
terminal.g.pause_sound_path = cls.terminal_pause_sound_path
terminal.g.prompt_sound_path = cls.terminal_prompt_sound_path
Expand Down
6 changes: 6 additions & 0 deletions auto_derby/single_mode/commands/globals.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

if TYPE_CHECKING:
from .command import Command
from .race import RaceResult

from .. import Context

Expand All @@ -20,6 +21,10 @@ def _default_on_command(ctx: Context, command: Command) -> None:
pass


def _default_on_race_result(ctx: Context, result: RaceResult) -> None:
pass


class g:
ignore_training_commands: Callable[[Context], bool]
rest_score: Callable[[Context], float]
Expand All @@ -28,3 +33,4 @@ class g:
pause_if_race_order_gt: int = -1
on_winning_live: Callable[[Context], None] = _default_on_winning_live
on_command: Callable[[Context, Command], None] = _default_on_command
on_race_result: Callable[[Context, RaceResult], None] = _default_on_race_result
50 changes: 37 additions & 13 deletions auto_derby/single_mode/commands/race.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,27 +31,51 @@ def _choose_running_style(ctx: Context, race1: Race) -> None:
scene.choose_runing_style(style_scores[0][0])


def _handle_race_result():
class RaceResult:
def __init__(self) -> None:
self.race = Race()
self.order = 0
self.is_failed = False

def __str__(self) -> str:
return f"RaceResult<race={self.race} order={self.order} fail={self.is_failed}>"


_RACE_ORDER_TEMPLATES = {
templates.RACE_RESULT_NO1: 1,
templates.RACE_RESULT_NO2: 2,
templates.RACE_RESULT_NO3: 3,
templates.RACE_RESULT_NO4: 4,
templates.RACE_RESULT_NO5: 5,
templates.RACE_RESULT_NO6: 6,
templates.RACE_RESULT_NO8: 8,
templates.RACE_RESULT_NO10: 10,
}


def _handle_race_result(ctx: Context, race: Race):
action.wait_tap_image(templates.RACE_RESULT_BUTTON)

_, pos = action.wait_image(
templates.RACE_RESULT_NO1,
templates.RACE_RESULT_NO2,
templates.RACE_RESULT_NO3,
templates.RACE_RESULT_NO4,
templates.RACE_RESULT_NO5,
templates.RACE_RESULT_NO6,
templates.RACE_RESULT_NO8,
templates.RACE_RESULT_NO10,
)
tmpl, pos = action.wait_image(*_RACE_ORDER_TEMPLATES.keys())
res = RaceResult()
res.race = race
res.order = _RACE_ORDER_TEMPLATES[tmpl.name]

def _emit_result():
_LOGGER.info("race result: %s", res)
g.on_race_result(ctx, res)

while True:
time.sleep(1)
if action.tap_image(templates.GREEN_NEXT_BUTTON):
break
if action.tap_image(templates.SINGLE_MODE_CONTINUE):
_handle_race_result()
res.is_failed = True
_emit_result()
_handle_race_result(ctx, race)
return
action.tap(pos)
_emit_result()


class RaceCommand(Command):
Expand Down Expand Up @@ -89,7 +113,7 @@ def execute(self, ctx: Context) -> None:

_choose_running_style(ctx, race1)

_handle_race_result()
_handle_race_result(ctx, race1)
ctx.fan_count = 0 # request update in next turn
tmpl, pos = action.wait_image(
templates.SINGLE_MODE_LIVE_BUTTON,
Expand Down

0 comments on commit d11d2cf

Please sign in to comment.