screenshot.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. import numpy as np
  2. import os
  3. import time
  4. from helper import (
  5. cv2,
  6. find_template,
  7. tap as tap_helper,
  8. swipe as swipe_helper,
  9. capture_current_screen,
  10. save_screenshot,
  11. )
  12. screenshot_address = os.getenv("SCREENSHOT_PATH")
  13. # templates
  14. close_sub_fights = cv2.imread("templates/close_sub_fights.jpg")
  15. close_fight = cv2.imread("templates/close_fight.jpg")
  16. fight_button = cv2.imread("templates/i.jpg")
  17. end_of_log = cv2.imread("templates/end_of_log.jpg")
  18. end_of_log2 = cv2.imread("templates/end_of_log2.jpg")
  19. # cursor positions
  20. fight_scroll_top = "2200 626"
  21. fight_scroll_bottom = "2200 1500"
  22. titan_fight = "1400 860"
  23. damage_taken = "450 850"
  24. close_details = "2175 450"
  25. close_titan_fight = "2650 570"
  26. defense_log = "350 870"
  27. def non_max_suppression(boxes, overlapThresh):
  28. if len(boxes) == 0:
  29. return []
  30. # Convert to float
  31. boxes = np.array(boxes, dtype="float")
  32. # Initialize the list of picked indexes
  33. pick = []
  34. # Grab the coordinates of the bounding boxes
  35. x1 = boxes[:, 0]
  36. y1 = boxes[:, 1]
  37. x2 = boxes[:, 2]
  38. y2 = boxes[:, 3]
  39. # Compute the area of the bounding boxes and sort by bottom-right y-coordinate
  40. area = (x2 - x1 + 1) * (y2 - y1 + 1)
  41. idxs = np.argsort(y2)
  42. # Keep looping while some indexes still remain in the indexes list
  43. while len(idxs) > 0:
  44. # Grab the last index in the indexes list and add the index value to the list of picked indexes
  45. last = len(idxs) - 1
  46. i = idxs[last]
  47. pick.append(i)
  48. # Find the largest (x, y) coordinates for the start of the bounding box and the smallest (x, y)
  49. # coordinates for the end of the bounding box
  50. xx1 = np.maximum(x1[i], x1[idxs[:last]])
  51. yy1 = np.maximum(y1[i], y1[idxs[:last]])
  52. xx2 = np.minimum(x2[i], x2[idxs[:last]])
  53. yy2 = np.minimum(y2[i], y2[idxs[:last]])
  54. # Compute the width and height of the bounding box
  55. w = np.maximum(0, xx2 - xx1 + 1)
  56. h = np.maximum(0, yy2 - yy1 + 1)
  57. # Compute the ratio of overlap
  58. overlap = (w * h) / area[idxs[:last]]
  59. # Delete all indexes from the index list that have overlap greater than the threshold
  60. idxs = np.delete(
  61. idxs, np.concatenate(([last], np.where(overlap > overlapThresh)[0]))
  62. )
  63. # Return only the bounding boxes that were picked
  64. return boxes[pick].astype("int")
  65. def sleep(t=1):
  66. print(f"sleep {t}s")
  67. time.sleep(t)
  68. def tap(location):
  69. tap_helper(location)
  70. sleep(1)
  71. def swipe(start, end, duration=1000):
  72. swipe_helper(start, end, duration)
  73. sleep(0.5)
  74. def tap_button(template):
  75. button = find_templates(template)
  76. if len(button) == 0:
  77. return
  78. tap(f"{button[0][0]} {button[0][1]}")
  79. def is_end_of_log():
  80. templates = find_templates(end_of_log)
  81. if len(templates) == 0:
  82. templates = find_templates(end_of_log2)
  83. result = len(templates) > 0
  84. if result:
  85. print("reached end of guild war log!")
  86. return result
  87. def find_max_y_pair(coordinates):
  88. # find the coordinate pair with the maximum y value
  89. result = max(coordinates, key=lambda x: x[1])
  90. return f"{result[0]} {result[1]}"
  91. def snap():
  92. save_screenshot(screenshot_address)
  93. sleep(0.5)
  94. def take_fight_screenshots():
  95. snap()
  96. # # if you desperately need submits
  97. # tap(damage_taken)
  98. # snap()
  99. tap_button(close_fight)
  100. def find_templates(template):
  101. capture_current_screen()
  102. return find_template(template)
  103. def process_war_log():
  104. buttons = find_templates(fight_button)
  105. if len(buttons) == 0:
  106. swipe(fight_scroll_bottom, fight_scroll_top)
  107. return
  108. # process all found buttons
  109. for pair in buttons:
  110. tap(f"{pair[0]} {pair[1]}")
  111. sub_buttons = find_templates(fight_button)
  112. if len(sub_buttons) == 0:
  113. take_fight_screenshots()
  114. else:
  115. for pair2 in sub_buttons:
  116. tap(f"{pair2[0]} {pair2[1]}")
  117. take_fight_screenshots()
  118. tap_button(close_sub_fights)
  119. swipe(find_max_y_pair(buttons), fight_scroll_top)
  120. # start
  121. while not is_end_of_log():
  122. process_war_log()
  123. # possible duplicates here, but necessary to be sure to get the last fights
  124. process_war_log()