blessing-skin-server/app/Http/Controllers/ClosetController.php

167 lines
4.8 KiB
PHP
Raw Normal View History

2016-07-21 22:01:57 +08:00
<?php
2016-08-28 10:05:21 +08:00
namespace App\Http\Controllers;
2016-07-21 22:01:57 +08:00
use App\Models\Texture;
2019-12-14 11:10:37 +08:00
use App\Models\User;
2019-12-14 14:30:38 +08:00
use App\Services\Filter;
2019-12-14 11:10:37 +08:00
use Auth;
use Illuminate\Http\Request;
2016-07-21 22:01:57 +08:00
class ClosetController extends Controller
2016-07-21 22:01:57 +08:00
{
2019-12-14 14:30:38 +08:00
public function index(Filter $filter)
{
2019-12-14 14:30:38 +08:00
$grid = [
'layout' => [
['md-8', 'md-4'],
],
'widgets' => [
[
[
'user.widgets.email-verification',
'user.widgets.closet.list',
],
['shared.previewer'],
],
],
];
$grid = $filter->apply('grid:user.closet', $grid);
2019-03-23 19:52:14 +08:00
return view('user.closet')
2019-12-14 14:30:38 +08:00
->with('grid', $grid)
2019-04-01 21:45:59 +08:00
->with('extra', [
2019-12-14 11:10:37 +08:00
'unverified' => option('require_verification') && !auth()->user()->verified,
2019-04-01 21:45:59 +08:00
'rule' => trans('user.player.player-name-rule.'.option('player_name_rule')),
'length' => trans(
'user.player.player-name-length',
['min' => option('player_name_length_min'), 'max' => option('player_name_length_max')]
),
]);
}
public function getClosetData(Request $request)
2016-07-21 22:01:57 +08:00
{
$category = $request->input('category', 'skin');
$page = abs($request->input('page', 1));
2019-03-14 23:55:49 +08:00
$perPage = (int) $request->input('perPage', 6);
$q = $request->input('q', null);
2016-07-21 22:01:57 +08:00
2019-03-14 23:55:49 +08:00
$perPage = $perPage > 0 ? $perPage : 6;
2017-11-06 11:07:24 +08:00
2019-03-14 23:55:49 +08:00
$user = auth()->user();
$closet = $user->closet();
2016-07-21 22:01:57 +08:00
2019-03-14 23:55:49 +08:00
if ($category == 'cape') {
$closet = $closet->where('type', 'cape');
2016-10-16 20:04:21 +08:00
} else {
2019-03-14 23:55:49 +08:00
$closet = $closet->where(function ($query) {
return $query->where('type', 'steve')->orWhere('type', 'alex');
});
}
if ($q) {
$closet = $closet->where('item_name', 'like', "%$q%");
2016-10-16 20:04:21 +08:00
}
2019-05-03 23:17:17 +08:00
$total = $closet->count();
2019-03-14 23:55:49 +08:00
$closet->offset(($page - 1) * $perPage)->limit($perPage);
2019-05-03 23:17:17 +08:00
$totalPages = ceil($total / $perPage);
2019-03-14 23:55:49 +08:00
$items = $closet->get()->map(function ($t) {
$t->name = $t->pivot->item_name;
2019-03-14 23:55:49 +08:00
return $t;
});
2016-07-21 22:01:57 +08:00
2019-04-23 19:14:41 +08:00
return json('', 0, [
'category' => $category,
'items' => $items,
2019-03-14 23:55:49 +08:00
'total_pages' => $totalPages,
]);
2016-07-21 22:01:57 +08:00
}
2019-09-07 11:15:23 +08:00
public function add(Request $request)
2016-07-21 22:01:57 +08:00
{
$this->validate($request, [
2019-12-14 11:10:37 +08:00
'tid' => 'required|integer',
'name' => 'required|no_special_chars',
]);
2016-07-21 22:01:57 +08:00
2019-03-14 23:55:49 +08:00
$user = Auth::user();
2019-03-23 00:20:28 +08:00
if ($user->score < option('score_per_closet_item')) {
return json(trans('user.closet.add.lack-score'), 7);
}
2017-11-04 17:18:01 +08:00
$tid = $request->tid;
2019-03-20 23:28:04 +08:00
$texture = Texture::find($tid);
2019-12-14 11:10:37 +08:00
if (!$texture) {
2017-11-04 17:18:01 +08:00
return json(trans('user.closet.add.not-found'), 1);
}
2019-12-14 11:10:37 +08:00
if (!$texture->public && $texture->uploader != $user->uid) {
2019-04-28 11:38:38 +08:00
return json(trans('skinlib.show.private'), 1);
}
2019-03-14 23:55:49 +08:00
if ($user->closet()->where('tid', $request->tid)->count() > 0) {
2016-09-24 22:49:20 +08:00
return json(trans('user.closet.add.repeated'), 1);
2016-07-21 22:01:57 +08:00
}
2019-03-14 23:55:49 +08:00
$user->closet()->attach($tid, ['item_name' => $request->name]);
2019-07-30 14:29:02 +08:00
$user->score -= option('score_per_closet_item');
$user->save();
2019-03-14 23:55:49 +08:00
$texture->likes++;
$texture->save();
2019-08-24 10:22:26 +08:00
$uploader = User::find($texture->uploader);
2019-03-20 23:28:04 +08:00
if ($uploader && $uploader->uid != $user->uid) {
$uploader->score += option('score_award_per_like', 0);
$uploader->save();
}
2019-03-14 23:55:49 +08:00
return json(trans('user.closet.add.success', ['name' => $request->input('name')]), 0);
2016-07-21 22:01:57 +08:00
}
2019-04-28 11:38:38 +08:00
public function rename(Request $request, $tid)
2016-09-25 10:35:16 +08:00
{
2019-04-28 11:38:38 +08:00
$this->validate($request, ['name' => 'required|no_special_chars']);
2019-03-14 23:55:49 +08:00
$user = auth()->user();
2019-03-14 23:55:49 +08:00
if ($user->closet()->where('tid', $request->tid)->count() == 0) {
2017-11-04 17:18:01 +08:00
return json(trans('user.closet.remove.non-existent'), 1);
2016-09-25 10:35:16 +08:00
}
2019-03-14 23:55:49 +08:00
2019-04-28 11:38:38 +08:00
$user->closet()->updateExistingPivot($request->tid, ['item_name' => $request->name]);
2019-04-28 11:38:38 +08:00
return json(trans('user.closet.rename.success', ['name' => $request->name]), 0);
2016-09-25 10:35:16 +08:00
}
2019-09-07 11:15:23 +08:00
public function remove($tid)
2016-07-21 22:01:57 +08:00
{
2019-03-14 23:55:49 +08:00
$user = auth()->user();
2016-07-21 22:01:57 +08:00
2019-03-20 23:28:04 +08:00
if ($user->closet()->where('tid', $tid)->count() == 0) {
2019-03-14 23:55:49 +08:00
return json(trans('user.closet.remove.non-existent'), 1);
}
2018-07-11 16:10:45 +08:00
2019-03-20 23:28:04 +08:00
$user->closet()->detach($tid);
2019-03-14 23:55:49 +08:00
if (option('return_score')) {
2019-07-30 14:29:02 +08:00
$user->score += option('score_per_closet_item');
$user->save();
2016-07-21 22:01:57 +08:00
}
2019-03-14 23:55:49 +08:00
$texture = Texture::find($tid);
$texture->likes--;
$texture->save();
2019-08-24 10:22:26 +08:00
$uploader = User::find($texture->uploader);
2019-03-20 23:28:04 +08:00
$uploader->score -= option('score_award_per_like', 0);
$uploader->save();
2019-03-14 23:55:49 +08:00
return json(trans('user.closet.remove.success'), 0);
2016-07-21 22:01:57 +08:00
}
}