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

feat: Add naughty list to the UI #119

Merged
merged 1 commit into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 5 additions & 3 deletions fantasy_stats/views.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import datetime
import pytz
from django.http import HttpResponse, Http404
from django.shortcuts import render, redirect, get_object_or_404
from django.http import HttpResponse
from django.shortcuts import render, redirect
from django.template import RequestContext

import espn_api
Expand All @@ -21,8 +21,8 @@
get_leagues_previous_year,
ordinal,
)
from src.doritostats.analytic_utils import get_naughty_list_str
from src.doritostats.fetch_utils import fetch_league
from src.doritostats.simulation_utils import simulate_season

MIN_WEEK_TO_DISPLAY = 4 # Only run simulations after Week 4 has completed
N_SIMULATIONS = 500 # Default number of simulations to run
Expand Down Expand Up @@ -311,6 +311,7 @@ def league(request, league_id: int, league_year: int, week: int = None):
league_obj, week
)
standings = django_standings(league_obj, week)
naughty_list_str = get_naughty_list_str(league_obj, week)

context = {
"league_info": league_info,
Expand All @@ -320,6 +321,7 @@ def league(request, league_id: int, league_year: int, week: int = None):
"weekly_awards": weekly_awards,
"power_rankings": power_rankings,
"luck_index": luck_index,
"naughty_list_str": naughty_list_str,
"strength_of_schedule": strength_of_schedule,
"sos_weeks": schedule_period,
"standings": standings,
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "doritostats"
version = "3.4.19"
version = "3.4.20"
description = "This project aims to make ESPN Fantasy Football statistics easily available. With the introduction of version 3 of the ESPN's API, this structure creates leagues, teams, and player classes that allow for advanced data analytics and the potential for many new features to be added."
authors = ["Desi Pilla <[email protected]>"]
license = "https://github.com/DesiPilla/espn-api-v3/blob/master/LICENSE"
Expand Down
51 changes: 51 additions & 0 deletions src/doritostats/analytic_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,57 @@ def get_leader_str(stats_list: list, high_first: bool = True) -> Tuple[float, st
return sorted_stats_list[0][1], "{}".format(", ".join(leaders))


def get_naughty_players(lineup: List[Player], week: int) -> List[Player]:
"""This function identifies all players that were started by their owners, but were inactive or on bye.
The function returns a list of all players that were started by their owners, but were inactive or on bye.

A player is only considered "naughty" once they have locked (i.e., their matchup has begun) for the week.
This is to prevent lineups that have not yet been set from being flagged as "naughty".

Args:
lineup (List[Player]): A list of players in a team's lineup
week (int): The week to check for inactive players

Returns:
List[Player]: A list of all players that were started by their owners, but were inactive or on bye.
"""
return [
player
for player in lineup
if player.active_status in ["bye", "inactive"] # Player was on bye or inactive
and player.slot_position
not in ["IR", "BE"] # Player was in the player's starting lineup
and "points" in player.stats[week].keys() # The player is locked for the week
]


def get_naughty_list_str(league: League, week: int) -> List[str]:
"""This function identifies all players that were started by their owners, but were inactive or on bye.

Args:
league (League): League object
week (int): The week to check for inactive players

Returns:
List[str]: A list of strings that list all players that were started by their owners, but were inactive or on bye.
"""
naughty_list_str = []
for team in league.teams:
lineup = get_lineup(league, team, week)
naughty_players = get_naughty_players(lineup, week)
for player in naughty_players:
naughty_list_str.append(
"❌ {} started {} ({})".format(
team.owner, player.name, player.active_status
)
)

if not naughty_list_str:
naughty_list_str = ["🎉 No teams started any inactive players!"]

return naughty_list_str


def make_ordinal(n: int) -> str:
"""
Convert an integer into its ordinal representation::
Expand Down
20 changes: 20 additions & 0 deletions templates/fantasy_stats/league.html
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,26 @@ <h2>Luck Index</h2>
</div>
</div>

{# Naughty list #}
<div class="wrapper naughty-list">
<h2>Naughty List</h2>
{% if scores_are_finalized %}
<em>
<strong>
<a>Note that scores have not yet been final for this week and the Naughty List is likely to change.</a>
<br />
<a>Please check back on Tuesday morning for the final results.</a>
</strong>
</em>
{% endif %}
{% comment %} Print out a list of all the elements in the naughty list {% endcomment %}
<ul>
{% for r in naughty_list_str %}
<li>{{ r }}</li>
{% endfor %}
</ul>
</div>

{# Standings #}
<div class="wrapper">
<h2>Standings</h2>
Expand Down
Loading