<gh_stars>1-10
from math import sqrt
from espn_api.basketball import League
from termcolor import colored
from constantsForLeague import ESPN_s2, SWID, LEAGUE_ID, LEAGUE_SEASON, FULL_SEASON_STATS, LAST7DAYS_STATS, LAST15DAYS_STATS, LAST30DAYS_STATS

playersDic = {}
statsDic = {}
minsList = []

mainLeague = League(league_id=LEAGUE_ID, year=LEAGUE_SEASON, espn_s2=ESPN_s2, swid=SWID)
yourTeam = mainLeague.teams[7]
yourPlayers = yourTeam.roster
agents = mainLeague.free_agents(size=300)
agents = agents + yourPlayers

# Function for Fantasy Points (FUll SEASON / HEAD TO HEAD) #
def myPoints(player):
    points = 0
    points += player.__getattribute__('stats')[FULL_SEASON_STATS]['avg']['FGM'] * 2
    points -= player.__getattribute__('stats')[FULL_SEASON_STATS]['avg']['FGM'] - \
              player.__getattribute__('stats')[FULL_SEASON_STATS]['avg']['FGA']
    points += player.__getattribute__('stats')[FULL_SEASON_STATS]['avg']['FTM']
    points -= player.__getattribute__('stats')[FULL_SEASON_STATS]['avg']['FTA'] - \
              player.__getattribute__('stats')[FULL_SEASON_STATS]['avg']['FTM']
    points += player.__getattribute__('stats')[FULL_SEASON_STATS]['avg']['3PTM']
    points += player.__getattribute__('stats')[FULL_SEASON_STATS]['avg']['OREB'] + \
              player.__getattribute__('stats')[FULL_SEASON_STATS]['avg']['DREB']
    points += player.__getattribute__('stats')[FULL_SEASON_STATS]['avg']['AST'] * 2
    points += player.__getattribute__('stats')[FULL_SEASON_STATS]['avg']['STL'] * 4
    points -= player.__getattribute__('stats')[FULL_SEASON_STATS]['avg']['TO'] * 2
    points += player.__getattribute__('stats')[FULL_SEASON_STATS]['avg']['PTS']
    return points

# Method for getting key value#
def get_key(val):
    for key, value in playersDic.items():
        if val == value:
            return key
    return "key doesn't exist"

# Main Code to Create Dictionary { PlayerName : Ranking Metric } #
for players in agents:
    if ((players.__getattribute__('injuryStatus') == 'ACTIVE') or
            (players.__getattribute__('injuryStatus')) == 'NORMAL') and (
            LAST7DAYS_STATS in players.__getattribute__('stats').keys()) and (
            LAST15DAYS_STATS in players.__getattribute__('stats').keys()) and (
            LAST30DAYS_STATS in players.__getattribute__('stats').keys()) and (
            players.__getattribute__('stats')[FULL_SEASON_STATS]['avg']['MIN'] >= 25):

        # vars #
        pointsPerMin = myPoints(players) / players.__getattribute__('stats')[FULL_SEASON_STATS]['avg']['MIN']
        lastSevenDayAverageMin = players.__getattribute__('stats')[LAST7DAYS_STATS]['avg']['MIN']
        lastFifteenDayAverageMin = players.__getattribute__('stats')[LAST15DAYS_STATS]['avg']['MIN']
        lastThirdlyDayAverageMin = players.__getattribute__('stats')[LAST30DAYS_STATS]['avg']['MIN']

        minsList.append(lastThirdlyDayAverageMin)
        minsList.append(lastFifteenDayAverageMin)
        minsList.append(lastSevenDayAverageMin)

        xAxis = [1, 2, 3]
        slope = 0
        count = 0

        # Slope Calculation: See Equations.txt #
        for num in range(0, 3):
            xSum = 6
            ySum = + minsList[num]
            xySum = + minsList[num] * xAxis[num]
            delta = 6
            slope = 3 * xySum - (xSum * ySum) / 6

        # Distance Formula: See Equations.txt #
        xValue = slope ** 2
        yValue = pointsPerMin ** 2
        ranking = sqrt((0.2 * xValue) + (0.8 * yValue))

        playersDic[players] = ranking
        minsList.clear()

for i in sorted(playersDic.values(), reverse=True):
    count += 1
    print(str(count) + '.', colored(get_key(i).__getattribute__('name'), 'red'))