medal.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. # This Python file uses the following encoding: utf-8
  2. # @author runhey
  3. # github https://github.com/runhey
  4. import time
  5. from module.logger import logger
  6. from tasks.GameUi.page import page_main, page_guild
  7. from tasks.GameUi.game_ui import GameUi
  8. from tasks.Component.Buy.buy import Buy
  9. from tasks.RichMan.assets import RichManAssets
  10. from tasks.RichMan.config import MedalRoom
  11. from tasks.RichMan.mall.friendship_points import FriendshipPoints
  12. class Medal(FriendshipPoints):
  13. def execute_medal(self, con: MedalRoom = None):
  14. if not con:
  15. con = self.config.rich_man.medal_room
  16. if not con.enable:
  17. logger.info('Medal is not enable')
  18. return
  19. self._enter_medal()
  20. # 记录识别失败的商品,用于事后核对是否为售罄
  21. not_found = []
  22. # 黑蛋
  23. if con.black_daruma:
  24. self.screenshot()
  25. if self.appear(self.I_ME_BLACK):
  26. self.buy_mall_one(buy_button=self.I_ME_BLACK, buy_check=self.I_ME_CHECK_BLACK,
  27. money_ocr=self.O_MALL_RESOURCE_3, buy_money=480)
  28. else:
  29. not_found.append('black_daruma')
  30. # 蓝票
  31. if con.mystery_amulet:
  32. self.screenshot()
  33. if self.appear(self.I_ME_BLUE):
  34. self.buy_mall_one(buy_button=self.I_ME_BLUE, buy_check=self.I_ME_CHECK_BLUE,
  35. money_ocr=self.O_MALL_RESOURCE_3, buy_money=180)
  36. else:
  37. not_found.append('mystery_amulet')
  38. # 体力100
  39. if con.ap_100:
  40. self.screenshot()
  41. if self.appear(self.I_ME_AP):
  42. self.buy_mall_one(buy_button=self.I_ME_AP, buy_check=self.I_ME_CHECK_AP,
  43. money_ocr=self.O_MALL_RESOURCE_3, buy_money=120)
  44. else:
  45. not_found.append('ap_100')
  46. # 随机御魂
  47. if con.random_soul:
  48. self.screenshot()
  49. if self.appear(self.I_ME_SOULS):
  50. self.buy_mall_one(buy_button=self.I_ME_SOULS, buy_check=self.I_ME_CHECK_SOULS,
  51. money_ocr=self.O_MALL_RESOURCE_3, buy_money=320)
  52. else:
  53. not_found.append('random_soul')
  54. # 两颗白蛋
  55. if con.white_daruma:
  56. self.screenshot()
  57. if self.appear(self.I_ME_WHITE):
  58. 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),
  59. buy_number=2, buy_max=2, buy_money=100)
  60. else:
  61. not_found.append('white_daruma')
  62. # 十张挑战券
  63. if con.challenge_pass:
  64. self.screenshot()
  65. if self.appear(self.I_ME_CHALLENGE_PASS):
  66. 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),
  67. buy_number=con.challenge_pass, buy_max=10, buy_money=30)
  68. else:
  69. not_found.append('challenge_pass')
  70. # 红蛋
  71. if con.red_daruma:
  72. self.screenshot()
  73. if self.appear(self.I_ME_RED):
  74. self.buy_mall_more(buy_button=self.I_ME_RED, remain_number=False,
  75. money_ocr=self.I_MALL_RESOURCE_MEDAL.build_mall_resource_ocr(self.device.image),
  76. buy_number=con.red_daruma, buy_max=99, buy_money=30)
  77. else:
  78. not_found.append('red_daruma')
  79. # 破碎的咒符
  80. if con.broken_amulet:
  81. self.screenshot()
  82. if self.appear(self.I_ME_BROKEN):
  83. self.buy_mall_more(buy_button=self.I_ME_BROKEN, remain_number=False,
  84. money_ocr=self.I_MALL_RESOURCE_MEDAL.build_mall_resource_ocr(self.device.image),
  85. buy_number=con.broken_amulet, buy_max=99, buy_money=20)
  86. else:
  87. not_found.append('broken_amulet')
  88. # 有识别失败的商品:核对售罄数量,确认失败是否为"商品售罄"(正常情况),而非页面异常。
  89. # 注意:页面上的售罄标签可能包含未配置购买的商品,因此用 >= 而非 ==,避免误报。
  90. if not_found:
  91. self.screenshot()
  92. soldout_count = self.count_soldout()
  93. if soldout_count >= len(not_found):
  94. logger.info(f'Medal: {len(not_found)} item(s) not detected, sold out count is {soldout_count}, '
  95. f'the not-detected items are sold out, skip normally')
  96. else:
  97. logger.warning(f'Medal: {len(not_found)} item(s) not detected, but sold out count is only {soldout_count}, '
  98. f'some items may not be sold out, page may be abnormal, please check manually')
  99. time.sleep(1)
  100. def count_soldout(self) -> int:
  101. """
  102. 统计勋章商店页面中"售罄"标签的数量。
  103. 通过 OCR 检测商品区域内含"售"字的文本块(售罄标签),用于核对识别失败的商品是否均为售罄。
  104. 需要先调用 screenshot 保证 self.device.image 为最新画面。
  105. :return: 售罄标签数量
  106. """
  107. results = self.O_SOLD_OUT.detect_and_ocr(self.device.image, logDisplay=False)
  108. count = sum(1 for r in results if '售' in r.ocr_text)
  109. logger.info(f'Medal sold out count: {count}')
  110. return count
  111. if __name__ == '__main__':
  112. from module.config.config import Config
  113. from module.device.device import Device
  114. c = Config('oas1')
  115. d = Device(c)
  116. t = Medal(c, d)
  117. t.execute_medal()