Skip to content

Commit

Permalink
linting fight state file, fixing moving to 1v1 state if 2v2 mode is l…
Browse files Browse the repository at this point in the history
…ocked, fixing exiting clan page after donating, making docking less intense
  • Loading branch information
matthewmiglio committed Dec 24, 2023
1 parent 3938959 commit ca2c595
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 47 deletions.
88 changes: 44 additions & 44 deletions src/pyclashbot/bot/do_fight_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,17 @@ def start_2v2_fight_state(vm_index, logger: Logger) -> Literal["restart", "2v2_f
scroll_up_on_left_side_of_screen(vm_index)
time.sleep(1)

#if there is a locked events page, return restart
# if there is a locked events page, return restart
if check_for_locked_events_page(vm_index):
logger.change_status("Locked events page! Doing 1v1 instead...")
click(vm_index, 170,589)
click(vm_index, 170, 589)
time.sleep(3)
if not check_if_on_clash_main_menu(vm_index):
logger.change_status('Failed to get from events tab to clash main after locked events page')
return 'restart'
return 'start_1v1_fight_state'
logger.change_status(
"Failed to get from events tab to clash main after locked events page"
)
return "restart"
return start_1v1_fight_state(vm_index, logger)

# click 2v2 icon location
click_2v2_icon_button(vm_index)
Expand Down Expand Up @@ -129,11 +131,11 @@ def check_for_locked_events_page(vm_index):
]

colors = [
[239, 130, 25],
[ 91, 227, 153],
[255 ,251, 239],
[241 ,129, 25],
[239 ,130, 27],
[239, 130, 25],
[91, 227, 153],
[255, 251, 239],
[241, 129, 25],
[239, 130, 27],
]

for i, p in enumerate(pixels):
Expand All @@ -143,7 +145,6 @@ def check_for_locked_events_page(vm_index):
return True



def start_1v1_fight_state(vm_index, logger: Logger) -> Literal["restart", "1v1_fight"]:
"""method to handle starting a 1v1 fight"""

Expand Down Expand Up @@ -374,7 +375,6 @@ def _2v2_fight_loop(vm_index, logger: Logger) -> Literal["restart", "good"]:

# while in battle:
while check_for_in_battle_with_delay(vm_index):

# if check_if_at_max_elixer(vm_index):
# logger.change_status("At max elixer so just mag dumping!!!")
# mag_dump(vm_index, logger)
Expand Down Expand Up @@ -457,10 +457,9 @@ def _2v2_fight_loop(vm_index, logger: Logger) -> Literal["restart", "good"]:
plays += 1

logger.change_status(
f"Played {id_string} on {this_play_side} side in {str(time.time() - this_play_start_time)[:4]}s"
f"Played {id_string} on {this_play_side} side in {str(time.time() - this_play_start_time)[:4]}s"
)


if logger.get_cards_played() != 0:
logger.remove_card_played(cards_to_remove=random.randint(0, 1))
cards_played = logger.get_cards_played()
Expand Down Expand Up @@ -725,8 +724,8 @@ def count_friendly_crowns(vm_index):

for index, pixel in enumerate(pixels):
color = colors[index]
if pixel_is_equal(pixel, color, tol=35):
crowns+=1
if pixel_is_equal(pixel, color, tol=35):
crowns += 1

return crowns

Expand All @@ -740,10 +739,10 @@ def check_for_2v2_chat_window(vm_index):
iar[19][320],
]
colors = [
[247 ,239, 235],
[255 ,255, 255],
[255 ,186, 104],
[255 ,154, 51],
[247, 239, 235],
[255, 255, 255],
[255, 186, 104],
[255, 154, 51],
]

for i, p in enumerate(pixels):
Expand All @@ -754,11 +753,11 @@ def check_for_2v2_chat_window(vm_index):


def close_2v2_chat_window(vm_index):
click(vm_index, 399,15)
click(vm_index, 399, 15)


def count_crowns(vm_index, logger:Logger):
#close chat window if its 2v2
def count_crowns(vm_index, logger: Logger):
# close chat window if its 2v2
if check_for_2v2_chat_window(vm_index):
close_2v2_chat_window(vm_index)
time.sleep(1)
Expand All @@ -770,11 +769,12 @@ def count_crowns(vm_index, logger:Logger):
logger.add_count_to_enemy_crowns(enemy_crowns)
logger.add_count_to_friendly_crowns(friendly_crowns)

print('New crown count')
print(f'friendly crowns: {friendly_crowns}')
print(f'enemy crowns: {enemy_crowns}')
print("New crown count")
print(f"friendly crowns: {friendly_crowns}")
print(f"enemy crowns: {enemy_crowns}")

return friendly_crowns, enemy_crowns

return friendly_crowns,enemy_crowns

