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

223 lines
6.4 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 View;
use Event;
use Option;
2016-07-21 22:01:57 +08:00
use App\Models\User;
use App\Models\Player;
use App\Models\Texture;
use Illuminate\Http\Request;
2016-10-16 18:16:15 +08:00
use App\Events\PlayerWasAdded;
use App\Events\PlayerWasDeleted;
2016-10-30 11:57:26 +08:00
use App\Events\CheckPlayerExists;
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 Illuminate\Support\Facades\Auth;
use App\Http\Middleware\CheckPlayerExist;
use App\Http\Middleware\CheckPlayerOwner;
2016-07-21 22:01:57 +08:00
class PlayerController extends Controller
2016-07-21 22:01:57 +08:00
{
/**
* Player Instance.
*
* @var \App\Models\Player
*/
private $player;
public function __construct()
2016-07-21 22:01:57 +08:00
{
$this->middleware(function ($request, $next) {
if ($request->has('pid')) {
if ($this->player = Player::find($request->pid)) {
2019-03-23 00:20:28 +08:00
foreach (['skin', 'cape'] as $type) {
$field = "tid_$type";
if (! Texture::find($this->player->$field)) {
$this->player->$field = 0;
}
}
$this->player->save();
}
}
return $next($request);
});
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
}
public function index()
{
return view('user.player')
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
),
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-04-23 19:14:41 +08:00
return json(
'',
0,
Auth::user()
->players()
->select('pid', 'name', 'tid_skin', 'tid_cape')
->get()
->toArray()
);
2018-07-30 15:45:08 +08:00
}
public function add(Request $request)
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);
}
$this->validate($request, [
'player_name' => 'required|player_name|min:'.option('player_name_length_min').'|max:'.option('player_name_length_max'),
]);
2016-07-21 22:01:57 +08:00
2017-11-14 23:25:04 +08:00
event(new CheckPlayerExists($request->input('player_name')));
2016-10-30 11:57:26 +08:00
2019-03-13 13:16:51 +08:00
if (! Player::where('name', $request->input('player_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
2017-11-14 23:25:04 +08:00
event(new PlayerWillBeAdded($request->input('player_name')));
2016-10-30 14:12:22 +08:00
2016-10-16 18:16:15 +08:00
$player = new Player;
$player->uid = $user->uid;
2019-03-13 13:16:51 +08:00
$player->name = $request->input('player_name');
$player->tid_skin = 0;
2016-10-16 18:16:15 +08:00
$player->save();
2017-11-14 23:25:04 +08:00
event(new PlayerWasAdded($player));
2016-10-16 18:16:15 +08:00
$user->setScore(option('score_per_player'), 'minus');
2016-07-21 22:01:57 +08:00
2016-10-07 16:06:38 +08:00
return json(trans('user.player.add.success', ['name' => $request->input('player_name')]), 0);
2016-07-21 22:01:57 +08:00
}
public function delete()
2016-07-21 22:01:57 +08:00
{
2019-03-13 13:16:51 +08:00
$playerName = $this->player->name;
2016-07-21 22:01:57 +08:00
2019-03-22 21:40:12 +08:00
if (option('single_player', false)) {
return json(trans('user.player.delete.single'), 1);
}
2017-11-14 23:25:04 +08:00
event(new PlayerWillBeDeleted($this->player));
2016-11-07 22:16:27 +08:00
2017-11-14 23:25:04 +08:00
$this->player->delete();
2018-02-16 17:31:04 +08:00
if (option('return_score')) {
Auth::user()->setScore(Option::get('score_per_player'), 'plus');
2018-02-16 17:31:04 +08:00
}
2016-07-21 22:01:57 +08:00
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
}
public function rename(Request $request)
2016-07-21 22:01:57 +08:00
{
$this->validate($request, [
'new_player_name' => 'required|player_name|min:'.option('player_name_length_min').'|max:'.option('player_name_length_max'),
]);
2016-07-21 22:01:57 +08:00
2018-02-16 17:31:04 +08:00
$newName = $request->input('new_player_name');
2016-07-21 22:01:57 +08:00
2019-03-13 13:16:51 +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-03-13 13:16:51 +08:00
$oldName = $this->player->name;
2019-03-23 00:20:28 +08:00
$this->player->name = $newName;
$this->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();
}
2018-02-16 17:31:04 +08:00
return json(trans('user.player.rename.success', ['old' => $oldName, 'new' => $newName]), 0);
2016-07-21 22:01:57 +08:00
}
public function setTexture(Request $request)
2016-07-21 22:01:57 +08:00
{
foreach ($request->input('tid') as $key => $value) {
2018-02-16 17:31:04 +08:00
$texture = Texture::find($value);
if (! $texture) {
return json(trans('skinlib.un-existent'), 6);
2018-02-16 17:31:04 +08:00
}
2016-07-21 22:01:57 +08:00
2019-03-23 00:20:28 +08:00
$field = $texture->type == 'cape' ? 'tid_cape' : 'tid_skin';
2016-07-21 22:01:57 +08:00
2019-03-23 00:20:28 +08:00
$this->player->$field = $value;
$this->player->save();
}
2016-07-21 22:01:57 +08:00
2019-03-13 13:16:51 +08:00
return json(trans('user.player.set.success', ['name' => $this->player->name]), 0);
2016-07-21 22:01:57 +08:00
}
public function clearTexture(Request $request)
2016-07-21 22:01:57 +08:00
{
2019-03-23 00:20:28 +08:00
array_map(function ($type) use ($request) {
if ($request->input($type)) {
$field = "tid_$type";
$this->player->$field = 0;
}
}, ['skin', 'cape']);
$this->player->save();
2019-04-19 19:36:36 +08:00
2019-03-13 13:16:51 +08:00
return json(trans('user.player.clear.success', ['name' => $this->player->name]), 0);
2016-07-21 22:01:57 +08:00
}
2019-03-22 21:40:12 +08:00
public function bind(Request $request)
{
$name = $this->validate($request, [
'player' => 'required|player_name|min:'.option('player_name_length_min').'|max:'.option('player_name_length_max'),
])['player'];
$user = Auth::user();
event(new CheckPlayerExists($name));
$player = Player::where('name', $name)->first();
if (! $player) {
event(new PlayerWillBeAdded($name));
$player = new Player;
$player->uid = $user->uid;
$player->name = $name;
$player->tid_skin = 0;
$player->save();
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
}