Skip to content

Commit

Permalink
Request, Upgrade, & Card Mastery Fixes (#399)
Browse files Browse the repository at this point in the history
  • Loading branch information
Stredy authored Mar 29, 2024
1 parent 4ada33d commit 995e493
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 24 deletions.
36 changes: 25 additions & 11 deletions src/pyclashbot/bot/card_mastery_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def collect_card_mastery_rewards(vm_index, logger: Logger) -> bool:
collect_first_mastery_reward(vm_index)
logger.change_status("Collected a card mastery reward!")
logger.add_card_mastery_reward_collection()
time.sleep(3)
time.sleep(2)

# get to clash main
logger.change_status("Returning to clash main menu")
Expand All @@ -69,32 +69,46 @@ def collect_card_mastery_rewards(vm_index, logger: Logger) -> bool:

def collect_first_mastery_reward(vm_index):
# click the card mastery reward icon
click(vm_index, 270, 480)
click(vm_index, 318, 444)
time.sleep(3)

# click first card
click(vm_index, 105, 170)
click(vm_index, 99, 166)
time.sleep(3)

# click rewards
for y in range(280, 520, 35):
# click rewards at specific Y positions
y_positions = [316, 403, 488]
for y in y_positions:
click(vm_index, 200, y)
time.sleep(0.5)

# click deadspace a bunch
click(vm_index, 5, 355, clicks=15, interval=0.5)
time.sleep(3)

click(vm_index, 243, 600)


def card_mastery_rewards_exist(vm_index):
# Convert the screenshot to a NumPy array for easier access
iar = numpy.asarray(screenshot(vm_index))
pixels = [
iar[460][280],
iar[467][282],
iar[464][279],

# Define the target color, positions to check, and tolerance
target_color = numpy.array([57, 9, 236])
positions_to_check = [
(435, 326),
(435, 336),
]
tolerance = 10 # Define how much color variation is acceptable

# Function to check if a pixel is within tolerance
def is_color_within_tolerance(pixel_color, target_color, tolerance):
return numpy.all(numpy.abs(pixel_color - target_color) <= tolerance)

for p in pixels:
if p[2] < p[0] + p[1]:
# Check each specified position for the target color within tolerance
for pos in positions_to_check:
pixel_color = iar[pos[0], pos[1]]
if not is_color_within_tolerance(pixel_color, target_color, tolerance):
return False
return True

Expand Down
23 changes: 15 additions & 8 deletions src/pyclashbot/bot/request_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
from pyclashbot.utils.logger import Logger


def find_request_button(vm_index):
def find_request_button(vm_index, logger: Logger):
"""
Finds the location of the request button on the screen.
Expand All @@ -47,10 +47,12 @@ def find_request_button(vm_index):
screenshot(vm_index),
folder_name,
names,
0.88,
0.7,
)

coord = get_first_location(locations)
logger.log(
f"The button coordinates were found, X: {coord[1]} Y: {coord[0]}")
if coord is None:
return None
return [coord[1], coord[0]]
Expand All @@ -75,8 +77,10 @@ def request_state(vm_index, logger: Logger, next_state: str) -> str:
# if not on main: return
clash_main_check = check_if_on_clash_main_menu(vm_index)
if clash_main_check is not True:
logger.change_status("Not on clash main for the start of request_state()")
logger.log("These are the pixels the bot saw after failing to find clash main:")
logger.change_status(
"Not on clash main for the start of request_state()")
logger.log(
"These are the pixels the bot saw after failing to find clash main:")
for pixel in clash_main_check:
logger.log(f" {pixel}")

Expand All @@ -86,7 +90,8 @@ def request_state(vm_index, logger: Logger, next_state: str) -> str:
logger.change_status("Checking if in a clan before requesting")
in_a_clan_return = request_state_check_if_in_a_clan(vm_index, logger)
if in_a_clan_return == "restart":
logger.change_status(status="Error 05708425 Failure with check_if_in_a_clan")
logger.change_status(
status="Error 05708425 Failure with check_if_in_a_clan")
return "restart"

if not in_a_clan_return:
Expand Down Expand Up @@ -220,7 +225,8 @@ def do_request(vm_index, logger: Logger) -> None:
time.sleep(3)

# max scrolls
logger.change_status(status="Counting the maximum scrolls in the request page")
logger.change_status(
status="Counting the maximum scrolls in the request page")
max_scrolls: int = count_scrolls_in_request_page(vm_index=vm_index)
logger.log(f"Found {max_scrolls} scrolls maximum in request page")
random_scroll_amount: int = random.randint(a=0, b=max_scrolls)
Expand Down Expand Up @@ -252,7 +258,7 @@ def do_request(vm_index, logger: Logger) -> None:
logger.change_status(status="Clicking request")

# get request button coord
coord = find_request_button(vm_index)
coord = find_request_button(vm_index, logger)
if coord is None:
continue

Expand All @@ -264,7 +270,8 @@ def do_request(vm_index, logger: Logger) -> None:
logger.add_request()

requests = logger.get_requests()
logger.log(f"Incremented requests stat from {prev_requests} to {requests}")
logger.log(
f"Incremented requests stat from {prev_requests} to {requests}")

time.sleep(3)
break
Expand Down
14 changes: 9 additions & 5 deletions src/pyclashbot/bot/upgrade_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,10 @@ def upgrade_cards_state(vm_index, logger: Logger, next_state):
# if not on clash main, return restart
clash_main_check = check_if_on_clash_main_menu(vm_index)
if clash_main_check is not True:
logger.change_status("Not on clash main at the start of upgrade_cards_state()")
logger.log("These are the pixels the bot saw after failing to find clash main:")
logger.change_status(
"Not on clash main at the start of upgrade_cards_state()")
logger.log(
"These are the pixels the bot saw after failing to find clash main:")
for pixel in clash_main_check:
logger.log(f" {pixel}")

Expand Down Expand Up @@ -106,16 +108,18 @@ def upgrade_cards_state(vm_index, logger: Logger, next_state):

time.sleep(2)
logger.change_status(status="Done upgrading cards")
click(vm_index, DEADSPACE_COORD[0], DEADSPACE_COORD[1], clicks=3, interval=1.5)
click(vm_index, 211, 607)
time.sleep(1)

# return to clash main
print("Returning to clash main after upgrading")
click(vm_index, 245, 593)
click(vm_index, 243, 600)
time.sleep(3)

# wait for main
if wait_for_clash_main_menu(vm_index, logger, deadspace_click=False) is False:
logger.change_status("Failed to wait for clash main after upgrading cards")
logger.change_status(
"Failed to wait for clash main after upgrading cards")
return "restart"

logger.update_time_of_last_card_upgrade(time.time())
Expand Down

0 comments on commit 995e493

Please sign in to comment.