-
Notifications
You must be signed in to change notification settings - Fork 1
/
top_tables.py
executable file
·107 lines (94 loc) · 3.46 KB
/
top_tables.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
#!/usr/bin/env python
import csv, sys, cgitb, os, cgi
from deck_mysql import DeckDB
from printers import TextOutput, HTMLOutput
from login import check_login
from swisscalc import calculateTop8Threshold
output = None
def top_tables(tournament, form):
try:
with DeckDB() as db:
id = db.getEventId(tournament)
currentRound = db.get_round(id)
totalRounds = db.getEventRounds(id)
playersWithEachByes = db.getPlayersForEachByeNumber(id)
(currentMarginalThreshold, currentTop8Threshold, undefeatedThreshold) = calculateTop8Threshold(playersWithEachByes, totalRounds, currentRound)
output.printMessage("Players with at least %s points can still make top 8" % currentTop8Threshold)
tables = db.get_top_tables(id)
with output.table("Table", "Score", "Name", "Previous Checks", "Score", "Name", "Previous Checks") as table:
for row in tables:
try:
score = row[0]
tablenum = row[1]
(player1, player2) = db.get_table(id, tablenum)
(name1, score1, _, _, _) = player1
(name2, score2, _, _, _) = player2
prevChecks1 = db.getPreviousChecks(id, name1)
prevChecks2 = db.getPreviousChecks(id, name2)
if (score1 == undefeatedThreshold or score2 == undefeatedThreshold):
table.setNextRowType('undefeated')
elif (score1 < currentMarginalThreshold and score2 < currentMarginalThreshold):
table.setNextRowType('dead')
elif (score1 >= currentTop8Threshold or score2 >= currentTop8Threshold):
table.setNextRowType('live')
elif (score1 > currentMarginalThreshold or score2 > currentMarginalThreshold):
table.setNextRowType('marginal')
else:
table.setNextRowType('unlikely')
table.printRow(
output.makeLink(form, 'get_table?table=%s'%tablenum, tablenum),
score1,
output.makeLink(form, 'get_player?name=%s'%name1, name1),
", ".join([str(x) for x in prevChecks1]),
score2,
output.makeLink(form, 'get_player?name=%s'%name2, name2),
", ".join([str(x) for x in prevChecks2]))
except Exception as e:
print str(e)
except Exception as e:
output.printMessage("Failed to print top tables: %s" % (e))
def docgi():
print """Content-type: text/html
<html>
<head><title>Deck Checks - top tables</title><link rel='stylesheet' href='style.css' /></head>
<body>
<h1>Top tables</h1>
"""
form = cgi.FieldStorage()
with DeckDB() as db:
db.checkEvent(form["event"].value, output)
roundnum = db.get_round(db.getEventId(form["event"].value))
output.pageHeader(db, form['event'].value, roundnum, form)
if not check_login(output, form['event'].value, form['password'].value if 'password' in form else '', 'top_tables'):
return
print """
<p>Key:
<span class='undefeated'>undefeated</span>
<span class='live'>definitely live for top 8</span>
<span class='marginal'>possibility of top 8</span>
<span class='unlikely'>theoretically possible</span>
<span class='dead'>cannot top 8</span>
</p>
"""
top_tables(form["event"].value, form)
output.printLink(form, 'export?type=top', 'Download as TSV')
output.printLink(form, 'root', 'Return to menu')
print """
</body>
</html>
"""
def main(args):
with DeckDB() as db:
db.checkEvent(args[0], output)
top_tables(args[0], {})
if __name__ == "__main__":
if 'REQUEST_URI' in os.environ:
cgitb.enable()
output = HTMLOutput()
docgi()
else:
if len(sys.argv) < 2:
print "Usage: top_tables.py <event>"
sys.exit(1)
output = TextOutput()
main(sys.argv[1:])