This repository has been archived by the owner on May 30, 2024. It is now read-only.
forked from jav/pybotwar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stats.py
503 lines (428 loc) · 11.3 KB
/
stats.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
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
# Copyright 2009-2014 Lee Harr
#
# This file is part of pybotwar.
# http://pybotwar.googlecode.com/
#
# Pybotwar is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Pybotwar is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Pybotwar. If not, see <http://www.gnu.org/licenses/>.
import sqlite3
import os
import hashlib
import conf
import util
if __name__ == '__main__':
util.setup_conf()
dbversion = 5
dbversion_reset = dbversion
def fullpath():
if conf.dbfile.startswith('~'):
fname = os.path.expanduser(conf.dbfile)
else:
fname = os.path.join(conf.base_dir, conf.dbfile)
fname = os.path.abspath(fname)
head, tail = os.path.split(fname)
if not os.path.exists(head):
fname = '/:::NONEXISTANT:::'
return fname
def dbopen():
global dbversion
if dbversion == ':memory:':
return
global conn
fname = fullpath()
newdb = not os.path.exists(fname)
try:
conn = sqlite3.connect(fname)
if newdb:
print 'New stats database file started'
except sqlite3.OperationalError:
print 'Cannot open database file. Working in :memory:'
conn = sqlite3.connect(':memory:')
dbversion = ':memory:'
conn.row_factory = sqlite3.Row
global c
c = conn.cursor()
if dbversion == ':memory:' or newdb:
initialize()
def dbclose(restart=False):
global dbversion
if restart or dbversion != ':memory:':
conn.close()
if restart:
dbversion = dbversion_reset
def trywrite():
dbopen()
q = '''
UPDATE trywrite SET tw=1;
'''
try:
c.execute(q)
except (sqlite3.OperationalError, sqlite3.ProgrammingError):
return False
return True
def dbcheck():
fname = fullpath()
print 'Checking', fname
if fname != '/:::NONEXISTANT:::' and not os.path.exists(fname):
dbopen()
if not dbcheckver():
print 'ERROR: Database version mismatch.'
return False
if not trywrite():
print 'Cannot write to database. Working in :memory:'
global dbversion
global conn
global c
conn = sqlite3.connect(':memory:')
conn.row_factory = sqlite3.Row
c = conn.cursor()
dbversion = ':memory:'
initialize()
return True
def dbcheckver():
dbopen()
q = '''SELECT count(*)
FROM sqlite_master
WHERE type='table' AND
name='dbversion'
'''
c.execute(q)
r = c.fetchone()
if not r[0]:
# No dbversion table exists
retval = 0
else:
q = '''SELECT n
FROM dbversion'''
c.execute(q)
r = c.fetchone()
ver = r[0]
retval = ver == dbversion
dbclose()
return retval
def dbremove():
fname = fullpath()
if os.path.exists(fname):
print 'Removing', fname
os.remove(fname)
def initialize():
'Create empty database'
schemadef = '''\
CREATE TABLE dbversion (
n integer
);
CREATE TABLE robot_stats (
program_name text,
fingerprint text,
matches integer,
wins integer,
opponents integer,
outlasted integer,
damage_caused integer,
kills integer
);
CREATE TABLE tournament_stats (
tournament datetime,
program_name text,
fingerprint text,
matches integer,
wins integer,
opponents integer,
outlasted integer,
damage_caused integer,
kills integer
);
CREATE TABLE trywrite (
tw integer
);
'''
dbopen()
conn.executescript(schemadef)
conn.commit()
q = '''INSERT INTO dbversion
VALUES (:n)
'''
n = dbversion
c.execute(q, locals())
conn.commit()
print 'Stats database initialized'
if dbversion != ':memory:':
dbclose()
def fingerprint(name):
fname = '%s.py' % name
rdirs = util.get_robot_dirs()
for d in rdirs:
pth = os.path.join(d, fname)
if os.path.exists(pth):
break
m = hashlib.md5()
for line in file(pth):
m.update(line)
return m.hexdigest()
def robot_exists(name, fp):
q = '''\
SELECT *
FROM robot_stats
WHERE program_name=:name AND
fingerprint=:fp
'''
c.execute(q, locals())
r = c.fetchall()
return bool(r)
def add_robot(name, fp):
q = '''\
INSERT INTO robot_stats
(program_name,
fingerprint,
matches,
wins,
opponents,
outlasted,
damage_caused,
kills)
VALUES
(:name,
:fp,
0,
0,
0,
0,
0,
0)
'''
try:
c.execute(q, locals())
conn.commit()
except sqlite3.OperationalError:
conn.rollback()
def update(name, win, opponents, outlasted, damage_caused, kills):
fp = fingerprint(name)
if robot_exists(name, fp):
q = '''\
UPDATE robot_stats
SET matches = matches + 1,
wins = wins + :win,
opponents = opponents + :opponents,
outlasted = outlasted + :outlasted,
damage_caused = damage_caused + :damage_caused,
kills = kills + :kills
WHERE
program_name = :name AND
fingerprint = :fp
'''
win = int(win) # turn True/False in to 1/0
try:
c.execute(q, locals())
conn.commit()
except sqlite3.OperationalError:
conn.rollback()
else:
add_robot(name, fp)
update(name, win, opponents, outlasted, damage_caused, kills)
def tournament_robot_exists(tournament, name, fp):
q = '''\
SELECT *
FROM tournament_stats
WHERE tournament = :tournament AND
program_name = :name AND
fingerprint = :fp
'''
c.execute(q, locals())
r = c.fetchall()
return bool(r)
def add_tournament_robot(tournament, name, fp):
q = '''\
INSERT INTO tournament_stats
(tournament,
program_name,
fingerprint,
matches,
wins,
opponents,
outlasted,
damage_caused,
kills)
VALUES
(:tournament,
:name,
:fp,
0,
0,
0,
0,
0,
0)
'''
try:
c.execute(q, locals())
conn.commit()
except sqlite3.OperationalError:
conn.rollback()
def tournament_update(tournament, kind, name, win, opponents, outlasted,
damage_caused, kills):
fp = fingerprint(kind)
win = int(win) # turn True/False in to 1/0
if tournament_robot_exists(tournament, name, fp):
q = '''\
UPDATE tournament_stats
SET matches = matches + 1,
wins = wins + :win,
opponents = opponents + :opponents,
outlasted = outlasted + :outlasted,
damage_caused = damage_caused + :damage_caused,
kills = kills + :kills
WHERE
tournament = :tournament AND
program_name = :name AND
fingerprint = :fp
'''
try:
c.execute(q, locals())
conn.commit()
except sqlite3.OperationalError:
conn.rollback()
else:
add_tournament_robot(tournament, name, fp)
tournament_update(tournament, kind, name, win, opponents, outlasted, damage_caused, kills)
def tournament_results(tournament):
q = '''
SELECT *
FROM tournament_stats
WHERE tournament = :tournament
ORDER BY
wins DESC,
outlasted DESC
'''
c.execute(q, locals())
r = c.fetchall()
return r
def top10():
q = '''
SELECT
program_name,
fingerprint,
matches,
wins,
opponents,
outlasted,
damage_caused,
kills
FROM robot_stats
ORDER BY
wins DESC,
outlasted DESC
'''
c.execute(q, locals())
results = c.fetchall()
print 'Top 10 List:'
if dbversion == ':memory:':
fname = dbversion
else:
fname = fullpath()
print '(%s)' % fname
if not results:
print 'No stats yet'
return
er = list(enumerate(results[:10]))
len_name = max(len(l[0]) for n, l in er)
len_wins = max(len(str(l[3])) for n, l in er)
len_def = max(len(str(l[5])) for n, l in er)
len_dmg = max(len(str(l[6])) for n, l in er)
for n, line in er:
wpct = round(float(line[3]) / line[2], 3)
opct = round(float(line[5]) / line[4], 3)
print '{0:2} :: {1:{len_name}} : {2:{len_wins}} wins ({5:.3f}) : {3:{len_def}} defeated ({6:.3f}) : {4:{len_dmg}} dmg caused'.format(
n+1, line[0], line[3], line[5], line[6], wpct, opct,
len_name = len_name,
len_wins = len_wins,
len_def = len_def,
len_dmg = len_dmg)
def get_robot_stats(sort='wpct DESC'):
q = '''
SELECT
program_name,
fingerprint,
matches,
wins,
CAST(wins AS REAL)/matches AS wpct,
opponents,
outlasted,
CAST(outlasted AS REAL)/opponents AS opct,
damage_caused,
CAST(damage_caused AS REAL)/matches AS dmg_per_match,
CAST(damage_caused AS REAL)/opponents AS dmg_per_opponent,
kills
FROM robot_stats
ORDER BY %s
''' % sort
c.execute(q, locals())
results = c.fetchall()
return results
def get_tournament_stats(dt, sort='wpct DESC'):
q = '''
SELECT
program_name,
fingerprint,
matches,
wins,
CAST(wins AS REAL)/matches AS wpct,
opponents,
outlasted,
CAST(outlasted AS REAL)/opponents AS opct,
damage_caused,
CAST(damage_caused AS REAL)/matches AS dmg_per_match,
CAST(damage_caused AS REAL)/opponents AS dmg_per_opponent,
kills
FROM tournament_stats
WHERE tournament=:dt
ORDER BY %s
''' % sort
c.execute(q, locals())
results = c.fetchall()
return results
def tournaments():
q = '''
SELECT
tournament,
program_name,
matches
FROM
tournament_stats
ORDER BY
tournament DESC
'''
c.execute(q)
results = c.fetchall()
rlist = []
rdict = {}
cdict = {}
for t, r, m in results:
if t not in rlist:
rlist.append(t)
if t not in rdict:
rdict[t] = dict(robots=[r], matches=[m])
cdict[t] = set((m,))
else:
rdict[t]['robots'].append(r)
cdict[t].add(m)
for t, s in cdict.items():
rdict[t]['robots'].sort()
if len(s) == 1:
rdict[t]['complete'] = True
rdict[t]['matches'] = s.pop()
else:
rdict[t]['complete'] = False
return rlist, rdict
if __name__ == '__main__':
dbopen()
top10()