Skip to content

Commit 02114fd

Browse files
committed
fixed dynamic loading screen issue - still only working with lvl1 tho
1 parent 5d537a9 commit 02114fd

File tree

13 files changed

+122
-33
lines changed

13 files changed

+122
-33
lines changed

.idea/misc.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
1.83 KB
Binary file not shown.

Action/action.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
12
import pyautogui
23
import time
34
import pyscreeze
5+
import os
6+
import re
47

58

69
class Action:
@@ -27,5 +30,22 @@ def simulate_play_execution(self, coordinates):
2730
def simulate_key_presses(self, keys):
2831
"""Simulates pressing a sequence of keys."""
2932
for key in keys:
33+
time.sleep(0.5)
3034
pyautogui.press(key)
31-
time.sleep(0.3) # Adjust the sleep duration if needed
35+
# Adjust the sleep duration if needed
36+
37+
def clean_root_directory(self):
38+
# Define the naming pattern for image files to be deleted
39+
pattern = re.compile(r'image_(\d+)\.png')
40+
41+
# Get a list of files in the root directory
42+
root_files = os.listdir()
43+
44+
# Iterate through files and delete those that match the naming pattern
45+
for file in root_files:
46+
if pattern.match(file):
47+
try:
48+
os.remove(file)
49+
print(f"Deleted: {file}")
50+
except Exception as e:
51+
print(f"Error deleting {file}: {e}")
-386 Bytes
Binary file not shown.
-1 Bytes
Binary file not shown.

Core/comparison_handling.py

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,26 @@
1+
import os
2+
import re
3+
from pyautogui import ImageNotFoundException
14
from PIL import Image
25
import pyautogui
3-
from pyautogui import ImageNotFoundException
4-
56

67
class ComparisonHandling:
8+
79
def compare_sequence(self):
810
# Open the template images
9-
up_template = Image.open('C:\\Users\\root\\Documents\\PythonProjects\\Wizzard101-PetAutomationTool\\Spy\\ImageTemplates\\up.png')
10-
down_template = Image.open('C:\\Users\\root\\Documents\\PythonProjects\\Wizzard101-PetAutomationTool\\Spy\\ImageTemplates\\down.png')
11-
left_template = Image.open('C:\\Users\\root\\Documents\\PythonProjects\\Wizzard101-PetAutomationTool\\Spy\\ImageTemplates\\left.png')
12-
right_template = Image.open('C:\\Users\\root\\Documents\\PythonProjects\\Wizzard101-PetAutomationTool\\Spy\\ImageTemplates\\right.png')
11+
up_template = Image.open('Spy\\ImageTemplates\\up.png')
12+
down_template = Image.open('Spy\\ImageTemplates\\down.png')
13+
left_template = Image.open('Spy\\ImageTemplates\\left.png')
14+
right_template = Image.open('Spy\\ImageTemplates\\right.png')
15+
16+
# Define the naming pattern for request images
17+
pattern = re.compile(r'image_(\d+)\.png')
1318

14-
# Open the request images
15-
first_request = Image.open('C:\\Users\\root\\Documents\\PythonProjects\\Wizzard101-PetAutomationTool\\Image_1.png')
16-
second_request = Image.open('C:\\Users\\root\\Documents\\PythonProjects\\Wizzard101-PetAutomationTool\\Image_2.png')
17-
third_request = Image.open('C:\\Users\\root\\Documents\\PythonProjects\\Wizzard101-PetAutomationTool\\Image_3.png')
19+
# Get a list of files in the root directory
20+
root_files = os.listdir()
1821

19-
# Define a list of request images for easier iteration
20-
request_images = [first_request, second_request, third_request]
22+
# Filter files based on the naming pattern
23+
request_images = [Image.open(file) for file in root_files if pattern.match(file)]
2124

