-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathloldb.py
90 lines (63 loc) · 1.86 KB
/
loldb.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
import pickle
import ranking
import numpy
import collections
import datetime
_dbfile = 'foosdb.pickle'
_dbhandle = None
def _getdb():
global _dbhandle
if _dbhandle is None:
try:
_dbhandle = pickle.load(open(_dbfile))
except:
print "Unable to load database, creating new one"
_dbhandle = _newdb()
return _dbhandle
def _commitback():
if _dbhandle is None:
raise Exception("Handle is None?")
pickle.dump(_dbhandle, open(_dbfile, 'w'))
def _newdb():
return {'matches': {}}
def getrankings():
return ranking.getRankings(_getdb()['matches'].values())
def getmatches():
return _getdb()['matches'].values()
def getrecent(n=3):
return sorted(_getdb()['matches'].values(),
key=lambda x: x.when, reverse=True)[:n]
def getgamecounts():
matches = getmatches()
r = collections.defaultdict(int)
for m in matches:
for p in m.players1 + m.players2:
r[p] += 1
return r
def getlastgame(uid):
matches = getmatches()
times = []
for m in matches:
if uid in m.players1 + m.players2:
times.append(m)
return sorted(times, key=lambda x: x.when)[-1]
def getlastgameall():
matches = getmatches()
latest = collections.defaultdict(lambda: datetime.datetime(1900, 1, 1))
for m in matches:
for uid in m.players1 + m.players2:
if m.when > latest[uid]:
latest[uid] = m.when
return latest
def _newid():
return ''.join(numpy.random.choice(['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'], 8))
def addmatch(m):
mid = None
while mid is None or mid in _getdb()['matches']:
mid = _newid()
_getdb()['matches'][mid] = m
_commitback()
return mid
def deletematch(mid):
del _getdb()['matches'][mid]
_commitback()