def end_fight_state(
vm_index, logger: Logger, next_state, disable_win_tracker_toggle=True
Expand Down Expand Up @@ -848,6 +848,7 @@ def check_if_previous_game_was_win(
status="Error 95867235 wait_for_clash_main_menu() in check_if_previous_game_was_win()"
)
return "restart"
time.sleep(2)

return is_a_win

Expand Down Expand Up @@ -876,12 +877,12 @@ def get_to_main_after_fight(vm_index, logger, next_state):

start_time = time.time()

logger.log('Counting crowns')
friendly_crowns,enemy_crowns = count_crowns(vm_index, logger)
logger.log(f'Score last match was: {friendly_crowns}/{enemy_crowns}')
logger.log("Counting crowns")
friendly_crowns, enemy_crowns = count_crowns(vm_index, logger)
logger.log(f"Score last match was: {friendly_crowns}/{enemy_crowns}")

while 1:
logger.log('Getting back to main')
logger.log("Getting back to main")

if time.time() - start_time > POST_FIGHT_TIMEOUT:
logger.log("took too long to get to clash main after a fight")
Expand All @@ -894,7 +895,7 @@ def get_to_main_after_fight(vm_index, logger, next_state):
handle_end_2v2_battle_condition_2(vm_index, logger)

# if on end of 2v2 battle screen c3, click OK
handle_end_2v2_battle_condition_3(logger,vm_index)
handle_end_2v2_battle_condition_3(logger, vm_index)

# if on end of 1v1 battle screen c1, click OK
handle_end_1v1_battle_condition_1(vm_index, logger)
Expand Down Expand Up @@ -1013,7 +1014,7 @@ def check_for_end_1v1_battle_condition_2(vm_index) -> bool:


def check_for_end_2v2_battle_condition_3(vm_index):
iar=numpy.asarray(screenshot(vm_index))
iar = numpy.asarray(screenshot(vm_index))
pixels = [
iar[518][56],
iar[517][76],
Expand All @@ -1022,23 +1023,23 @@ def check_for_end_2v2_battle_condition_3(vm_index):
]

colors = [
(255,255,255),
(255,255,255),
(78,175,255),
(104,187,255),
(255, 255, 255),
(255, 255, 255),
(78, 175, 255),
(104, 187, 255),
]

for i,p in enumerate(pixels):
if not pixel_is_equal(p,colors[i],tol=35):
for i, p in enumerate(pixels):
if not pixel_is_equal(p, colors[i], tol=35):
return False
return True


def handle_end_2v2_battle_condition_3(logger,vm_index):
def handle_end_2v2_battle_condition_3(logger, vm_index):
"""method to handle the #3 possible end of 2v2 battle screen"""
if check_for_end_2v2_battle_condition_3(vm_index):
logger.log("On the end of 2v2 (c3) battle screen to clicking OK button")
click(vm_index, 216,554)
click(vm_index, 216, 554)


def do_2v2_fight_state(
Expand Down Expand Up @@ -1114,6 +1115,5 @@ def _2v2_random_fight_loop(vm_index, logger: Logger):
logger = Logger()
vm_index = 12



while 1:print(check_for_locked_events_page(vm_index))
while 1:
print(check_for_locked_events_page(vm_index))
8 changes: 7 additions & 1 deletion src/pyclashbot/bot/donate.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from pyclashbot.bot.nav import (
check_if_on_clash_main_menu,
get_to_clan_tab_from_clash_main,
handle_trophy_reward_menu,check_for_trophy_reward_menu,
)
from pyclashbot.detection.image_rec import (
find_references,
Expand Down Expand Up @@ -82,9 +83,14 @@ def donate_cards_main(vm_index, logger: Logger) -> bool:

# get to clash main
logger.change_status("Returning to clash main after donating")
click(vm_index, 175, 600)
click(vm_index, 175, 600, clicks = 3)
time.sleep(5)

# handle geting stuck on trophy road screen
if check_for_trophy_reward_menu(vm_index):
handle_trophy_reward_menu(vm_index, logger)
time.sleep(2)

if not check_if_on_clash_main_menu(vm_index):
logger.log("Failed to get to clash main after doanting! Retsrating")
return False
Expand Down
9 changes: 7 additions & 2 deletions src/pyclashbot/memu/docker.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import threading

import time
import pygetwindow as gw

GUI_NAME = "py-clash-bot | dev"
Expand Down Expand Up @@ -53,7 +53,7 @@ def resize_memu():

new_height = gui_size[1] - 7

new_width = int(new_height*ratio)
new_width = int(new_height*ratio)+1

resize_window(MEMU_CLIENT_NAME,new_width,new_height)

Expand Down Expand Up @@ -99,9 +99,14 @@ def docker_main():
while 1:
try:
if not check_sizing():
print('[docker] resize...')
resize_memu()
continue
if not check_position():
print('[docker] Dock...')
dock_memu()
continue
time.sleep(0.33)
except:
pass

Expand Down

0 comments on commit ca2c595

Please sign in to comment.