Skip to content

Commit

Permalink
Drop pyautogui as dependency (#217)
Browse files Browse the repository at this point in the history
* dropping pyautogui
    * replacing with AHK for scrolls and renamed functions for simplicity
    * Pillow for screenshots, added fix for region api change
  • Loading branch information
martinmiglio authored Apr 3, 2023
1 parent 8a0e5b8 commit 913307a
Show file tree
Hide file tree
Showing 12 changed files with 268 additions and 3,159 deletions.
3,246 changes: 184 additions & 3,062 deletions backend/poetry.lock

Large diffs are not rendered by default.

13 changes: 6 additions & 7 deletions backend/pyclashbot/bot/clashmain.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import time

import numpy
import pyautogui
from ahk import AHK

from pyclashbot.bot.navigation import (
Expand All @@ -20,11 +19,11 @@
get_first_location,
pixel_is_equal,
)
from pyclashbot.memu import click, screenshot, scroll_down, scroll_up_fast
from pyclashbot.memu import click, screenshot, scroll_down, scroll_up
from pyclashbot.memu.client import (
get_file_count,
make_reference_image_list,
scroll_down_fast,
scroll_down,
)
from pyclashbot.utils.logger import Logger

Expand Down Expand Up @@ -210,27 +209,27 @@ def get_to_account(logger, account_number):

elif account_number == 4:
# scroll then click
scroll_down_fast()
scroll_down()
click(170, 640)

elif account_number == 5:
# scroll then click
for _ in range(4):
scroll_down_fast()
scroll_down()
time.sleep(0.5)
click(230, 585)

elif account_number == 6:
# scroll then click
for _ in range(7):
scroll_down_fast()
scroll_down()
time.sleep(0.5)
click(240, 550)

elif account_number == 7:
# scroll then click
for _ in range(7):
scroll_down_fast()
scroll_down()
time.sleep(0.5)

click(230, 625)
Expand Down
14 changes: 7 additions & 7 deletions backend/pyclashbot/bot/deck.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
make_reference_image_list,
screenshot,
scroll_down,
scroll_down_super_fast,
scroll_down_fast,
scroll_up,
scroll_up_fast,
scroll_up_super_fast,
)

"""Methods that have to do with the randomization of the current deck"""
Expand Down Expand Up @@ -65,15 +65,15 @@ def count_scrolls_in_card_page(logger) -> int | Literal["restart"]:

# Count scrolls
count: int = 1
scroll_down_super_fast()
scroll_down_fast()
loops = 0
while check_if_can_still_scroll_in_card_page():
print("Scrolling down in card page: ", count)
loops += 1
if loops > 40:
logger.change_status("Failed counting scrolls in card page")
return "restart"
scroll_down_super_fast()
scroll_down_fast()
time.sleep(0.1)
count += 1

Expand Down Expand Up @@ -461,7 +461,7 @@ def randomize_this_deck(logger):
"""main method for randomizing the current deck
args:
logger: Logger object
returns:
returns:
restart state upon failure
"""

Expand Down Expand Up @@ -502,7 +502,7 @@ def randomize_this_deck(logger):

# scroll that amount
for _ in range(random_scroll_amount):
scroll_down_super_fast()
scroll_down_fast()
time.sleep(0.1)

# check for scrolling failure
Expand All @@ -511,7 +511,7 @@ def randomize_this_deck(logger):
"detected a failure when randomly scrolling during deck randomization... attempting to save the bot"
)
for _ in range(3):
scroll_down_super_fast()
scroll_down_fast()
time.sleep(0.1)

# click randomly until we get a 'use' button
Expand Down
4 changes: 2 additions & 2 deletions backend/pyclashbot/bot/free_offer_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
get_file_count,
make_reference_image_list,
screenshot,
scroll_down_super_fast,
scroll_down_fast,
)


Expand Down Expand Up @@ -48,7 +48,7 @@ def collect_free_offer_from_shop(logger):
loops = 0
print("Scrolling and looking for a free offer")
while free_offer_coords is None:
scroll_down_super_fast()
scroll_down_fast()
time.sleep(2)

free_offer_coords = find_free_offer_icon()
Expand Down
19 changes: 10 additions & 9 deletions backend/pyclashbot/bot/navigation.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import time

import numpy
import pyautogui

from pyclashbot.detection.image_rec import (
check_for_location,
Expand All @@ -16,8 +15,9 @@
make_reference_image_list,
screenshot,
scroll_down,
scroll_down,
scroll_up,
scroll_up_fast,
scroll_up_super_fast,
)

