summaryrefslogtreecommitdiffstats
path: root/gamestats/sauerstats
blob: 8772f8ff3c7a55a11068d3e983746c03ec5d42ac (plain)
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
#!/usr/bin/env python3
"""
# sauerstats.py
# Copyright (c) 2010-2011 Michael Buesch <m@bues.ch>
# Licensed under the GNU/GPL version 2 or later.
"""

import sys
from gamestats import *


class SauerbratenParser(Parser):
	_re_name = r'(.*)' # Player/item name

	re_init = re.compile(r'^init: .*$')
	re_waypoints = re.compile(r'^loaded (\d+) waypoints from ' + _re_name + r'$')
	re_intermission = re.compile(r'^intermission:$')
	re_info = re.compile(r'^Info: .*$')
	re_rendering = re.compile(r'^Rendering using .*$')

	re_game_mode = re.compile(r'^game mode is ' + _re_name + r'$')
	re_game_ended = re.compile(r'^game has ended!$')

	re_you_disconn = re.compile(r'^disconnected$')
	re_player_disconn = re.compile(r'^player ' + _re_name + r' disconnected$')
	re_conn_attempt = re.compile(r'^attempting to connect.*$')
	re_connected = re.compile(r'^connected to server$')
	re_player_connected = re.compile(r'^connected: ' + _re_name + r'$')
	re_connected_from = re.compile(r'^' + _re_name + r' connected from ' +\
				       _re_name + r'$')
	re_connected_from2 = re.compile(r'^' + _re_name + r' is fragging in ' +\
					_re_name + r'$')
	re_disconn_attempt = re.compile(r'^attempting to disconnect\.\.\.$')
	re_rename = re.compile(r'^' + _re_name + r' is now known as ' + _re_name + r'$')

	re_read_map = re.compile(r'^read map (.*) \(\d+\.\d+ seconds\)$')
	re_suggest = re.compile(r'^' + _re_name + r' suggests ' + _re_name + r' on map ' +
				_re_name + r' \(select map to vote\)$')

	re_you_got_fragged = re.compile(r'^you got fragged by ' + _re_name + r'$')
	re_fragged = re.compile(r'^' + _re_name + r' fragged ' + _re_name + r'$')
	re_fragged_teammate = re.compile(r'^' + _re_name + r' fragged a teammate \(' +\
					 _re_name + r'\)$')
	re_yougotkilledby_teammate = re.compile(r'^you got fragged by a teammate \(' +\
						_re_name + r'\)$')
	re_suicide = re.compile(r'^' + _re_name + r' suicided!?$')

	re_dropped_your = re.compile(r'^' + _re_name + r' dropped your flag$')
	re_dropped_enemy = re.compile(r'^' + _re_name + r' dropped the enemy flag$')
	re_stole_your = re.compile(r'^' + _re_name + r' stole your flag$')
	re_stole_enemy = re.compile(r'^' + _re_name + r' stole the enemy flag$')
	re_picked_your = re.compile(r'^' + _re_name + r' picked up your flag$')
	re_picked_enemy = re.compile(r'^' + _re_name + r' picked up the enemy flag$')
	re_score_your = re.compile(r'^' + _re_name + r' scored for your team$')
	re_score_enemy = re.compile(r'^' + _re_name + r' scored for the enemy team$')
	re_returned_your = re.compile(r'^' + _re_name + r' returned your flag$')
	re_returned_enemy = re.compile(r'^' + _re_name + r' returned the enemy flag$')
	re_stopped_by = re.compile(r'^' + _re_name + r' was stopped by ' + _re_name + r'$')

	re_rampage = re.compile(r'^' + _re_name + r' is on a RAMPAGE!!$')
	re_dominate = re.compile(r'^' + _re_name + r' is DOMINATING!!$')
	re_triplekill = re.compile(r'^' + _re_name + r' scored a TRIPLE KILL!$')

	re_msg = re.compile(r'^' + _re_name + r': .*$')
	# chat annotation patch
	re_chat = re.compile(r'^chat-message: ' + _re_name + r':\s*(.*)$')
	re_teamchat = re.compile(r'^teamchat-message: ' + _re_name + r':\s*(.*)$')

	def __init__(self, options):
		Parser.__init__(self, options)

		self.lastReadMap = "unknown"

	def __parseKill(self, timestamp, name0, name1, tk=False):
		# name0 fragged name1
		if not self.assertCurrentGame("kill"):
			return
		player0 = self.currentGame.playerOrMe(name0)
		player1 = self.currentGame.playerOrMe(name1)
		if tk:
			player0.addTeamkill(timestamp, player1)
		else:
			player0.addFrag(timestamp, player1)
		self.currentGame.hadFirstBlood = True

	def doParseLine(self, stamp, line):
		line = line.strip()
		m = self.re_chat.match(line)
		if m:
			debugMsg("Chat (%s: %s)" % (m.group(1), m.group(2)))
			return
		m = self.re_teamchat.match(line)
		if m:
			debugMsg("Teamchat (%s: %s)" % (m.group(1), m.group(2)))
			return
		m = self.re_info.match(line)
		if m:
			debugMsg("Info (%s)" % line)
			return
		m = self.re_game_ended.match(line)
		if m:
			if not self.assertCurrentGame("game ended"):
				return
			debugMsg("Game ended (%s)" % line)
			if self.currentGame.ended:
				print("Current game ended twice?!")
				return
			self.currentGame.ended = stamp
			self.gameEnded()
			return
		m = self.re_game_mode.match(line)
		if m:
			self.currentGame = None
			debugMsg("Game mode (%s)" % line)
			newGame = Game(options=self.options,
				       timestamp=stamp, mode=m.group(1),
				       mapname=self.lastReadMap,
				       selfIDs=("you",))
			self.currentGame = newGame
			self.games.append(newGame)
			return
		m = self.re_yougotkilledby_teammate.match(line)
		if m:
			debugMsg("TK 1 (%s)" % line)
			self.__parseKill(stamp, m.group(1), None, tk=True)
			return
		m = self.re_fragged_teammate.match(line)
		if m:
			debugMsg("TK 2 (%s)" % line)
			self.__parseKill(stamp, m.group(1), m.group(2), tk=True)
			return
		m = self.re_you_got_fragged.match(line)
		if m:
			debugMsg("Frag 1 (%s)" % line)
			self.__parseKill(stamp, m.group(1), None)
			return
		m = self.re_fragged.match(line)
		if m:
			debugMsg("Frag 2 (%s)" % line)
			self.__parseKill(stamp, m.group(1), m.group(2))
			return
		m = self.re_suicide.match(line)
		if m:
			debugMsg("Suicide (%s)" % line)
			if not self.assertCurrentGame("suicide"):
				return
			player = self.currentGame.playerOrMe(m.group(1))
			player.addSuicide(stamp)
			return
		m = self.re_dropped_your.match(line)
		if m:
			debugMsg("Dropped your flag (%s)" % line)
			if not self.assertCurrentGame("dropped-your"):
				return
			player = self.currentGame.player(m.group(1))
			player.addCtfDrop(stamp)
			return
		m = self.re_dropped_enemy.match(line)
		if m:
			debugMsg("Dropped enemy flag (%s)" % line)
			if not self.assertCurrentGame("dropped-enemy"):
				return
			player = self.currentGame.playerOrMe(m.group(1))
			player.addCtfDrop(stamp)
			return
		m = self.re_stole_your.match(line)
		if m:
			debugMsg("Stole your flag (%s)" % line)
			if not self.assertCurrentGame("stole-your"):
				return
			player = self.currentGame.player(m.group(1))
			player.addCtfSteal(stamp)
			return
		m = self.re_stole_enemy.match(line)
		if m:
			debugMsg("Stole enemy flag (%s)" % line)
			if not self.assertCurrentGame("stole-enemy"):
				return
			player = self.currentGame.playerOrMe(m.group(1))
			player.addCtfSteal(stamp)
			return
		m = self.re_picked_your.match(line)
		if m:
			debugMsg("Picked your flag (%s)" % line)
			if not self.assertCurrentGame("picked-your"):
				return
			player = self.currentGame.player(m.group(1))
			player.addCtfPick(stamp)
			return
		m = self.re_picked_enemy.match(line)
		if m:
			debugMsg("Picked enemy flag (%s)" % line)
			if not self.assertCurrentGame("picked-enemy"):
				return
			player = self.currentGame.playerOrMe(m.group(1))
			player.addCtfPick(stamp)
			return
		m = self.re_score_your.match(line)
		if m:
			debugMsg("Scored for your team (%s)" % line)
			if not self.assertCurrentGame("scored-your"):
				return
			player = self.currentGame.playerOrMe(m.group(1))
			player.addCtfScore(stamp)
			return
		m = self.re_score_enemy.match(line)
		if m:
			debugMsg("Scored for enemy team (%s)" % line)
			if not self.assertCurrentGame("scored-enemy"):
				return
			player = self.currentGame.player(m.group(1))
			player.addCtfScore(stamp)
			return
		m = self.re_returned_your.match(line)
		if m:
			debugMsg("Returned your flag (%s)" % line)
			if not self.assertCurrentGame("returned-your"):
				return
			player = self.currentGame.playerOrMe(m.group(1))
			player.addCtfReturn(stamp)
			return
		m = self.re_returned_enemy.match(line)
		if m:
			debugMsg("Returned enemy flag (%s)" % line)
			if not self.assertCurrentGame("returned-enemy"):
				return
			player = self.currentGame.player(m.group(1))
			player.addCtfReturn(stamp)
			return
		m = self.re_stopped_by.match(line)
		if m:
			debugMsg("Stopped by (%s)" % line)
			return
		m = self.re_rampage.match(line)
		if m:
			debugMsg("Rampage (%s)" % line)
			return
		m = self.re_dominate.match(line)
		if m:
			debugMsg("Dominate (%s)" % line)
			return
		m = self.re_triplekill.match(line)
		if m:
			debugMsg("Triplekill (%s)" % line)
			return
		m = self.re_suggest.match(line)
		if m:
			debugMsg("Suggest (%s)" % line)
			return
		m = self.re_player_connected.match(line)
		if m:
			debugMsg("Player connected (%s)" % line)
			if not self.assertCurrentGame("player connected"):
				return
			name = m.group(1)
			self.currentGame.player(name).connected()
			return
		m = self.re_player_disconn.match(line)
		if m:
			debugMsg("Player disconnected (%s)" % line)
			if not self.assertCurrentGame("player connected"):
				return
			name = m.group(1)
			self.currentGame.player(name).disconnected()
			return
		m = self.re_conn_attempt.match(line)
		if m:
			debugMsg("Connection attempt (%s)" % line)
			return
		m = self.re_connected.match(line)
		if m:
			debugMsg("Connected (%s)" % line)
			return
		m = self.re_connected_from.match(line)
		if m:
			debugMsg("Connected from (%s)" % line)
			return
		m = self.re_connected_from2.match(line)
		if m:
			debugMsg("Connected from (%s)" % line)
			return
		m = self.re_disconn_attempt.match(line)
		if m:
			debugMsg("Disconnect attempt (%s)" % line)
			return
		m = self.re_you_disconn.match(line)
		if m:
			debugMsg("Disconnected (%s)" % line)
			return
		m = self.re_rename.match(line)
		if m:
			debugMsg("Player rename (%s)" % line)
			if not self.assertCurrentGame("player rename"):
				return
			oldName = m.group(1)
			newName = m.group(2)
			self.currentGame.player(oldName).rename(newName)
			return
		m = self.re_read_map.match(line)
		if m:
			if self.options.splitLogs:
				closeRawLog()
			debugMsg("Map (%s)" % line)
			mapname = m.group(1)
			mapname = mapname.split('/')[-1]
			if mapname.endswith(".ogz"):
				mapname = mapname[0:-4]
			self.lastReadMap = mapname
			return
		m = self.re_rendering.match(line)
		if m:
			debugMsg("Renderer (%s)" % line)
			return
		m = self.re_init.match(line)
		if m:
			debugMsg("Init (%s)" % line)
			return
		m = self.re_waypoints.match(line)
		if m:
			debugMsg("Waypoints (%s)" % line)
			return
		m = self.re_intermission.match(line)
		if m:
			debugMsg("Intermission (%s)" % line)
			return
		m = self.re_msg.match(line)
		if m:
			debugMsg("Message (%s)" % line)
			return
		if not line.replace("*", "") or\
		   not line.replace(">", ""):
			debugMsg("Spacer (%s)" % line)
			return
		debugMsg("UNKNOWN console message: '%s'" % line)

def main():
	return genericMain(scriptname="sauerstats",
			   usageinfo=" Example: Convert and log stats into directory:\n"
				     "  sauerbraten_unix | sauerstats -n mynick -l ./logs\n"
				     "\n"
				     " Example: Create stats from logfile:\n"
				     "  sauerstats -n mynick ./logs/2011....log",
			   parserClass=SauerbratenParser)

if __name__ == "__main__":
	sys.exit(main())
bues.ch cgit interface