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

270 lines
8.1 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
2016-10-16 18:16:15 +08:00
use App\Events\PlayerWasAdded;
use App\Events\PlayerWasDeleted;
2016-10-30 14:12:22 +08:00
use App\Events\PlayerWillBeAdded;
2016-11-07 22:16:27 +08:00
use App\Events\PlayerWillBeDeleted;
use App\Http\Middleware\CheckPlayerExist;
use App\Http\Middleware\CheckPlayerOwner;
2019-12-14 11:10:37 +08:00
use App\Models\Player;
use App\Models\Texture;
use App\Rules;
2019-12-14 11:10:37 +08:00
use Auth;
use Blessing\Filter;
use Blessing\Rejection;
2019-12-14 11:10:37 +08:00
use Event;
2019-08-31 12:14:39 +08:00
use Illuminate\Contracts\Events\Dispatcher;
2019-12-14 11:10:37 +08:00
use Illuminate\Http\Request;
use Option;
use View;
2016-07-21 22:01:57 +08:00
class PlayerController extends Controller
2016-07-21 22:01:57 +08:00
{
public function __construct()
2016-07-21 22:01:57 +08:00
{
2018-02-16 17:31:04 +08:00
$this->middleware([CheckPlayerExist::class, CheckPlayerOwner::class], [
'only' => ['delete', 'rename', 'setTexture', 'clearTexture'],
2018-02-16 17:31:04 +08:00
]);
2016-07-21 22:01:57 +08:00
}
2019-12-15 22:52:49 +08:00
public function index(Filter $filter)
2016-07-21 22:01:57 +08:00
{
2019-12-15 22:52:49 +08:00
$grid = [
'layout' => [
2019-12-15 23:16:55 +08:00
['md-6', 'md-6'],
2019-12-15 22:52:49 +08:00
],
'widgets' => [
[
[
'user.widgets.players.list',
2019-12-15 23:16:55 +08:00
'user.widgets.players.notice',
2019-12-15 22:52:49 +08:00
],
2019-12-15 23:16:55 +08:00
['shared.previewer'],
],
],
2019-12-15 22:52:49 +08:00
];
$grid = $filter->apply('grid:user.player', $grid);
return view('user.player')
2019-12-15 22:52:49 +08:00
->with('grid', $grid)
2019-03-23 19:52:14 +08:00
->with('extra', [
'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')]
2019-04-19 19:36:36 +08:00
),
2020-02-08 17:09:43 +08:00
'score' => auth()->user()->score,
'cost' => (int) option('score_per_player'),
2019-03-23 19:52:14 +08:00
]);
2016-07-21 22:01:57 +08:00
}
2018-07-30 15:45:08 +08:00
public function listAll()
{
2019-12-31 18:34:52 +08:00
$players = Auth::user()
->players()
->select('pid', 'name', 'tid_skin', 'tid_cape')
->get()
->toArray();
return json('', 0, $players);
2018-07-30 15:45:08 +08:00
}
2020-01-05 11:05:22 +08:00
public function add(Request $request, Dispatcher $dispatcher)
2016-07-21 22:01:57 +08:00
{
$user = Auth::user();
2019-03-22 21:40:12 +08:00
if (option('single_player', false)) {
return json(trans('user.player.add.single'), 1);
}
2019-04-23 23:03:12 +08:00
$name = $this->validate($request, [
'name' => [
'required',
new Rules\PlayerName(),
'min:'.option('player_name_length_min'),
'max:'.option('player_name_length_max'),
],
2019-04-23 23:03:12 +08:00
])['name'];
2016-07-21 22:01:57 +08:00
2020-01-05 11:05:22 +08:00
$dispatcher->dispatch('player.add.attempt', [$name, $user]);
2016-10-30 11:57:26 +08:00
2019-12-14 11:10:37 +08:00
if (!Player::where('name', $name)->get()->isEmpty()) {
2016-09-24 22:49:20 +08:00
return json(trans('user.player.add.repeated'), 6);
2016-10-07 16:06:38 +08:00
}
2016-07-21 22:01:57 +08:00
2019-03-23 00:20:28 +08:00
if ($user->score < Option::get('score_per_player')) {
2016-09-24 22:49:20 +08:00
return json(trans('user.player.add.lack-score'), 7);
2016-10-07 16:06:38 +08:00
}
2016-07-21 22:01:57 +08:00
2020-01-05 11:05:22 +08:00
$dispatcher->dispatch('player.adding', [$name, $user]);
2019-04-23 23:03:12 +08:00
event(new PlayerWillBeAdded($name));
2016-10-30 14:12:22 +08:00
2019-12-14 11:10:37 +08:00
$player = new Player();
$player->uid = $user->uid;
2019-04-23 23:03:12 +08:00
$player->name = $name;
$player->tid_skin = 0;
2019-04-26 21:38:26 +08:00
$player->tid_cape = 0;
2016-10-16 18:16:15 +08:00
$player->save();
2019-07-30 14:29:02 +08:00
$user->score -= option('score_per_player');
$user->save();
2016-07-21 22:01:57 +08:00
2020-01-05 11:05:22 +08:00
$dispatcher->dispatch('player.added', [$player, $user]);
event(new PlayerWasAdded($player));
2019-04-24 13:10:03 +08:00
return json(trans('user.player.add.success', ['name' => $name]), 0, $player->toArray());
2016-07-21 22:01:57 +08:00
}
2020-01-05 11:05:22 +08:00
public function delete(Dispatcher $dispatcher, $pid)
2016-07-21 22:01:57 +08:00
{
2020-01-05 11:05:22 +08:00
$user = auth()->user();
2019-04-24 13:10:03 +08:00
$player = Player::find($pid);
$playerName = $player->name;
2016-07-21 22:01:57 +08:00
2020-01-05 11:05:22 +08:00
$dispatcher->dispatch('player.delete.attempt', [$player, $user]);
2019-03-22 21:40:12 +08:00
if (option('single_player', false)) {
return json(trans('user.player.delete.single'), 1);
}
2020-01-05 11:05:22 +08:00
$dispatcher->dispatch('player.deleting', [$player, $user]);
2019-04-24 13:10:03 +08:00
event(new PlayerWillBeDeleted($player));
2016-11-07 22:16:27 +08:00
2019-04-24 13:10:03 +08:00
$player->delete();
2018-02-16 17:31:04 +08:00
if (option('return_score')) {
2019-07-30 14:29:02 +08:00
$user->score += option('score_per_player');
$user->save();
2018-02-16 17:31:04 +08:00
}
2016-07-21 22:01:57 +08:00
2020-01-05 11:05:22 +08:00
$dispatcher->dispatch('player.deleted', [$player, $user]);
2018-02-16 17:31:04 +08:00
event(new PlayerWasDeleted($playerName));
2016-10-16 18:16:15 +08:00
2018-02-16 17:31:04 +08:00
return json(trans('user.player.delete.success', ['name' => $playerName]), 0);
2016-07-21 22:01:57 +08:00
}
2019-09-03 18:41:19 +08:00
public function rename(
Request $request,
Dispatcher $dispatcher,
Filter $filter,
$pid
) {
2019-04-24 13:10:03 +08:00
$newName = $this->validate($request, [
'name' => [
'required',
new Rules\PlayerName(),
'min:'.option('player_name_length_min'),
'max:'.option('player_name_length_max'),
],
2019-04-24 13:10:03 +08:00
])['name'];
$player = Player::find($pid);
2016-07-21 22:01:57 +08:00
2019-09-02 20:42:26 +08:00
$dispatcher->dispatch('player.renaming', [$player, $newName]);
2019-08-31 12:14:39 +08:00
2019-09-03 23:07:10 +08:00
$can = $filter->apply('user_can_rename_player', true, [$player, $newName]);
2019-09-02 23:33:51 +08:00
if ($can instanceof Rejection) {
return json($can->getReason(), 1);
}
2019-12-14 11:10:37 +08:00
if (!Player::where('name', $newName)->get()->isEmpty()) {
2016-09-24 22:49:20 +08:00
return json(trans('user.player.rename.repeated'), 6);
2016-10-07 16:06:38 +08:00
}
2019-04-24 13:10:03 +08:00
$oldName = $player->name;
$player->name = $newName;
$player->save();
2016-07-21 22:01:57 +08:00
2019-03-22 21:40:12 +08:00
if (option('single_player', false)) {
$user = auth()->user();
$user->nickname = $newName;
$user->save();
}
2019-09-02 20:42:26 +08:00
$dispatcher->dispatch('player.renamed', [$player, $oldName]);
2019-08-31 12:14:39 +08:00
2019-04-24 13:10:03 +08:00
return json(trans('user.player.rename.success', ['old' => $oldName, 'new' => $newName]), 0, $player->toArray());
2016-07-21 22:01:57 +08:00
}
2020-01-05 11:05:22 +08:00
public function setTexture(Request $request, Dispatcher $dispatcher, $pid)
2016-07-21 22:01:57 +08:00
{
2019-04-24 13:10:03 +08:00
$player = Player::find($pid);
foreach (['skin', 'cape'] as $type) {
2020-01-05 11:05:22 +08:00
$tid = $request->input($type);
if ($tid) {
2019-04-24 13:10:03 +08:00
$texture = Texture::find($tid);
2019-12-14 11:10:37 +08:00
if (!$texture) {
2019-04-24 13:10:03 +08:00
return json(trans('skinlib.non-existent'), 1);
}
2018-02-16 17:31:04 +08:00
2020-01-05 11:05:22 +08:00
$dispatcher->dispatch('player.texture.updating', [$player, $texture]);
2019-04-24 13:10:03 +08:00
$field = "tid_$type";
$player->$field = $tid;
$player->save();
2020-01-05 11:05:22 +08:00
$dispatcher->dispatch('player.texture.updated', [$player, $texture]);
2018-02-16 17:31:04 +08:00
}
}
2016-07-21 22:01:57 +08:00
2019-04-24 13:10:03 +08:00
return json(trans('user.player.set.success', ['name' => $player->name]), 0, $player->toArray());
2016-07-21 22:01:57 +08:00
}
2020-01-05 11:05:22 +08:00
public function clearTexture(Request $request, Dispatcher $dispatcher, $pid)
2016-07-21 22:01:57 +08:00
{
2019-04-24 13:10:03 +08:00
$player = Player::find($pid);
2020-01-05 11:05:22 +08:00
$types = $request->input('type', []);
foreach (['skin', 'cape'] as $type) {
if ($request->has($type) || in_array($type, $types)) {
$dispatcher->dispatch('player.texture.resetting', [$player, $type]);
2019-03-23 00:20:28 +08:00
$field = "tid_$type";
2019-04-24 13:10:03 +08:00
$player->$field = 0;
2020-01-05 11:05:22 +08:00
$player->save();
$dispatcher->dispatch('player.texture.reset', [$player, $type]);
2019-03-23 00:20:28 +08:00
}
2020-01-05 11:05:22 +08:00
}
2019-04-19 19:36:36 +08:00
2019-04-24 13:10:03 +08:00
return json(trans('user.player.clear.success', ['name' => $player->name]), 0, $player->toArray());
2016-07-21 22:01:57 +08:00
}
2019-03-22 21:40:12 +08:00
2020-01-05 11:05:22 +08:00
public function bind(Request $request, Dispatcher $dispatcher)
2019-03-22 21:40:12 +08:00
{
$name = $this->validate($request, [
'player' => [
'required',
new Rules\PlayerName(),
'min:'.option('player_name_length_min'),
'max:'.option('player_name_length_max'),
],
2019-03-22 21:40:12 +08:00
])['player'];
$user = Auth::user();
$player = Player::where('name', $name)->first();
2019-12-14 11:10:37 +08:00
if (!$player) {
2020-01-05 11:05:22 +08:00
$dispatcher->dispatch('player.adding', [$name, $user]);
2019-03-22 21:40:12 +08:00
event(new PlayerWillBeAdded($name));
2019-12-14 11:10:37 +08:00
$player = new Player();
2019-03-22 21:40:12 +08:00
$player->uid = $user->uid;
$player->name = $name;
$player->tid_skin = 0;
$player->save();
2020-01-05 11:05:22 +08:00
$dispatcher->dispatch('player.added', [$player, $user]);
2019-03-22 21:40:12 +08:00
event(new PlayerWasAdded($player));
} elseif ($player->uid != $user->uid) {
return json(trans('user.player.rename.repeated'), 1);
}
$user->players()->where('name', '<>', $name)->delete();
$user->nickname = $name;
$user->save();
return json(trans('user.player.bind.success'), 0);
}
2016-07-21 22:01:57 +08:00
}