Skip to content

Commit

Permalink
revamping shop offer purchases to allow users to buy offers for gold …
Browse files Browse the repository at this point in the history
…too, fixing screenshot so it works in dev env
  • Loading branch information
matthewmiglio committed Dec 21, 2023
1 parent 1f6cf44 commit b0a821b
Show file tree
Hide file tree
Showing 33 changed files with 278 additions and 242 deletions.
16 changes: 12 additions & 4 deletions src/pyclashbot/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,22 @@ def make_job_dictionary(values: dict[str, str | int]) -> dict[str, str | int]:
Returns:
A dictionary of job toggles and increments based on the values of the GUI window.
"""
print("values in job_dict:\n")
print(
"free_offer_user_toggle:",
values["free_offer_user_toggle"],
"gold_offer_user_toggle:",
values["gold_offer_user_toggle"],
)

jobs_dictionary: dict[str, str | int] = {
# job toggles
"open_chests_user_toggle": values["open_chests_user_toggle"],
"request_user_toggle": values["request_user_toggle"],
"donate_toggle": values["donate_toggle"],
"card_mastery_user_toggle": values["card_mastery_user_toggle"],
"free_offer_user_toggle": values["free_offer_user_toggle"],
"gold_offer_user_toggle": values["gold_offer_user_toggle"],
"1v1_battle_user_toggle": values["1v1_user_toggle"],
"2v2_battle_user_toggle": values["2v2_user_toggle"],
"upgrade_user_toggle": values["card_upgrade_user_toggle"],
Expand All @@ -68,13 +77,12 @@ def make_job_dictionary(values: dict[str, str | int]) -> dict[str, str | int]:
"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"
],
"free_offer_collection_increment_user_input": values[
"free_offer_collection_increment_user_input"
],
"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"],
"card_mastery_collect_increment_user_input": values[
Expand Down Expand Up @@ -195,7 +203,7 @@ def show_invalid_job_increment_input_popup(key) -> None:
# A dictionary mapping the job increment input keys to their corresponding job names.
key_to_job_dict: dict[str, str] = {
"card_upgrade_increment_user_input": "Card Upgrade Increment",
"free_offer_collection_increment_user_input": "Free Offer Collection Increment",
"shop_buy_increment_user_input": "Shop Purchase Increment",
"request_increment_user_input": "Request Increment",
"donate_increment_user_input": "Donate Increment",
"card_mastery_collect_increment_user_input": "Card Mastery Collect Increment",
Expand Down
164 changes: 164 additions & 0 deletions src/pyclashbot/bot/buy_shop_offers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
import time

from pyclashbot.bot.nav import (
check_if_on_clash_main_menu,
get_to_shop_page_from_clash_main,
)
from pyclashbot.detection.image_rec import (
find_references,
get_first_location,
get_file_count,
make_reference_image_list,
)
from pyclashbot.memu.client import (
screenshot,
click,
scroll_down_slowly_in_shop_page,
)

from pyclashbot.utils.logger import Logger


def search_for_free_purchases(vm_index):
"""method to find the free offer icon image in the shop pages"""

folder_name = "free_offer_icon"
size = get_file_count(folder_name)
names = make_reference_image_list(size)
locations = find_references(
screenshot(vm_index),
folder_name,
names,
0.9,
)
coord = get_first_location(locations)
if coord is None:
return None
return [coord[1], coord[0]]


def buy_free_offer(vm_index):
coord = search_for_free_purchases(vm_index)

if coord is None:
return False

# click the location of the free offer icon
click(vm_index, coord[0], coord[1])

# click the second 'buy' button
click(vm_index, 200, 433)

# click deadspace to close this offer
click(vm_index, 15, 200, clicks=10)


def search_for_gold_purchases(vm_index):
"""method to find the offers for gold icon image in the shop pages"""

folder_name = "offers_for_gold"
size = get_file_count(folder_name)
names = make_reference_image_list(size)
locations = find_references(
screenshot(vm_index),
folder_name,
names,
0.9,
)
coord = get_first_location(locations)
if coord is None:
return None
return [coord[1], coord[0]]


def buy_offers_from_this_shop_page(vm_index, gold_buy_toggle, free_offers_toggle):
coord = None

if gold_buy_toggle:
coord = search_for_gold_purchases(vm_index)

# if no gold purchases, find a free purchase
if coord is None and free_offers_toggle:
coord = search_for_free_purchases(vm_index)

# if there are no purchases at this point, return False
if coord is None:
return False

# click the location of the 'cards for gold' icon
click(vm_index, coord[0], coord[1])
time.sleep(2)

# click the second 'buy' button
click(vm_index, 200, 433)
click(vm_index, 204, 394)
time.sleep(2)

# click deadspace to close this offer
click(vm_index, 15, 200, clicks=10)
time.sleep(1)
return True


def buy_shop_offers_state(
vm_index: int,
logger: Logger,
gold_buy_toggle: bool,
free_offers_toggle: bool,
next_state: str,
):
print("Entering buy_shop_offers_state()")
print(f"gold_buy_toggle: {gold_buy_toggle}")
print(f"free_offers_toggle: {free_offers_toggle}")

logger.add_shop_buy_attempt()

# if not on clash main, return False
if not check_if_on_clash_main_menu(vm_index):
logger.change_status(
"Not on clash main to being buying offers. Returning restart"
)
return "restart"

# get to shop page
logger.change_status("Getting to shop page to buy offers")
if get_to_shop_page_from_clash_main(vm_index, logger) is False:
logger.change_status("Failed to get to shop page to buy offers")
return "restart"

# scroll incrementally while searching for rewards, clicking and buying any rewards found
start_time = time.time()
timeout = 90
logger.change_status("Starting to buy offers")
while 1:
if time.time() - start_time > timeout:
break

# scroll a little
scroll_down_slowly_in_shop_page(vm_index)
time.sleep(0.33)

if gold_buy_toggle:
while (
buy_offers_from_this_shop_page(
vm_index, gold_buy_toggle, free_offers_toggle
)
is True
):
logger.change_status("Bought an offer from the shop!")
logger.add_shop_buy()

# get to clash main from shop page
click(vm_index, 245, 596)
time.sleep(4)

# 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 buying offers. Returning restart")
return "restart"

return next_state


if __name__ == "__main__":
pass
Loading

0 comments on commit b0a821b

Please sign in to comment.