-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstats.py
More file actions
139 lines (131 loc) · 4.41 KB
/
Copy pathstats.py
File metadata and controls
139 lines (131 loc) · 4.41 KB
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
import DmoParser, os, maputils, sqlite3, sys
def processFile(filename):
players = {}
mapname = ""
gamelimit = 0
mode = 0
acdr = DmoParser.AssaultCubeDmoReader()
welcome = None
time = 0
for r in acdr.consumeFile(filename):
if r['type'] == 'welcome':
#print r
mapname = r['mapname']
mode = r['smode']
gamelimit = r['gamelimit']
for cn, p in r['playernames'].items():
players[cn] = {'name': p['cname']}
players[cn].update(r['players'][cn])
players[cn]['numshots'] = 0
players[cn]['nkills'] = 0
players[cn]['nreloads'] = 0
players[cn]['npickups'] = 0
#print players[cn]
welcome = r
pass
elif r['type'] == 'newplayer':
r['numshots'] = 0
r['nkills'] = 0
r['nreloads'] = 0
r['points'] = 0
r['npickups'] = 0
players[r['cn']] = r
elif r['type'] == 'position':
if r['shooting'] == 1:
players[r['cn']]['numshots'] += 1
if r['gametime'] > time:
time = r['gametime']
elif r['type'] == 'damage':
if r['killed']:
players[r['shooter']]['nkills'] += 1
elif r['type'] == 'reload':
players[r['cn']]['nreloads'] += 1
elif r['type'] == 'weapon_change':
#players[r['client_id']][
pass
elif r['type'] == 'points':
for cn,pnts in r['points'].items():
players[cn]['points'] += pnts
elif r['type'] == 'itemacc':
players[r['cn']]['npickups'] += 1
pass
elif r['type'] == 'itemspawn':
# Ignore
pass
else:
#print(r)
pass
pass
print(welcome['mapname'],welcome['smode'])
for cn,p in players.items():
print(p['name'], p['numshots'], p['nkills'], p['nreloads'], p['points'])
welcome['game_length'] = time
return welcome, players
def tolit(v):
try:
return int(v)
except:
return "'%s'"%v
def dmoFileToSqlite(filename, gameid, cursor, game_fields, player_fields):
try:
print()
print(filename)
welcome, players = processFile(filename)
except KeyboardInterrupt:
return 1
except Exception as e:
print(filename)
#raise e
return 2
gfs = []
for gf in game_fields:
gfs.append(tolit(welcome[gf]))
gfs = list(map(lambda g: str(g), gfs))
pfs = []
for i in players.keys():
player = []
for pf in player_fields:
player.append(tolit(players[i][pf]))
player = map(lambda p: str(p), player)
q = "INSERT INTO games VALUES ('%s', %s, %s, %s)"%(gameid, ",".join(gfs), i, ",".join(player))
#print(q)
cursor.execute(q)
return 0
def loadDemosToSqlite(demo_directory='demos', dbname=':memory:'):
conn = sqlite3.connect(dbname)
c = conn.cursor()
try:
c.execute('''CREATE TABLE games (gameid text, mapname text, smode int, gamelimit int, duration int, playerid int, playername text, nkills int, npickups int, numshots int, nreloads int)''')
except:
pass
fails = []
nfails = 0
ndemos = 0
for fname in os.listdir(demo_directory):
if not fname.endswith(".dmo"):
continue
ndemos += 1
c.execute('''DELETE FROM games WHERE gameid = "%s"'''%fname)
ret = dmoFileToSqlite('%s/%s'%(demo_directory,fname), fname, c, ['mapname', 'smode', 'gamelimit', 'game_length'], ['name', 'nkills', 'npickups', 'numshots', 'nreloads'])
if ret == 1:
break
elif ret == 2:
fails.append(fname)
nfails += 1
try:
c.execute("drop table gamestats")
except:
pass
c.execute('''create table gamestats as select gameid,mapname,smode,gamelimit,duration,sum(nkills)/count(1) as nkills,sum(numshots)/count(1) as numshots,count(1) as nplayers from games group by gameid;''')
print("Failed %s times out of %s"%(nfails, ndemos))
for f in fails:
print(f)
return conn
if __name__ == '__main__':
import sys
if len(sys.argv) > 1:
conn = loadDemosToSqlite(sys.argv[1], 'test.db')
conn.commit()
conn.close()
else:
print("You must pass a directory full of demo files")