Skip to content

Commit be2ebb4

Browse files
feat(make_coffee): 增加做咖啡平民版 (#320)
* feat(make_coffee): 增加做咖啡平民版 平民版做咖啡1-1,无需白藏娜娜莉 优化做咖啡入口逻辑,在店长特供对话附近可进入界面. Refs: #128 * fix: 去除冗余引用 * fix: 去除冗余引用并显式捕获错误 * fix: 修改PrintT传入正确参数
1 parent d1050fd commit be2ebb4

20 files changed

Lines changed: 637 additions & 68 deletions

File tree

agent/custom/action/auto_make_coffee.py renamed to agent/custom/action/AutoCoffee/auto_make_coffee.py

Lines changed: 13 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,13 @@
11
import time
22
import json
3-
import random
43

54
from maa.agent.agent_server import AgentServer
65
from maa.custom_action import CustomAction
76
from maa.context import Context
87

98
from utils.maafocus import PrintT
10-
11-
12-
def get_image(controller):
13-
job = controller.post_screencap()
14-
job.wait()
15-
img = controller.cached_image
16-
return img
17-
18-
19-
def click_rect(controller, rect):
20-
x, y, w, h = rect
21-
cx = x + w // 2
22-
cy = y + h // 2
23-
for _ in range(3):
24-
controller.post_touch_down(cx, cy).wait()
25-
time.sleep(0.05)
26-
controller.post_touch_up().wait()
9+
from ..Common.utils import get_image, click_rect_multiple
10+
from .utils import wait_and_claim, press_key_f
2711

2812

2913
@AgentServer.custom_action("auto_make_coffee")
@@ -41,10 +25,11 @@ def run(
4125
params = json.loads(argv.custom_action_param)
4226
make_count = params.get("count", 10)
4327
check_freq = params.get("freq", 0.5)
44-
except:
45-
pass
46-
47-
KEY_F = 70
28+
except json.JSONDecodeError as e:
29+
PrintT(context,
30+
f"[AutoCoffee] Failed to parse custom_action_param as JSON: {e}. "
31+
f"Raw value: {argv.custom_action_param!r}"
32+
)
4833

4934
click_roi = [28, 272, 65, 56]
5035
exit_roi = [11, 12, 38, 37]
@@ -74,7 +59,7 @@ def run(
7459
if target_result and target_result.hit:
7560
break
7661

77-
click_rect(
62+
click_rect_multiple(
7863
controller,
7964
[
8065
target_result.box.x,
@@ -90,7 +75,7 @@ def run(
9075
continue
9176

9277
PrintT(context, "coffee.step_start_click")
93-
click_rect(
78+
click_rect_multiple(
9479
controller,
9580
[
9681
start_result.box.x,
@@ -108,42 +93,22 @@ def run(
10893
while True:
10994
if context.tasker.stopping:
11095
return CustomAction.RunResult(success=False)
111-
click_rect(controller, click_roi)
96+
click_rect_multiple(controller, click_roi)
11297
img = get_image(controller)
11398
star_result = context.run_recognition("MakeCoffeeStar", img)
11499
if star_result and star_result.hit:
115100
PrintT(context, "coffee.step_star_click")
116-
click_rect(controller, exit_roi)
101+
click_rect_multiple(controller, exit_roi)
117102
time.sleep(1)
118103
break
119104
time.sleep(2)
120105

121106
# Step 3: 点击领取
122107
PrintT(context, "coffee.step_wait_claim")
123-
while True:
124-
if context.tasker.stopping:
125-
return CustomAction.RunResult(success=False)
126-
img = get_image(controller)
127-
claim_result = context.run_recognition("MakeCoffeeClaim", img)
128-
if claim_result and claim_result.hit:
129-
PrintT(context, "coffee.step_claim_click")
130-
click_rect(
131-
controller,
132-
[
133-
claim_result.box.x,
134-
claim_result.box.y,
135-
claim_result.box.w,
136-
claim_result.box.h,
137-
],
138-
)
139-
time.sleep(1)
140-
break
141-
time.sleep(check_freq)
108+
wait_and_claim(context, controller, check_freq)
142109

143110
PrintT(context, "coffee.round_finished")
144-
controller.post_key_down(KEY_F)
145-
time.sleep(0.1)
146-
controller.post_key_up(KEY_F)
111+
press_key_f(controller)
147112

148113
time.sleep(2)
149114
PrintT(context, "coffee.iteration_done")
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
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)
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import time
2+
from ..Common.utils import get_image, click_rect_multiple
3+
4+
5+
6+
def make_croissant(context):
7+
"""制作牛角包"""
8+
context.run_action("MakeCoffeeLiteDish1_SelectIngredient1")
9+
context.run_action("MakeCoffeeLiteDish1_SelectIngredient2")
10+
context.run_action("MakeCoffeeLiteDish1_Confirm")
11+
12+
13+
def make_cake(context):
14+
"""制作小蛋糕"""
15+
context.run_action("MakeCoffeeLiteDish2_SelectIngredient1")
16+
context.run_action("MakeCoffeeLiteDish2_SelectIngredient2")
17+
context.run_action("MakeCoffeeLiteDish2_Confirm")
18+
19+
20+
def make_bread(context):
21+
"""制作面包"""
22+
context.run_action("MakeCoffeeLiteDish3_SelectIngredient1")
23+
context.run_action("MakeCoffeeLiteDish3_SelectIngredient2")
24+
context.run_action("MakeCoffeeLiteDish3_Confirm")
25+
26+
27+
def wait_and_claim(context, controller, check_freq=0.5):
28+
"""等待并点击领取按钮"""
29+
while True:
30+
if context.tasker.stopping:
31+
return False
32+
img = get_image(controller)
33+
claim_result = context.run_recognition("MakeCoffeeClaim", img)
34+
if claim_result and claim_result.hit:
35+
click_rect_multiple(
36+
controller,
37+
[
38+
claim_result.box.x,
39+
claim_result.box.y,
40+
claim_result.box.w,
41+
claim_result.box.h,
42+
],
43+
)
44+
time.sleep(1)
45+
return True
46+
time.sleep(check_freq)
47+
48+
49+
def press_key_f(controller):
50+
"""按下并释放 F 键"""
51+
KEY_F = 70
52+
controller.post_key_down(KEY_F)
53+
time.sleep(0.1)
54+
controller.post_key_up(KEY_F)

agent/custom/action/Common/utils.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,16 @@ def click_rect(controller, rect, delay=0.001):
1717
time.sleep(delay)
1818
controller.post_touch_up().wait()
1919

20+
def click_rect_multiple(controller, rect, repeat=3):
21+
"""点击多次以确保可靠性"""
22+
x, y, w, h = rect
23+
cx = x + w // 2
24+
cy = y + h // 2
25+
for _ in range(repeat):
26+
controller.post_touch_down(cx, cy).wait()
27+
time.sleep(0.05)
28+
controller.post_touch_up().wait()
29+
2030
def match_template_in_region(img, region, template, min_similarity=0.8, green_mask=False):
2131
if img is None or not isinstance(img, np.ndarray):
2232
return False, 0.0, 0, 0

agent/custom/action/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
from .AutoFish.auto_fish import *
22
from .AutoFish.auto_buy_fish_bait import *
33
from .AutoFish.auto_sell_fish import *
4-
from .auto_make_coffee import *
4+
from .AutoCoffee.auto_make_coffee import *
5+
from .AutoCoffee.auto_make_coffee_lite import *
56
from .rhythm.feats.play import *
67
from .rhythm.feats.repeat_decision import *
78
from .rhythm.feats.select_song import *
@@ -28,6 +29,7 @@
2829

2930
__all__ = [
3031
"AutoMakeCoffee",
32+
"AutoMakeCoffeeLite",
3133
"AutoFish",
3234
"AutoBuyFishBait",
3335
"AutoSellFish",

assets/interface.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@
110110
"resource/tasks/Fish.json",
111111
"resource/tasks/PinkPawHeist.json",
112112
"resource/tasks/MakeCoffee.json",
113+
"resource/tasks/MakeCoffeeLite.json",
113114
"resource/tasks/Rhythm.json",
114115
"resource/tasks/Tetris.json",
115116
"resource/tasks/BagelSpam.json",
3.37 KB
Loading
2.9 KB
Loading

0 commit comments

Comments
 (0)