Created
May 4, 2017 15:01
-
-
Save ArnoldsK/15e8e8884e6065a33b3baa513952db46 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Characters | |
{ | |
const ERROR_MESSAGE = '<p class="text-danger">The account name is not found or the characters tab is private.</p><p><img src="//i.imgur.com/C0v0IDq.png" class="thumbnail"></p>'; | |
public $config; | |
public $accountName; | |
public $leagueFilter; | |
public $levelFilter; | |
protected $data = null; | |
protected $classes = [ | |
'Duelist' => ['Slayer', 'Gladiator', 'Champion'], | |
'Shadow' => ['Assassin', 'Saboteur', 'Trickster'], | |
'Marauder' => ['Juggernaut', 'Berserker', 'Chieftain'], | |
'Witch' => ['Necromancer', 'Elementalist', 'Occultist'], | |
'Ranger' => ['Deadeye', 'Raider', 'Pathfinder'], | |
'Templar' => ['Inquisitor', 'Hierophant', 'Guardian'], | |
'Scion' => ['Ascendant'], | |
]; | |
public function __construct($accountName, $leagueFilter, $levelFilter) | |
{ | |
$this->config = require 'config.php'; | |
$this->accountName = strtolower($accountName); | |
$this->leagueFilter = strtolower(str_replace(' ', '-', $leagueFilter)); | |
$this->levelFilter = (int)$levelFilter; | |
} | |
/** | |
* Fetch character details from poe website | |
* | |
* @return array|null | |
*/ | |
public function getData() | |
{ | |
if (!$this->accountName) { | |
return false; | |
} | |
if ($this->data === null) { | |
$ch = curl_init('https://www.pathofexile.com/character-window/get-characters'); | |
curl_setopt($ch, CURLOPT_POST, true); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, 'accountName=' . $this->accountName); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
$data = curl_exec($ch); | |
curl_close($ch); | |
$this->data = json_decode($data); | |
$this->addExtraData(); | |
} | |
return $this->data; | |
} | |
public function compareLeagues($leagueA, $leagueB) | |
{ | |
return strtolower(str_replace(' ', '-', $leagueA)) == strtolower(str_replace(' ', '-', $leagueB)); | |
} | |
/** | |
* Add extra data to character data | |
* | |
* @return array|null | |
*/ | |
protected function addExtraData() | |
{ | |
if (!$this->data) return; | |
foreach ($this->data as $key => $item) { | |
/** | |
* Turn any ascendancy class into the base class | |
* e.g. Raider becomes Ranger | |
*/ | |
$normalizedClass = $this->normalizeClass($item->class); | |
$this->data[$key]->normalizedClass = $normalizedClass; | |
} | |
} | |
public function normalizeClass($class) | |
{ | |
foreach ($this->classes as $name => $ascendants) { | |
if (in_array($class, $ascendants)) { | |
return $name; | |
} elseif ($class == $name) { | |
return $name; | |
} | |
} | |
return $class; | |
} | |
public function getLeagues($format = false) | |
{ | |
if (!$data = $this->getData()) { | |
return null; | |
} | |
$leagues = []; | |
foreach ($data as $item) { | |
$leagues[] = $format ? strtolower(str_replace(' ', '-', $item->league)) : $item->league; | |
} | |
return array_unique($leagues); | |
} | |
public function getClasses() | |
{ | |
if (!$data = $this->getData()) { | |
return null; | |
} | |
$classes = []; | |
$totalClassesCount = 0; | |
// Add class default values | |
foreach ($this->classes as $class => $ascendants) { | |
foreach ($ascendants as $key => $name) { | |
$ascendants[$name] = 0; | |
unset($ascendants[$key]); | |
} | |
$classes[$class] = [ | |
'count' => 0, | |
'percentage' => 0, | |
'avatar' => $this->config->url . 'img/' . $class . '_avatar.png', | |
'ascendants' => $ascendants, | |
'ascendantCount' => 0, | |
]; | |
} | |
// Set the count of each class | |
foreach ($data as $item) { | |
// Make sure league filter exists | |
if (!in_array($this->leagueFilter, $this->getLeagues(true))) $this->leagueFilter = null; | |
// Add league filter | |
if ($this->leagueFilter) { | |
if (!$this->compareLeagues($this->leagueFilter, $item->league)) continue; | |
} | |
// Make sure level filter is correct | |
if (!in_array($this->levelFilter, range(1, 100))) $this->levelFilter = null; | |
// Add level filter | |
if ($this->levelFilter) { | |
if ($item->level < $this->levelFilter) continue; | |
} | |
$totalClassesCount++; | |
$classes[$item->normalizedClass]['count']++; | |
// Add ascendancy class count | |
if ($item->class !== $item->normalizedClass) { | |
$classes[$item->normalizedClass]['ascendantCount']++; | |
$classes[$item->normalizedClass]['ascendants'][$item->class]++; | |
} | |
} | |
// Return an empty array if no classes | |
if ($totalClassesCount == 0) { | |
return $classes; | |
} | |
// Change the percentage values | |
foreach ($classes as $class => $stats) { | |
$classes[$class]['percentage'] = ($stats['count'] * 100) / $totalClassesCount; | |
// Change ascendancy item to show count and percentage | |
foreach ($stats['ascendants'] as $name => $count) { | |
$classes[$class]['ascendants'][$name] = [ | |
'count' => $count, | |
'percentage' => $stats['ascendantCount'] ? ($count * 100) / $stats['ascendantCount'] : 0, | |
'avatar' => $this->config->url . 'img/120px-' . $name . '_avatar.png', | |
]; | |
} | |
} | |
return $classes; | |
} | |
public function outputHtml() | |
{ | |
return require 'output.phtml'; | |
} | |
public function getStateUrl() | |
{ | |
$url = $this->config->url; | |
if ($this->accountName) $url .= $this->accountName; | |
if ($this->leagueFilter) $url .= '/' . $this->leagueFilter; | |
return $url; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment