diff --git a/src/pyclashbot/bot/request_state.py b/src/pyclashbot/bot/request_state.py index 4b7c782cd..268bdf680 100644 --- a/src/pyclashbot/bot/request_state.py +++ b/src/pyclashbot/bot/request_state.py @@ -112,7 +112,7 @@ def request_state(vm_index, logger: Logger, next_state: str) -> str: # get to clan page logger.change_status("Getting to clan tab to request a card") - if get_to_clan_tab_from_clash_main(vm_index, logger) == "restart": + if get_to_clan_tab_from_clash_main(vm_index, logger) is False: logger.change_status(status="ERROR 74842744443 Not on clan tab") return "restart" @@ -274,6 +274,45 @@ def request_state_check_pixels_for_clan_flag(vm_index) -> bool: return False +def click_random_requestable_card(vm_index) -> bool: + def make_coord_list(x_range,y_range): + coords = [] + for x in range(x_range[0],x_range[1]): + for y in range(y_range[0],y_range[1]): + c = [x,y] + coords.append(c) + + random.shuffle(coords) + return coords + + def is_valid_pixel(pixel): + def is_greyscale_pixel(pixel): + #if all the values are wihtin 5 of eachother, it's greyscale + if abs(pixel[0] - pixel[1]) < 5 and abs(pixel[1] - pixel[2]) < 5: + return True + + return False + + if is_greyscale_pixel(pixel): + return False + + if pixel_is_equal(pixel,[222,235,241],tol=20): + return False + + return True + + #check pixels in the request card grid region + iar = screenshot(vm_index) + for coord in make_coord_list((69,356),(213,557)): + pixel = iar[coord[1]][coord[0]] + #if the pixel indicates requestable, click it, return True + if is_valid_pixel(pixel): + click(vm_index,coord[0],coord[1]) + return True + + #fail return + return False + def do_request(vm_index, logger: Logger) -> bool: logger.change_status(status="Initiating request process") @@ -316,11 +355,17 @@ def do_request(vm_index, logger: Logger) -> bool: # Click on a random card logger.change_status(status="Clicking a random card to request") - click( - vm_index=vm_index, - x_coord=random.randint(a=67, b=358), - y_coord=random.randint(a=211, b=547), - ) + + click_try_limit = 10 + click_tries = 0 + while click_random_requestable_card(vm_index) is False: + print('Failed to click an upgradable card.') + click_tries += 1 + if click_tries>click_try_limit: + logger.change_status('Failed to click an upgradable card. too many times') + return False + + time.sleep(3) # Attempt to find the request button @@ -480,4 +525,4 @@ def check_if_can_request_3(vm_index): if __name__ == "__main__": - request_state(1, Logger(), "next_state") + print(request_state(1, Logger(), "next_state"))