-
Notifications
You must be signed in to change notification settings - Fork 0
/
roster_check.py
executable file
·202 lines (172 loc) · 5.66 KB
/
roster_check.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
#!/usr/bin/env python
import os
import sys
import psycopg2
import yaml
import DB
import injreport as IR
import startsdb
ros_max = 36 # max players on roster
ros_lim = 26 # active player limit
ros_exp = 22 # first week where all players can be active
# dump environment and parameters for testing
# not really necessary, mostly for learning purposes
def dumpenv(form):
for (env, val) in list(os.environ.items()):
print("<br>", env + " : ", val)
print("<p>parameters")
for param in list(form.keys()):
print("<br>", param + " : ", end=' ')
for val in form.getlist(param):
print(val, end=' ')
print()
print("<p>")
return
def main():
do_json = False
do_header = True
is_cgi = False
if 'GATEWAY_INTERFACE' in os.environ:
import cgi
#import cgitb; cgitb.enable()
form = cgi.FieldStorage()
is_cgi = True
if 'noheader' in form:
do_header = False
if 'json' in form:
import json
do_json = True
j={}
print("Content-Type: application/json")
print()
else:
do_json = False
print("Content-Type: text/html")
print()
if do_header:
print("<html><head><title>Legal Roster check</title></head><body>")
#dumpenv(form)
db = DB.connect()
cursor = db.cursor()
week = 1
if len(sys.argv) > 1:
week = int(sys.argv[1])
elif is_cgi and 'week' in form:
week = int(form.getfirst('week'))
else:
# no user input so we'll find latest week with reported results
cursor.execute("select week, count(*) from games\
group by week order by week desc limit 1;");
if cursor.rowcount > 0:
(week, num) = cursor.fetchone()
# and use first week afterward
week += 1
# current UC designation
sql = "select max(uncarded) from rosters;"
cursor.execute(sql)
( UCyy, ) = cursor.fetchone()
# skip UC in roster count?
ignore_uc = False
starts = {}
startsdb.main( starts, module=True )
inj = {}
IR.main( inj, module=True )
# teams/status
active = 1
inactive = 2
# item type
pitcher = 1
batter = 2
# minimum SP for legal roster
sp_min = 4
if week in IR.LONG_SERIES:
sp_min += 1
def ok( week ):
for series in list(week.keys()):
out = [z & (IR.off + IR.inj + IR.sus) for z in week[series]]
if not [y for y in out if y == 0]:
return False
return True
sql_count = "select status, count(*) from rosters \
where ibl_team = (%s) and item_type != 0 and uncarded < (%s) \
group by status;"
sql_ros = "select trim(tig_name), item_type from rosters \
where ibl_team = (%s) and item_type != 0 and status = 1 \
and uncarded < (%s);"
if not do_json:
if is_cgi:
print("<pre>")
print("WEEK %-2s pit/bat # 1 2 3 4 5 6 7 8 9" % week)
cursor.execute("select distinct(ibl_team) from rosters \
where ibl_team != 'FA' order by ibl_team;")
for (ibl, ) in cursor.fetchall():
ros = {}
if ignore_uc:
cursor.execute( sql_count, (ibl, UCyy ) )
else:
cursor.execute( sql_count, (ibl, UCyy + 1 ) )
for status, count in cursor.fetchall():
ros[status] = count
legal = []
for pos in range(10):
legal.append(0)
batters = 0
pitchers = 0
cursor.execute( sql_ros, (ibl, UCyy ) )
for player, kind in cursor.fetchall():
#print player, starts[player]
if kind & pitcher == pitcher:
pitchers += 1
if kind & batter == batter:
batters += 1
# check if player has appearances left
try:
if starts[player][0] <= 0:
continue
except KeyError:
continue
# check if player is out for a series
if player in inj:
if week in inj[player]:
if not ok( inj[player][week] ):
#print "inj: %s" % player
continue
for pos in range(10):
try:
if starts[player][pos] > 0:
legal[pos] += 1
except KeyError:
pass
if sum(ros.values()) <= ros_max \
and ( ros[active] <= ros_lim or \
ros[active] <= ros_max and week >= ros_exp ) \
and legal[1] >= sp_min \
and len( [z for z in legal[2:] if z >= 2] ) == 8:
status = "LEGAL"
else:
status = "ILLEGAL"
if do_json:
j[ibl] = { 'total': sum(ros.values()),\
'active': ros[active],\
'starts': legal,\
'status': status
}
else:
print(ibl, end=' ')
print("total %s" % sum(ros.values()), end=' ')
print("active %s" % ros[active], end=' ')
print("(%2d/%2d)" % ( pitchers, batters ), end=' ')
print("starts (", end=' ')
for pos in range(10):
print("%2s" % legal[pos], end=' ')
print(")", end=' ')
print(status)
if do_json:
print( json.dumps(j) )
else:
if is_cgi:
print("</pre>")
if do_header:
print("</body></html>")
if __name__ == "__main__":
main()