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

193 lines
5.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;
use Auth;
use Blessing\Filter;
2020-05-28 18:44:42 +08:00
use Blessing\Rejection;
use Illuminate\Contracts\Events\Dispatcher;
2020-03-21 11:06:24 +08:00
use Illuminate\Database\Eloquent\Builder;
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');
2016-07-21 22:01:57 +08:00
2020-03-21 11:06:24 +08:00
return auth()
->user()
->closet()
->when($category === 'cape', function (Builder $query) {
return $query->where('type', 'cape');
}, function (Builder $query) {
return $query->whereIn('type', ['steve', 'alex']);
})
->when($request->input('q'), function (Builder $query, $search) {
2020-03-21 11:08:41 +08:00
return $query->like('item_name', $search);
2020-03-21 11:06:24 +08:00
})
->paginate(6);
2016-07-21 22:01:57 +08:00
}
2020-03-24 18:05:46 +08:00
public function allIds()
{
return auth()->user()->closet()->pluck('texture_tid');
}
2020-05-28 18:44:42 +08:00
public function add(
Request $request,
Dispatcher $dispatcher,
Filter $filter
) {
['tid' => $tid, 'name' => $name] = $request->validate([
2019-12-14 11:10:37 +08:00
'tid' => 'required|integer',
'name' => 'required',
]);
2016-07-21 22:01:57 +08:00
2020-05-28 18:44:42 +08:00
/** @var User */
2019-03-14 23:55:49 +08:00
$user = Auth::user();
2020-05-28 18:44:42 +08:00
$name = $filter->apply('add_closet_item_name', $name, [$tid]);
$dispatcher->dispatch('closet.adding', [$tid, $name]);
$can = $filter->apply('can_add_closet_item', true, [$tid, $name]);
if ($can instanceof Rejection) {
return json($can->getReason(), 1);
}
2019-03-23 00:20:28 +08:00
if ($user->score < option('score_per_closet_item')) {
2020-05-28 18:44:42 +08:00
return json(trans('user.closet.add.lack-score'), 1);
}
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);
}
2020-03-14 14:59:52 +08:00
if (!$texture->public && ($texture->uploader != $user->uid && !$user->isAdmin())) {
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();
2020-05-28 18:44:42 +08:00
$dispatcher->dispatch('closet.added', [$texture, $name]);
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
}
2020-05-28 18:44:42 +08:00
public function rename(
Request $request,
Dispatcher $dispatcher,
Filter $filter,
$tid
) {
['name' => $name] = $request->validate(['name' => 'required']);
/** @var User */
2019-03-14 23:55:49 +08:00
$user = auth()->user();
2020-05-28 18:44:42 +08:00
$name = $filter->apply('rename_closet_item_name', $name, [$tid]);
$dispatcher->dispatch('closet.renaming', [$tid, $name]);
$item = $user->closet()->find($tid);
if (empty($item)) {
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
}
2020-05-28 18:44:42 +08:00
$previousName = $item->pivot->item_name;
2019-03-14 23:55:49 +08:00
2020-05-28 18:44:42 +08:00
$can = $filter->apply('can_rename_closet_item', true, [$item, $name]);
if ($can instanceof Rejection) {
return json($can->getReason(), 1);
}
2020-05-28 18:44:42 +08:00
$user->closet()->updateExistingPivot($tid, ['item_name' => $name]);
$dispatcher->dispatch('closet.renamed', [$item, $previousName]);
return json(trans('user.closet.rename.success', ['name' => $name]), 0);
2016-09-25 10:35:16 +08:00
}
2020-05-28 18:44:42 +08:00
public function remove(Dispatcher $dispatcher, Filter $filter, $tid)
2016-07-21 22:01:57 +08:00
{
2020-05-28 18:44:42 +08:00
/** @var User */
2019-03-14 23:55:49 +08:00
$user = auth()->user();
2016-07-21 22:01:57 +08:00
2020-05-28 18:44:42 +08:00
$dispatcher->dispatch('closet.removing', [$tid]);
$item = $user->closet()->find($tid);
if (empty($item)) {
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
2020-05-28 18:44:42 +08:00
$can = $filter->apply('can_remove_closet_item', true, [$item]);
if ($can instanceof Rejection) {
return json($can->getReason(), 1);
}
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();
2020-05-28 18:44:42 +08:00
$dispatcher->dispatch('closet.removed', [$texture]);
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
}
}