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
222
223
224
225
226
227
228
229
230
231
232
233
|
#!/usr/bin/env pypy3
import sys, re
from datetime import datetime
def parse_realtime(text):
try:
date = datetime.strptime(text, "%Y/%m/%d %H:%M:%S")
except ValueError:
date = datetime.strptime(text, "%Y-%m-%d %H:%M:%S")
return date.timestamp()
def parse_gametime(text):
parts = text.split(":");
return int(parts[0]) * 60 + int(parts[1])
class StateTracker:
def __init__(self):
self.hist = list()
self.time_ref = None
self.time = None
self.time_last = None
self.pcount = None
self.pcount_last = None
self.pings = list()
self.hist_pings = list()
def update(self):
if self.time_last and self.time > self.time_last:
self.hist.append((self.time_last, self.pcount_last))
self.time_last = self.time
self.pcount_last = self.pcount
def finish(self):
if self.time != self.time_last or \
self.pcount != self.pcount_last:
self.hist.append((self.time, self.pcount))
if len(self.pings):
self.hist_pings.append((self.time_ref, self.pings))
self.pings = list()
def ev_begin(self, realtime):
self.time_ref = parse_realtime(realtime)
self.time = self.time_ref
self.pcount = 0
self.update()
if len(self.pings):
self.hist_pings.append((self.time_ref, self.pings))
self.pings = list()
def ev_connect(self, gametime):
self.time = self.time_ref + parse_gametime(gametime)
self.pcount += 1
self.update()
if self.pcount > 64:
raise ValueError("too many players")
def ev_disconnect(self, gametime):
self.time = self.time_ref + parse_gametime(gametime)
self.pcount -= 1
self.update()
def ev_endgame_stat(self, gametime, score, ping):
if score == "0":
return
if ping == "999":
return
game_length = parse_gametime(gametime)
self.pings.append((int(ping), game_length))
class WeightedMean:
def __init__(self):
self.total = 0
self.weights = 0
def feed(self, sample, weight):
self.total += sample * weight;
self.weights += weight
def read(self):
if self.weights != 0:
return self.total / self.weights
else:
return 0
class Day:
def __init__(self, date):
self.date = date
self.pcount_sum = 0
self.pcount_time = 0
self.pcount_peak = 0
self.pings = list()
def avg_pcount(self):
return self.pcount_sum / self.pcount_time
def peak_pcount(self):
return self.pcount_peak
def ping_stats(self):
mean = WeightedMean()
above_60 = 0
above_110 = 0
above_160 = 0
above_210 = 0
above_260 = 0
for ping in self.pings:
mean.feed(ping[0], ping[1]);
if ping[0] > 60:
above_60 += ping[1]
if ping[0] > 110:
above_110 += ping[1]
if ping[0] > 160:
above_160 += ping[1]
if ping[0] > 210:
above_210 += ping[1]
if ping[0] > 260:
above_260 += ping[1]
if len(self.pings):
above_60 /= mean.weights
above_110 /= mean.weights
above_160 /= mean.weights
above_210 /= mean.weights
above_260 /= mean.weights
return "%f %f%% %f%% %f%% %f%% %f" % (mean.read(), above_60, \
above_110, above_160, above_210, above_260)
class Analyzer:
def __init__(self):
self.time_last = None
self.pcount_last = None
self.days = dict()
def feed(self, time, pcount):
if self.time_last == None:
self.time_last = time
self.pcount_last = pcount
return
date = datetime.fromtimestamp(time).date()
if date not in self.days:
self.days[date] = Day(date)
delta = time - self.time_last
self.days[date].pcount_sum += delta * self.pcount_last
self.days[date].pcount_time += delta
if pcount > self.days[date].pcount_peak:
self.days[date].pcount_peak = pcount
self.time_last = time
self.pcount_last = pcount
def feed_pings(self, time, pings):
date = datetime.fromtimestamp(time).date()
if date not in self.days:
self.days[date] = Day(date)
self.days[date].pings += pings
def finish(self):
for date, day in self.days.items():
if day.pcount_time < 80000:
continue
print("%s %f %s %d" % (date, day.avg_pcount(), \
day.ping_stats(), day.peak_pcount()))
pass
def decoder(raw):
return raw.decode("ISO-8859-1")
def main():
state = StateTracker()
re_realtime = re.compile("^\s*\d+:\d\d RealTime: (.*)$")
re_connect = re.compile("^\s*(\d+:\d\d) ClientConnect:")
re_disconnect = re.compile("^\s*(\d+:\d\d) ClientDisconnect:")
re_endgame_stat = re.compile("^\s*(\d+:\d\d) score: (-?[0-9]+) ping: ([0-9]+)")
for (i, line) in enumerate(map(decoder, sys.stdin.buffer)):
try:
if "RealTime" in line:
rv = re.search(re_realtime, line)
if rv:
state.ev_begin(rv.group(1))
continue
elif "ClientConnect" in line:
rv = re.search(re_connect, line)
if rv:
state.ev_connect(rv.group(1))
continue
elif "ClientDisconnect" in line:
rv = re.search(re_disconnect, line)
if rv:
state.ev_disconnect(rv.group(1))
continue
elif "score:" in line:
rv = re.search(re_endgame_stat, line)
if rv:
state.ev_endgame_stat(rv.group(1), \
rv.group(2), \
rv.group(3))
continue
except:
print("ERROR on line %d:" % (i + 1))
raise
state.finish()
analyzer = Analyzer()
for (time, count) in state.hist:
analyzer.feed(time, count)
for (time, pings) in state.hist_pings:
analyzer.feed_pings(time, pings)
analyzer.finish()
main()
|