| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171 |
- import numpy as np
- import os
- import time
- from helper import (
- cv2,
- find_template,
- tap as tap_helper,
- swipe as swipe_helper,
- capture_current_screen,
- save_screenshot,
- )
- screenshot_address = os.getenv("SCREENSHOT_PATH")
- # templates
- close_sub_fights = cv2.imread("templates/close_sub_fights.jpg")
- close_fight = cv2.imread("templates/close_fight.jpg")
- fight_button = cv2.imread("templates/i.jpg")
- end_of_log = cv2.imread("templates/end_of_log.jpg")
- end_of_log2 = cv2.imread("templates/end_of_log2.jpg")
- # cursor positions
- fight_scroll_top = "2200 626"
- fight_scroll_bottom = "2200 1500"
- titan_fight = "1400 860"
- damage_taken = "450 850"
- close_details = "2175 450"
- close_titan_fight = "2650 570"
- defense_log = "350 870"
- def non_max_suppression(boxes, overlapThresh):
- if len(boxes) == 0:
- return []
- # Convert to float
- boxes = np.array(boxes, dtype="float")
- # Initialize the list of picked indexes
- pick = []
- # Grab the coordinates of the bounding boxes
- x1 = boxes[:, 0]
- y1 = boxes[:, 1]
- x2 = boxes[:, 2]
- y2 = boxes[:, 3]
- # Compute the area of the bounding boxes and sort by bottom-right y-coordinate
- area = (x2 - x1 + 1) * (y2 - y1 + 1)
- idxs = np.argsort(y2)
- # Keep looping while some indexes still remain in the indexes list
- while len(idxs) > 0:
- # Grab the last index in the indexes list and add the index value to the list of picked indexes
- last = len(idxs) - 1
- i = idxs[last]
- pick.append(i)
- # Find the largest (x, y) coordinates for the start of the bounding box and the smallest (x, y)
- # coordinates for the end of the bounding box
- xx1 = np.maximum(x1[i], x1[idxs[:last]])
- yy1 = np.maximum(y1[i], y1[idxs[:last]])
- xx2 = np.minimum(x2[i], x2[idxs[:last]])
- yy2 = np.minimum(y2[i], y2[idxs[:last]])
- # Compute the width and height of the bounding box
- w = np.maximum(0, xx2 - xx1 + 1)
- h = np.maximum(0, yy2 - yy1 + 1)
- # Compute the ratio of overlap
- overlap = (w * h) / area[idxs[:last]]
- # Delete all indexes from the index list that have overlap greater than the threshold
- idxs = np.delete(
- idxs, np.concatenate(([last], np.where(overlap > overlapThresh)[0]))
- )
- # Return only the bounding boxes that were picked
- return boxes[pick].astype("int")
- def sleep(t=1):
- print(f"sleep {t}s")
- time.sleep(t)
- def tap(location):
- tap_helper(location)
- sleep(1)
- def swipe(start, end, duration=1000):
- swipe_helper(start, end, duration)
- sleep(0.5)
- def tap_button(template):
- button = find_templates(template)
- if len(button) == 0:
- return
- tap(f"{button[0][0]} {button[0][1]}")
- def is_end_of_log():
- templates = find_templates(end_of_log)
- if len(templates) == 0:
- templates = find_templates(end_of_log2)
- result = len(templates) > 0
- if result:
- print("reached end of guild war log!")
- return result
- def find_max_y_pair(coordinates):
- # find the coordinate pair with the maximum y value
- result = max(coordinates, key=lambda x: x[1])
- return f"{result[0]} {result[1]}"
- def snap():
- save_screenshot(screenshot_address)
- sleep(0.5)
- def take_fight_screenshots():
- snap()
- # # if you desperately need submits
- # tap(damage_taken)
- # snap()
- tap_button(close_fight)
- def find_templates(template):
- capture_current_screen()
- return find_template(template)
- def process_war_log():
- buttons = find_templates(fight_button)
- if len(buttons) == 0:
- swipe(fight_scroll_bottom, fight_scroll_top)
- return
- # process all found buttons
- for pair in buttons:
- tap(f"{pair[0]} {pair[1]}")
- sub_buttons = find_templates(fight_button)
- if len(sub_buttons) == 0:
- take_fight_screenshots()
- else:
- for pair2 in sub_buttons:
- tap(f"{pair2[0]} {pair2[1]}")
- take_fight_screenshots()
- tap_button(close_sub_fights)
- swipe(find_max_y_pair(buttons), fight_scroll_top)
- # start
- while not is_end_of_log():
- process_war_log()
- # possible duplicates here, but necessary to be sure to get the last fights
- process_war_log()
|