-
Notifications
You must be signed in to change notification settings - Fork 0
/
logparse.py
55 lines (39 loc) · 1.39 KB
/
logparse.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
from player_state import PlayerState
from utils import p
import re
import tornado.ioloop
card_update_pattern = re.compile('^.*id=(?P<id>\d*) .*cardId=(?P<card_id>[a-zA-Z0-9\_]*).*player=(?P<player>\d*)\](?: CardID=(?P<alternate_card_id>[a-zA-Z0-9\_]*))?.*$')
game_complete_pattern = re.compile('^.*TAG_CHANGE Entity=GameEntity tag=STATE value=COMPLETE.*$')
player_state = PlayerState()
def parse_log_lines(line):
if game_complete_pattern.match(line):
player_state.clear_players()
match = card_update_pattern.match(line)
if not match:
return
id = match.group('id')
cardId = match.group('card_id')
alternate_card_id = match.group('alternate_card_id')
player = match.group('player')
if player_state.card_drawn(id, cardId or alternate_card_id, player):
p("Updated player state")
def create_log_handler(file_path):
f = file_path.open(encoding='utf-8')
f.seek(0, 2)
partyvan = {
'last_known_size': file_path.stat().st_size
}
def tail():
while True:
line = f.readline()
if not line:
current_size = file_path.stat().st_size
# Has hearthstone sneakily recreated the log file?
if current_size < partyvan['last_known_size']:
player_state.clear_players()
f.seek(0, 0)
partyvan['last_known_size'] = current_size
break
tornado.ioloop.IOLoop.current().add_callback(
parse_log_lines, line)
return tornado.ioloop.PeriodicCallback(tail, 1000), player_state