diff --git a/Analytics.php b/Analytics.php index 3475f2c..fbd65d5 100644 --- a/Analytics.php +++ b/Analytics.php @@ -4,11 +4,19 @@ include_once('ServiceCall.php'); include_once('helper.php'); +/** + * Class Analytics + */ class Analytics { + /** + * Analytics constructor. + */ function __construct() { if (file_exists('tmp/data/' . LEAGUE_KEY . '_free_agents.json') && file_exists('tmp/data/' . LEAGUE_KEY . '_my_team.json')) { + // TODO: Remove team and free agent files from temp/ every time a new Analytics object is made ... + //deleteAllFromFolder('tmp/data/*.json'); $roster = getContents('tmp/data/' . LEAGUE_KEY . '_my_team.json'); $free_agents = getContents('tmp/data/' . LEAGUE_KEY . '_free_agents.json'); writeToFile(json_encode($roster['team']['roster']['players']), 'bin/team_' . TEAM_ID . '_roster.json'); @@ -17,50 +25,107 @@ function __construct() return $this; } - function generateReport() { + /** + * @return string + */ + function generateReport() + { + $averages['FA'] = $this->createPlayerAverages('FA'); + $averages['Roster'] = $this->createPlayerAverages('Roster'); + $player_weight = []; + $data = ""; - } + foreach ($averages['Roster'] as $rplayer => $rplayer_avg) { + $free_agents = -1; + foreach ($averages['FA'] as $fplayer => $fplayer_avg) { + $free_agents++; + if ($fplayer_avg >= $rplayer_avg) { + $player_weight[$fplayer] = isset($player_weight[$fplayer]) ? $player_weight[$fplayer] + 1 : 1 - $free_agents; + $player_weight[$rplayer] = isset($player_weight[$rplayer]) ? $player_weight[$rplayer] - 1 : -1; + } else { + break; + } + } + } - function setProtectedPlayers($players) { - $data = getContents('bin/team_' . TEAM_ID . '_roster.json'); - foreach ($data as &$player) { - if (!empty($player['player_id']) && in_array($player['player_id'], $players)) { - $player['user_drop_protected'] = "1"; + foreach ($player_weight as $player => $weight) { + if (array_key_exists($player, $averages['Roster'])) { + print "You should consider dropping: ${player}. (${weight})" . PHP_EOL; + $data .= "You should consider dropping: ${player}. (${weight})" . PHP_EOL; + } else { + if ($weight > 0) { + print "You should consider picking up: ${player}. (${weight})" . PHP_EOL; + $data .= "You should consider picking up: ${player}. (${weight})" . PHP_EOL; + } } } - writeToFile(json_encode($data), 'bin/team/team_' . TEAM_ID . '_roster.json'); + writeToFile(json_encode($player_weight), 'bin/analysis_' . time() . '.json'); + return $data; + } - function unsetProtectedPlayers($players) { - $data = getContents('bin/team_' . TEAM_ID . '_roster.json'); - foreach ($data as &$player) { - if (!empty($player['player_id']) && in_array($player['player_id'], $players)) { - unset($player['user_drop_protected']); + /** + * @param $type + * @param string $player + * @return array + */ + function createPlayerStats($type, $player = '') { + // If we've specified a player, get their weekly stats (if available) + if (empty($player)) { + $files = glob("tmp/data/players/${type}/*.json", GLOB_BRACE); + } else { + $files = glob("tmp/data/players/${type}/player_${player}_week_*.json", GLOB_BRACE); + if (count($files) < 2) { + print "Not enough data given for player ${player}!"; + return FALSE; } } - writeToFile(json_encode($data), 'bin/team/team_' . TEAM_ID . '_roster.json'); - } - function createPlayerAverages() { - global $scored_stats; - $files = glob('tmp/data/players/*.json', GLOB_BRACE); $players = []; foreach ($files as $file) { + $week = substr($file, -7, 2); $data = getContents($file); + global $scored_stats; $gp = $data['player']['player_stats']['stats']['stat']['0']['value']; $averages = []; - foreach ($data['player']['player_stats']['stats']['stat'] as $stat) { - if (array_key_exists($stat['stat_id'], $scored_stats)) { - $averages[$stat['stat_id']] = ((float)$scored_stats[$stat['stat_id']] * (float)$stat['value']) / (float)$gp; + if ($gp > 0) { + foreach ($data['player']['player_stats']['stats']['stat'] as $stat) { + if (array_key_exists($stat['stat_id'], $scored_stats)) { + $averages[$stat['stat_id']] = ((float)$scored_stats[$stat['stat_id']] * (float)$stat['value']) / (float)$gp; + } } } - $final = array_sum($averages); - $players[$data['player']['player_id']] = $final; + $players[$week][$data['player']['name']['full']] = array_sum($averages); } - arsort($players); + return $players; } + function getSevenDayAverage($type, $player = '') { + $data = $this->createPlayerStats($type, $player); + $stats = []; + foreach ($data as $week => $players) { + foreach ($players as $player => $value) { + if (!empty($data[$week-1][$player])) { + $stats[$week][$player] = $data[$week][$player] - $data[$week-1][$player]; + } + } + } + return $stats; + } + // TODO: Maybe add another parameter to merge 2 functions ... + function getFourteenDayAverage($type, $player = '') { + $data = $this->createPlayerStats($type, $player); + $stats = []; + foreach ($data as $week => $players) { + foreach ($players as $player => $value) { + if (!empty($data[$week-2][$player])) { + $stats[$week][$player] = $data[$week][$player] - $data[$week-2][$player]; + } + } + } + return $stats; + } } diff --git a/FantasyAPI.php b/FantasyAPI.php index 7e31264..08b9d77 100644 --- a/FantasyAPI.php +++ b/FantasyAPI.php @@ -3,12 +3,24 @@ include_once('constants.php'); include_once('helper.php'); +/** + * Class FantasyAPI + */ class FantasyAPI { + /** + * @var bool|mixed + */ private $credentials; + /** + * @var string + */ private $auth_json_file; + /** + * FantasyAPI constructor. + */ function __construct() { $this->auth_json_file = 'tmp/auth/auth_credentials_' . CONSUMER_KEY . '.json'; @@ -22,6 +34,10 @@ function __construct() return $this; } + /** + * @param $url + * @return bool|mixed + */ function makeAPIRequest($url) { $curl = curl_init(); @@ -41,6 +57,7 @@ function makeAPIRequest($url) $this->makeAPIRequest($url); } else { print 'Trouble Getting Refresh Token...'; + } } else if (strpos($resp, "error")) { print "Error in making the API Request"; @@ -54,6 +71,9 @@ function makeAPIRequest($url) return FALSE; } + /** + * @return bool|mixed + */ function refreshToken() { // If our auth file doesn't exist, make one @@ -91,9 +111,13 @@ function refreshToken() curl_close($ch); writeToFile($resp, $this->auth_json_file); $this->credentials['expiry_time'] = time() + 3600; + print_r($resp); return json_decode($resp, TRUE); } + /** + * @return bool|mixed + */ function initializeToken() { $auth_code = readline('Go to: https://api.login.yahoo.com/oauth2/request_auth?client_id=' . CONSUMER_KEY . '&redirect_uri=oob&response_type=code&language=en-us and copy the code: '); diff --git a/README.md b/README.md index e69de29..9c2ab1f 100644 --- a/README.md +++ b/README.md @@ -0,0 +1,4 @@ +#Fantasy Tracker + +Built for NBA Yahoo! Fantasy games to recommend free agents to add to your roster based on performance. + diff --git a/ServiceCall.php b/ServiceCall.php index 271f8f4..9540b14 100644 --- a/ServiceCall.php +++ b/ServiceCall.php @@ -4,20 +4,52 @@ include_once('FantasyAPI.php'); include_once('helper.php'); +/** + * Class ServiceCall + */ class ServiceCall { + /** + * @var FantasyAPI + */ private $api; - + /** + * @var int + */ + private $week; + + /** + * ServiceCall constructor. + */ function __construct() { $this->api = new FantasyAPI(); + if (empty($this->week)) { + $this->getCurrentWeek(); + } return $this; } + /** + * @return FantasyAPI + */ function getFantasyAPI() { return $this->api; } + /** + * @return mixed + */ + function getCurrentWeek() { + $current_week = "https://fantasysports.yahooapis.com/fantasy/v2/team/". LEAGUE_KEY . ".t.". TEAM_ID ."/roster"; + $answer = $this->api->makeAPIRequest($current_week)['team']['roster_adds']['coverage_value']; + $this->week = $answer; + return $answer; + } + + /** + * @return bool|mixed + */ function getMyPlayers() { $my_team = "https://fantasysports.yahooapis.com/fantasy/v2/team/". LEAGUE_KEY . ".t.". TEAM_ID ."/roster"; $answer = $this->api->makeAPIRequest($my_team); @@ -25,49 +57,48 @@ function getMyPlayers() { return $answer; } + /** + * @return array + */ function getFreeAgents() { - $free_agents = "https://fantasysports.yahooapis.com/fantasy/v2/league/". LEAGUE_KEY ."/players;status=FA;start=0;sort=OR"; - $answer = $this->api->makeAPIRequest($free_agents); + $answer = []; + for ($num =0; $num <= FREE_AGENTS_MAX; $num +=25) { + $free_agents = "https://fantasysports.yahooapis.com/fantasy/v2/league/". LEAGUE_KEY ."/players;status=FA;start=${num};sort=OR"; + $answer = array_merge_recursive($answer, $this->api->makeAPIRequest($free_agents)); + } writeToFile(json_encode($answer), 'tmp/data/' . LEAGUE_KEY . '_free_agents.json'); return $answer; } - function getPlayerStats($player_key) { - $player = "https://fantasysports.yahooapis.com/fantasy/v2/player/395.p." . $player_key . "/stats"; + /** + * @param $player_key + * @param $type + * @return bool|mixed + */ + function getPlayerStats($player_key, $type) { + $week = $this->week; + $player = "https://fantasysports.yahooapis.com/fantasy/v2/player/395.p.${player_key}/stats"; $answer = $this->api->makeAPIRequest($player); - writeToFile(json_encode($answer), 'tmp/data/players/player_' . $player_key . '.json'); + writeToFile(json_encode($answer), "tmp/data/players/${type}/player_${player_key}_week_${week}.json"); return $answer; } - function getMyWeeklyStats() { - $stats = "https://fantasysports.yahooapis.com/fantasy/v2/team/" . LEAGUE_KEY . ".t." . TEAM_ID . "/stats;type=week;week=9"; - $answer = $this->api->makeAPIRequest($stats); - writeToFile(json_encode($answer), 'tmp/data/' . LEAGUE_KEY . '_week_' . '.json'); - return $answer; - } - - function getRosterStats() { - foreach ($this->getMyPlayers()['team']['roster']['players']['player'] as $player) { - $this->getPlayerStats($player['player_id']); - } - } - + /** + * + */ function getFreeAgentsStats() { foreach ($this->getFreeAgents()['league']['players']['player'] as $player) { - $this->getPlayerStats($player['player_id']); + $this->getPlayerStats($player['player_id'], 'FA'); } } - function getWeeklyPlayerStats($player_id) { - $player = "https://fantasysports.yahooapis.com/fantasy/v2/league/" . LEAGUE_KEY . "/players;player_keys=395.p." . $player_id . "/stats"; - $answer = $this->api->makeAPIRequest($player); - writeToFile(json_encode($answer), 'tmp/data/' . LEAGUE_KEY . '_week_' . '.json'); - return $answer; + /** + * + */ + function getRosterStats() { + foreach ($this->getMyPlayers()['team']['roster']['players']['player'] as $player) { + $this->getPlayerStats($player['player_id'], 'Roster'); + } } - - - - - } diff --git a/bin/.gitkeep b/bin/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/bin/team/.gitkeep b/bin/team/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/constants.php b/constants.php index 8033896..df44174 100644 --- a/constants.php +++ b/constants.php @@ -1,12 +1,14 @@ 1, //PTS '15' => 1.2, //REB @@ -15,3 +17,10 @@ '18' => 3, //STL '19' => -1, //TO ]; + +/* + * Non-Configurable Constants + */ + +define('AUTH_ENDPOINT', 'https://api.login.yahoo.com/oauth2/get_token'); + diff --git a/helper.php b/helper.php index 1dce683..e80a4f4 100644 --- a/helper.php +++ b/helper.php @@ -1,5 +1,9 @@