-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbattletext.py
181 lines (146 loc) · 4.84 KB
/
battletext.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
import pyautogui as pg
import PIL.ImageGrab
import pynput
import random, os, time
listener = None
current_position = None
word_history = []
os.chdir('words')
def get_words(letter, skipped, length):
# Get list from json file with starting letter
cur = eval(open(f'{letter[:1]}.json', 'r').read())
# Sort words by length descendingly
cur_sorted = sorted(cur, key = len)[::-1]
# Filter words that may contain chars from skipped_letters
# and <= given length
end_letter = ''
if len(letter) == 2:
end_letter = letter[1]
for word in cur_sorted:
if len(word) <= length:
if word.endswith(end_letter):
if all(l not in word for l in skipped):
yield word
def smart_randomizer(words:list, randomized=True):
global word_history
# Toggle between human like typing and auto-robot
new_list = words[:]
# Get all words larger than 7 chars
new_list = list(filter(lambda i: len(i) > 7, new_list))
random.shuffle(new_list)
if not randomized:
new_list = words[:]
# Never get the same word twice
i = 0
word = new_list[i]
while word in word_history or len(word) > 20:
i += 1
word = new_list[i]
word_history.append(word)
return word
def on_click(x, y, button, pressed):
global current_position
# If mouse clicked store position and stop listener
if pressed:
current_position = (x, y)
listener.stop()
time.sleep(1)
def capture_click():
global listener
with pynput.mouse.Listener(on_click=on_click) as listener:
listener.join()
def get_keyboard(f=1):
keys = {}
x, y = current_position
keys['del'] = (x, y)
for i in range(7):
m = 69 * (i + 1) - sum(range(i + 1)) * 3.85
m *= f
keys['mnbvcxz'[i]] = (x - m, y)
keys['l'] = (x, y - 80 * f)
for i in range(8):
m = 57 * (i + 1)
m *= f
keys['kjhgfdsa'[i]] = (x - m, y - 80 * f)
keys['p'] = (x + 20 * f, y - 152 * f)
for i in range(9):
m = 55 * (i + 1)
m *= f
keys['oiuytrewq'[i]] = (x - m + 20 * f, y - 152 * f)
keys['done'] = (x, y - 225 * f)
return keys
def get_current_colors(keys):
# return active green key
# and disabled grey keys
GRN, GRY = '', ''
screen_colors = PIL.ImageGrab.grab().load()
for key in keys:
if key in ['del', 'done']:
continue
x, y = keys[key]
rgb = screen_colors[x, y]
if rgb == (133, 196, 59):
GRN = key
if rgb == (145, 146, 152):
GRY += key
return GRN, GRY
def auto_typer(keys, word, delay=0.09, human_like=False):
# Using some randomness to simulate a human typing
if human_like:
time.sleep(random.randint(1, 3))
for letter in word:
pg.click(keys[letter])
time.sleep(random.randint(1, 4) * delay)
else:
for letter in word:
pg.click(keys[letter])
pg.click(keys['done'])
def wait_other_player(keys):
time.sleep(2)
x, y = keys['done']
while True:
done_color = PIL.ImageGrab.grab().load()[x, y]
if done_color == (133, 196, 59):
return 0
if done_color == (45, 48, 71):
return 1
time.sleep(1)
def main():
isHuman = input('Enter 0 for Robot or 1 for Human: ')
try:
isHuman = int(isHuman)
if not -1 < isHuman < 2:
isHuman = 1
except:
isHuman = 1
print('Click above backspace middle-point:\n')
# Capture click on on-screen del button
capture_click()
print('- Captured Click')
# Map the rest of the keys
keys = get_keyboard()
print('- Mapped Keys')
rounds = 1
while True:
print(f'\n\nRound: {rounds}')
# Get active letter and skipped ones from screen
letter, skipped = get_current_colors(keys)
print(f'\n- Active Letter: {letter}'
f'\n- Skipped Letters: {skipped}\n')
# Get corresponding words and shuffling them
words = list(get_words(letter, skipped, 30))
print(f'- Found {len(words)} words')
# Auto-Type the word and click done
word = smart_randomizer(words, randomized=isHuman) # word = words[0]
print(f'- Auto-typing word: {word}')
auto_typer(keys, word, human_like=isHuman)
# Wait for next round or win screen
print('- Waiting for next round...')
waiter = wait_other_player(keys)
if waiter:
print('- Opponent has been defeated XD')
return
rounds += 1
if __name__ == '__main__':
main()
input()