|
| 1 | +import time |
| 2 | +import json |
| 3 | + |
| 4 | +from maa.agent.agent_server import AgentServer |
| 5 | +from maa.custom_action import CustomAction |
| 6 | +from maa.context import Context |
| 7 | + |
| 8 | +from utils.maafocus import PrintT |
| 9 | + |
| 10 | +from ..Common.utils import get_image, click_rect_multiple |
| 11 | +from .utils import wait_and_claim, press_key_f, make_croissant, make_cake, make_bread |
| 12 | + |
| 13 | + |
| 14 | + |
| 15 | +@AgentServer.custom_action("auto_make_coffee_lite") |
| 16 | +class AutoMakeCoffeeLite(CustomAction): |
| 17 | + |
| 18 | + def run( |
| 19 | + self, context: Context, argv: CustomAction.RunArg |
| 20 | + ) -> CustomAction.RunResult: |
| 21 | + PrintT(context, "coffee.started") |
| 22 | + controller = context.tasker.controller |
| 23 | + make_count = 10 |
| 24 | + check_freq = 0.5 |
| 25 | + timeout = 5 |
| 26 | + if argv.custom_action_param: |
| 27 | + try: |
| 28 | + params = json.loads(argv.custom_action_param) |
| 29 | + make_count = params.get("count", 10) |
| 30 | + check_freq = params.get("freq", 0.5) |
| 31 | + timeout = params.get("timeout", 5) |
| 32 | + except json.JSONDecodeError as e: |
| 33 | + PrintT(context, |
| 34 | + f"[AutoCoffeeLite] Failed to parse custom_action_param as JSON: {e}. " |
| 35 | + f"Raw value: {argv.custom_action_param!r}" |
| 36 | + ) |
| 37 | + |
| 38 | + for count in range(make_count): |
| 39 | + if context.tasker.stopping: |
| 40 | + return CustomAction.RunResult(success=False) |
| 41 | + PrintT(context, "coffee.making", count + 1, make_count) |
| 42 | + |
| 43 | + # Step 1: 选择关卡 |
| 44 | + PrintT(context, "coffee.step_wait_start") |
| 45 | + while True: |
| 46 | + if context.tasker.stopping: |
| 47 | + return CustomAction.RunResult(success=False) |
| 48 | + img = get_image(controller) |
| 49 | + start_result = context.run_recognition("MakeCoffeeStart", img) |
| 50 | + if start_result and start_result.hit: |
| 51 | + while True: |
| 52 | + if context.tasker.stopping: |
| 53 | + return CustomAction.RunResult(success=False) |
| 54 | + context.run_action("MakeCoffeeScrollToTop") |
| 55 | + time.sleep(1) |
| 56 | + img = get_image(controller) |
| 57 | + target_result = context.run_recognition( |
| 58 | + "MakeCoffeeTargetCoffeeMaster", img |
| 59 | + ) |
| 60 | + if target_result and target_result.hit: |
| 61 | + break |
| 62 | + |
| 63 | + click_rect_multiple( |
| 64 | + controller, |
| 65 | + [ |
| 66 | + target_result.box.x, |
| 67 | + target_result.box.y, |
| 68 | + target_result.box.w, |
| 69 | + target_result.box.h, |
| 70 | + ], |
| 71 | + ) |
| 72 | + img = get_image(controller) |
| 73 | + start_result = context.run_recognition("MakeCoffeeStart", img) |
| 74 | + if not (start_result and start_result.hit): |
| 75 | + time.sleep(check_freq) |
| 76 | + continue |
| 77 | + |
| 78 | + PrintT(context, "coffee.step_start_click") |
| 79 | + click_rect_multiple( |
| 80 | + controller, |
| 81 | + [ |
| 82 | + start_result.box.x, |
| 83 | + start_result.box.y, |
| 84 | + start_result.box.w, |
| 85 | + start_result.box.h, |
| 86 | + ], |
| 87 | + ) |
| 88 | + time.sleep(3) |
| 89 | + break |
| 90 | + time.sleep(check_freq) |
| 91 | + |
| 92 | + |
| 93 | + # Step 2: 制作三道菜 |
| 94 | + PrintT(context, "coffee.step_making_dishes") |
| 95 | + |
| 96 | + make_croissant(context) |
| 97 | + make_cake(context) |
| 98 | + make_bread(context) |
| 99 | + |
| 100 | + # Step 3: 达成营业额 |
| 101 | + PrintT(context, "coffee.step_wait_star") |
| 102 | + start_time = time.time() |
| 103 | + exit_roi = [11, 12, 38, 37] |
| 104 | + while True: |
| 105 | + if context.tasker.stopping: |
| 106 | + return CustomAction.RunResult(success=False) |
| 107 | + if time.time() - start_time > timeout: |
| 108 | + return CustomAction.RunResult(success=False) |
| 109 | + img = get_image(controller) |
| 110 | + star_result = context.run_recognition("MakeCoffeeStar", img) |
| 111 | + if star_result and star_result.hit: |
| 112 | + PrintT(context, "coffee.step_star_click") |
| 113 | + click_rect_multiple(controller, exit_roi) |
| 114 | + time.sleep(1) |
| 115 | + break |
| 116 | + time.sleep(2) |
| 117 | + |
| 118 | + # Step 4: 点击领取 |
| 119 | + PrintT(context, "coffee.step_wait_claim") |
| 120 | + wait_and_claim(context, controller, check_freq) |
| 121 | + |
| 122 | + PrintT(context, "coffee.round_finished") |
| 123 | + press_key_f(controller) |
| 124 | + |
| 125 | + time.sleep(2) |
| 126 | + PrintT(context, "coffee.iteration_done") |
| 127 | + |
| 128 | + PrintT(context, "coffee.all_done") |
| 129 | + return CustomAction.RunResult(success=True) |
0 commit comments