WowPress-Tailwind/theme/wowpress/Models/Character.php

137 lines
4.0 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);
$options = get_option('wowpress_api');
if(isset($options['bnet'])){
$key = get_option('wowpress_api')['bnet']['key'];
$id = get_option('wowpress_api')['bnet']['id'];
}else{
return false;
}
$this->api = new BattleNet($id,$key);
}
public function user(){
return $this->belongsTo(User::class);
}
public function getCanEditAttribute(){
return current_user_can('wowpress_edit_raids') || (get_current_user_id() == $this->ID);
}
protected function sanitize($string,$delimiter_space="")
{
$string = strtolower(str_replace(" ", $delimiter_space, $string));
$string = strtolower(str_replace("'", "", $string));
return $string;
}
public function getRankAttribute($rank){
switch($rank){
case 0: return "Gildenmeister";
case 1: return "Offizier";
case 2: return "Offi-Twink";
case 3: return "Raidlead";
case 4: return "Ehrenmitglied";
case 5: return "Raider";
case 6: return "Raider-Twink";
case 7: return "F&F";
case 8: return "Novize";
case 9: return "Sleeper";
default: return "Gast";
}
}
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()
{
$realm = $this->sanitize($this->realm,"-");
$char_from_api = $this->api->getCharacter($this->name,$realm);
if(!array_key_exists('character_class',$char_from_api)){
return false;
}
$this->class = $char_from_api['character_class']['name'];
$this->spec = $char_from_api['active_spec']['name'];
$this->id_blizz = $char_from_api['id'];
$guild_data = $this->api->getGuildRank($this->name,$realm);
if(!empty($guild_data['rank'])){
$this->guild = $guild_data['guild'];
$this->rank = $guild_data['rank'];
}else{
$this->rank = 99;
}
$this->save();
return $char_from_api;
}
public function updateMedia($force = false){
if(empty($this->id_blizz)){
return false;
}
$character_dir = wp_upload_dir()['basedir']."/characters/".$this->ID."/";
if(!is_dir($character_dir)){
mkdir($character_dir,0777,true);
}
$id = $this->id_blizz;
$mod = $id % 256;
$realm = $this->sanitize($this->realm,'-');
$img_url = "https://render.worldofwarcraft.com/eu/character/$realm/$mod/$id-avatar.jpg";
file_put_contents($character_dir."avatar.jpg",file_get_contents($img_url));
}
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 "";
}
}
public function getClassButtonAttribute(){
require(get_template_directory()."/components/class-button.php");
}
}