2225
# Define a list of templates for easier iteration
2326
templates = [up_template, down_template, left_template, right_template]
@@ -34,7 +37,7 @@ def compare_sequence(self):
3437
for template_index, template in enumerate(templates):
3538
try:
3639
# Locate the template within the current request image
37-
location = pyautogui.locate(request_image, template, confidence=0.9)
40+
location = pyautogui.locate(request_image, template, confidence=0.91)
3841
# If the template is found, set the result for the current request
3942
if location:
4043
result_for_request = ['Up', 'Down', 'Left', 'Right'][template_index]
@@ -47,4 +50,4 @@ def compare_sequence(self):
4750
results.append(result_for_request)
4851

4952
# Return the list of results
50-
return results
53+
return results

Spy/__pycache__/spy.cpython-312.pyc

796 Bytes
Binary file not shown.

Spy/spy.py

Lines changed: 61 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,16 @@ class Spy:
1212
def locate_dance_game_popup(self):
1313
try:
1414
img = Image.open(
15-
'C:\\Users\\root\\Documents\\PythonProjects\\Wizzard101-PetAutomationTool\\Spy\\ImageTemplates\\DanceGamePopUp.png')
16-
pyautogui.locateOnScreen(img, 0.0, confidence=0.7)
15+
'Spy\\ImageTemplates\\DanceGamePopUp.png')
16+
coordinates = pyautogui.locateOnScreen(img, 0.0, confidence=0.7)
17+
return coordinates
1718
except ImageNotFoundException:
1819
return ImageNotFoundException
1920

2021
def locate_level(self):
2122
try:
2223
img = Image.open(
23-
'C:\\Users\\root\\Documents\\PythonProjects\\Wizzard101-PetAutomationTool\\Spy\\ImageTemplates\\Level.png')
24+
'Spy\\ImageTemplates\\Level.png')
2425
coordinates = pyautogui.locateOnScreen(img, 0.0, confidence=0.9)
2526
return coordinates
2627
except ImageNotFoundException:
@@ -29,7 +30,7 @@ def locate_level(self):
2930
def locate_play(self):
3031
try:
3132
img = Image.open(
32-
'C:\\Users\\root\\Documents\\PythonProjects\\Wizzard101-PetAutomationTool\\Spy\\ImageTemplates\\Play.png')
33+
'Spy\\ImageTemplates\\Play.png')
3334
coordinates = pyautogui.locateOnScreen(img, 0.0, confidence=0.7)
3435
return coordinates
3536
except ImageNotFoundException:
@@ -42,6 +43,10 @@ def wait_for_loading(self):
4243
# Maximum time (in seconds) to search for the image
4344
max_search_time = 10
4445

46+
im = ImageGrab.grab(region)
47+
center = pyscreeze.center(region)
48+
loading_pixel = im.getpixel(center)
49+
4550
start_time = time.time()
4651

4752
while True:
@@ -50,11 +55,11 @@ def wait_for_loading(self):
5055
center = pyscreeze.center(region)
5156

5257
try:
53-
pixel = im.getpixel(center)
54-
if pixel != (67, 64, 135):
55-
self.catch_sequence()
58+
new_pixel = im.getpixel(center)
59+
60+
if new_pixel != loading_pixel:
61+
return bool('true')
5662
# yay, finished loading
57-
break
5863

5964
except Exception as e:
6065
# Handle exceptions here, if needed
@@ -65,16 +70,59 @@ def wait_for_loading(self):
6570
print("Image not found within the specified time.")
6671
break
6772

