-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot_code.py
More file actions
214 lines (187 loc) · 5.5 KB
/
bot_code.py
File metadata and controls
214 lines (187 loc) · 5.5 KB
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
import cv2
import os
import pytesseract
import time
import win32api, win32con
import numpy as np
import random
from PIL import Image, ImageGrab
from pytesseract import image_to_string
pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'
X_PAD = 1909
Y_PAD = 194
def mouse_pos(cord):
"""
Takes (x, y) coordinates as a tuple, applies the correction for the game area origin and moves the mouse position to these coordinates.
"""
loc=(X_PAD + cord[0], Y_PAD + cord[1])
win32api.SetCursorPos(loc)
time.sleep(.1)
def left_click():
"""
Sends an event representing a click of the left mouse button.
"""
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,0,0)
time.sleep(.1)
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,0,0)
time.sleep(.1)
def place_bet(cord):
"""
Takes (x,y) coordinates as a tuple, applies the correction for the game area origin, moves the mouse to this location and left clicks
"""
loc=(X_PAD + cord[0], Y_PAD + cord[1])
win32api.SetCursorPos(loc)
time.sleep(.1)
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,0,0)
time.sleep(.1)
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,0,0)
def get_cords():
"""
Returns the x,y coordinates of the cursor adjusted for the game area origin
"""
x,y = win32api.GetCursorPos()
x = x - X_PAD
y = y - Y_PAD
clicked_cord = (x, y)
return clicked_cord
def bet_clear():
"""
Clears all bets from the roulette board
"""
mouse_pos(bet_cord['clear'])
left_click()
def spin():
"""
Clicks the spin button to start the roulette wheel spinning and clicks through the subsequent events to speed the process up
"""
mouse_pos((1193, 952))
left_click()
time.sleep(.1)
left_click()
time.sleep(.1)
left_click()
time.sleep(1)
left_click()
time.sleep(1)
def get_result(num_results, even_tracker, red_tracker):
"""
Takes three global variables which track results between spins and updates them based on the last result
"""
red = 0
even = 0
# Grab the number image
box = (X_PAD + 1246, Y_PAD + 344, X_PAD + 1281, Y_PAD + 382)
img = ImageGrab.grab(box)
# "Read" the image
arr = np.array(img) # OpenCV and PyTesseract need array input
gry = cv2.cvtColor(arr, cv2.COLOR_BGR2GRAY)
txt = image_to_string(gry, config="--psm 7")
txt.replace('\n', '')
# Get even or odd
if int(txt) % 2 == 0: # even
even = 1
elif int(txt) % 2 != 0: # odd
even = -1
# Get the colour
col = sorted(img.getcolors())
hex = ('#%02x%02x%02x' % col[-2][1])
#print(hex)
if hex == '#000000':
red = -1
elif hex == '#ff0000':
red = 1
# Update the results
num_results.insert(0, int(txt))
if len(num_results) > 20:
num_results.pop()
even_tracker += even
red_tracker += red
return num_results, even_tracker, red_tracker
bet_cord = { # This dictionary stores the (x, y) coordinates of the main locations on the roulette board relative to the game area origin
00 : (175, 416),
0 : (180, 551),
1 : (256, 576),
2 : (256, 488),
3 : (268, 378),
4 : (345, 583),
5 : (341, 500),
6 : (348, 375),
7 : (412, 584),
8 : (413, 481),
9 : (418, 405),
10 : (489, 584),
11 : (496, 486),
12 : (498, 380),
13 : (573, 587),
14 : (575, 488),
15 : (576, 397),
16 : (650, 584),
17 : (638, 486),
18 : (636, 381),
19 : (710, 584),
20 : (711, 485),
21 : (723, 392),
22 : (794, 580),
23 : (794, 482),
24 : (793, 407),
25 : (878, 575),
26 : (874, 491),
27 : (873, 373),
28 : (951, 581),
29 : (949, 491),
30 : (953, 395),
31 : (1016, 598),
32 : (1020, 482),
33 : (1015, 387),
34 : (1098, 572),
35 : (1095, 481),
36 : (1103, 394),
'1st' : (1178, 577),
'2nd' : (1182, 476),
'3rd' : (1178, 382),
'1-12' : (397, 685),
'13-24' : (693, 681),
'25-36' : (989, 676),
'1-18' : (292, 773),
'19-36' : (1084, 772),
'even' : (470, 766),
'odd' : (920, 765),
'red' : (603, 771),
'black' : (777, 775),
'clear' : (87, 294)
}
def main():
"""
Runs the main loop of the bot; spinning 20 times with random numbers and betting when even/odd and red/black events are likely
"""
num_results = []
even_tracker = 0 # This tracks the current likelihood of even (+ve) or odd (-ve)
red_tracker = 0 # This tracks the current likelihood of red (+ve) or black (-ve)
for i in range(1, 21):
choice = random.randint(1, 36)
bet_clear()
print(f"Placing bet on {choice}")
place_bet(bet_cord[choice])
# bet if even/odd likely
if even_tracker < -2:
# bet even
place_bet(bet_cord['even'])
elif even_tracker > 2:
# bet odd
place_bet(bet_cord['odd'])
# bet if red/black likely
if red_tracker < -2:
# bet red
place_bet(bet_cord['red'])
elif red_tracker > 2:
# bet black
place_bet(bet_cord['black'])
spin()
num_results, even_tracker, red_tracker = \
get_result(num_results, even_tracker, red_tracker)
print(f"The result was {num_results[0]}")
print(f"Last results were:{num_results}")
print(f"The red tracker is:{red_tracker}")
print(f"The even tracker is:{even_tracker}")
if __name__ == '__main__':
main()