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

199 lines
4.4 KiB
PHP
Raw Normal View History

2016-07-21 22:01:57 +08:00
<?php
namespace App\Models;
class Closet
{
public $uid;
/**
* 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
/**
* 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 = [];
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;
$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);
$this->textures = is_null($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-07-21 22:01:57 +08:00
* @param string $category
* @return array
*/
public function getItems($category = "skin")
{
// reverse the array to sort desc by add_at
2016-07-21 22:01:57 +08:00
return array_reverse(($category == "skin") ? $this->textures_skin : $this->textures_cape);
}
/**
* Add an item to the closet.
*
* @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
/**
* Remove a texture from closet
* @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) {
// 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
}
private function checkTextureExist($tid)
{
return ! Texture::where('tid', $tid)->isEmpty();
2016-07-21 22:01:57 +08:00
}
private function save()
{
if (empty($this->items_modified)) return;
$this->db->where('uid', $this->uid)->update(['textures' => json_encode($this->textures)]);
}
public function __destruct()
{
$this->save();
}
2016-07-21 22:01:57 +08:00
}