mower-ng/mower/solvers/sign_in/shop.py
zhbaor b42b052e29
All checks were successful
ci/woodpecker/push/check_format Pipeline was successful
活动商店点击物品使用ctap
2025-02-02 09:32:53 +08:00

113 lines
4.1 KiB
Python

"""
Copyright (c) 2025 zhbaor <zhbaor@zhaozuohong.vip>
This file is part of mower-ng (https://git-cf.zhaozuohong.vip/mower-ng/mower-ng).
Mower-ng is free software: you may copy, redistribute and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation, version 3 or later.
This file is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
from datetime import timedelta
from functools import cmp_to_key
import cv2
from skimage.feature import peak_local_max
from mower.utils import config
from mower.utils.email import notify
from mower.utils.graph import SceneGraphSolver
from mower.utils.image import crop2content, cropimg, loadres, thres2
from mower.utils.rapidocr import ocr_rec
from mower.utils.scene import Scene
from mower.utils.solver import BaseSolver
from mower.utils.vector import sa, va, vs
class Shop(BaseSolver):
solver_name = "活动商店购买"
solver_max_duration = timedelta(minutes=5)
def run(self):
self.product = None
return super().run()
def next_product(self):
coin = loadres("sign_in/shop/coin", True)
result = cv2.matchTemplate(config.recog.gray, coin, cv2.TM_CCOEFF_NORMED)
coords = peak_local_max(
result, min_distance=100, threshold_abs=0.5, exclude_border=False
)
coords = coords.tolist()
coords.sort(
key=cmp_to_key(
lambda x, y: x[0] - y[0] if abs(x[1] - y[1]) < 10 else x[1] - y[1]
)
)
result = []
for y, x in coords:
top_left = vs((x, y), (22, 170))
scope = sa(((185, 80), (305, 150)), top_left)
if self.find("sign_in/shop/out_of_stock", scope=scope):
continue
name = sa(((5, 5), (260, 60)), top_left)
name = cropimg(config.recog.gray, name)
name = thres2(name, 127)
nh, nw = name.shape
if cv2.countNonZero(name) > nh * nw * 0.5:
name = cv2.bitwise_not(name)
name = crop2content(name)
name = ocr_rec(name)
if name not in config.conf.activity_shop_blacklist:
return name, va(top_left, (245, 110))
return None, None
def transition(self):
if (scene := self.scene()) == Scene.TERMINAL_MAIN:
self.terminal_entry("navigation/activity/terminal.jpg")
elif scene == Scene.ACTIVITY_MAIN:
self.tap("sign_in/shop/entry")
elif scene == Scene.ACTIVITY_SHOP:
if self.animation():
return
name, pos = self.next_product()
if pos is not None:
if self.ctap(pos):
self.product = name
return
self.swipe_ext([(1600, 340), (300, 340), (300, 150)], [0.2, 0.3], 0, 0.1)
elif scene == Scene.ACTIVITY_SHOP_BUY:
if self.product is None:
SceneGraphSolver().step(Scene.ACTIVITY_SHOP)
return
if self.find("sign_in/shop/insufficient"):
return True
if self.animation():
return
if pos := self.find("sign_in/shop/max"):
if self.find("sign_in/shop/0"):
return True
self.tap(pos, update=False)
self.sleep(0.5, update=False)
self.ctap("sign_in/shop/buy", id=self.product)
elif scene == Scene.MATERIEL:
if self.product:
self.sleep()
notify(f"活动商店物品购买:{self.product}")
self.product = None
self.tap((960, 200))
elif scene == Scene.ROGUE_AGENT:
if self.product:
self.product = None
notify("活动商店时装购买")
self.tap((960, 200))
else:
SceneGraphSolver().step(Scene.TERMINAL_MAIN)