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

260 lines
6.0 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;
2017-11-05 16:54:01 +08:00
use Illuminate\Support\Collection;
2016-10-23 11:41:52 +08:00
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.
*
2017-11-05 16:54:01 +08:00
* @var Collection
2016-07-21 22:01:57 +08:00
*/
2017-11-05 16:54:01 +08:00
private $textures;
2016-07-21 22:01:57 +08:00
/**
2017-11-05 16:54:01 +08:00
* Indicates if closet has been modified
2016-10-23 11:41:52 +08:00
*
2016-07-21 22:01:57 +08:00
* @var array
*/
2017-11-05 16:54:01 +08:00
private $closet_modified = false;
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');
2018-02-16 17:31:04 +08:00
// Create a new closet if not exists
2018-07-11 16:45:07 +08:00
if ($this->db->where('uid', $uid)->count() == 0) {
$this->db->insert([
'uid' => $uid,
2017-11-05 16:54:01 +08:00
'textures' => '[]'
]);
}
2016-07-21 22:01:57 +08:00
2018-02-16 17:31:04 +08:00
// Load items from json string
2017-11-05 16:54:01 +08:00
$this->textures = collect(json_decode(
$this->db->where('uid', $uid)->first()->textures,
true
));
2016-07-22 10:45:36 +08:00
2018-02-16 17:31:04 +08:00
// Traverse items in the closet
$removedCount = $this->textures->filter(function ($texture) use ($uid) {
2017-11-07 20:45:29 +08:00
$t = Texture::find($texture['tid']);
2018-02-16 17:31:04 +08:00
// If the texture was deleted
2017-11-07 20:45:29 +08:00
if (is_null($t)) {
return true;
}
2018-07-16 11:10:01 +08:00
if (!$t->public && $t->uploader != $uid && !app('users')->get($uid)->isAdmin()) {
2017-11-07 20:45:29 +08:00
return true;
}
return false;
})->each(function ($texture) use ($uid) {
$this->remove($texture['tid']);
})->count();
2018-02-16 17:31:04 +08:00
// Return scores if the texture was deleted or set as private
if (option('return_score')) {
app('users')->get($uid)->setScore(
option('score_per_closet_item') * $removedCount,
'plus'
);
}
2016-07-21 22:01:57 +08:00
}
/**
* Get array of instances of App\Models\Texture.
*
2018-02-16 17:31:04 +08:00
* @param string $category "skin" or "cape" or "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
{
2017-11-05 16:54:01 +08:00
$textures = Texture::whereIn('tid', $this->textures->pluck('tid')->all())
->get()
->map(function ($texture) {
$in_closet = $this->textures
->where('tid', $texture->tid)
->first();
return [
'tid' => $texture->tid,
'name' => $in_closet['name'],
'type' => $texture->type,
'add_at' => $in_closet['add_at']
];
})
->sortByDesc('add_at');
2016-10-16 20:04:21 +08:00
if ($category == "all") {
2017-11-05 16:54:01 +08:00
return $textures->values()->all();
} elseif ($category == 'cape') {
return $textures->filter(function ($texture) {
return $texture['type'] == 'cape';
})->values();
2016-10-16 20:04:21 +08:00
} else {
2017-11-05 16:54:01 +08:00
return $textures->reject(function ($texture) {
return $texture['type'] == 'cape';
})->values();
2016-10-16 20:04:21 +08:00
}
2016-07-21 22:01:57 +08:00
}
/**
* Add an item to the closet.
*
2018-02-16 17:31:04 +08:00
* @param int $tid
2016-10-23 11:41:52 +08:00
* @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
{
2017-11-05 16:54:01 +08:00
if ($this->has($tid)) {
return false;
2016-07-21 22:01:57 +08:00
}
2017-11-05 16:54:01 +08:00
$this->textures->push([
2017-11-05 19:29:30 +08:00
'tid' => (int) $tid,
2016-07-22 10:45:36 +08:00
'name' => $name,
'add_at' => time()
2017-11-05 16:54:01 +08:00
]);
2016-07-21 22:01:57 +08:00
2017-11-05 16:54:01 +08:00
$this->closet_modified = true;
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)
{
2017-11-05 16:54:01 +08:00
return $this->textures->contains('tid', $tid);
}
/**
* Get one texture info
*
2018-02-16 17:31:04 +08:00
* @param int $tid
2017-11-05 16:54:01 +08:00
* @return array|null Result
*/
public function get($tid)
{
return $this->textures->where('tid', $tid)->first();
2016-07-21 22:01:57 +08:00
}
2016-09-25 10:35:16 +08:00
/**
* Rename closet item.
*
* @param integer $tid
2018-02-16 17:31:04 +08:00
* @param string $newName
2017-11-04 17:18:01 +08:00
* @return bool
2016-09-25 10:35:16 +08:00
*/
2018-02-16 17:31:04 +08:00
public function rename($tid, $newName)
2016-09-25 10:35:16 +08:00
{
2018-02-16 17:31:04 +08:00
if (! $this->has($tid)) {
2017-11-04 17:18:01 +08:00
return false;
}
2018-02-16 17:31:04 +08:00
$this->textures->transform(function ($texture) use ($tid, $newName) {
2017-11-05 16:54:01 +08:00
if ($texture['tid'] == $tid) {
2018-02-16 17:31:04 +08:00
$texture['name'] = $newName;
2016-09-25 10:35:16 +08:00
}
2017-11-05 16:54:01 +08:00
return $texture;
});
2016-09-25 10:35:16 +08:00
2017-11-05 16:54:01 +08:00
$this->closet_modified = true;
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.
2018-02-16 17:31:04 +08:00
*
2016-07-21 22:01:57 +08:00
* @param int $tid
2018-02-16 17:31:04 +08:00
* @return bool
2016-07-21 22:01:57 +08:00
*/
public function remove($tid)
{
2018-02-16 17:31:04 +08:00
if (! $this->has($tid)) {
2017-11-05 16:54:01 +08:00
return false;
2016-07-21 22:01:57 +08:00
}
2017-11-05 16:54:01 +08:00
$this->textures = $this->textures->reject(function ($texture) use ($tid) {
return $texture['tid'] == $tid;
});
2016-07-21 22:01:57 +08:00
2017-11-05 16:54:01 +08:00
$this->closet_modified = true;
return true;
2016-07-21 22:01:57 +08:00
}
/**
* Set textures string manually.
*
* @param string $textures
2017-11-05 16:54:01 +08:00
* @return int
*/
public function setTextures($textures)
{
2017-11-05 16:54:01 +08:00
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()
{
2018-02-16 17:31:04 +08:00
if (! $this->closet_modified) {
return false;
}
2018-07-11 16:10:45 +08:00
$this->closet_modified = false;
2018-01-14 19:57:06 +08:00
return $this->setTextures($this->textures->values()->toJson());
}
2016-10-23 11:41:52 +08:00
/**
* Save when the object will be destructed.
2018-02-16 17:31:04 +08:00
*
* @return void
2016-10-23 11:41:52 +08:00
*/
public function __destruct()
{
$this->save();
}
2016-07-21 22:01:57 +08:00
/**
* Get all closets.
*
* @return array
*/
public static function all()
{
$result = [];
foreach (DB::table('closets')->pluck('uid') as $uid) {
$result[] = new Closet($uid);
}
return $result;
}
2016-07-21 22:01:57 +08:00
}