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

188 lines
5.3 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 Utils;
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\Exceptions\PrettyPageException;
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
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)) {
$this->player->checkForInvalidTextures();
}
}
return $next($request);
});
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()
{
$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();
}
public function add(Request $request)
2016-07-21 22:01:57 +08:00
{
$user = Auth::user();
$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
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
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;
$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
$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
{
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();
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 show()
{
2017-11-14 23:25:04 +08:00
return response()->json($this->player->toArray());
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
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
*/
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
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-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
}
public function clearTexture(Request $request)
2016-07-21 22:01:57 +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
}
public function setPreference(Request $request)
2016-07-21 22:01:57 +08:00
{
$this->validate($request, [
'preference' => 'required|preference'
]);
2016-07-21 22:01:57 +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
}
}