[go: nahoru, domu]

Skip to content

Commit

Permalink
Uniform commands (#11)
Browse files Browse the repository at this point in the history
* about to redo scoreboard

* made commands uniform
  • Loading branch information
coolbrett committed Feb 8, 2023
1 parent 1735860 commit 477089a
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 74 deletions.
1 change: 0 additions & 1 deletion FirebaseData.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ def get_all_guild_ids(self):
"""
ref = db.reference('fbbot')
data = ref.get('guilds')
print(f"data: {data}")
guild_ids = list()
if data[0] != None:
keys_as_list = list(data[0]['guilds'].keys())
Expand Down
12 changes: 4 additions & 8 deletions LeagueData.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,12 +174,8 @@ def get_box_score_of_matchup(self, week: int, team: Team) -> box_score:
def get_box_scores_and_matchups_of_week(self, week: int) -> list:
"""Grabs list of box scores and matchups of week given, and returns a list of dictionaries
containing the matchups and their corresponding box scores"""
# print(f"in get_box_scores_and_matchups_of_week")
box_scores = self.league.box_scores(matchup_period=week)
# print("got box scores")
# print(f"Year: {self.league.year} Week: {week}")
matchups = self.league.scoreboard(matchupPeriod=week)
# print('got scoreboard')
data = []
count = 0
for matchup in matchups:
Expand All @@ -192,14 +188,14 @@ def shorten_player_name(self, player_name: str) -> str:
temp = player_name.split()
return f"{temp[0][0]}. {temp[1]}"

def find_length_of_longest_team_name(self, matchup: Matchup) -> int:
def find_length_of_longest_team_name(self, box_score: box_score) -> int:
"""Returns the length of the longest team name of a given matchup"""
num = 0

if len(matchup.away_team.team_name) > len(matchup.home_team.team_name):
num = len(matchup.away_team.team_name)
if len(box_score.away_team.team_name) > len(box_score.home_team.team_name):
num = len(box_score.away_team.team_name)
else:
num = len(matchup.home_team.team_name)
num = len(box_score.home_team.team_name)

return num

Expand Down
16 changes: 13 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,24 @@ PROCESS

TO-DO
-----
- Try and make the commands uniform -- scoreboard kind of sucks, get rid of '|'across commands if possible
- Try and make the commands uniform -- scoreboard kind of sucks, record-vs-all-teams looks the best
- replace usages of scoreboard()
- get_record_vs_all_teams doesn't work if team has a bye
- make /issue command to report issues
- Add logging?
- bot token and firebase service account creds need to be regenerated since they were pushed to git
- deploy the bot to Azure
- Test on another server to make sure all functionalities work there too
- Support server?
- Cleanup git repo and README before going public
- deploy the bot to Firebase?
- set up github actions for automated deployments
- Add new useful and advanced stats
- Add new useful and advanced stat
- Add commands that fire every so often (weekly and yearly awards)
- Add compare players command (player1 season1 player2 season2)
- Add schedule command
- team specific or week in league specific
- Look into message options thing that Elijah showed you




Expand All @@ -37,6 +46,7 @@ FUTURE WORK - once the bot is polished, bulletproof, and well-designed
- Look into adding team logo's on certain commands -- teams have logo url fields
There is a branch on the repo called logos -- fetching the pics and sending them
through the bot is working, but the logos have weird lines coming off the right
- Add commands that fire every so often (weekly and yearly awards)


NOTES
Expand Down
112 changes: 50 additions & 62 deletions bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import os
from dotenv import load_dotenv
from pathlib import Path
import json
import espn_api
from FirebaseData import FirebaseData

Expand Down Expand Up @@ -50,17 +49,13 @@ async def before_each_command(context: discord.ApplicationContext):

if guild_fb['league_id'] == None:
await context.interaction.followup.send("Your league is not set up! Use /setup to configure your league credentials")
print(context.command.name)
return

league_id = guild_fb['league_id']

#create league with and without private credentials
if 'espn_s2' in guild_fb and 'swid' in guild_fb:
print("has private creds")
espn_s2 = guild_fb['espn_s2']

#swid being an obj is not tested??
swid = guild_fb['swid']
league_data = await create_league_data(interaction=context.interaction, league_id=league_id, espn_s2=espn_s2, swid=swid)
else:
Expand Down Expand Up @@ -109,7 +104,7 @@ async def standings(interaction: discord.Interaction, year: int = None):
description = ""
description += "`# Team".ljust(15) + "W-L-T`\n"
for team in list_of_teams:
description += "`" + str(place) + ") " + team.team_abbrev.ljust(5) + " | {}-{}-{}".format(team.wins, team.losses, team.ties).ljust(9) + "`\n"
description += "`" + str(place) + ") " + team.team_abbrev.ljust(5) + " {}-{}-{}".format(team.wins, team.losses, team.ties).ljust(9) + "`\n"
place += 1
embed.add_field(name=str(div), value=description, inline=False)

Expand Down Expand Up @@ -144,13 +139,13 @@ async def draft_recap(interaction: discord.Interaction, year: int = None, round:
player_name = league_data.shorten_player_name(player_name=player_name)

description += "`" + str(str(pick.round_pick) + ")").ljust(4) + pick.team.team_abbrev.ljust(
5) + "| {}".format(player_name).ljust(9) + "`\n"
5) + " {}".format(player_name).ljust(9) + "`\n"

description += "\n"
embed.add_field(name=("Round: " + str(round_num)), value=description, inline=False)
else:
round_list_of_picks = draft_recap[round]
description = "`# Team".ljust(13) + "Pick`\n"
description = "`# Team".ljust(12) + "Pick`\n"
for pick in round_list_of_picks:
# append pick info
player_name = pick.playerName
Expand All @@ -159,7 +154,7 @@ async def draft_recap(interaction: discord.Interaction, year: int = None, round:
player_name = f"{temp[0][0]}. {temp[1]}"

description += "`" + str(str(pick.round_pick) + ")").ljust(4) + pick.team.team_abbrev.ljust(
5) + "| {}".format(player_name).ljust(9) + "`\n"
5) + " {}".format(player_name).ljust(9) + "`\n"

description += "\n"
embed.add_field(name=("Round: " + str(round)), value=description, inline=False)
Expand All @@ -182,12 +177,12 @@ async def abbreviations(interaction: discord.Interaction, year: int = None):
league_data.set_year(year=year)

embed = discord.Embed(title=str((league_data.league.year - 1)) + "-" + str(league_data.league.year))
description = "`Abbrev".ljust(8) + "| Team`\n"
description = "`Abbrev".ljust(8) + " Team`\n"

abbreviations = league_data.get_abbreviations()

for abbrev, team_name in abbreviations.items():
description += f"`{abbrev}".ljust(8) + f"| {team_name}`\n"
description += f"`{abbrev}".ljust(8) + f" {team_name}`\n"
embed.add_field(name="Abbreviations", value=description, inline=False)

# set year back to original year if it was changed
Expand Down Expand Up @@ -221,7 +216,7 @@ async def history(interaction: discord.Interaction, year: int):
title = f"Champion🏆: {team.team_name}"
else:
# the rest
description += f"`{place}) {team.team_abbrev}".ljust(10) + f"| {team.wins}-{team.losses}-{team.ties}`\n"
description += f"`{place}) {team.team_abbrev}".ljust(10) + f" {team.wins}-{team.losses}-{team.ties}`\n"
place += 1

embed.add_field(name=title, value=description, inline=False)
Expand All @@ -244,67 +239,58 @@ async def scoreboard(interaction: discord.Interaction, week: int = None, year: i
original_year = league_data.league.year
if year is not None:
if week is not None:
league_data.set_year(year=year)
if year < 2019:
# Build scoreboard for years 2018 and earlier
embeds = []
embed = discord.Embed(title="Week " + str(week) + " Scoreboard (" + str(league_data.league.year - 1) + "-" + str(league_data.league.year) + ")")
embeds.append(embed)
matchups = league_data.league.scoreboard(matchupPeriod=week)

for matchup in matchups:
pad_amount = league_data.find_length_of_longest_team_name(matchup=matchup) + 10
embed = discord.Embed(title="")
embed.add_field(name="".ljust(pad_amount,
"-") + f"\n{matchup.away_team.team_name}:" + f" {int(matchup.away_final_score)}\n",
value=f"**{matchup.away_team.wins}-{matchup.away_team.losses}-{matchup.away_team.ties}**\n\n", inline=False)
embed.add_field(name=f"{matchup.home_team.team_name}:" + f" {int(matchup.home_final_score)}\n",
value=f"**{matchup.home_team.wins}-{matchup.home_team.losses}-{matchup.home_team.ties}**\n**" + "".ljust(pad_amount, "-") + "**", inline=False)
embeds.append(embed)

# set year back to original year if it was changed
if league_data.league.year != original_year:
league_data.set_year(original_year)

await interaction.followup.send(embeds=embeds)
await interaction.followup.send("Cannot get box scores prior to 2019")
return
league_data.set_year(year=year)
else:
await interaction.followup.send("Provide a week number if going into previous seasons!")
return

if week is None:
week = league_data.find_current_week()

# embedded messages can only have 25 fields, so multiple embedded messages are needed just in case a league
# has more than 12 teams
embeds = []
embed = discord.Embed(title="Week " + str(week) + " Scoreboard (" + str(league_data.league.year - 1) + "-" + str(
league_data.league.year) + ")")
embeds.append(embed)
list_of_matchup_dicts = league_data.get_box_scores_and_matchups_of_week(week=week)

for data in list_of_matchup_dicts:
for matchup, box_score in data.items():

top_scorer_home = league_data.get_top_scorer(lineup=box_score.home_lineup)
top_scorer_away = league_data.get_top_scorer(lineup=box_score.away_lineup)

# Top performer field goes off the side on the app :/
embed = discord.Embed(title="")
if len(top_scorer_away.name) > 18:
top_scorer_away.name = league_data.shorten_player_name(player_name=top_scorer_away.name)
elif len(top_scorer_home.name) > 18:
top_scorer_home.name = league_data.shorten_player_name(player_name=top_scorer_home.name)

pad_amount = league_data.find_length_of_longest_team_name(matchup=matchup) + 10
embed.add_field(name="".ljust(pad_amount,
"-") + f"\n{matchup.away_team.team_name}:" + f" {int(matchup.away_final_score)}\n",
value=f"**{matchup.away_team.wins}-{matchup.away_team.losses}-{matchup.away_team.ties}**\n\n__**Top Performer**__\n{top_scorer_away.name}: " + str(
int(top_scorer_away.points)) + "\n|", inline=False)
embed.add_field(name=f"{matchup.home_team.team_name}:" + f" {int(matchup.home_final_score)}\n",
value=f"**{matchup.home_team.wins}-{matchup.home_team.losses}-{matchup.home_team.ties}**\n\n__**Top Performer**__\n{top_scorer_home.name}: " + str(
int(top_scorer_home.points)) + "\n**" + "".ljust(pad_amount, "-") + "**", inline=False)
embeds.append(embed)

box_scores = league_data.league.box_scores(matchup_period=week)

for box_score in box_scores:
#each box score will be an embed message
embed = discord.Embed(title="")
team_name_away = box_score.away_team.team_name
team_name_home = box_score.home_team.team_name
score_away = box_score.away_score
score_home = box_score.home_score
top_scorer_home = league_data.get_top_scorer(lineup=box_score.home_lineup)
top_scorer_away = league_data.get_top_scorer(lineup=box_score.away_lineup)

TEAM_ABBREV_SPACING = 6
PLAYER_NAME_MAX = 18

if len(top_scorer_home.name) > PLAYER_NAME_MAX:
top_scorer_home.name = league_data.shorten_player_name(top_scorer_home.name)
if len(top_scorer_away.name) > PLAYER_NAME_MAX:
top_scorer_away.name = league_data.shorten_player_name(top_scorer_away.name)

abbrev_away = box_score.away_team.team_abbrev
abbrev_home = box_score.home_team.team_abbrev

name = f"{team_name_away}"
value = f"`{int(score_away)}`"
embed.add_field(name=name, value=value, inline=False)

name = f"{team_name_home}"
value = f"`{int(score_home)}`"
embed.add_field(name=name, value=value, inline=False)

name = f"~~-----------------~~\n**Top Performers**"
value = f"`{abbrev_away.ljust(TEAM_ABBREV_SPACING)}{top_scorer_away.name.ljust(PLAYER_NAME_MAX)}{int(top_scorer_away.points)}`\n`{abbrev_home.ljust(TEAM_ABBREV_SPACING)}{top_scorer_home.name.ljust(PLAYER_NAME_MAX)}{int(top_scorer_home.points)}`"
embed.add_field(name=name, value=value, inline=False)

embeds.append(embed)

# set year back to original year if it was changed
if league_data.league.year != original_year:
Expand Down Expand Up @@ -409,13 +395,15 @@ async def record_vs_all_teams(interaction: discord.Interaction, year: int = None
if year is None:
year = league_data.league.year
data = league_data.get_record_vs_all_teams()
name = "`TEAM".ljust(8) + "RECORD".ljust(12) + "PERC%`"
name = "`#".ljust(6) + "TEAM".ljust(8) + "RECORD".ljust(12) + "PERC%`"
description = ""
place = 1
for team_id, record in data.items():
team = league_data.league.get_team_data(team_id=team_id)
description += f"`{team.team_abbrev}".ljust(8) + f"{record['wins']}-{record['losses']}-{record['ties']}".ljust(12)
description += f"`{place})".ljust(6) + f"{team.team_abbrev}".ljust(8) + f"{record['wins']}-{record['losses']}-{record['ties']}".ljust(12)
win_percentage = league_data.get_win_percentage(wins=record['wins'], losses=record['losses'], ties=record['ties'])
description += f"{win_percentage}`\n"
place += 1
embed.add_field(name=name, value=description)


Expand Down
Binary file added requirements.txt
Binary file not shown.

0 comments on commit 477089a

Please sign in to comment.