2016-07-21 22:01:57 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
|
|
|
use App\Exceptions\E;
|
|
|
|
use Utils;
|
|
|
|
|
|
|
|
class Closet
|
|
|
|
{
|
|
|
|
public $uid;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Instance of App\Models\ClosetModel
|
|
|
|
* @var null
|
|
|
|
*/
|
|
|
|
private $eloquent_model = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Textures array generated from json
|
|
|
|
* @var Array
|
|
|
|
*/
|
|
|
|
private $textures = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Array of App\Models\Texture instances
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private $textures_skin = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Array of App\Models\Texture instances
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private $textures_cape = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Construct Closet object with owner's uid
|
|
|
|
* @param int $uid
|
|
|
|
*/
|
|
|
|
function __construct($uid)
|
|
|
|
{
|
|
|
|
$this->uid = $uid;
|
|
|
|
$this->eloquent_model = ClosetModel::find($uid);
|
|
|
|
|
2016-07-22 19:36:24 +08:00
|
|
|
if ($this->eloquent_model) {
|
|
|
|
$this->textures = json_decode($this->eloquent_model->textures, true);
|
|
|
|
$this->textures = is_null($this->textures) ? [] : $this->textures;
|
2016-07-21 22:01:57 +08:00
|
|
|
|
2016-07-22 19:36:24 +08:00
|
|
|
$textures_invalid = [];
|
2016-07-22 10:45:36 +08:00
|
|
|
|
2016-07-22 19:36:24 +08:00
|
|
|
foreach ($this->textures as $texture) {
|
|
|
|
$result = Texture::find($texture['tid']);
|
|
|
|
if ($result) {
|
|
|
|
// user custom texture name
|
|
|
|
$result->name = $texture['name'];
|
|
|
|
|
|
|
|
if ($result->type == "cape") {
|
|
|
|
$this->textures_cape[] = $result;
|
|
|
|
} else {
|
|
|
|
$this->textures_skin[] = $result;
|
|
|
|
}
|
2016-07-21 22:01:57 +08:00
|
|
|
} else {
|
2016-07-22 19:36:24 +08:00
|
|
|
$textures_invalid[] = $texture['tid'];
|
|
|
|
continue;
|
2016-07-21 22:01:57 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-22 19:36:24 +08:00
|
|
|
foreach ($textures_invalid as $tid) {
|
|
|
|
$this->remove($tid);
|
|
|
|
}
|
|
|
|
|
|
|
|
unset($textures_invalid);
|
2016-07-23 21:43:42 +08:00
|
|
|
} else {
|
|
|
|
$this->eloquent_model = new ClosetModel();
|
|
|
|
$this->eloquent_model->uid = $uid;
|
|
|
|
$this->eloquent_model->save();
|
2016-07-21 22:01:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get array of instances of App\Models\Texture
|
|
|
|
* @param string $category
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getItems($category = "skin")
|
|
|
|
{
|
|
|
|
// need to reverse the array to sort desc by add_at
|
|
|
|
return array_reverse(($category == "skin") ? $this->textures_skin : $this->textures_cape);
|
|
|
|
}
|
|
|
|
|
2016-07-22 10:45:36 +08:00
|
|
|
public function add($tid, $name)
|
2016-07-21 22:01:57 +08:00
|
|
|
{
|
|
|
|
foreach ($this->textures as $item) {
|
|
|
|
if ($item['tid'] == $tid)
|
|
|
|
throw new E('你已经收藏过这个材质啦', 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->textures[] = array(
|
2016-07-22 10:45:36 +08:00
|
|
|
'tid' => $tid,
|
|
|
|
'name' => $name,
|
|
|
|
'add_at' => time()
|
2016-07-21 22:01:57 +08:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->eloquent_model->textures = json_encode($this->textures);
|
|
|
|
return $this->eloquent_model->save();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if texture is in the closet
|
|
|
|
* @param int $tid
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function has($tid)
|
|
|
|
{
|
|
|
|
foreach ($this->textures as $item) {
|
|
|
|
if ($item['tid'] == $tid) return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove a texture from closet
|
|
|
|
* @param int $tid
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function remove($tid)
|
|
|
|
{
|
|
|
|
$offset = 0;
|
|
|
|
// remove array element
|
|
|
|
foreach ($this->textures as $item) {
|
|
|
|
if ($item['tid'] == $tid) {
|
|
|
|
array_splice($this->textures, $offset, 1);
|
|
|
|
$this->eloquent_model->textures = json_encode($this->textures);
|
|
|
|
return $this->eloquent_model->save();
|
|
|
|
}
|
|
|
|
$offset++;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new E('The texture is not in the closet.', 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function checkTextureExist($tid)
|
|
|
|
{
|
|
|
|
return (Texture::where('tid', $tid)->count() > 0) ? true : false;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
class ClosetModel extends \Illuminate\Database\Eloquent\Model
|
|
|
|
{
|
|
|
|
public $primaryKey = 'uid';
|
|
|
|
protected $table = 'closets';
|
|
|
|
public $timestamps = false;
|
|
|
|
}
|