|
| 1 | +import json |
| 2 | +import time |
| 3 | + |
| 4 | +from maa.agent.agent_server import AgentServer |
| 5 | +from maa.context import Context |
| 6 | +from maa.custom_action import CustomAction |
| 7 | + |
| 8 | +from utils.logger import logger |
| 9 | + |
| 10 | +_TOP_RESET_COUNT = 1 |
| 11 | +_MAX_SCROLL_COUNT = 1 |
| 12 | +_SWIPE_DURATION_MS = 500 |
| 13 | +_SWIPE_DELAY_SEC = 0.1 |
| 14 | +_TOP_SWIPE_BEGIN = (40, 170) |
| 15 | +_TOP_SWIPE_END = (160, 470) |
| 16 | +_NEXT_SWIPE_BEGIN = (40, 470) |
| 17 | +_NEXT_SWIPE_END = (160, 170) |
| 18 | + |
| 19 | + |
| 20 | +def _screencap(controller): |
| 21 | + job = controller.post_screencap() |
| 22 | + job.wait() |
| 23 | + return controller.cached_image |
| 24 | + |
| 25 | + |
| 26 | +def _box_to_rect(box): |
| 27 | + if isinstance(box, (list, tuple)): |
| 28 | + return list(box) |
| 29 | + return [box.x, box.y, box.w, box.h] |
| 30 | + |
| 31 | + |
| 32 | +def _click_rect(controller, rect): |
| 33 | + cx = rect[0] + rect[2] // 2 |
| 34 | + cy = rect[1] + rect[3] // 2 |
| 35 | + controller.post_touch_move(cx, cy).wait() |
| 36 | + controller.post_touch_down(cx, cy).wait() |
| 37 | + time.sleep(0.05) |
| 38 | + controller.post_touch_up().wait() |
| 39 | + time.sleep(0.05) |
| 40 | + |
| 41 | + |
| 42 | +def _swipe(controller, begin, end): |
| 43 | + controller.post_swipe( |
| 44 | + begin[0], begin[1], end[0], end[1], duration=_SWIPE_DURATION_MS |
| 45 | + ).wait() |
| 46 | + time.sleep(_SWIPE_DELAY_SEC) |
| 47 | + |
| 48 | + |
| 49 | +def _parse_target(custom_action_param) -> str | None: |
| 50 | + if not custom_action_param: |
| 51 | + return None |
| 52 | + |
| 53 | + if isinstance(custom_action_param, str): |
| 54 | + try: |
| 55 | + params = json.loads(custom_action_param) |
| 56 | + except json.JSONDecodeError: |
| 57 | + return custom_action_param.strip() or None |
| 58 | + |
| 59 | + if isinstance(params, str): |
| 60 | + return params.strip() or None |
| 61 | + if isinstance(params, dict): |
| 62 | + target = params.get("target") |
| 63 | + if isinstance(target, str): |
| 64 | + return target.strip() or None |
| 65 | + return None |
| 66 | + |
| 67 | + if isinstance(custom_action_param, dict): |
| 68 | + target = custom_action_param.get("target") |
| 69 | + if isinstance(target, str): |
| 70 | + return target.strip() or None |
| 71 | + |
| 72 | + return None |
| 73 | + |
| 74 | + |
| 75 | +@AgentServer.custom_action("furniture_choose_property") |
| 76 | +class FurnitureChooseProperty(CustomAction): |
| 77 | + def run( |
| 78 | + self, context: Context, argv: CustomAction.RunArg |
| 79 | + ) -> CustomAction.RunResult: |
| 80 | + target = _parse_target(getattr(argv, "custom_action_param", None)) |
| 81 | + if not target: |
| 82 | + logger.warning(f"furniture_choose_property missing target:{argv}") |
| 83 | + return CustomAction.RunResult(success=False) |
| 84 | + |
| 85 | + controller = context.tasker.controller |
| 86 | + |
| 87 | + for _ in range(_TOP_RESET_COUNT): |
| 88 | + if context.tasker.stopping: |
| 89 | + return CustomAction.RunResult(success=False) |
| 90 | + _swipe(controller, _TOP_SWIPE_BEGIN, _TOP_SWIPE_END) |
| 91 | + |
| 92 | + for scroll_index in range(_MAX_SCROLL_COUNT + 1): |
| 93 | + if context.tasker.stopping: |
| 94 | + return CustomAction.RunResult(success=False) |
| 95 | + |
| 96 | + image = _screencap(controller) |
| 97 | + result = context.run_recognition(target, image) |
| 98 | + if result and result.hit and result.box is not None: |
| 99 | + rect = _box_to_rect(result.box) |
| 100 | + logger.debug( |
| 101 | + f"furniture_choose_property target found: {target} rect={rect}" |
| 102 | + ) |
| 103 | + _click_rect(controller, rect) |
| 104 | + return CustomAction.RunResult(success=True) |
| 105 | + |
| 106 | + if scroll_index >= _MAX_SCROLL_COUNT: |
| 107 | + break |
| 108 | + |
| 109 | + logger.debug( |
| 110 | + f"furniture_choose_property target not found, scroll next: {target} step={scroll_index+1}" |
| 111 | + ) |
| 112 | + _swipe(controller, _NEXT_SWIPE_BEGIN, _NEXT_SWIPE_END) |
| 113 | + |
| 114 | + logger.warning(f"furniture_choose_property target not found: {target}") |
| 115 | + return CustomAction.RunResult(success=False) |
0 commit comments