Просмотр исходного кода

fix(RichMan): handle sold-out medal shop items gracefully

- add sold-out OCR detection to count 'sold out' labels in medal shop page
- buy item only when its card template is detected; record undetected items
- after processing, verify undetected items are all sold out via OCR count,
  so a fully/partially sold-out shop finishes normally instead of hitting
  GameStuckError from blindly clicking back button
- add timeout guard to enter-medal click and back_mall to avoid infinite loops
m123711 2 месяцев назад
Родитель
Сommit
c363395d8f
3 измененных файлов с 83 добавлено и 20 удалено
  1. 2 0
      tasks/RichMan/assets.py
  2. 77 18
      tasks/RichMan/mall/medal.py
  3. 4 2
      tasks/RichMan/mall/navbar.py

+ 2 - 0
tasks/RichMan/assets.py

@@ -140,6 +140,8 @@ class RichManAssets:
 	I_ME_RED = RuleImage(roi_front=(847,146,137,129), roi_back=(141,129,864,454), threshold=0.8, method="Template matching", file="./tasks/RichMan/mall/medal/medal_me_red.png")
 	# 破碎的咒符 
 	I_ME_BROKEN = RuleImage(roi_front=(398,144,143,116), roi_back=(146,116,866,475), threshold=0.8, method="Template matching", file="./tasks/RichMan/mall/medal/medal_me_broken.png")
+	# 售罄 OCR:统计勋章商店页面中"售"字(售罄标签)数量,用于核对识别失败的商品是否均为售罄
+	O_SOLD_OUT = RuleOcr(roi=(0,100,1280,500), area=(0,100,1280,500), mode="Full", method="Default", keyword="售", name="medal_sold_out")
 	# 购买检查 
 	I_ME_CHECK_BLACK = RuleImage(roi_front=(593,229,100,100), roi_back=(558,177,202,200), threshold=0.8, method="Template matching", file="./tasks/RichMan/mall/medal/medal_me_check_black.png")
 	# 购买检查 

+ 77 - 18
tasks/RichMan/mall/medal.py

@@ -23,43 +23,102 @@ class Medal(FriendshipPoints):
             return
         self._enter_medal()
 
+        # 记录识别失败的商品,用于事后核对是否为售罄
+        not_found = []
+
         # 黑蛋
         if con.black_daruma:
-            self.buy_mall_one(buy_button=self.I_ME_BLACK, buy_check=self.I_ME_CHECK_BLACK,
-                              money_ocr=self.O_MALL_RESOURCE_3, buy_money=480)
+            self.screenshot()
+            if self.appear(self.I_ME_BLACK):
+                self.buy_mall_one(buy_button=self.I_ME_BLACK, buy_check=self.I_ME_CHECK_BLACK,
+                                  money_ocr=self.O_MALL_RESOURCE_3, buy_money=480)
+            else:
+                not_found.append('black_daruma')
         # 蓝票
         if con.mystery_amulet:
-            self.buy_mall_one(buy_button=self.I_ME_BLUE, buy_check=self.I_ME_CHECK_BLUE,
-                              money_ocr=self.O_MALL_RESOURCE_3, buy_money=180)
+            self.screenshot()
+            if self.appear(self.I_ME_BLUE):
+                self.buy_mall_one(buy_button=self.I_ME_BLUE, buy_check=self.I_ME_CHECK_BLUE,
+                                  money_ocr=self.O_MALL_RESOURCE_3, buy_money=180)
+            else:
+                not_found.append('mystery_amulet')
         # 体力100
         if con.ap_100:
-            self.buy_mall_one(buy_button=self.I_ME_AP, buy_check=self.I_ME_CHECK_AP,
-                              money_ocr=self.O_MALL_RESOURCE_3, buy_money=120)
+            self.screenshot()
+            if self.appear(self.I_ME_AP):
+                self.buy_mall_one(buy_button=self.I_ME_AP, buy_check=self.I_ME_CHECK_AP,
+                                  money_ocr=self.O_MALL_RESOURCE_3, buy_money=120)
+            else:
+                not_found.append('ap_100')
         # 随机御魂
         if con.random_soul:
-            self.buy_mall_one(buy_button=self.I_ME_SOULS, buy_check=self.I_ME_CHECK_SOULS,
-                              money_ocr=self.O_MALL_RESOURCE_3, buy_money=320)
+            self.screenshot()
+            if self.appear(self.I_ME_SOULS):
+                self.buy_mall_one(buy_button=self.I_ME_SOULS, buy_check=self.I_ME_CHECK_SOULS,
+                                  money_ocr=self.O_MALL_RESOURCE_3, buy_money=320)
+            else:
+                not_found.append('random_soul')
         # 两颗白蛋
         if con.white_daruma:
-            self.buy_mall_more(buy_button=self.I_ME_WHITE, remain_number=True, money_ocr=self.I_MALL_RESOURCE_MEDAL.build_mall_resource_ocr(self.device.image),
-                               buy_number=2, buy_max=2, buy_money=100)
+            self.screenshot()
+            if self.appear(self.I_ME_WHITE):
+                self.buy_mall_more(buy_button=self.I_ME_WHITE, remain_number=True, money_ocr=self.I_MALL_RESOURCE_MEDAL.build_mall_resource_ocr(self.device.image),
+                                   buy_number=2, buy_max=2, buy_money=100)
+            else:
+                not_found.append('white_daruma')
         # 十张挑战券
         if con.challenge_pass:
