[go: nahoru, domu]

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added top half perc command and updated readme #2

Merged
merged 3 commits into from
Jan 15, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
got top half players working, needs year feature then can merge
  • Loading branch information
coolbrett committed Jan 15, 2023
commit 6453777c82a9e052886168e8078c761e2479a9f7
58 changes: 57 additions & 1 deletion LeagueData.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
load_dotenv(dotenv_path=dotenv_path)

"""
Class for the data being built around fantasy league ID and year given
Class for the data being built around the league ID and year given

@github coolbrett
"""
Expand Down Expand Up @@ -95,11 +95,13 @@ def __get_list_team_names_for_past_three_weeks(self, list_to_populate):


def __get_list_team_abbreviations_for_past_three_weeks(self, list_to_populate):
"""Helper method to get team abbreviations for past three weeks stat"""
for team in self.league.teams:
list_to_populate.append({'team_abbrev': team.team_abbrev, 'past_three_weeks_total': 0, 'team_object': team})


def __report_three_weeks_list(self, three_weeks_list):
"""Helper method to build report for three weeks statd"""
temp = ""
count = 1
temp = "`# Team".ljust(12) + "3WT`"
Expand Down Expand Up @@ -199,6 +201,9 @@ def find_length_of_longest_team_name(self, matchup: Matchup) -> int:
return num

def get_lineup_for_team(self, team_id: int, week: int = None, year: int = None) -> list:
"""
Method to get a team's lineup for a given week and/or year
"""
#return a sorted list of players by point totals
if year is not None:
self.set_year(year=year)
Expand All @@ -218,7 +223,58 @@ def get_lineup_for_team(self, team_id: int, week: int = None, year: int = None)
return box_score.away_lineup

def get_team_by_abbreviation(self, team_abbreviation: str) -> Team:
"""
Method to get the Team object by their corresponding team abbreviation
"""
for team in self.league.teams:
print(f"loop: {team.team_abbrev}")
if team.team_abbrev.casefold() == team_abbreviation.casefold():
return team

def get_list_of_all_players_rostered(self, stat: str = None) -> list:
"""
Iterates all team rosters and appends all players to a list,
then returns a sorted list by fantasy points either by
avg or total points (determined by stat parameter)
"""

rostered_players = []
for team in self.league.teams:
for player in team.roster:
rostered_players.append(player)

#if-else branch here to decide to sort by totals or by avg
if stat == "avg":
rostered_players.sort(key=lambda player: player.avg_points, reverse=True)
else:
rostered_players.sort(key=lambda player: player.total_points, reverse=True)

return rostered_players

def get_top_half_percentage_for_each_team(self, stat: str = None) -> dict:
"""
Gets all players on a roster as a list, sorted by either total points or average points, and
returns a dictionary with the keys as team_id's and the values as percentage
of players they have on the top half of the list
"""
rostered_players = []
if stat == "avg":
rostered_players = self.get_list_of_all_players_rostered(stat=stat)
else:
rostered_players = self.get_list_of_all_players_rostered()

rostered_players = rostered_players[0:int(len(rostered_players)/2)]
top_half_player_percentages_by_team = dict()
perc = float(1/len(rostered_players))

for roster_player in rostered_players:
for team in self.league.teams:
for team_player in team.roster:
if team_player.playerId == roster_player.playerId:
if team.team_id in top_half_player_percentages_by_team.keys():
top_half_player_percentages_by_team[team.team_id] += perc
else:
top_half_player_percentages_by_team.__setitem__(team.team_id, perc)

return top_half_player_percentages_by_team

21 changes: 21 additions & 0 deletions bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,27 @@ async def box_score(interaction: discord.Interaction, team_abbreviation: str, we

await interaction.response.send_message(embed=embed)


@bot.command(name="top-half-players-percentage", description="Gets the top half of all rostered players and gives percentage of how many top-half players a thas", guild_ids=[guild_id])
async def top_half_players_percentage(interaction: discord.Interaction, year: int = None, stat: str = None):
top_half_players = {}
if stat == "avg":
top_half_players = league_data.get_top_half_percentage_for_each_team(stat=stat)
else:
top_half_players = league_data.get_top_half_percentage_for_each_team()

embed = discord.Embed(title=f"Top Half Players of Rostered Players %")
description = "`TEAM".ljust(8) + "PERC%`\n"
for team_id, perc in top_half_players.items():
team = league_data.league.get_team_data(team_id=team_id)
perc = float("%.1f" % (perc * 100))
description += f"`{team.team_abbrev}".ljust(8) + f"{perc}%`\n"
embed.add_field(name="", value=description)

await interaction.response.send_message(embed=embed)



@bot.event
async def on_ready():
print(f'We have logged in as {bot.user}')
Expand Down
21 changes: 11 additions & 10 deletions tester.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def get_list_of_all_players_rostered(league_data: LeagueData) -> list:
for player in team.roster:
rostered_players.append(player)

rostered_players.sort(key=lambda player: player.total_points, reverse=True)
rostered_players.sort(key=lambda player: player.avg_points, reverse=True)
"""
count = 1
for player in rostered_players:
Expand All @@ -122,20 +122,21 @@ def get_top_half_percentage_for_each_team(league_data: LeagueData):
for team in league_data.league.teams:
for team_player in team.roster:
if team_player.playerId == roster_player.playerId:
#print(f"roster_player: {roster_player.name}\t\t{roster_player.avg_points}")
if team.team_id in top_half_player_percentages_by_team.keys():
top_half_player_percentages_by_team[team.team_id] += perc
else:
top_half_player_percentages_by_team.__setitem__(team.team_id, perc)


print("here")
total = 0
for team_id, perc in top_half_player_percentages_by_team.items():
perc = float("%.3f" % perc)
total += perc
percantage = perc * 100
percantage = float("%.3f" % percantage)
print(f"{team_id}:\t\t{percantage}%")
print(f"total: {total}")
# total = 0
# for team_id, perc in top_half_player_percentages_by_team.items():
# team = league_data.league.get_team_data(team_id=team_id)
# perc = float("%.3f" % perc)
# total += perc
# percantage = perc * 100
# percantage = float("%.3f" % percantage)
# print(f"{team.team_abbrev}:\t\t{percantage}%")
# print(f"total: {total}")

main()