-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpraisebot.py
286 lines (216 loc) · 8.63 KB
/
praisebot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
from pyautogui import *
import cv2
import pyautogui
import time
import keyboard
import numpy as np
import random
import pprint
def group_locations(locations, threshold=10):
"""
Group nearby locations to treat them as a single location.
"""
grouped = []
for loc in locations:
if not grouped:
grouped.append([loc])
else:
match = False
for group in grouped:
if abs(group[0][0] - loc[0]) < threshold and abs(group[0][1] - loc[1]) < threshold:
group.append(loc)
match = True
break
if not match:
grouped.append([loc])
return [tuple(np.mean(g, axis=0).astype(int)) for g in grouped]
# Load the reference image and ensure it is in the correct format
reference_image_path = 'green_space_near_player.png'
reference_image = cv2.imread(reference_image_path, cv2.IMREAD_COLOR)
# Load the defensive player image and ensure it is in the correct format
defensive_player_image_path = 'orange_down_arrow_chat.png'
defensive_player_image = cv2.imread(defensive_player_image_path, cv2.IMREAD_COLOR)
# Verify that the reference image is loaded properly
if reference_image is None:
raise ValueError("Reference image not found or unable to load.")
# Verify that the defensive player image is loaded properly
if defensive_player_image is None:
raise ValueError("Defensive player image not found or unable to load.")
def define_region(bottom_right, top_left):
bottom_right_x, bottom_right_y = bottom_right
top_left_x, top_left_y = top_left
return (top_left_x, top_left_y,
(bottom_right_x - top_left_x),
(bottom_right_y - top_left_y))
# Define the Rating region of the screen to capture (x, y, width, height)
RATING_REGION_BOT_RIGHT = (1189, 2122)
RATING_REGION_TOP_LEFT = (1076, 412)
RATING_REGION = define_region(RATING_REGION_BOT_RIGHT, RATING_REGION_TOP_LEFT)
# Define region of screen where player's face and info appears in Quick Chat
PLAYER_CHAT_BOT_RIGHT= (1376, 1511)
PLAYER_CHAT_TOP_LEFT = (958, 375)
PLAYER_CHAT_REGION = define_region(PLAYER_CHAT_BOT_RIGHT, PLAYER_CHAT_TOP_LEFT)
# GLOBALS
HOVER_DURATION = 0.2
# Bookmark link in bottom left
INDY_TRAINING_AREA = (1387, 179)
# Location of first player row top left
TOP_PLAYER_AREA = (1267, 405)
# Roughly equivalent to number of players that
# have > 7.0 scores in total.
PLAYERS_TO_PRAISE = 17 # Start with 17 and see how far we get
# Top right praise button, white rectangle top right
# We aim for the white baloon
TOP_RIGHT_PRAISE_BUTTON = (3474,284)
# Praise button in the pop-up dialog after hitting
# top right praise button
MODAL_PRAISE_BUTTON = (1597, 1018)
# Back down button in the pop-up dialog.
# These can vary based on size of response window.
MODAL_BACK_DOWN_BUTTON_1 = (1516, 833)
MODAL_BACK_DOWN_BUTTON_2 = (1516, 1033)
# End the quick chat in modal pop-up
END_QC_BUTTON = (2759, 317)
# We need to "scroll down" to find more players if we have good
# rating players below the fold. We click in the right gutter area
SCROLL_DOWN_GUTTER = (1218, 2064)
# We need to "scroll down" to find more players if we have good
# rating players below the fold. We click in the right gutter area
SCROLL_UP_GUTTER = (1218, 417)
# Set up "defensive" player color and position. The color _may_ change
# maybe? Not sure if configurable. The "color bar" is the bar that
# displays player body language. We choose the section that has not text
# to prevent this from misfiring.
# Set up "defensive" player color and position
DEFENSIVE_COLOR = (153, 147, 87)
DEFENSIVE_PLAYER_COLOR_BAR = (1237, 717)
# Define separate move and click functions
def my_move(coord_tuple):
x, y = coord_tuple
pyautogui.moveTo(x, y, duration=HOVER_DURATION)
def my_click():
pyautogui.mouseDown()
#time.sleep(0.1)
pyautogui.mouseUp()
def orientate():
# Training area setup
time.sleep(5)
# Navigate to Training Menu using FM shortcut
pyautogui.hotkey('ctrl', 'r')
time.sleep(1)
# Click on Individual Training
time.sleep(2)
my_move(INDY_TRAINING_AREA)
my_click()
# Click first player
my_move(TOP_PLAYER_AREA)
my_click()
def player_is_defensive(player_chat_region, reference_image=reference_image, defensive_player_image=defensive_player_image):
# Take a screenshot of the specified player chat region
screenshot = pyautogui.screenshot(region=player_chat_region)
screenshot = np.array(screenshot)
screenshot = cv2.cvtColor(screenshot, cv2.COLOR_RGB2BGR)
# Ensure both images are of the same type (either both grayscale or both color)
# If using grayscale, uncomment the following lines:
# reference_image = cv2.cvtColor(reference_image, cv2.COLOR_BGR2GRAY)
# screenshot = cv2.cvtColor(screenshot, cv2.COLOR_BGR2GRAY)
# Find the reference image within the screenshot
result = cv2.matchTemplate(screenshot, defensive_player_image, cv2.TM_CCOEFF_NORMED)
# Set a threshold for the maximum score
threshold = 0.9
# Find the maximum score and its location
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(result)
# Check if the maximum score is above the threshold
if max_val > threshold:
print('Template image found at location:', max_loc)
return True
else:
print('Template image not found')
return None
# To display mouse position
# pyautogui.displayMousePosition()
def my_praise_click():
my_move(TOP_RIGHT_PRAISE_BUTTON)
my_click()
# Click 1st praise choice
my_move(MODAL_PRAISE_BUTTON)
my_click()
# If player gets defensive, we should see an orange-ey down arrow in certain region.
# We should back down after a praise attempt, and the remaining logic should stand.
if player_is_defensive(PLAYER_CHAT_REGION):
print("We have found a defensive player")
my_move(MODAL_BACK_DOWN_BUTTON_1)
my_click()
time.sleep(2)
my_move(MODAL_BACK_DOWN_BUTTON_2)
my_click()
# End chat
my_move(END_QC_BUTTON)
my_click()
def praise_button_exists(button):
x, y = button
return pyautogui.pixel(x, y) == (255,255,255)
def click_praise_buttons():
my_move(TOP_RIGHT_PRAISE_BUTTON)
my_click()
# Click 1st praise choice
my_move(MODAL_PRAISE_BUTTON)
my_click()
# End chat
my_move(END_QC_BUTTON)
my_click()
def click_ratings(rating_region):
# Take a screenshot of the specified rating_region
screenshot = pyautogui.screenshot(region=rating_region)
screenshot = np.array(screenshot)
screenshot = cv2.cvtColor(screenshot, cv2.COLOR_RGB2BGR)
# Ensure both images are of the same type (either both grayscale or both color)
# If using grayscale, uncomment the following lines:
# reference_image = cv2.cvtColor(reference_image, cv2.COLOR_BGR2GRAY)
# screenshot = cv2.cvtColor(screenshot, cv2.COLOR_BGR2GRAY)
# Find the reference image within the screenshot
result = cv2.matchTemplate(screenshot, reference_image, cv2.TM_CCOEFF_NORMED)
# Define a threshold
threshold = 0.80
# Get the locations where the matches exceed the threshold
locations = np.where(result >= threshold)
locations = list(zip(*locations[::-1]))
# Group the locations
grouped_locations = group_locations(locations)
# Pretty print locations detected
#pprint.pprint(locations)
# Process each matching location
i = 0
for loc in grouped_locations:
# Calculate the center point
center_x = loc[0] + int(reference_image.shape[1] / 2)
center_y = loc[1] + int(reference_image.shape[0] / 2)
# Add the rating_region's offset to get the absolute screen position
screen_x = rating_region[0] + center_x
screen_y = rating_region[1] + center_y
# Perform the click and praise action
pyautogui.click(screen_x, screen_y)
print(f"Clicked {screen_x}, {screen_y} and now sleeping" )
time.sleep(0.5) # Adjust sleep time as needed
# Add your 'praise player' code here
if praise_button_exists(TOP_RIGHT_PRAISE_BUTTON):
my_praise_click()
# Diagnostic code in case we need to bug fix
#i += 1
#if i > 5:
# break
# Optionally, add a small delay between processing each location
time.sleep(0.2)
if __name__ == "__main__":
# Provide time to move alt-tab to app
time.sleep(2)
# Go to Training>Indivdual menus
orientate()
my_move(SCROLL_UP_GUTTER)
my_click()
click_ratings(RATING_REGION)
my_move(SCROLL_DOWN_GUTTER)
my_click()
click_ratings(RATING_REGION)
my_move(SCROLL_UP_GUTTER)
my_click()