Skip to content

Commit

Permalink
Release 1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
dkutin committed Jan 29, 2020
1 parent 67782b9 commit cb5be90
Show file tree
Hide file tree
Showing 8 changed files with 207 additions and 57 deletions.
113 changes: 89 additions & 24 deletions Analytics.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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;
}

}
24 changes: 24 additions & 0 deletions FantasyAPI.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -22,6 +34,10 @@ function __construct()
return $this;
}

/**
* @param $url
* @return bool|mixed
*/
function makeAPIRequest($url)
{
$curl = curl_init();
Expand All @@ -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";
Expand All @@ -54,6 +71,9 @@ function makeAPIRequest($url)
return FALSE;
}

/**
* @return bool|mixed
*/
function refreshToken()
{
// If our auth file doesn't exist, make one
Expand Down Expand Up @@ -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: ');
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#Fantasy Tracker

Built for NBA Yahoo! Fantasy games to recommend free agents to add to your roster based on performance.

91 changes: 61 additions & 30 deletions ServiceCall.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,70 +4,101 @@
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);
writeToFile(json_encode($answer), 'tmp/data/' . LEAGUE_KEY . '_my_team.json');
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');
}
}






}
Empty file removed bin/.gitkeep
Empty file.
Empty file removed bin/team/.gitkeep
Empty file.
15 changes: 12 additions & 3 deletions constants.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
<?php

/*
* Configurable Constants
*/

// User specific values to be configed before run
define('CONSUMER_KEY', '');
define('CONSUMER_SECRET', '');
define('LEAGUE_KEY', 'nba.l.');
define('LEAGUE_KEY', '');
define('TEAM_ID', '');
define('AUTH_ENDPOINT', 'https://api.login.yahoo.com/oauth2/get_token');
define('FREE_AGENTS_MAX', 50);
static $scored_stats = [
'12' => 1, //PTS
'15' => 1.2, //REB
Expand All @@ -15,3 +17,10 @@
'18' => 3, //STL
'19' => -1, //TO
];

/*
* Non-Configurable Constants
*/

define('AUTH_ENDPOINT', 'https://api.login.yahoo.com/oauth2/get_token');

Loading

0 comments on commit cb5be90

Please sign in to comment.