Skip to content
This repository has been archived by the owner on Feb 15, 2020. It is now read-only.

Commit

Permalink
Merge pull request #31 from bahatron/master
Browse files Browse the repository at this point in the history
Retain last modified date for Stats
  • Loading branch information
Tustin authored Jul 30, 2018
2 parents 7b46acc + a6e9d5d commit 8cc587f
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 29 deletions.
2 changes: 1 addition & 1 deletion src/Auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ private function __construct($access_token, $refresh_token, $account_id, $expire
$this->account_id = $account_id;
$this->expires_in = $expires_in;
$this->profile = new Profile($this->access_token, $this->account_id);
$this->leaderboard = new Leaderboard($this->access_token);
$this->account = new Account($this->access_token);
$this->leaderboard = new Leaderboard($this->access_token, $this->account);
$this->store = new Store($this->access_token);
$this->news = new News($this->access_token);
$this->status = new Status($this->access_token);
Expand Down
42 changes: 23 additions & 19 deletions src/Leaderboard.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@
class Leaderboard
{
private $access_token;
private $account;

public function __construct($access_token) {
public function __construct($access_token, Account $account)
{
$this->account = $account;
$this->access_token = $access_token;
}

Expand All @@ -33,33 +36,33 @@ public function get($platform, $type)
&& $platform !== Platform::PS4
&& $platform !== Platform::XBOX1)
throw new \Exception('Please select a platform');

if ($type !== Mode::DUO
&& $type !== Mode::SOLO
&& $type !== Mode::SQUAD)
throw new \Exception('Please select a game mode');

if ($type !== Mode::DUO
&& $type !== Mode::SOLO
&& $type !== Mode::SQUAD) {
throw new \Exception('Please select a game mode');
}

try {
$data = FortniteClient::sendFortnitePostRequest(FortniteClient::FORTNITE_API . "leaderboards/type/global/stat/br_placetop1_{$platform}_m0{$type}/window/weekly?ownertype=1&itemsPerPage=50",
$this->access_token);
$data = FortniteClient::sendFortnitePostRequest(
FortniteClient::FORTNITE_API . "leaderboards/type/global/stat/br_placetop1_{$platform}_m0{$type}/window/weekly?ownertype=1&itemsPerPage=50",
$this->access_token
);
$entries = $data->entries;


$ids = array();
foreach ($entries as $entry)
{
foreach ($entries as $entry) {
$entry->accountId = str_replace("-", "", $entry->accountId);
array_push($ids, $entry->accountId);
}

$accounts = $this->account->getDisplayNamesFromID($ids);

foreach($accounts as $account)
{
foreach($entries as $entry)
{
if ($entry->accountId == $account->id) {
$entry->displayName = $account->displayName;
foreach ($entries as $entry) {
foreach ($accounts as $account) {
if ($entry->accountId === $account->id) {
$entry->displayName = $account->displayName ?? null;
break;
}
}
Expand All @@ -72,9 +75,10 @@ public function get($platform, $type)

return $leaderboard;
} catch (GuzzleException $e) {
if ($e->getResponse()->getStatusCode() == 404) throw new LeaderboardNotFoundException('Could not get leaderboards.');
if ($e->getResponse()->getStatusCode() == 404) {
throw new LeaderboardNotFoundException('Could not get leaderboards.');
}
throw $e;
}
}

}
}
22 changes: 13 additions & 9 deletions src/Model/FortniteStats.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

use Fortnite\Exception\InvalidStatException;

class FortniteStats {
class FortniteStats
{
public $wins = 0;
public $top3 = 0;
public $top5 = 0;
Expand All @@ -19,13 +20,15 @@ class FortniteStats {
public $kill_death_per_game = 0;
public $score_per_match = 0;
public $win_loss_ratio = 0;
public $last_modified = null;


/**
* Constructs a new Fortnite\Model\FortniteStats instance.
* @param array $stats Array of mapped stats
*/
public function __construct($stats) {
/**
* Constructs a new Fortnite\Model\FortniteStats instance.
* @param array $stats Array of mapped stats
*/
public function __construct($stats)
{
foreach ($stats as $key => $value) {
switch ($key) {
case "placetop1":
Expand Down Expand Up @@ -61,16 +64,17 @@ public function __construct($stats) {
case "minutesplayed":
$this->minutes_played = $value;
break;
case "lastmodified": break;
case "lastmodified":
$this->last_modified = $value;
break;
default:
throw new InvalidStatException('Stat name '. $key . ' is not supported'); // I expect a PR if someone runs into this exception
}
}

// TODO: Cleanup
$this->kill_death_per_game = ($this->matches_played === 0) ? 0 : round($this->kills / $this->matches_played, 2);
$this->score_per_match = ($this->matches_played === 0) ? 0 : round($this->score / $this->matches_played , 2);
$this->score_per_match = ($this->matches_played === 0) ? 0 : round($this->score / $this->matches_played, 2);
$this->win_loss_ratio = ($this->matches_played === 0) ? 0 : round($this->wins / $this->matches_played, 2);
}

}

0 comments on commit 8cc587f

Please sign in to comment.