friendship_points.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 module.atom.image import RuleImage
  7. from module.atom.ocr import RuleOcr
  8. from tasks.RichMan.mall.special import Special
  9. from tasks.RichMan.config import FriendshipPoints as FriendshipPointsConfig
  10. import re
  11. class FriendshipPoints(Special):
  12. def execute_friendship(self, con: FriendshipPointsConfig = None):
  13. if not con:
  14. con = self.config.rich_man.friendship_points
  15. if not con.enable:
  16. logger.info('Friendship points is not enable')
  17. return
  18. self._enter_friendship()
  19. # 购买
  20. if con.white_daruma:
  21. self.buy_mall_one(buy_button=self.I_FS_WHITE_CLICK, buy_check=self.I_FS_WHITE_CHECK,
  22. money_ocr=self.O_MALL_RESOURCE_5, buy_money=1000)
  23. if con.red_daruma != 0:
  24. self.buy_mall_more(buy_button=self.I_FS_RED, remain_number=False, money_ocr=self.O_MALL_RESOURCE_5,
  25. buy_number=con.red_daruma, buy_max=99, buy_money=150)
  26. if con.broken_amulet != 0:
  27. self.buy_mall_more(buy_button=self.I_FS_BROKEN, remain_number=False, money_ocr=self.O_MALL_RESOURCE_5,
  28. buy_number=con.broken_amulet, buy_max=99, buy_money=100)
  29. def buy_mall_one(self, buy_button: RuleImage, buy_check: RuleImage, money_ocr: RuleOcr, buy_money: int):
  30. """
  31. 针对只能买一个的
  32. :param buy_button:
  33. :param buy_check:
  34. :param money_ocr:
  35. :param buy_money: 买这一个花多少
  36. :return:
  37. """
  38. logger.hr(buy_button.name, 3)
  39. self.screenshot()
  40. # 检查是否出现了购买按钮
  41. if not self.appear(buy_button):
  42. logger.warning('Buy button is not appear')
  43. return False
  44. # 是否检查剩余数量
  45. _remain = self._special_check_remain(buy_button)
  46. if _remain == 0:
  47. logger.warning('Remain number is 0')
  48. return False
  49. # 检查总勋章
  50. current_money = money_ocr.ocr(self.device.image)
  51. if not isinstance(current_money, int):
  52. logger.warning('Money ocr failed')
  53. return False
  54. money_enough = current_money >= buy_money
  55. if not money_enough:
  56. logger.warning(f'No enough money {current_money}')
  57. return False
  58. # 点击购买
  59. return self.buy_one(buy_button, buy_check)
  60. def buy_mall_more(self, buy_button: RuleImage, remain_number: bool, money_ocr: RuleOcr,
  61. buy_number: int, buy_max: int, buy_money: int):
  62. """
  63. 针对可以买多个的
  64. :param money_ocr: 检查钱的第几个
  65. :param remain_number: 是否需要检查剩余数量
  66. :param buy_button:
  67. :param buy_number:
  68. :param buy_max:
  69. :param buy_money:
  70. :return:
  71. """
  72. logger.hr(buy_button.name, 3)
  73. if buy_number == 0:
  74. logger.info('Buy number is 0')
  75. return
  76. self.screenshot()
  77. # 检查是否出现了购买按钮
  78. if not self.appear(buy_button):
  79. logger.warning('Buy button is not appear')
  80. return
  81. # 是否检查剩余数量
  82. if remain_number:
  83. _remain = self._special_check_remain(buy_button)
  84. if _remain == 0:
  85. logger.warning('Remain number is 0')
  86. return
  87. if _remain < buy_number:
  88. logger.warning(f'Remain number is {_remain}, buy number is {buy_number}')
  89. buy_number = _remain
  90. # 检查钱够不够
  91. current_money = money_ocr.ocr(self.device.image)
  92. if not isinstance(current_money, int):
  93. logger.warning('Money ocr failed')
  94. return
  95. money_enough = current_money >= buy_money * buy_number
  96. if not money_enough:
  97. logger.warning(f'Money is not enough {current_money}')
  98. # 判断够不够买2个
  99. if current_money < buy_money * 2:
  100. logger.warning('Money is not enough 2')
  101. return
  102. buy_number = current_money // buy_money
  103. # 购买
  104. logger.info(f'Buy number is {buy_number}')
  105. if buy_number >= buy_max:
  106. buy_cycles_number = buy_number // buy_max
  107. buy_res_number = buy_number % buy_max
  108. else:
  109. buy_cycles_number = None
  110. buy_res_number = buy_number
  111. if buy_cycles_number:
  112. for i in range(buy_cycles_number):
  113. self.buy_more(buy_button)
  114. time.sleep(0.5)
  115. if buy_res_number:
  116. self.buy_more(buy_button, buy_res_number)
  117. time.sleep(0.5)
  118. if __name__ == '__main__':
  119. from module.config.config import Config
  120. from module.device.device import Device
  121. c = Config('test')
  122. d = Device(c)
  123. t = FriendshipPoints(c, d)
  124. t.execute_friendship()