-            self.buy_mall_more(buy_button=self.I_ME_CHALLENGE_PASS, remain_number=True, money_ocr=self.I_MALL_RESOURCE_MEDAL.build_mall_resource_ocr(self.device.image),
-                               buy_number=con.challenge_pass, buy_max=10, buy_money=30)
+            self.screenshot()
+            if self.appear(self.I_ME_CHALLENGE_PASS):
+                self.buy_mall_more(buy_button=self.I_ME_CHALLENGE_PASS, remain_number=True, money_ocr=self.I_MALL_RESOURCE_MEDAL.build_mall_resource_ocr(self.device.image),
+                                   buy_number=con.challenge_pass, buy_max=10, buy_money=30)
+            else:
+                not_found.append('challenge_pass')
         # 红蛋
         if con.red_daruma:
-            self.buy_mall_more(buy_button=self.I_ME_RED, remain_number=False,
-                               money_ocr=self.I_MALL_RESOURCE_MEDAL.build_mall_resource_ocr(self.device.image),
-                               buy_number=con.red_daruma, buy_max=99, buy_money=30)
+            self.screenshot()
+            if self.appear(self.I_ME_RED):
+                self.buy_mall_more(buy_button=self.I_ME_RED, remain_number=False,
+                                   money_ocr=self.I_MALL_RESOURCE_MEDAL.build_mall_resource_ocr(self.device.image),
+                                   buy_number=con.red_daruma, buy_max=99, buy_money=30)
+            else:
+                not_found.append('red_daruma')
         # 破碎的咒符
         if con.broken_amulet:
-            self.buy_mall_more(buy_button=self.I_ME_BROKEN, remain_number=False,
-                               money_ocr=self.I_MALL_RESOURCE_MEDAL.build_mall_resource_ocr(self.device.image),
-                               buy_number=con.broken_amulet, buy_max=99, buy_money=20)
+            self.screenshot()
+            if self.appear(self.I_ME_BROKEN):
+                self.buy_mall_more(buy_button=self.I_ME_BROKEN, remain_number=False,
+                                   money_ocr=self.I_MALL_RESOURCE_MEDAL.build_mall_resource_ocr(self.device.image),
+                                   buy_number=con.broken_amulet, buy_max=99, buy_money=20)
+            else:
+                not_found.append('broken_amulet')
+
+        # 有识别失败的商品:核对售罄数量,确认失败是否为"商品售罄"(正常情况),而非页面异常。
+        # 注意:页面上的售罄标签可能包含未配置购买的商品,因此用 >= 而非 ==,避免误报。
+        if not_found:
+            self.screenshot()
+            soldout_count = self.count_soldout()
+            if soldout_count >= len(not_found):
+                logger.info(f'Medal: {len(not_found)} item(s) not detected, sold out count is {soldout_count}, '
+                            f'the not-detected items are sold out, skip normally')
+            else:
+                logger.warning(f'Medal: {len(not_found)} item(s) not detected, but sold out count is only {soldout_count}, '
+                               f'some items may not be sold out, page may be abnormal, please check manually')
 
         time.sleep(1)
 
+    def count_soldout(self) -> int:
+        """
+        统计勋章商店页面中"售罄"标签的数量。
+        通过 OCR 检测商品区域内含"售"字的文本块(售罄标签),用于核对识别失败的商品是否均为售罄。
+        需要先调用 screenshot 保证 self.device.image 为最新画面。
+        :return: 售罄标签数量
+        """
+        results = self.O_SOLD_OUT.detect_and_ocr(self.device.image, logDisplay=False)
+        count = sum(1 for r in results if '售' in r.ocr_text)
+        logger.info(f'Medal sold out count: {count}')
+        return count
+
 
 if __name__ == '__main__':
     from module.config.config import Config

+ 4 - 2
tasks/RichMan/mall/navbar.py

@@ -74,7 +74,8 @@ class MallNavbar(GameUi, RichManAssets):
         :return:
         """
         self._enter_sundry()
-        self.ui_click(self.I_SIDE_SURE_MEDAL, self.I_SIDE_CHECK_MEDAL)
+        # 增加超时保护:15秒内未出现勋章页确认则放弃,避免入口点击无效时无限循环
+        self.ui_click(self.I_SIDE_SURE_MEDAL, self.I_SIDE_CHECK_MEDAL, timeout=15)
 
     def _enter_charisma(self):
         """
@@ -89,7 +90,8 @@ class MallNavbar(GameUi, RichManAssets):
         返回商城
         :return:
         """
-        self.ui_click(self.I_UI_BACK_YELLOW, self.I_CHECK_MALL)
+        # 增加超时保护:15秒内未回到商城则放弃,避免界面异常时无限点击返回按钮导致卡死
+        self.ui_click(self.I_UI_BACK_YELLOW, self.I_CHECK_MALL, timeout=15)
 
     def mall_resource(self, index: int) -> int:
         """