68-
def catch_sequence(self):
69-
time.sleep(2)
73+
def wait_for_start(self, loaded):
74+
time.sleep(1)
75+
hwndMain = win32gui.FindWindow('Wizard Graphical Client', 'Wizard101')
76+
region = win32gui.GetWindowRect(hwndMain)
77+
78+
# Calculate the height of the original region
79+
original_height = region[3] - region[1]
80+
81+
# Calculate the new bottom coordinate for half of the lower region
82+
half_lower_bottom = region[1] + original_height // 2
83+
84+
# Create a new tuple representing half of the lower region
85+
half_lower_region = (region[0], half_lower_bottom, region[2], region[3])
86+
87+
hay = ImageGrab.grab(half_lower_region)
88+
needle = Image.open('Spy\\ImageTemplates\\request.png')
89+
request_coordinates = pyautogui.locate(needle, hay, confidence=0.7)
90+
request_center = pyscreeze.center(request_coordinates)
91+
92+
default_request_pixel = hay.getpixel(request_center)
93+
94+
code_executed = False # Flag to track whether the code has been executed
95+
96+
while True:
97+
# Check if loaded is False
98+
if not loaded:
99+
code_executed = False # Reset the flag when not loaded
100+
time.sleep(0.1)
101+
continue
102+
103+
# Execute the code only if the flag is False
104+
if not code_executed:
105+
# Take a screenshot of the specified region
106+
loaded_im = ImageGrab.grab(half_lower_region)
107+
try:
108+
new_pixel = loaded_im.getpixel(request_center)
109+
if new_pixel != default_request_pixel:
110+
self.catch_sequence(num=3)
111+
code_executed = True # Set the flag to True after executing the code
112+
break
113+
except Exception as e:
114+
# Handle exceptions here, if needed
115+
pass
116+
117+
def catch_sequence(self, num):
70118
MatchThisRequest = Image.open(
71-
'C:\\Users\\root\\Documents\\PythonProjects\\Wizzard101-PetAutomationTool\\Spy\\ImageTemplates\\request.png')
119+
'Spy\\ImageTemplates\\request.png')
72120
window_region = pyautogui.locateOnScreen(MatchThisRequest, 0.0, confidence=0.7)
73121
region = (
74122
int(window_region[0]), int(window_region[1]), int(window_region[2]), int(window_region[3]))
75-
num_screenshots = 3
123+
num_screenshots = num
76124

77125
for i in range(num_screenshots):
78-
time.sleep(0.5)
79126
screenshot = pyscreeze.screenshot(region=region)
80127
screenshot.save(f"image_{i + 1}.png")
128+
time.sleep(0.5)

app.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,41 @@ def __init__(self, state_handling, spy, comparison_handling, action):
1414
self.action = action.Action()
1515

1616
def application_entrance(self):
17+
self.action.clean_root_directory()
18+
self.l1()
19+
20+
def l1(self):
1721
# set Wizard101 Game client to the active Window
18-
self.state_handling.set_Active()
22+
self.state_handling.set_active()
23+
time.sleep(1)
1924
# locates the Dance Game Popup and presses X if found
2025
popup_coordinates = self.spy.locate_dance_game_popup()
2126
self.action.simulate_popup_press(coordinates=popup_coordinates)
27+
time.sleep(0.5)
2228
# proceeds with level selection
2329
level_coordinates = self.spy.locate_level()
2430
self.action.simulate_level_selection(coordinates=level_coordinates)
31+
time.sleep(0.5)
2532
# proceeds with play instruction
2633
play_button_coordinates = self.spy.locate_play()
2734
self.action.simulate_play_execution(coordinates=play_button_coordinates)
35+
time.sleep(1.5)
2836
# proceeds with loading screen handling and begins catching the sequence
2937
# after loading is complete(...)
30-
self.spy.wait_for_loading()
38+
loading_screen_status = self.spy.wait_for_loading()
39+
self.spy.wait_for_start(loaded=loading_screen_status)
40+
# indicates it is the ->first<- level with 3 keys to catch
3141
# compares the caught sequence with templates
3242
key_sequence = self.comparison_handler.compare_sequence()
3343
self.action.simulate_key_presses(keys=key_sequence)
44+
time.sleep(1)
45+
46+
def l2(self):
47+
self.spy.catch_sequence(num=4)
48+
time.sleep(1)
49+
keys = self.comparison_handler.compare_sequence
50+
self.action.simulate_key_presses(keys=keys)
3451

3552

36-
app_instance = PetAutomationTool()
53+
app_instance = PetAutomationTool(state_handling, spy, comparison_handling, action)
3754
app_instance.application_entrance()

image_1.png

117 Bytes
Loading

0 commit comments

Comments
 (0)