Skip to content

Commit

Permalink
spring 2024
Browse files Browse the repository at this point in the history
  • Loading branch information
kevswanberg committed May 11, 2024
1 parent 7d5def9 commit d1aebe6
Showing 1 changed file with 23 additions and 18 deletions.
41 changes: 23 additions & 18 deletions mnl_elo_bot/elo_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
SLACK_CLIENT_ID = os.environ.get("SLACK_CLIENT_ID")
SLACK_CLIENT = WebClient(SLACK_CLIENT_ID)
IMGUR_CLIENT_ID = os.environ.get("IMGUR_CLIENT_ID")
CSV_ID = "1qEEtOdzQv2n1_jhIiF6ACW8nTyRmNcERrHVrSDf8WVk"
CSV_ID = "1I7Oe2iGS2zP33P7Lvh6CEVfKISf1dGWgW9hafSSBT2g"
CSV_GID = "834633730"


Expand Down Expand Up @@ -177,6 +177,12 @@ def print_elos(teams, on_date, message):
print(get_print_message(teams, on_date, message))


def save_plot():
buf = io.BytesIO()
plt.savefig(buf)
buf.seek(0)
return buf

def plot_elos(teams):
"""
Returns an in memory PNG of the picture of our teams ratings
Expand All @@ -194,17 +200,14 @@ def plot_elos(teams):
legend.append(f"{team.name}: {team.elo:.1f}")
plt.xticks(range(len(team.history)))
plt.legend(legend, loc="upper left")
buf = io.BytesIO()
plt.savefig(buf)
buf.seek(0)
return buf



def get_print_message(teams, on, message):
winner_icons = []
message += f"MNL Elo ratings for {on}\n"
for team in teams.values():
if team.latest_change > 0:
if team.latest_change > 0 and team.emoji:
winner_icons.append(team.emoji)

message += " ".join(winner_icons)
Expand Down Expand Up @@ -276,15 +279,17 @@ def process_results(teams, results):
continue
weekly_teams_played.extend(process_game(row, teams))
games += 1
except ValueError:
except ValueError as ve:
print(f"detected end of games due to {ve}")
break
except KeyError:
continue

if games % 3 == 0: # finished the week
if games % len(teams) // 2 == 0: # finished the week
last_game_date = row["Date"]
odd_team_out = list(set(teams.values()) - set(weekly_teams_played))[0]
odd_team_out.bye_week()
if len(teams) % 2 != 0:
odd_team_out = list(set(teams.values()) - set(weekly_teams_played))[0]
odd_team_out.bye_week()
weekly_teams_played = []

return get_date_str(last_game_date)
Expand All @@ -294,24 +299,24 @@ def main(post, channel, message):
teams = {
team.name: team
for team in [
Team("Americans", "#000000", ":america:"),
Team("Tigers", "#9900FF", ":tigers:"),
Team("Scouts", "#A61C00", ":scouts:"),
Team("North Stars", "#38761D", ":north_stars:"),
Team("Golden Seals", "#783F04", ":seals:"),
Team("Whalers", "#1C4587", ":whalers:"),
Team("Nordiques", "#6FA8DC", ":nordiques:"),
Team("Nottos", "#245029", None),
Team("Screamin' Vikings", "#7258c6", None),
Team("Fellowship of the Puck", "#B5B28b", None),
Team("High Life Heros", "#a9483d", None)
]
}
reader = get_raw_results_reader()
last = process_results(teams, reader)

if post:
image = plot_elos(teams)
plot_elos(teams)
image = save_plot()
link = upload_picture_to_imgur(image)
post_elos_to_slack(teams, link, last, channel, message)
else:
plot_elos(teams)
print_elos(teams, last, message)
plt.show()
return teams


Expand Down

0 comments on commit d1aebe6

Please sign in to comment.