-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollect.py
221 lines (185 loc) · 6.89 KB
/
collect.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
218
219
220
221
import os
from os import path
from os import listdir
from os.path import join, isfile
import datetime
import re
import time
from enum import Enum
import subprocess
import signal
import numpy as np
from _thread import start_new_thread
from threading import Lock
import tailer
from system_hotkey import SystemHotkey
import pyautogui
from hslog import LogParser
from hslog.export import FriendlyPlayerExporter
from libs.hslog_exporters import LastTurnExporter
from random import randrange
from libs import screen
from libs.template import create_battlefield
class Position(Enum):
FATAL_ERROR = -1
UNKNOWN = 0
HUB = 1
ADVENTURE = 2
DRAFT = 3
TAVERN_BRAWL = 4
COLLECTIONMANAGER = 5
PACKOPENING = 5
GAMEPLAY = 6
ARENA = 7
TOURNAMENT = 8
class Game:
position = Position.UNKNOWN
path = None
regex_loading = re.compile(r"LoadingScreen.OnSceneUnloaded\(\) - prevMode=.* nextMode=(.*) ")
def __init__(self, log_path):
self.thread = None
self.path = log_path
self.collector = BaseCollector(self)
self.lock = Lock()
self.p = None
self.running = True
self.hk = SystemHotkey()
self.hk.register(('control', 'alt', 'z'), callback=self.delete_last_img)
self.images = []
def delete_last_img(self, evnt):
if self.images:
os.remove(self.images[-1])
os.remove(self.images[-1].replace(".png", ".xml"))
if self.p:
self.p.terminate()
self.p.wait()
self.images.pop(-1)
if self.images and self.images[-1]:
self.p = subprocess.Popen(["python3", "labelImg/labelImg.py", self.images[-1]])
def get_position(self, lines):
""" set self.position and returns true if the position is new"""
for line in reversed(lines):
match = self.regex_loading.search(line)
if(match and self.position != Position[match.group(1)]):
self.position = Position[match.group(1)]
return True ## new position true
return False ## not a new position
def set_collector(self):
if(self.position is Position.ARENA):
self.collector = ArenaCollector(self)
elif(self.position is Position.GAMEPLAY):
self.collector = BattlefieldCollector(self)
elif(self.position is Position.COLLECTIONMANAGER):
self.collector = CollectionCollertor(self)
else:
self.collector = BaseCollector(self)
def show_img(self, path):
self.images.append(path)
if(len(self.images) >= 5):
self.images.remove(self.images[0])
if(self.p):
self.p.terminate()
self.p.wait()
self.p = subprocess.Popen(["python3", "labelImg/labelImg.py", path])
def run(self):
## get current position
with open(path.join(self.path, "LoadingScreen.log")) as f:
if self.get_position(f.readlines()):
self.set_collector()
start_new_thread(self.run_collector, ())
# watch file
for line in tailer.follow(open(path.join(self.path, "LoadingScreen.log"))):
if self.get_position([line]):
self.lock.acquire()
self.set_collector()
self.lock.release()
def terminate(self, signal, frame):
self.lock.acquire()
self.running = False
self.p.terminate()
self.p.wait()
self.lock.release()
if self.p:
self.p.terminate()
self.p.wait()
exit()
def run_collector(self):
while(self.running):
self.lock.acquire()
self.collector.run()
self.lock.release()
class BaseCollector:
def __init__(self, game):
self.game = game
def run(self):
pass
class CollectionCollertor(BaseCollector):
def __init__(self, game):
super().__init__(game)
game.hk.register(('control', 'alt', 'g'), callback=self.start)
game.hk.register(('control', 'alt', 's'), callback=self.stop)
print("CollectionCollertor: Start process with ctrl+alt+g")
print("CollectionCollertor: Stop process with ctrl+alt+s")
self.last_img = None
self.idx = 0
self.running = False
def start(self, evnt):
self.idx = 0
print("Start CollectionCollertor ...")
self.running = True
def stop(self, evnt):
print("Stop CollectionCollertor ...")
self.running = False
def run(self):
if(self.running):
img = screen.shot()
if(self.last_img and screen.simple_compare(np.array(img), np.array(self.last_img))):
self.running = False
print("Finish!")
else:
self.last_img = img
screen.save(img, "images/collection_{}.png".format(self.idx))
self.idx += 1
pyautogui.click(1223, 521)
time.sleep(1.2)
## build xml
## click next
class ArenaCollector(BaseCollector):
pass
class BattlefieldCollector(BaseCollector):
def __init__(self, game):
super().__init__(game)
self.path = path.join(game.path, "Power.log")
print(game.path)
self.last_ts = datetime.time(0)
self.last_turn = None
self.parser = LogParser()
def run(self):
with open(self.path) as f:
self.parser.read(f)
self.parser.flush()
tps = self.parser.games
fp_id = FriendlyPlayerExporter(tps[-1]).export()
turn = LastTurnExporter(tps[-1], self.last_ts, fp_id).export()
if not self.last_turn:
self.last_turn = turn
if(turn.ts > self.last_turn.ts and turn.player):
## GET MINIONS / HAND_CARDS / HERO_POWERS
time.sleep(1.2)
img = screen.shot()
print("EMinions {} - PMinions {} - HandCards {}".format(turn.enemy_minions, turn.player_minions, turn.hand_cards))
base_name = "em{}_pm{}_hc{}".format(turn.enemy_minions, turn.player_minions, turn.hand_cards)
count = len([f for f in listdir("images") if isfile(join("images", f)) and base_name in f and ".png" in f])
if(count > 2):
count = randrange(0, 2)
base_name = "images/{}_{}".format(count, base_name)
img_name = base_name + ".png"
xml_name = base_name + ".xml"
temp = create_battlefield(img, turn.hand_cards, turn.enemy_minions, turn.player_minions, turn.enemy_power, turn.player_power)
temp.save(xml_name)
screen.save(img, img_name)
self.game.show_img(img_name)
self.last_turn = turn
game = Game("/home/dee/.PlayOnLinux/wineprefix/hs/drive_c/Program Files/Hearthstone/Logs/")
# signal.signal(signal.SIGINT, game.terminate)
game.run()