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-09-04 15:35:12 +08:00
|
|
|
use View;
|
|
|
|
use Event;
|
|
|
|
use Utils;
|
|
|
|
use Option;
|
2016-07-21 22:01:57 +08:00
|
|
|
use App\Models\User;
|
|
|
|
use App\Models\Player;
|
|
|
|
use App\Models\Texture;
|
2016-09-04 15:35:12 +08:00
|
|
|
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;
|
2018-07-20 14:42:43 +08:00
|
|
|
use Illuminate\Support\Facades\Auth;
|
2016-08-29 19:47:30 +08:00
|
|
|
use App\Exceptions\PrettyPageException;
|
2017-11-15 14:00:11 +08:00
|
|
|
use App\Http\Middleware\CheckPlayerExist;
|
|
|
|
use App\Http\Middleware\CheckPlayerOwner;
|
2016-10-23 11:41:52 +08:00
|
|
|
use App\Services\Repositories\UserRepository;
|
2016-07-21 22:01:57 +08:00
|
|
|
|
2016-09-04 15:35:12 +08:00
|
|
|
class PlayerController extends Controller
|
2016-07-21 22:01:57 +08:00
|
|
|
{
|
2016-09-04 15:35:12 +08:00
|
|
|
/**
|
|
|
|
* Player Instance.
|
|
|
|
*
|
|
|
|
* @var \App\Models\Player
|
|
|
|
*/
|
|
|
|
private $player;
|
2016-08-16 22:52:00 +08:00
|
|
|
|
2018-07-20 14:42:43 +08:00
|
|
|
public function __construct()
|
2016-07-21 22:01:57 +08:00
|
|
|
{
|
2018-07-20 14:42:43 +08:00
|
|
|
$this->middleware(function ($request, $next) {
|
2018-07-11 16:09:22 +08:00
|
|
|
if ($request->has('pid')) {
|
|
|
|
if ($this->player = Player::find($request->pid)) {
|
|
|
|
$this->player->checkForInvalidTextures();
|
|
|
|
}
|
2017-06-28 17:13:07 +08:00
|
|
|
}
|
2018-07-11 16:09:22 +08:00
|
|
|
|
|
|
|
return $next($request);
|
|
|
|
});
|
2017-11-15 14:00:11 +08:00
|
|
|
|
2018-02-16 17:31:04 +08:00
|
|
|
$this->middleware([CheckPlayerExist::class, CheckPlayerOwner::class], [
|
|
|
|
'only' => ['delete', 'rename', 'setTexture', 'clearTexture', 'setPreference']
|
|
|
|
]);
|
2016-07-21 22:01:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public function index()
|
|
|
|
{
|
2018-07-20 14:42:43 +08:00
|
|
|
$user = Auth::user();
|
|
|
|
return view('user.player')
|
|
|
|
->with('players', $user->players->toArray())
|
|
|
|
->with('user', $user);
|
2016-07-21 22:01:57 +08:00
|
|
|
}
|
|
|
|
|
2018-07-30 15:45:08 +08:00
|
|
|
public function listAll()
|
|
|
|
{
|
|
|
|
return Auth::user()
|
|
|
|
->players()
|
|
|
|
->select('pid', 'player_name', 'preference', 'tid_steve', 'tid_alex')
|
|
|
|
->get();
|
|
|
|
}
|
|
|
|
|
2016-09-04 15:35:12 +08:00
|
|
|
public function add(Request $request)
|
2016-07-21 22:01:57 +08:00
|
|
|
{
|
2018-07-20 14:42:43 +08:00
|
|
|
$user = Auth::user();
|
|
|
|
|
2016-09-04 15:35:12 +08:00
|
|
|
$this->validate($request, [
|
2018-06-19 11:51:34 +08:00
|
|
|
'player_name' => 'required|player_name|min:'.option('player_name_length_min').'|max:'.option('player_name_length_max')
|
2016-09-04 15:35:12 +08:00
|
|
|
]);
|
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
|
|
|
|
2018-02-16 17:31:04 +08:00
|
|
|
if (! Player::where('player_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
|
|
|
|
2018-07-20 14:42:43 +08:00
|
|
|
if ($user->getScore() < 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;
|
|
|
|
|
2018-07-20 14:42:43 +08:00
|
|
|
$player->uid = $user->uid;
|
2016-10-16 18:16:15 +08:00
|
|
|
$player->player_name = $request->input('player_name');
|
|
|
|
$player->preference = "default";
|
|
|
|
$player->last_modified = Utils::getTimeFormatted();
|
|
|
|
$player->save();
|
|
|
|
|
2017-11-14 23:25:04 +08:00
|
|
|
event(new PlayerWasAdded($player));
|
2016-10-16 18:16:15 +08:00
|
|
|
|
2018-07-20 14:42:43 +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
|
|
|
}
|
|
|
|
|
2018-07-20 14:42:43 +08:00
|
|
|
public function delete()
|
2016-07-21 22:01:57 +08:00
|
|
|
{
|
2018-02-16 17:31:04 +08:00
|
|
|
$playerName = $this->player->player_name;
|
2016-07-21 22:01:57 +08:00
|
|
|
|
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();
|
2016-12-21 22:53:53 +08:00
|
|
|
|
2018-02-16 17:31:04 +08:00
|
|
|
if (option('return_score')) {
|
2018-07-20 14:42:43 +08:00
|
|
|
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 show()
|
|
|
|
{
|
2017-11-14 23:25:04 +08:00
|
|
|
return response()->json($this->player->toArray());
|
2016-07-21 22:01:57 +08:00
|
|
|
}
|
|
|
|
|
2016-09-04 15:35:12 +08:00
|
|
|
public function rename(Request $request)
|
2016-07-21 22:01:57 +08:00
|
|
|
{
|
2016-09-04 15:35:12 +08:00
|
|
|
$this->validate($request, [
|
2018-06-19 11:51:34 +08:00
|
|
|
'new_player_name' => 'required|player_name|min:'.option('player_name_length_min').'|max:'.option('player_name_length_max')
|
2016-09-04 15:35:12 +08:00
|
|
|
]);
|
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
|
|
|
|
2018-02-16 17:31:04 +08:00
|
|
|
if (! Player::where('player_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
|
|
|
}
|
|
|
|
|
2018-02-16 17:31:04 +08:00
|
|
|
$oldName = $this->player->player_name;
|
2016-07-21 22:01:57 +08:00
|
|
|
|
2018-02-16 17:31:04 +08:00
|
|
|
$this->player->rename($newName);
|
2016-07-21 22:01:57 +08:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-02-16 17:31:04 +08:00
|
|
|
* A wrapper of Player::setTexture().
|
2017-11-14 23:25:04 +08:00
|
|
|
*
|
|
|
|
* @param Request $request
|
|
|
|
* @return \Illuminate\Http\JsonResponse
|
2016-07-21 22:01:57 +08:00
|
|
|
*/
|
2016-09-04 15:35:12 +08:00
|
|
|
public function setTexture(Request $request)
|
2016-07-21 22:01:57 +08:00
|
|
|
{
|
2016-11-13 12:20:30 +08:00
|
|
|
foreach ($request->input('tid') as $key => $value) {
|
2018-02-16 17:31:04 +08:00
|
|
|
$texture = Texture::find($value);
|
|
|
|
|
|
|
|
if (! $texture) {
|
2016-11-13 12:20:30 +08:00
|
|
|
return json(trans('skinlib.un-existent'), 6);
|
2018-02-16 17:31:04 +08:00
|
|
|
}
|
2016-07-21 22:01:57 +08:00
|
|
|
|
2018-02-16 17:31:04 +08:00
|
|
|
$fieldName = "tid_{$texture->type}";
|
2016-07-21 22:01:57 +08:00
|
|
|
|
2018-02-16 17:31:04 +08:00
|
|
|
$this->player->setTexture([$fieldName => $value]);
|
2016-11-13 12:20:30 +08:00
|
|
|
}
|
2016-07-21 22:01:57 +08:00
|
|
|
|
2016-09-24 22:49:20 +08:00
|
|
|
return json(trans('user.player.set.success', ['name' => $this->player->player_name]), 0);
|
2016-07-21 22:01:57 +08:00
|
|
|
}
|
|
|
|
|
2017-04-26 23:39:10 +08:00
|
|
|
public function clearTexture(Request $request)
|
2016-07-21 22:01:57 +08:00
|
|
|
{
|
2017-04-26 23:39:10 +08:00
|
|
|
$types = array_filter(['steve', 'alex', 'cape'], function ($type) use ($request) {
|
|
|
|
return $request->input($type);
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->player->clearTexture($types);
|
2016-07-21 22:01:57 +08:00
|
|
|
|
2016-09-24 22:49:20 +08:00
|
|
|
return json(trans('user.player.clear.success', ['name' => $this->player->player_name]), 0);
|
2016-07-21 22:01:57 +08:00
|
|
|
}
|
|
|
|
|
2016-09-04 15:35:12 +08:00
|
|
|
public function setPreference(Request $request)
|
2016-07-21 22:01:57 +08:00
|
|
|
{
|
2016-09-04 15:35:12 +08:00
|
|
|
$this->validate($request, [
|
|
|
|
'preference' => 'required|preference'
|
|
|
|
]);
|
2016-07-21 22:01:57 +08:00
|
|
|
|
2016-09-04 15:35:12 +08:00
|
|
|
$this->player->setPreference($request->preference);
|
2016-07-21 22:01:57 +08:00
|
|
|
|
2016-09-24 22:49:20 +08:00
|
|
|
return json(trans('user.player.preference.success', ['name' => $this->player->player_name, 'preference' => $request->preference]), 0);
|
2016-07-21 22:01:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|