Skip to content

Commit

Permalink
adding 'disable win tracking' feature, fixing check_if_in_battle() me…
Browse files Browse the repository at this point in the history
…thods
  • Loading branch information
matthewmiglio committed Dec 16, 2023
1 parent 307f4f9 commit 6e3b26b
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 74 deletions.
9 changes: 5 additions & 4 deletions src/pyclashbot/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ def make_job_dictionary(values: dict[str, str | int]) -> dict[str, str | int]:
"open_chests_user_toggle": values["open_chests_user_toggle"],
"request_user_toggle": values["request_user_toggle"],


# "card_mastery_user_toggle": values["card_mastery_user_toggle"],
"card_mastery_user_toggle": False,
"free_offer_user_toggle": values["free_offer_user_toggle"],
Expand All @@ -67,9 +66,8 @@ def make_job_dictionary(values: dict[str, str | int]) -> dict[str, str | int]:
"random_decks_user_toggle": False,
"open_bannerbox_user_toggle": values["open_bannerbox_user_toggle"],
"random_plays_user_toggle": values["random_plays_user_toggle"],
"skip_fight_if_full_chests_user_toggle": values[
"skip_fight_if_full_chests_user_toggle"
],
"skip_fight_if_full_chests_user_toggle": values["skip_fight_if_full_chests_user_toggle"],
'disable_win_track_toggle': values['disable_win_track_toggle'],
# job increments
"card_upgrade_increment_user_input": values[
"card_upgrade_increment_user_input"
Expand All @@ -95,6 +93,9 @@ def make_job_dictionary(values: dict[str, str | int]) -> dict[str, str | int]:
"next_account": 0,
}




return jobs_dictionary


Expand Down
55 changes: 31 additions & 24 deletions src/pyclashbot/bot/do_fight_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@

from pyclashbot.bot.card_detection import get_play_coords_for_card
from pyclashbot.bot.nav import (
check_if_in_1v1_battle,
check_if_in_2v2_battle,
check_if_in_battle,
check_for_in_2v2_battle_with_delay,
check_if_on_clash_main_challenges_tab,
check_if_on_clash_main_menu,
Expand Down Expand Up @@ -75,7 +74,7 @@ def start_2v2_fight_state(vm_index, logger: Logger) -> Literal["restart", "2v2_f
if clash_main_check is not True:
logger.change_status(status="ERROR 34 Not on main for start of start 2v2")
logger.log(
f"There are the pixels the bot saw after failing to find clash main:"
"There 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 @@ -126,7 +125,7 @@ def start_1v1_fight_state(vm_index, logger: Logger) -> Literal["restart", "1v1_f
status="ERROR 46246 Not on main menu for start of start 1v1 fight"
)
logger.log(
f"There are the pixels the bot saw after failing to find clash main:"
"There 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 @@ -240,7 +239,7 @@ def _1v1_random_fight_loop(vm_index, logger):
logger.add_card_played()

# while in battle:
while check_if_in_1v1_battle(vm_index):
while check_if_in_battle(vm_index):
time.sleep(8)

mag_dump(vm_index, logger)
Expand Down Expand Up @@ -357,7 +356,7 @@ def _2v2_fight_loop(vm_index, logger: Logger) -> Literal["restart", "good"]:
logger.change_status("At max elixer so just mag dumping!!!")
mag_dump(vm_index, logger)

# emote sometimes to do daily challenge (jk its to be funny and annoy ur teammate)
# emote sometimes to do daily challenge (jk its to be funny)
if random.randint(0, 10) == 1:
emote_in_2v2(vm_index, logger)

Expand Down Expand Up @@ -463,7 +462,7 @@ def _1v1_fight_loop(vm_index, logger: Logger) -> Literal["restart", "good"]:
prev_cards_played = logger.get_cards_played()

# while in battle:
while check_if_in_1v1_battle(vm_index):
while check_if_in_battle(vm_index):
logger.log(f"Battle play #{plays}:")

# wait for 6 elixer
Expand Down Expand Up @@ -559,10 +558,10 @@ def wait_for_4_elixer(vm_index, logger, mode="1v1"):
if time.time() - start_time > ELIXER_WAIT_TIMEOUT:
return "restart"

if mode == "1v1" and not check_if_in_1v1_battle(vm_index):
if mode == "1v1" and not check_if_in_battle(vm_index):
logger.change_status(status="Not in battle, stopping waiting for 4 elixer.")
return "no battle"
if mode == "2v2" and not check_if_in_2v2_battle(vm_index):
if mode == "2v2" and not check_if_in_battle(vm_index):
logger.change_status(status="Not in battle, stopping waiting for 4 elixer.")
return "no battle"

Expand Down Expand Up @@ -610,11 +609,11 @@ def wait_for_6_elixer(
if time.time() - start_time > ELIXER_WAIT_TIMEOUT:
return "restart"

if mode == "1v1" and not check_if_in_1v1_battle(vm_index):
if mode == "1v1" and not check_if_in_battle(vm_index):
logger.change_status(status="Not in battle, stopping waiting for 6 elixer")
return "no battle"

if mode == "2v2" and not check_if_in_2v2_battle(vm_index):
if mode == "2v2" and not check_if_in_battle(vm_index):
logger.change_status(status="Not in battle, stopping waiting for 6 elixer")
return "no battle"

Expand Down Expand Up @@ -658,7 +657,7 @@ def check_enemy_tower_statuses(
return (left_tower_status, right_tower_status)


def end_fight_state(vm_index, logger: Logger, next_state):
def end_fight_state(vm_index, logger: Logger, next_state, disable_win_tracker_toggle=True):
"""method to handle the time after a fight and before the next state"""

# get to clash main after this fight
Expand All @@ -669,18 +668,21 @@ def end_fight_state(vm_index, logger: Logger, next_state):
logger.log("Made it to clash main after doing a fight")

# check if the prev game was a win
if not disable_win_tracker_toggle:
win_check_return = check_if_previous_game_was_win(vm_index, logger)

win_check_return = check_if_previous_game_was_win(vm_index, logger)
if win_check_return == "restart":
logger.log("Error 885869 Failed while checking if previous game was a win")
return "restart"

if win_check_return == "restart":
logger.log("Error 885869 Failed while checking if previous game was a win")
return "restart"
if win_check_return:
logger.add_win()
return next_state

if win_check_return:
logger.add_win()
return next_state
logger.add_loss()
else:
logger.log('Not checking win/loss because check is disabled')

logger.add_loss()
return next_state


Expand All @@ -698,7 +700,7 @@ def check_if_previous_game_was_win(
status='534594784234 Error Not on main menu, returning "restart"'
)
logger.log(
f"There are the pixels the bot saw after failing to find clash main:"
"There 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 @@ -955,13 +957,18 @@ def _2v2_random_fight_loop(vm_index, logger: Logger):


if __name__ == "__main__":
logger = Logger()
vm_index = 5
# logger = Logger()
# vm_index = 5

# mag_dump(vm_index,logger)

# _2v2_random_fight_loop(vm_index, logger)

_1v1_random_fight_loop(vm_index, logger)
# _1v1_random_fight_loop(vm_index, logger)

# mag_dump(vm_index,logger)


# while 1:print(check_if_in_battle(vm_index))

pass
54 changes: 12 additions & 42 deletions src/pyclashbot/bot/nav.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
pixel_is_equal,
region_is_color,
)
from pyclashbot.memu.client import click, save_screenshot, screenshot, scroll_up
from pyclashbot.memu.client import click, screenshot, scroll_up
from pyclashbot.utils.logger import Logger

_2V2_START_WAIT_TIMEOUT = 60 # s
Expand Down Expand Up @@ -114,11 +114,12 @@ def wait_for_2v2_battle_start(vm_index, logger: Logger) -> Literal["restart", "g
_2v2_start_wait_start_time = time.time()

while time.time() - _2v2_start_wait_start_time < _2V2_START_WAIT_TIMEOUT:
time_taken =str(time.time() - _2v2_start_wait_start_time)[:4]
logger.change_status(
status=f"Waiting for 2v2 battle to start for {str(time.time() - _2v2_start_wait_start_time)[:4]}s"
status=f"Waiting for 2v2 battle to start for {time_taken}s"
)

if check_if_in_2v2_battle(vm_index=vm_index):
if check_if_in_battle(vm_index=vm_index):
logger.change_status("Detected an ongoing 2v2 battle!")
return True

Expand Down Expand Up @@ -149,7 +150,7 @@ def wait_for_1v1_battle_start(
logger.change_status(status="Waiting for 1v1 battle to start")
else:
logger.log(message="Waiting for 1v1 battle to start")
while not check_if_in_1v1_battle(vm_index=vm_index):
while not check_if_in_battle(vm_index=vm_index):
time_taken: float = time.time() - start_time
if time_taken > 60:
logger.change_status(
Expand Down Expand Up @@ -177,15 +178,15 @@ def check_for_in_2v2_battle_with_delay(vm_index):
Returns:
bool: True if the virtual machine is in a 2v2 battle, False otherwise.
"""
timeout = 2 # s
timeout = 3 # s
start_time = time.time()
while time.time() - start_time < timeout:
if check_if_in_2v2_battle(vm_index):
if check_if_in_battle(vm_index):
return True
return False


def check_if_in_2v2_battle(vm_index) -> bool:
def check_if_in_battle(vm_index) -> bool:
"""
Checks if the virtual machine is in a 2v2 battle.
Expand All @@ -209,42 +210,13 @@ def check_if_in_2v2_battle(vm_index) -> bool:
[236, 91, 252],
]

for index in range(len(pixels)):
color = colors[index]
pixel = pixels[index]
if not pixel_is_equal(pixel, color, tol=35):
for index, pixel in enumerate(pixels):
if not pixel_is_equal(pixel, colors[index], tol=35):
return False

return True


def check_if_in_1v1_battle(vm_index) -> bool:
"""
Checks if the virtual machine is in a 1v1 battle.
Args:
vm_index (int): The index of the virtual machine.
Returns:
bool: True if the virtual machine is in a 1v1 battle, False otherwise.
"""
iar = numpy.asarray(screenshot(vm_index))
pixels = [
iar[613][112],
iar[621][121],
]

colors = [
[232, 70, 252],
[223, 25, 248],
]

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


def get_to_clash_main_from_clan_page(
vm_index, logger: Logger, printmode=False
Expand Down Expand Up @@ -1220,8 +1192,6 @@ def check_for_end_2v2_battle_screen(vm_index) -> bool:


if __name__ == "__main__":
vm_index = 11




pass

8 changes: 4 additions & 4 deletions src/pyclashbot/bot/states.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ def state_tree(
logger.log(
f"This state: {state} took {str(time.time() - start_time)[:5]} seconds"
)
return end_fight_state(vm_index, logger, next_state)
return end_fight_state(vm_index, logger, next_state, job_list['disable_win_track_toggle'])

if state == "card_mastery": # --> war
next_state = "war"
Expand Down Expand Up @@ -325,19 +325,19 @@ def state_tree(
):
logger.log("Account switching job isn't ready. Skipping this state")
return next_state

logger.log(
f"Attempt to switch to account #{job_list['next_account']} of {job_list['account_switching_slider']}"
)

if switch_accounts(vm_index, logger, job_list["next_account"]) is False:
return "restart"

# increment next account iteration
job_list["next_account"] += 1
if job_list["next_account"] >= job_list["account_switching_slider"]:
job_list["next_account"] = 0

logger.log(f"Next account is {job_list['next_account']} / {job_list['account_switching_slider']}")

# update current account # to GUI
Expand Down
6 changes: 6 additions & 0 deletions src/pyclashbot/interface/joblist.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ def job_check_box(text: str, element_key: str, default_value=True) -> sg.Checkbo
"free_offer_user_toggle",
),
],
[
job_check_box(
"Disable win/loss tracking",
"disable_win_track_toggle",
),
],
[
job_check_box(
"War Participation",
Expand Down
1 change: 1 addition & 0 deletions src/pyclashbot/interface/layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ def get_random_donate_image_path():
"open_chests_user_toggle",
"request_user_toggle",
#DISABLED: "card_mastery_user_toggle",
'disable_win_track_toggle',
"free_offer_user_toggle",
"1v1_user_toggle",
"2v2_user_toggle",
Expand Down

0 comments on commit 6e3b26b

Please sign in to comment.