-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbot.py
82 lines (59 loc) · 2.54 KB
/
bot.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
import io
import random
from actions.screen import screencap
from actions.touch import *
from detectors.cards import *
from detectors.mana import *
from detectors.units import *
# Process inputs
from strategy.strategy import predictions_to_actions
from utils import copy_image_to_np_array
from adb.client import Client as AdbClient
if __name__ == '__main__':
winName = 'Clash Royale AI'
cv.namedWindow(winName, cv.WINDOW_NORMAL)
outputFile = "yolo_out_py.avi"
client = AdbClient(host="127.0.0.1", port=5037)
devices = client.devices()
print(devices)
device = devices[0]
width, height = Image.open(io.BytesIO(device.screencap())).size
# Get the video writer initialized to save the output video
# vid_writer = cv.VideoWriter(outputFile, cv.VideoWriter_fourcc('M', 'J', 'P', 'G'), 30, (round(width), round(height)))
classes = loadUnitsNNClasses()
next_actions = list()
same_cards_stable = 3
last_cards = []
def is_cards_prediction_stable():
if len(last_cards) >= same_cards_stable:
head = last_cards[0]
return all(cards == head for cards in last_cards[1:same_cards_stable])
else:
return False
while cv.waitKey(1) < 0:
if len(next_actions) == 0:
screen = screencap(device)
# todo parse only in game?
mana = parseMana(screen)
frame = copy_image_to_np_array(screen)
if mana is not None:
cards = parse_cards(screen)
last_cards.insert(0, cards)
last_cards = last_cards[0:same_cards_stable]
if cards is not None and is_cards_prediction_stable():
predictions = predict_units(screen)
draw_predictions(frame, classes, predictions)
# predictions = []
newActions = predictions_to_actions(screen, predictions, classes, mana, cards)
next_actions.extend(list(newActions))
cv.putText(frame, 'Mana: %s, cards: %s' % (str(mana), str(cards)), (0, 35), cv.FONT_HERSHEY_SIMPLEX,
0.5,
(255, 255, 255))
# vid_writer.write(frame.astype(np.uint8))
cv.imshow(winName, frame)
else:
action, params = next_actions.pop(0)
if action == 'click':
print('making click at [{},{}]'.format(params[0], params[1]))
tap(device, params[0], params[1])
time.sleep(0.03 + random.random() * 0.07)