-
Notifications
You must be signed in to change notification settings - Fork 19
/
hero_action.py
218 lines (144 loc) · 7.78 KB
/
hero_action.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
import time
from colorama import Fore
import pyautogui
import random
class HeroAction:
def __init__( self, poker_window ):
self.window = poker_window
self.window_activation_error_reported = False
# Mapping of actions to their corresponding button positions
"""action_to_button = {
'Fold': (0.516, 0.909), # Button1 = 'Fold'
'Check': (0.679, 0.909), # Button2 = 'Check' or 'Call'
'Call': (0.679, 0.909), # Button2 = 'Check' or 'Call'
'Raise': (0.842, 0.909), # Button3 = 'Raise' or 'Call' or 'Bet'
'Bet': (0.842, 0.909), # Button3 = 'Raise' or 'Call' or 'Bet'
}"""
#-----------------------------------------------------
# Call in GameState to execute the AI suggested action in Pokerstars window
def execute_action( self, button_coordinates, action, amount):
self.activate_window()
if button_coordinates is None:
first_button = (0.516, 0.907)
self.click_button(first_button)
second_button = (0.679, 0.907)
self.click_button(second_button)
return
# Split the action string and take the first part
action = action.split()[0]
match action:
case 'Bet':
self.bet(button_coordinates, amount)
case 'Raise':
self.raise_bet(button_coordinates, amount)
case 'All':
self.all_in(button_coordinates)
case 'Call' | 'Fold' | 'Check' | 'Resume' | 'Cash Out':
self.click_button(button_coordinates)
case _:
print(f"{Fore.RED}HeroActions -> execute_action(): Player Action {action} not recognized.")
#-----------------------------------------------------
def bet( self, button_coordinates, amount ):
self.click_and_drag_input_box(0.728, 0.858, 0.642, 0.858)
if amount > 0:
self.input_bet_raise_amount(amount)
self.click_button(button_coordinates)
def raise_bet( self, button_coordinates, amount ):
self.click_and_drag_input_box(0.728, 0.858, 0.642, 0.858)
if amount > 0:
self.input_bet_raise_amount(amount)
self.click_button(button_coordinates)
def all_in( self, button_coordinates ):
self.click_max_bet_button()
self.click_button(button_coordinates)
#-----------------------------------------------------
def click_button( self, button_coordinates ):
"""Click a button in the poker client window."""
if not self.window:
print(F"{Fore.RED}HeroActions -> click_button(): Window not located. Please locate the window first.")
return
# Get the button coordinates
button_x, button_y = button_coordinates
# Add randomness to the position
random_position_x = random.uniform(0.0, 0.05)
random_position_y = random.uniform(0.0, 0.05)
# Calculate the absolute position of the button
abs_x = self.window.left + int(self.window.width * (button_x + random_position_x))
abs_y = self.window.top + int(self.window.height * (button_y + random_position_y))
random_duration = random.uniform(0.1, 0.2)
# Move the mouse cursor to the position for visual confirmation
pyautogui.moveTo(abs_x, abs_y, duration=0.1)
# Perform the click
pyautogui.click(abs_x, abs_y)
print(F"{Fore.YELLOW}Button clicked at x={abs_x}, y={abs_y}")
# Move the mouse cursor to the position for visual confirmation
pyautogui.moveTo((self.window.width/2)+random.uniform(0, 500), (self.window.height/2)+random.uniform(0, 500), duration=random_duration)
#-----------------------------------------------------
# Sets the maximum bet size (so we can go All-in!) *NOT USED*
def click_max_bet_button(self):
"""Click the 'Max' button in the poker client window."""
if not self.window:
print(F"{Fore.RED}HeroActions -> Window not located. Please locate the window first.")
return
random_position_add = random.uniform(-0.02, 0.02)
# Calculate the absolute position of the 'Bet' button
abs_x = self.window.left + int(self.window.width * (0.945 + random_position_add))
abs_y = self.window.top + int(self.window.height * (0.809 + random_position_add))
random_cursor_speed = random.uniform(0.01, 0.1)
# Move the mouse cursor to the position for visual confirmation
pyautogui.moveTo(abs_x, abs_y, duration=random_cursor_speed) # Move the cursor over 1 second
# Perform the click
pyautogui.click(abs_x, abs_y)
print(F"{Fore.YELLOW}HeroActions -> Max bet Button clicked at x={abs_x}, y={abs_y}")
def input_bet_raise_amount(self, amount):
"""Simulate keyboard input for betting or raising a specific amount."""
amount_str = str(amount) # Convert the amount to a string
# Generate a random float value
random_sleep_time = random.uniform(0.1, 0.2)
write_speed = random.uniform(0.01, 0.1)
time.sleep(random_sleep_time)
pyautogui.write(amount_str, interval=write_speed) # Type out the amount
print(F"{Fore.YELLOW}HeroActions -> Input bet/raise amount: {amount_str}")
#-----------------------------------------------------
def click_and_drag_input_box(self, start_rel_x, start_rel_y, end_rel_x, end_rel_y, duration=0.1):
"""
Mouse start coordinates in relative space:
0.728, 0.858
Mouse end coordinates in relative space:
0.649, 0.858
Simulates a click and drag action using relative coordinates within the window.
:param start_rel_x: Relative X coordinate of the starting point.
:param start_rel_y: Relative Y coordinate of the starting point.
:param end_rel_x: Relative X coordinate of the end point.
:param end_rel_y: Relative Y coordinate of the end point.
:param duration: Time taken for the drag action in seconds.
"""
if not self.window:
print("HeroActions -> Window not located. Please locate the window first.")
return
# Calculate absolute coordinates from relative coordinates
start_abs_x = self.window.left + int(self.window.width * start_rel_x)
start_abs_y = self.window.top + int(self.window.height * start_rel_y)
end_abs_x = self.window.left + int(self.window.width * end_rel_x)
end_abs_y = self.window.top + int(self.window.height * end_rel_y)
# Perform the click and drag
pyautogui.moveTo(start_abs_x, start_abs_y, duration=duration)
pyautogui.mouseDown()
time.sleep(random.uniform(0.1, 0.2)) # Short delay to ensure the mouse down event is registered
pyautogui.moveTo(end_abs_x, end_abs_y, duration=duration)
pyautogui.mouseUp()
#-----------------------------------------------------
def activate_window(self):
"""Activate the poker client window."""
if self.window:
try:
self.window.activate()
self.window_activation_error_reported = False # Reset the flag if activation is successful
except Exception as e:
if not self.window_activation_error_reported:
print(F"{Fore.RED}HeroActions -> Error activating window: {e}")
self.window_activation_error_reported = True
else:
if not self.window_activation_error_reported:
print(F"{Fore.RED}HeroActions -> Window not located or cannot be activated.")
self.window_activation_error_reported = True