-
Notifications
You must be signed in to change notification settings - Fork 0
/
format_utils.py
264 lines (238 loc) · 10.5 KB
/
format_utils.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
import html_utils
import utils
def format_subject(league,week):
subject = 'Power Rankings: Week ' \
+ str(week) \
+ ' - ' \
+ str(league.settings.name)
return subject
def format_league_info(league,week):
lines = list()
top_team = league.top_team_week(week)
lines.append('<strong>Highest scoring team this week:</strong> ' \
+ str(top_team.scores[week - 1]) \
+ ' points - ' \
+ top_team.team_name)
bottom_team = league.bottom_team_week(week)
lines.append('<strong>Lowest scoring team this week:</strong> ' \
+ str(bottom_team.scores[week - 1]) \
+ ' points - ' \
+ bottom_team.team_name)
top_player = league.top_player_week(week)
lines.append('<strong>Highest scoring player this week:</strong> ' \
+ str(top_player.points) + ' points - ' \
+ top_player.name \
+ ' (' \
+ top_player.slot_position \
+ ' - ' \
+ top_player.team.team_name \
+ ')')
bottom_player = league.bottom_player_week(week)
lines.append('<strong>Lowest scoring player this week:</strong> ' \
+ str(bottom_player.points) \
+ ' points - ' \
+ bottom_player.name \
+ ' (' \
+ bottom_player.slot_position \
+ ' - ' \
+ bottom_player.team.team_name \
+ ')')
overachiever = league.overachiever(week)
over_diff = '+' + str(overachiever.diff) if overachiever.diff > 0 else overachiever.diff
lines.append('<strong>Biggest overachiever:</strong> ' \
+ str(over_diff) \
+ ' (' \
+ str(overachiever.points) \
+ ' points - ' \
+ str(overachiever.projected_points) \
+ ' projected) - ' \
+ overachiever.name \
+ ' (' \
+ overachiever.slot_position \
+ ' - ' \
+ overachiever.team.team_name \
+ ')')
underachiever = league.underachiever(week)
under_diff = '+' + str(underachiever.diff) if underachiever.diff > 0 else underachiever.diff
lines.append('<strong>Worst underachiever:</strong> ' \
+ str(under_diff) \
+ ' (' \
+ str(underachiever.points) \
+ ' points - ' \
+ str(underachiever.projected_points) \
+ ' projected) - ' \
+ underachiever.name \
+ ' (' \
+ underachiever.slot_position \
+ ' - ' \
+ underachiever.team.team_name \
+ ')')
large_mov = league.largest_mov(week)
lines.append('<strong>Biggest blowout:</strong> ' \
+ str(large_mov['mov']) \
+ ' points - ' \
+ large_mov['winner'].team_name \
+ ' vs ' \
+ large_mov['loser'].team_name)
small_mov = league.smallest_mov(week)
lines.append('<strong>Closest finish:</strong> ' \
+ str(small_mov['mov']) \
+ ' points - ' \
+ small_mov['winner'].team_name \
+ ' vs ' \
+ small_mov['loser'].team_name)
formatted = html_utils.format_list(lines)
html = '<p>' + formatted + '</p>'
return html
def format_power_rankings(league,week):
print('', flush=True)
print('FORMATTING POWER RANKINGS EMAIL... ', flush=True)
power_rankings = league.all_power_rankings[week - 1]
header = 'Power rankings:'
html = '<p><h3>' + header + '</h3>'
for t in power_rankings:
rank_info = format_rank_info(league,t,week)
extra_info = format_extra_info(league,t,week)
html = html + '<p>' + rank_info + extra_info + '</p><br>'
html = html + '</p>'
return html
def format_rank_info(league,t,week):
lines = list()
lines.append(str(t['rank']) + ') ' + t['team'].team_name)
lines.append(t['team'].owner)
s = league.get_standings_info(t['team'], week)
team_WLT = str(s['wins']) + '-' + str(s['losses'])
team_WLT += '-' + str(s['ties']) if s['ties'] > 0 else ''
lines.append(team_WLT + ', ' + str(s['season_points']) + ' points')
if week > 1:
previous_rank = league.get_power_rank(t['team'], week - 1)
lines.append('Last week rank: ' + str(previous_rank['rank']))
html = html_utils.format_list(lines)
return html
def format_extra_info(league,t,week):
team = t['team']
box = team.boxscores[week - 1]
team_stats = league.get_standings_info(t['team'], week)
html = ''
row = list()
row.append({'Dominance score': str(t['score'])})
row.append({'Standings position': str(league.get_standings_position(team, week))})
if week > 1:
row.append({'Prev standing pos': str(league.get_standings_position(team, week - 1))})
html+= html_utils.format_table(row)
row = list()
row.append({'Points': str(box.points)})
row.append({'Bench points': str(box.bench_points)})
row.append({'The Ratio™': str(utils.ratio(box.points,box.bench_points))})
html += html_utils.format_table(row)
row = list()
row.append({'Season points': str(team_stats['season_points'])})
row.append({'Seas. pts against': str(team_stats['season_points_against'])})
season_diff = team_stats['season_points'] - team_stats['season_points_against']
season_diff = '+' + str(season_diff) if season_diff > 0 else str(season_diff)
row.append({'Season pts diff': str(season_diff)})
html += html_utils.format_table(row)
row = list()
total_pts_rank = (league.get_total_points_rank(team,week))
row.append({'Season points rank': str(total_pts_rank)})
total_pts_against_rank = league.get_total_points_against_rank(team,week)
row.append({'Season pts against rank': str(total_pts_against_rank)})
html += html_utils.format_table(row)
row = list()
row.append({'Game result': team.streak_type})
MOV = box.points - box.opponent_points
MOV = '+' + str(MOV) if MOV > 0 else MOV
row.append({'Margin of victory': str(MOV)})
row.append({'Current streak': str(team.streak_length) + 'x ' + team.streak_type})
html += html_utils.format_table(row)
row = list()
row.append({'Opponent': box.opponent.team_name if box.opponent != 'BYE' else 'BYE'})
row.append({'Opponent points': str(box.opponent_points) if box.opponent != 'BYE' else 'N/A'})
if week > 1:
row.append({'Oppt prev rank': str(league.get_power_rank(box.opponent,week - 1)['rank']) if box.opponent != 'BYE' else 'N/A'})
html += html_utils.format_table(row)
row = list()
row.append({'Top player': str(box.top_player.points) \
+ ': ' \
+ box.top_player.name \
+ ' - ' \
+ box.top_player.slot_position})
row.append({'Worst player': str(box.bottom_player.points) \
+ ': ' \
+ box.bottom_player.name \
+ ' - ' \
+ box.bottom_player.slot_position})
html += html_utils.format_table(row)
row = list()
over_diff = '+' + str(box.overachiever.diff) if box.overachiever.diff > 0 else box.overachiever.diff
row.append({'Overachiever': str(over_diff) \
+ ' (' \
+ str(box.overachiever.points) \
+ 'pts - ' \
+ str(box.overachiever.projected_points) \
+ 'prj)<br>' \
+ box.overachiever.name \
+ ' - ' \
+ box.overachiever.slot_position})
under_diff = '+' + str(box.underachiever.diff) if box.underachiever.diff > 0 else box.underachiever.diff
row.append({'Underachiever': str(under_diff) \
+ ' (' \
+ str(box.underachiever.points) \
+ 'pts - ' \
+ str(box.underachiever.projected_points) \
+ 'prj)<br>' \
+ box.underachiever.name \
+ ' - ' \
+ box.underachiever.slot_position})
html += html_utils.format_table(row)
if len(box.goose_eggs) > 0:
row = list()
row.append({'Goose eggs': format_goose_eggs(box.goose_eggs)})
html += html_utils.format_table(row)
if len(box.bench_heroes) > 0:
row = list()
row.append({'Outstanding benchwarmers': format_bench_heroes(box.bench_heroes)})
html += html_utils.format_table(row)
next_week = week + 1
if next_week <= len(team.schedule):
row = list()
next_oppt = team.schedule[next_week - 1]
if next_oppt.team_id == team.team_id:
next_oppt_name = 'BYE'
next_oppt_rank = 'N/A'
next_oppt_WLT = 'N/A'
else:
next_oppt_name = next_oppt.team_name
next_oppt_rank = str(league.get_power_rank(next_oppt, week)['rank'])
o = league.get_standings_info(next_oppt, week)
next_oppt_WLT = str(o['wins']) + '-' + str(o['losses'])
next_oppt_WLT += '-' + str(o['ties']) if o['ties'] > 0 else ''
row.append({'Next opponent': next_oppt_name})
row.append({'Next oppt rank': next_oppt_rank})
row.append({'Next oppt W-L': next_oppt_WLT})
html += html_utils.format_table(row)
return html
def format_goose_eggs(goose_eggs):
html = ''
for player in goose_eggs:
html += str(player.points) \
+ ' (' \
+ str(player.projected_points) \
+ ' proj): ' \
+ player.name \
+ ' - ' + player.slot_position
if goose_eggs.index(player) + 1 != len(goose_eggs):
html += '<br>'
return html
def format_bench_heroes(bench_heroes):
html = ''
for p in bench_heroes:
html += str(p['player'].points) \
+ ' (' \
+ str(p['player'].projected_points) \
+ ' proj): ' \
+ p['player'].name \
+ ' - ' + p['position']
if bench_heroes.index(p) + 1 != len(bench_heroes):
html += '<br>'
return html