-
Notifications
You must be signed in to change notification settings - Fork 2
/
admin.py
executable file
·152 lines (117 loc) · 5.03 KB
/
admin.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
#!/usr/bin/python
import os
import sys
import time
import random
#import glicko
#import DbText
import DbSqlite
if __name__ == "__main__":
#db = DbText.DbText()
db = DbSqlite.DbSqlite()
if len(sys.argv) <= 1:
print "send arguments!"
exit(-1)
if sys.argv[1] == "DbText2DbSqlite":
db = DbText.DbText();
db2 = DbSqlite.DbSqlite();
db2.clear()
# add all players
for p in db.getPlayerList():
db2.addPlayer(p, db.getPlayerRating(p), db.getPlayerRD(p), db.getPlayerT(p))
# add all games
for g in db.getGames():
db2.recordGame(g)
if sys.argv[1] == "offsetGameTime":
if len(sys.argv) != 3:
print 'admin.py offsetGameTime <offset>'
exit(-1)
db.offsetGameTime(sys.argv[2])
if sys.argv[1] == "recalculate":
players = db.getPlayerList()
games = db.getGames()
db.init()
db.clear()
for p in players:
db.addPlayer(p)
for g in games:
ratings = map(lambda x: db.getPlayerRating(x), [g['a1'], g['a2'], g['b1'], g['b2']])
rds = map(lambda x: db.getPlayerRD(x), [g['a1'], g['a2'], g['b1'], g['b2']])
rps = map(lambda x: glicko.secToRatingPeriods(g['t'] - db.getPlayerT(x)), [g['a1'], g['a2'], g['b1'], g['b2']])
# log game
data = {
't':g['t'],
'a1':g['a1'], 'a1_r':ratings[0], 'a1_rd':rds[0],
'a2':g['a2'], 'a2_r':ratings[1], 'a2_rd':rds[1],
'b1':g['b1'], 'b1_r':ratings[2], 'b1_rd':rds[2],
'b2':g['b2'], 'b2_r':ratings[3], 'b2_rd':rds[3]
}
print data
db.recordGame(data)
# compute new scores
[stats1, stats2, stats3, stats4] = glicko.calcGameScores(ratings, rds, rps)
# store new scores
db.setPlayerStats(g['a1'], stats1 + [g['t']])
db.setPlayerStats(g['a2'], stats2 + [g['t']])
db.setPlayerStats(g['b1'], stats3 + [g['t']])
db.setPlayerStats(g['b2'], stats4 + [g['t']])
if sys.argv[1] == "dumpgames":
games = db.getGames(0)
for g in games:
print g
if sys.argv[1] == "seeddb" or sys.argv[1] == "dbseed":
# virtual time point one year ago
tnow = time.time() - 365*24*3600
#######################################################################
# new players
#######################################################################
print "initializating database with random players"
db.init()
db.clear()
players = ["George Washington", "John Adams", "Thomas Jefferson",
"James Madison", "James Monroe", "John Quincy Adams",
"Andrew Jackson"];
#ratings = map(lambda x: random.randint(500,1500), range(len(players)))
ratings = [1000]*len(players)
#rds = map(lambda x: 350, range(len(players)))
rds = [350]*len(players)
for i,p in enumerate(players):
print "%s %d.%d (last game: %s))" % \
(p, ratings[i], rds[i], time.strftime("%a, %d %b %y %H:%M:%S", time.localtime(0)))
db.setPlayerStats(p, [ratings[i], rds[i], tnow])
#######################################################################
# new games
#######################################################################
players = db.getPlayerList()
print "playing 1000 random games"
for i in range(1000):
# scramble the players
for j in range(len(players)):
k = random.randint(0, len(players)-1)
l = random.randint(0, len(players)-1)
temp = players[k];
players[k] = players[l]
players[l] = temp
# players are at front of list
[a1,a2,b1,b2] = players[0:4]
print "%s,%s > %s,%s\n" % (a1, a2, b1, b2)
ratings = map(lambda x: db.getPlayerRating(x), [a1,a2,b1,b2])
rds = map(lambda x: db.getPlayerRD(x), [a1,a2,b1,b2])
rps = map(lambda x: glicko.secToRatingPeriods(tnow - db.getPlayerT(x)), [a1,a2,b1,b2])
# log game
db.recordGame({
't': tnow,
'a1':a1, 'a1_r':ratings[0], 'a1_rd':rds[0],
'a2':a2, 'a2_r':ratings[1], 'a2_rd':rds[1],
'b1':b1, 'b1_r':ratings[2], 'b1_rd':rds[2],
'b2':b2, 'b2_r':ratings[3], 'b2_rd':rds[3]
})
# compute new scores
[stats1, stats2, stats3, stats4] = glicko.calcGameScores(ratings, rds, rps)
# store new scores
db.setPlayerStats(a1, stats1 + [tnow])
db.setPlayerStats(a2, stats2 + [tnow])
db.setPlayerStats(b1, stats3 + [tnow])
db.setPlayerStats(b2, stats4 + [tnow])
# advance virtual time by at most an hour
tnow += random.randint(1, 3600)