-
Notifications
You must be signed in to change notification settings - Fork 2
/
tools.py
166 lines (149 loc) · 4.89 KB
/
tools.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
from urllib.request import urlopen
from time import time, sleep
from krevetka import *
import pyautogui
from random import *
'''
from vosk import Model, KaldiRecognizer
import pyaudio
from fuzzywuzzy import process
model = Model("vosk-model-small-fr-0.22")
recognizer = KaldiRecognizer(model, 16000)
mic = pyaudio.PyAudio()
def recognize_audio():
stream = mic.open(format=pyaudio.paInt16, channels=1, rate=16000, input=True, frames_per_buffer=8192)
stream.start_stream()
text = "1"*20
print("start")
words = [""]
while text[14:-3]!="":
data = stream.read(4096)
if recognizer.AcceptWaveform(data):
text = recognizer.Result()
print(text[14:-3])
words.append(text[14:-3])
return words[-2]
def get_part_of_move(is_number):
List = ["a", "b", "c", "d", "e", "f", "g", "h"]
if is_number:
List = ["un", "deux", "trois", "six", "sept", "huit"]
return process.extract(recognize_audio(), List)[0][0]
'''
def get_mouse_board():
x, y = pyautogui.position()
print("mouse", x, y)
return [x, y]
"""
def goTo(x3, y3, click = True):
print("goTo")
x0, y0 = pyautogui.position()
a = random.choice([-1, 1])
A = 50
B = 100
x1 = (x3-x0)/2 + random.randint(A, B) * a
y1 = (x3-x0)/2 + random.randint(A, B) * a
x2 = (x3-x0)/2 + random.randint(A, B) * (-a)
y2 = (x3-x0)/2 + random.randint(A, B) * (-a)
x = lambda t: t+1
X = lambda t: (1-t)**3 * x0 + 3*t*(1-t)**2 * x1 + 3*t**2*(1-t) * x2 + t**3 * x3
Y = lambda t: (1-t)**3 * y0 + 3*t*(1-t)**2 * y1 + 3*t**2*(1-t) * y2 + t**3 * y3
#print(X(0))
n = 2
for i in range(0, 10**n + 1):
t = i/(10**n)
pyautogui.moveTo(X(t), Y(t), 0.01)
pyautogui.click()
"""
def play_mouse(move, coord_mouse, turn):
x1 = coord_mouse[0]
y1 = coord_mouse[1]
x2 = coord_mouse[2]
y2 = coord_mouse[3]
dx = abs(x1-x2)//8
dy = abs(y1-y2)//8
alpha = "abcdefgh"
start = move[:2]
finish = move[2:]
startx = (alpha.find(start[0])+0.5)*dx+x1
starty = y2 - (int(start[1])-0.5)*dy
finishx = (alpha.find(finish[0])+0.5)*dx+x1
finishy = y2 - (int(finish[1])-0.5)*dy
if turn:
startx = x2-startx+x1
starty = y1+(int(start[1])-0.5)*dy
finishx = x2-finishx+x1
finishy = y1+(int(finish[1])-0.5)*dy
pyautogui.click(startx, starty)
pyautogui.click(finishx, finishy)
if len(move) == 5:
pyautogui.click()
def get_move(a, b, turn="white", game="chess.com"):
im = pyautogui.screenshot("board.png", (a[0], a[1], b[0]-a[0], b[1]-a[1]))
onex = im.size[0]//8
oney = im.size[1]//8
d = {}
pixel = pixel2 = None
with open("images/"+game+"/pixel.txt", "r") as file:
pixel = eval(file.readline())
pixel2 = eval(file.readline())
file.close()
for x in range(8*onex):
for y in range(8*oney):
rgb = im.getpixel((x, y))
square = str(x//onex)+str(y//oney)
if rgb in pixel:
if square in d:
d[square] += 1
else:
d[square] = 1
squares = ["No", "No"]
squares_val = [0, 0]
for x in range(8):
for y in range(8):
square = str(x)+str(y)
if square in d:
val = d[square]
if val > squares_val[0]:
squares_val = [val, squares_val[0]]
squares = [square, squares[0]]
elif val > squares_val[1]:
squares_val = [squares_val[0], val]
squares = [squares[0], square]
start = squares[0]
end = squares[1]
alpha = "abcdefgh"
num = 8
if turn == "black":
alpha = alpha[::-1]
num = -1
if "No" not in squares:
start = alpha[int(start[0])]+str(abs(num-int(start[1])))
end = alpha[int(end[0])]+str(abs(num-int(end[1])))
print(start, end)
return [start, end]
def find_first_pixel(website):
im = pyautogui.screenshot()
with open("images/"+website+"/pixel.txt", "r") as f:
f.readline()
pixel2=eval(f.readline())
f.close()
for y in range(im.size[1]):
for x in range(im.size[0]):
rgb = im.getpixel((x, y))
if rgb in pixel2:
return [x, y]
raise TypeError
def find_last_pixel(website):
im = pyautogui.screenshot()
with open("images/"+website+"/pixel.txt", "r") as f:
f.readline()
pixel2=eval(f.readline())
f.close()
for y in range(im.size[1]-1, 0, -1):
for x in range(im.size[0]-1, 0, -1):
rgb = im.getpixel((x, y))
if rgb in pixel2:
return [x, y]
raise TypeError
def write_move(move):
pyautogui.write(move)