"""methods that have to do with navigating the game's menus"""
Expand Down Expand Up @@ -80,11 +80,11 @@ def get_to_war_page_from_main(logger):
click(280, 620)

if random.randint(1, 2) == 2:
origin = pyautogui.position()
pyautogui.moveTo(300, 300)
# origin = pyautogui.position()
# pyautogui.moveTo(300, 300)
time.sleep(1)
scroll_up_fast()
pyautogui.moveTo(origin[0], origin[1])
scroll_up()
# pyautogui.moveTo(origin[0], origin[1])

time.sleep(1)
return None
Expand Down Expand Up @@ -1243,7 +1243,6 @@ def wait_for_card_page():

start_time = time.time()
while not check_if_on_card_page():

if time.time() - start_time > 10:
print("timed out waiting for card page")
return "fail"
Expand Down Expand Up @@ -1436,6 +1435,7 @@ def handle_bonus_bank_popup_in_battlepass_page():
click(216, 466)
time.sleep(3)


def get_to_challenges_tab():
"""method for getting to the challenges tab
args:
Expand All @@ -1446,6 +1446,7 @@ def get_to_challenges_tab():
click(x=394, y=634)
time.sleep(1)

for _ in range(5):scroll_up_super_fast()
for _ in range(5):
scroll_up_fast()
time.sleep(1)
print('done')
print("done")
12 changes: 6 additions & 6 deletions backend/pyclashbot/bot/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
from pyclashbot.memu import (
click,
screenshot,
scroll_down_super_fast,
scroll_up_super_fast,
scroll_down_fast,
scroll_up_fast,
)
from pyclashbot.memu.client import get_file_count, make_reference_image_list

Expand Down Expand Up @@ -102,7 +102,7 @@ def request_random_card(logger, maximum_scrolls=10):
print("Scrolling randomly with maximum scrolls of ", maximum_scrolls)
random_scroll_amount = Random().randint(0, maximum_scrolls)
for _ in range(random_scroll_amount):
scroll_down_super_fast()
scroll_down_fast()

# click random cards in the card list until a request button appears.
has_card_to_request = False
Expand Down Expand Up @@ -189,7 +189,7 @@ def count_maximum_request_scrolls(logger):
# get to the top of this page
# b/c on epic sunday it automatically scrolls sorta halfway down to show the epic cards.
for _ in range(10):
scroll_up_super_fast()
scroll_up_fast()

# count scrolls
scrolls = 0
Expand All @@ -203,11 +203,11 @@ def count_maximum_request_scrolls(logger):
"Failed counting maximum scrolls in request page in count_maximum_request_scrolls()"
)
return "restart"
scroll_down_super_fast()
scroll_down_fast()
scrolls += 1

for _ in range(10):
scroll_up_super_fast()
scroll_up_fast()

return scrolls

Expand Down
4 changes: 2 additions & 2 deletions backend/pyclashbot/bot/upgrade.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
pixel_is_equal,
)
from pyclashbot.memu import click, get_file_count, screenshot
from pyclashbot.memu.client import make_reference_image_list, scroll_up_fast
from pyclashbot.memu.client import make_reference_image_list, scroll_up


def check_for_upgradable_cards():
Expand All @@ -36,7 +36,7 @@ def check_for_upgradable_cards():

upgrade_card_bool_list = []

scroll_up_fast()
scroll_up()

for card_index in range(8):
this_card_coord = card_coord_list[card_index]
Expand Down
8 changes: 4 additions & 4 deletions backend/pyclashbot/bot/war.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
get_file_count,
make_reference_image_list,
screenshot,
scroll_down_super_fast,
scroll_up_super_fast,
scroll_down_fast,
scroll_up_fast,
)


Expand Down Expand Up @@ -190,9 +190,9 @@ def click_war_icon():
return "failed"

if random.randint(0, 1) == 0:
scroll_up_super_fast()
scroll_up_fast()
else:
scroll_down_super_fast()
scroll_down_fast()
time.sleep(3)
coord = find_battle_icon_on_war_page()
click(coord[0], coord[1])
Expand Down
8 changes: 4 additions & 4 deletions backend/pyclashbot/memu/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
orientate_terminal,
screenshot,
scroll_down,
scroll_down_super_fast,
scroll_down_fast,
scroll_up,
scroll_up_fast,
scroll_up_super_fast,
)

__all__ = [
Expand All @@ -16,8 +16,8 @@
"make_reference_image_list",
"screenshot",
"scroll_down",
"scroll_down_super_fast",
"scroll_down_fast",
"scroll_up",
"scroll_up_fast",
"scroll_up_super_fast",
"orientate_terminal",
]
Loading

0 comments on commit 913307a

Please sign in to comment.