Skip to content

Commit

Permalink
implementign daily reward collection
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewmiglio committed Dec 21, 2023
1 parent b0a821b commit 018cba5
Show file tree
Hide file tree
Showing 8 changed files with 356 additions and 5 deletions.
3 changes: 3 additions & 0 deletions src/pyclashbot/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ def make_job_dictionary(values: dict[str, str | int]) -> dict[str, str | int]:
# "random_decks_user_toggle": values["random_decks_user_toggle"],
"random_decks_user_toggle": False,
"open_bannerbox_user_toggle": values["open_bannerbox_user_toggle"],
"daily_rewards_user_toggle": values["daily_rewards_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"
Expand All @@ -85,6 +86,7 @@ def make_job_dictionary(values: dict[str, str | int]) -> dict[str, str | int]:
"shop_buy_increment_user_input": values["shop_buy_increment_user_input"],
"request_increment_user_input": values["request_increment_user_input"],
"donate_increment_user_input": values["donate_increment_user_input"],
"daily_reward_increment_user_input": values["daily_reward_increment_user_input"],
"card_mastery_collect_increment_user_input": values[
"card_mastery_collect_increment_user_input"
],
Expand Down Expand Up @@ -206,6 +208,7 @@ def show_invalid_job_increment_input_popup(key) -> None:
"shop_buy_increment_user_input": "Shop Purchase Increment",
"request_increment_user_input": "Request Increment",
"donate_increment_user_input": "Donate Increment",
"daily_reward_increment_user_input": "Daily Reward Increment",
"card_mastery_collect_increment_user_input": "Card Mastery Collect Increment",
"open_chests_increment_user_input": "Open Chests Increment",
"deck_randomization_increment_user_input": "Randomize Deck Increment",
Expand Down
252 changes: 252 additions & 0 deletions src/pyclashbot/bot/daily_challenge_collection.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,252 @@
from pyclashbot.bot.nav import check_if_on_clash_main_menu
from pyclashbot.detection.image_rec import pixel_is_equal
from pyclashbot.utils.logger import Logger
import numpy
from pyclashbot.memu.client import screenshot, click
import time


def collect_daily_rewards_state(vm_index, logger, next_state):
if collect_all_daily_rewards(vm_index, logger) is False:
logger.change_status("Failed to collect daily rewards")
return "restart"

return next_state


def check_for_daily_rewards(vm_index) -> bool:
iar = numpy.asarray(screenshot(vm_index))
pixels = [
iar[185][40],
iar[195][40],
iar[200][45],
iar[210][45],
iar[220][40],
iar[230][40],
]
colors = [
[232, 202, 0],
[179, 123, 0],
[17, 114, 181],
[62, 60, 88],
[174, 78, 5],
[244, 223, 15],
]

for i, p in enumerate(pixels):
if not pixel_is_equal(p, colors[i], 10):
return True

return False


def collect_challenge_rewards(vm_index, logger, rewards) -> bool:
# if not on clash main, reutrn False
if not check_if_on_clash_main_menu(vm_index):
logger.change_status(
"Not on clash main at start of collect_challenge_rewards(). Returning False"
)
return False

# open daily rewards menu
click(vm_index, 41, 206)
time.sleep(2)

# click first task's reward
if rewards[0]:
click(vm_index, 195, 190)
logger.change_status("Collected 1st daily challenge reward")
logger.add_daily_reward()
time.sleep(1)

# click third task's reward
if rewards[2]:
click(vm_index, 207, 320)
logger.change_status("Collected 2nd daily challenge reward")
logger.add_daily_reward()
time.sleep(1)

# click second task's reward
if rewards[1]:
click(vm_index, 250, 254)
logger.change_status("Collected 3rd daily challenge reward")
logger.add_daily_reward()
time.sleep(1)

# click deadspace a bunch
deadspace_clicks = 5
if rewards[1]:
deadspace_clicks = 10
click(vm_index, 15, 290, clicks=deadspace_clicks, interval=0.33)

# if not on clash main, reutrn False
if not check_if_on_clash_main_menu(vm_index):
logger.change_status(
"Not on clash main at start of collect_challenge_rewards(). Returning False"
)
return False

return True


def collect_daily_bonus(vm_index, logger) -> bool:
# if not on clash main, retunr False
if not check_if_on_clash_main_menu(vm_index):
logger.change_status(
"Not on clash main at start of collect_daily_bonus(). Returning False"
)
return False

# open daily rewards menu
click(vm_index, 41, 206)
time.sleep(2)

# click the daily bonus reward
click(vm_index, 206, 415)
logger.add_daily_reward()
logger.change_status("Collected daily reward chest")
time.sleep(1)

# click deadspace a bunch
click(vm_index, 10, 300, clicks=10, interval=1)

# if not on clash main, retunr False
if not check_if_on_clash_main_menu(vm_index):
logger.change_status(
"Not on clash main at start of collect_daily_bonus(). Returning False"
)
return False

return True


def collect_weekly_bonus(vm_index, logger) -> bool:
# if not on clash main, retunr False
if not check_if_on_clash_main_menu(vm_index):
logger.change_status(
"Not on clash main at start of collect_weekly_bonus(). Returning False"
)
return False

# open daily rewards menu
click(vm_index, 41, 206)
time.sleep(2)

# click the weekly bonus reward
click(vm_index, 197, 500)
logger.change_status("Collected weekly reward chest")
logger.add_daily_reward()
time.sleep(1)

# click deadspace a bunch
click(vm_index, 15, 300, clicks=10, interval=0.33)

# if not on clash main, retunr False
if not check_if_on_clash_main_menu(vm_index):
logger.change_status(
"Not on clash main at start of collect_weekly_bonus(). Returning False"
)
return False

return True


def collect_all_daily_rewards(vm_index, logger):
# if not on clash main, reutrn False
if not check_if_on_clash_main_menu(vm_index):
logger.change_status(
"Not on clash main at start of collect_daily_rewards(). Returning False"
)
return False

# check which rewards are available
rewards = check_which_rewards_are_available(vm_index, logger)
time.sleep(1)

# collect the basic 3 daily rewards for completing tasks
if rewards[0] or rewards[1] or rewards[2]:
if collect_challenge_rewards(vm_index, logger, rewards) is False:
logger.change_status("Failed to collect challenge rewards")
return False

# collect the daily bonus reward if it exists
if rewards[3] and collect_daily_bonus(vm_index, logger) is False:
logger.change_status("Failed to collect daily bonus reward")
return False

# collect the weekly bonus reward if it exists
if rewards[4] and collect_weekly_bonus(vm_index, logger) is False:
logger.change_status("Failed to collect weekly bonus reward")
return False

return True


def check_which_rewards_are_available(vm_index, logger):
logger.change_status("Checking which rewards are available")

# if not on clash main, return False
if not check_if_on_clash_main_menu(vm_index):
logger.change_status(
"Not on clash main before check_which_rewards_are_available() "
)
return False

# open daily rewards menu
click(vm_index, 41, 206)
time.sleep(2)

# check which rewards are available
rewards = check_rewards_menu_pixels(vm_index)

# click deadspace a bunch
click(vm_index, 15, 290, clicks=3, interval=0.33)
time.sleep(2)

# if not on clash main, return False
if not check_if_on_clash_main_menu(vm_index):
logger.change_status(
"Not on clash main after check_which_rewards_are_available()"
)
return False

positives = 0
for _ in rewards:
if _:
positives += 1

print(f"There are {positives} to collect")
return rewards


def check_rewards_menu_pixels(vm_index):
iar = numpy.asarray(screenshot(vm_index))
pixels = [
iar[192][345],
iar[345][262],
iar[330][345],
iar[415][242],
iar[502][235],
]

colors = [
[125, 161, 188],
[181, 211, 231],
[126, 162, 189],
[223, 131, 28],
[113, 156, 0],
]

bools = []
for i, p in enumerate(pixels):
bool = pixel_is_equal(p, colors[i], 15)
bools.append(not (bool))

return bools


if __name__ == "__main__":
vm_index = 12
logger = Logger()


23 changes: 21 additions & 2 deletions src/pyclashbot/bot/states.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
)
from pyclashbot.bot.buy_shop_offers import buy_shop_offers_state
from pyclashbot.utils.logger import Logger
from pyclashbot.bot.daily_challenge_collection import collect_daily_rewards_state


