91 lines
2.6 KiB
PHP
91 lines
2.6 KiB
PHP
|
<?php
|
||
|
|
||
|
namespace WoWPress\Models;
|
||
|
|
||
|
use Wenprise\Eloquent\Model;
|
||
|
use WoWPress\Api\BattleNet;
|
||
|
|
||
|
class Character extends Model
|
||
|
{
|
||
|
|
||
|
protected $table = "wowpress_characters";
|
||
|
public $timestamps = false;
|
||
|
protected $primaryKey = 'ID';
|
||
|
protected $guarded = ['ID'];
|
||
|
|
||
|
private $api;
|
||
|
|
||
|
public function __construct($attrs = [])
|
||
|
{
|
||
|
parent::__construct($attrs);
|
||
|
$key = get_option('wowpress_api')['bnet']['key'];
|
||
|
$id = get_option('wowpress_api')['bnet']['id'];
|
||
|
$this->api = new BattleNet($id,$key);
|
||
|
}
|
||
|
|
||
|
protected function sanitize($string)
|
||
|
{
|
||
|
return strtolower(str_replace(" ", "", $string));
|
||
|
}
|
||
|
|
||
|
public function getColorAttribute()
|
||
|
{
|
||
|
return $this->sanitize($this->class);
|
||
|
}
|
||
|
|
||
|
public function getClassIconAttribute()
|
||
|
{
|
||
|
$classname = $this->sanitize($this->class);
|
||
|
return get_template_directory_uri() . "/icons/class/$classname/$classname.png";
|
||
|
}
|
||
|
|
||
|
public function getSpecIconAttribute()
|
||
|
{
|
||
|
$classname = $this->sanitize($this->class);
|
||
|
$specname = $this->sanitize($this->spec);
|
||
|
return get_template_directory_uri() . "/icons/class/$classname/$specname.png";
|
||
|
}
|
||
|
|
||
|
public function updateFromAPI()
|
||
|
{
|
||
|
$char_from_api = $this->api->getCharacter($this->name,$this->realm);
|
||
|
$this->class = $char_from_api['character_class']['name'];
|
||
|
$this->spec = $char_from_api['active_spec']['name'];
|
||
|
|
||
|
$this->rank = $this->api->getGuildRank($this->name,$this->realm);
|
||
|
|
||
|
$this->save();
|
||
|
return $char_from_api;
|
||
|
}
|
||
|
|
||
|
public function updateMedia($force = false){
|
||
|
$character_dir = wp_upload_dir()['basedir']."/characters/".$this->ID."/";
|
||
|
if(!is_dir($character_dir)){
|
||
|
mkdir($character_dir,0777,true);
|
||
|
}
|
||
|
|
||
|
$char_from_api = $this->api->getMedia($this->name,$this->realm,$force?-1:360);
|
||
|
if(empty($char_from_api['assets'])){
|
||
|
return $char_from_api;
|
||
|
}
|
||
|
foreach($char_from_api['assets'] as $asset){
|
||
|
switch($asset['key']){
|
||
|
case 'avatar':
|
||
|
file_put_contents($character_dir."avatar.jpg",file_get_contents($asset['value']));
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public function getAvatarAttribute(){
|
||
|
$character_dir = wp_upload_dir()['basedir']."/characters/".$this->ID."/avatar.jpg";
|
||
|
$character_dir_uri = wp_upload_dir()['baseurl']."/characters/".$this->ID."/avatar.jpg";
|
||
|
if(file_exists($character_dir)){
|
||
|
return $character_dir_uri;
|
||
|
}else{
|
||
|
return "";
|
||
|
}
|
||
|
|
||
|
}
|
||
|
}
|