-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.py
199 lines (146 loc) · 4.46 KB
/
parser.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
from datetime import datetime
class CSTime(object):
@staticmethod
def parse(line):
d = line[2:23]
parts = d.split(' - ')
month, day, year = map(int, parts[0].split('/'))
hour, minute, second = map(int, parts[1].split(':'))
return datetime(year, month, day, hour, minute, second)
class FactoryMatch(object):
def __init__(self):
self.actions = []
self.actions.append(MapName)
def process(self, fp):
f = open(fp, 'r')
output = None
for line in f:
output = self.process_line(line)
if output is not None:
break
return output
def process_line(self, line):
output = None
for a in self.actions:
if a.match(line):
output = a.execute(line)
break
return output
class MapName(object):
@staticmethod
def match(line):
if line[25:36] == "Loading map":
return True
return False
@staticmethod
def execute(line):
output = {}
output['created_on'] = CSTime.parse(line)
output['map'] = line[38:-2]
return output
class FactoryEvent(object):
def __init__(self):
self.actions = []
self.actions.append(AttackEvent)
self.actions.append(KillEvent)
self.actions.append(ScoreEvent)
def process(self, line):
for a in self.actions:
if a.match(line):
return a.execute(line)
class ScoreEvent(object):
@staticmethod
def match(line):
if line[0:4] == "Team":
parts = line.split(' ')
if parts[2] == "scored":
return True
return False
@staticmethod
def execute(line):
parts = line.split(' ')
team = parts[1].strip('"')
score = int(parts[3].strip('"'))
return team, score
class AttackEvent(object):
@staticmethod
def match(line):
if line[0] == '"' and line[-1:] == ')':
if ' attacked ' in line:
return True
return False
@staticmethod
def execute(line):
parts = line.split(" with ")
event = Player2Player.execute(parts[0])
damage = Damage.execute(parts[1])
return event, damage
class KillEvent(object):
@staticmethod
def match(line):
if line[0] == '"' and (line[-1] == '"' or line [-1] == ')'):
if ' killed ' in line:
return True
return False
@staticmethod
def execute(line):
parts = line.split(' with ')
event = Player2Player.execute(parts[0])
damage = Damage.execute(parts[1])
return event, damage
'''
if line[-10:] == '(headshot)':
headshot = True
else:
headshot = False
parts = line.split(" with ")
event = Player2Player.execute(parts[0])
if parts[1][-1:] == ')':
headshot = True
weapon = parts[1][1:-12]
else:
headshot = False
weapon = parts[1][1:-1]
kill = {'headshot':headshot, 'weapon':weapon}
return event, kill
'''
class Player2Player(object):
@staticmethod
def execute(line):
# player_a, action, player_b
parts = line[1:].split('"')[:-1]
player_a = Player.execute(parts[0])
action = parts[1].strip(' ')
player_b = Player.execute(parts[2])
output = {'player_a':player_a, 'player_b':player_b, 'action':action}
return output
class Player(object):
@staticmethod
def execute(line):
parts = line.split("<")
output = {}
output['alias'] = parts[0]
parts = map(lambda x: x.strip(">"), parts[1:])
try:
output['number'] = parts[0]
output['steam_id'] = parts[1]
output['team'] = parts[2]
except:
print line
raise Exception("FUCK")
return output
class Damage(object):
@staticmethod
def execute(line):
parts = line.split(' (')
output = {}
output['weapon'] = parts[0].strip('"')
for i in map(lambda x: x[:-1].split(' '), parts[1:]):
if i[0] == 'hitgroup':
value = ' '.join(map(lambda x: x.strip('"'), i[1:]))
elif i[0] == 'headshot':
value = True
else:
value = int(i[1].strip('"'))
output[i[0]] = value
return output