class StateException(Exception):
Expand Down Expand Up @@ -202,14 +203,32 @@ def state_tree(
next_state,
)

if state == "bannerbox": # --> randomize_deck
next_state = "randomize_deck"
if state == "bannerbox": # --> daily_rewards
next_state = "daily_rewards"
if not job_list["open_bannerbox_user_toggle"]:
logger.log("Bannerbox job isn't toggled. Skipping")
return next_state

return collect_bannerbox_rewards_state(vm_index, logger, next_state)

if state == "daily_rewards": # --> randomize_deck
next_state = "randomize_deck"

#if job not toggled, return next state
if not job_list["daily_rewards_user_toggle"]:
logger.log("daily_rewards job isn't toggled. Skipping")
return next_state

# if job not ready, return next state
if not logger.check_if_can_collect_daily_rewards(
job_list["daily_reward_increment_user_input"]
):
logger.log("daily_rewards job isn't ready")
return next_state

#run this job, return its output
return collect_daily_rewards_state(vm_index, logger, next_state)

if state == "randomize_deck": # --> start_fight
next_state = "start_fight"

Expand Down
8 changes: 5 additions & 3 deletions src/pyclashbot/interface/controls.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def make_job_increment_control_object(key):
[sg.Text("Donate Cards Every:")],
[sg.Text("Collect Free Offer Every:")],
[sg.Text("Upgrade Current Deck Every:")],
[sg.Text("Collect Daily Rewards Every:")],
[sg.Text("Collect Card Mastery Every:")],
[sg.Text("Open Chests Every:")],
[sg.Text("Randomize Deck Every:")],
Expand All @@ -44,14 +45,15 @@ def make_job_increment_control_object(key):
[
[make_job_increment_control_object("request_increment_user_input")],
[make_job_increment_control_object("donate_increment_user_input")],
[make_job_increment_control_object("shop_buy_increment_user_input")],
[
make_job_increment_control_object(
"shop_buy_increment_user_input"
"card_upgrade_increment_user_input"
)
],
[
make_job_increment_control_object(
"card_upgrade_increment_user_input"
"daily_reward_increment_user_input"
)
],
[
Expand Down Expand Up @@ -84,7 +86,7 @@ def make_job_increment_control_object(key):
[sg.Text("games")],
[sg.Text("games")],
[sg.Text("games")],
[sg.Text("games")]
[sg.Text("games")],
],
justification="left",
),
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 @@ -103,6 +103,12 @@ def job_check_box(text: str, element_key: str, default_value=True) -> sg.Checkbo
"open_bannerbox_user_toggle",
),
],
[
job_check_box(
"Daily Rewards",
"daily_rewards_user_toggle",
),
],
[
job_check_box(
"Card Upgrading",
Expand Down
2 changes: 2 additions & 0 deletions src/pyclashbot/interface/layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,11 +203,13 @@ def get_random_donate_image_path():
"war_user_toggle",
#DISABLED: "random_decks_user_toggle",
"open_bannerbox_user_toggle",
'daily_rewards_user_toggle',
"random_plays_user_toggle",
"skip_fight_if_full_chests_user_toggle",
# job increment controls keys
"request_increment_user_input",
"donate_increment_user_input",
"daily_reward_increment_user_input",
"shop_buy_increment_user_input",
"card_upgrade_increment_user_input",
"card_mastery_collect_increment_user_input",
Expand Down
Loading

0 comments on commit 018cba5

Please sign in to comment.