blessing-skin-server/app/Models/Closet.php

242 lines
5.2 KiB
PHP
Raw Normal View History

2016-07-21 22:01:57 +08:00
<?php
namespace App\Models;
2016-10-23 11:41:52 +08:00
use DB;
2016-07-21 22:01:57 +08:00
class Closet
{
public $uid;
/**
2016-10-23 11:41:52 +08:00
* Instance of Query Builder.
*
* @var \Illuminate\Database\Query\Builder
2016-07-21 22:01:57 +08:00
*/
private $db;
2016-07-21 22:01:57 +08:00
/**
2016-10-23 11:41:52 +08:00
* Textures array generated from json.
*
2016-07-21 22:01:57 +08:00
* @var Array
*/
private $textures = [];
/**
2016-10-23 11:41:52 +08:00
* Array of App\Models\Texture instances.
*
2016-07-21 22:01:57 +08:00
* @var array
*/
private $textures_skin = [];
/**
2016-10-23 11:41:52 +08:00
* Array of App\Models\Texture instances.
*
2016-07-21 22:01:57 +08:00
* @var array
*/
private $textures_cape = [];
2016-10-23 11:41:52 +08:00
/**
* Items that are modified.
*
* @var array
*/
private $items_modified = [];
2016-07-21 22:01:57 +08:00
/**
* Construct Closet object with owner's uid.
*
2016-07-21 22:01:57 +08:00
* @param int $uid
*/
public function __construct($uid)
2016-07-21 22:01:57 +08:00
{
$this->uid = $uid;
2016-10-23 11:41:52 +08:00
$this->db = DB::table('closets');
// create a new closet if not exists
if (!$this->db->where('uid', $uid)->get()) {
$this->db->insert([
'uid' => $uid,
'textures' => ''
]);
}
2016-07-21 22:01:57 +08:00
// load items from json string
$this->textures = json_decode($this->db->where('uid', $uid)->get()[0]->textures, true);
2016-12-16 22:51:50 +08:00
$this->textures = is_array($this->textures) ? $this->textures : [];
2016-07-21 22:01:57 +08:00
$textures_invalid = [];
2016-07-22 10:45:36 +08:00
// traverse items in the closet
foreach ($this->textures as $texture) {
$result = Texture::find($texture['tid']);
2016-07-22 19:36:24 +08:00
if ($result) {
// set user custom texture name
$result->name = $texture['name'];
// push instances of App\Models\Texture to the bag
if ($result->type == "cape") {
$this->textures_cape[] = $result;
2016-07-21 22:01:57 +08:00
} else {
$this->textures_skin[] = $result;
2016-07-21 22:01:57 +08:00
}
} else {
$textures_invalid[] = $texture['tid'];
continue;
2016-07-21 22:01:57 +08:00
}
}
2016-07-21 22:01:57 +08:00
// remove invalid textures from closet
foreach ($textures_invalid as $tid) {
$this->remove($tid);
2016-07-21 22:01:57 +08:00
}
unset($textures_invalid);
2016-07-21 22:01:57 +08:00
}
/**
* Get array of instances of App\Models\Texture.
*
2016-10-16 20:04:21 +08:00
* @param string $category skin|cape|all
2016-07-21 22:01:57 +08:00
* @return array
*/
2016-10-16 20:04:21 +08:00
public function getItems($category = "all")
2016-07-21 22:01:57 +08:00
{
2016-10-16 20:04:21 +08:00
if ($category == "all") {
$items = array_merge($this->textures_skin, $this->textures_cape);
} else {
$property = "textures_$category";
$items = $this->$property;
}
// reverse the array to sort desc by add_at
2016-10-16 20:04:21 +08:00
return array_reverse($items);
2016-07-21 22:01:57 +08:00
}
/**
* Add an item to the closet.
*
2016-10-23 11:41:52 +08:00
* @param int $tid
* @param string $name
* @return bool
*/
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)
2016-09-10 21:39:45 +08:00
return false;
2016-07-21 22:01:57 +08:00
}
$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->items_modified[] = $tid;
return true;
2016-07-21 22:01:57 +08:00
}
/**
* Check if texture is in the closet.
*
2016-07-21 22:01:57 +08:00
* @param int $tid
* @return bool
2016-07-21 22:01:57 +08:00
*/
public function has($tid)
{
foreach ($this->textures as $item) {
if ($item['tid'] == $tid) return true;
}
return false;
}
2016-09-25 10:35:16 +08:00
/**
* Rename closet item.
*
* @param integer $tid
* @param string $new_name
* @return void
*/
public function rename($tid, $new_name)
{
$offset = 0;
foreach ($this->textures as $item) {
if ($item['tid'] == $tid) {
$this->textures[$offset]['name'] = $new_name;
}
$offset++;
}
$this->items_modified[] = $tid;
return true;
2016-09-25 10:35:16 +08:00
}
2016-07-21 22:01:57 +08:00
/**
2016-10-23 11:41:52 +08:00
* Remove a texture from closet.
2016-07-21 22:01:57 +08:00
* @param int $tid
* @return boolean
*/
public function remove($tid)
{
$offset = 0;
// traverse items
2016-07-21 22:01:57 +08:00
foreach ($this->textures as $item) {
if ($item['tid'] == $tid) {
2016-10-30 15:31:49 +08:00
$this->items_modified[] = $tid;
// remove element from array
return array_splice($this->textures, $offset, 1);
2016-07-21 22:01:57 +08:00
}
$offset++;
}
2016-09-10 21:39:45 +08:00
return false;
2016-07-21 22:01:57 +08:00
}
2016-10-23 11:41:52 +08:00
/**
* Check if given tid is valid.
*
* @param int $tid
* @return bool
*/
2016-07-21 22:01:57 +08:00
private function checkTextureExist($tid)
{
return ! Texture::where('tid', $tid)->isEmpty();
2016-07-21 22:01:57 +08:00
}
/**
* Set textures string manually.
*
* @param string $textures
*/
public function setTextures($textures)
{
return $this->db->where('uid', $this->uid)->update(['textures' => $textures]);;
}
2016-10-23 11:41:52 +08:00
/**
* Do really database operations.
*
* @return bool
*/
public function save()
{
if (empty($this->items_modified)) return;
return $this->setTextures(json_encode($this->textures));
}
2016-10-23 11:41:52 +08:00
/**
* Save when the object will be destructed.
*/
public function __destruct()
{
$this->save();
}
2016-07-21 22:01:57 +08:00
}