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 App\Models\PlayerModel;
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use App\Events\PlayerWasAdded;
|
2016-08-29 19:47:30 +08:00
|
|
|
use App\Exceptions\PrettyPageException;
|
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
|
|
|
/**
|
|
|
|
* User Instance.
|
|
|
|
*
|
|
|
|
* @var \App\Models\User
|
|
|
|
*/
|
|
|
|
private $user;
|
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
|
|
|
|
2016-09-04 15:35:12 +08:00
|
|
|
public function __construct(Request $request)
|
2016-07-21 22:01:57 +08:00
|
|
|
{
|
2016-08-28 10:05:21 +08:00
|
|
|
$this->user = new User(session('uid'));
|
2016-08-16 22:52:00 +08:00
|
|
|
|
2016-09-04 15:35:12 +08:00
|
|
|
if ($request->has('pid'))
|
|
|
|
$this->player = new Player($request->pid);
|
2016-07-21 22:01:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public function index()
|
|
|
|
{
|
2016-09-04 15:35:12 +08:00
|
|
|
return view('user.player')->with('players', $this->user->getPlayers()->toArray())->with('user', $this->user);
|
2016-07-21 22:01:57 +08:00
|
|
|
}
|
|
|
|
|
2016-09-04 15:35:12 +08:00
|
|
|
public function add(Request $request)
|
2016-07-21 22:01:57 +08:00
|
|
|
{
|
2016-09-04 15:35:12 +08:00
|
|
|
$this->validate($request, [
|
|
|
|
'player_name' => 'required|'.(Option::get('allow_chinese_playername') == "1") ? 'pname_chinese' : 'player_name'
|
|
|
|
]);
|
2016-07-21 22:01:57 +08:00
|
|
|
|
2016-09-04 15:35:12 +08:00
|
|
|
$player_name = $request->input('player_name');
|
2016-07-21 22:01:57 +08:00
|
|
|
|
|
|
|
if (!PlayerModel::where('player_name', $player_name)->get()->isEmpty())
|
2016-09-24 22:49:20 +08:00
|
|
|
return json(trans('user.player.add.repeated'), 6);
|
2016-07-21 22:01:57 +08:00
|
|
|
|
2016-08-16 22:52:00 +08:00
|
|
|
if ($this->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-07-21 22:01:57 +08:00
|
|
|
|
2016-07-23 15:20:10 +08:00
|
|
|
$player = new PlayerModel();
|
2016-08-16 22:52:00 +08:00
|
|
|
$player->uid = $this->user->uid;
|
2016-07-23 15:20:10 +08:00
|
|
|
$player->player_name = $player_name;
|
|
|
|
$player->preference = "default";
|
|
|
|
$player->last_modified = Utils::getTimeFormatted();
|
2016-07-21 22:01:57 +08:00
|
|
|
$player->save();
|
|
|
|
|
2016-08-30 08:55:02 +08:00
|
|
|
Event::fire(new PlayerWasAdded($player));
|
|
|
|
|
2016-08-16 22:52:00 +08:00
|
|
|
$this->user->setScore(Option::get('score_per_player'), 'minus');
|
2016-07-21 22:01:57 +08:00
|
|
|
|
2016-09-24 22:49:20 +08:00
|
|
|
return json(trans('user.player.add.success', ['name' => $player_name]), 0);
|
2016-07-21 22:01:57 +08:00
|
|
|
}
|
|
|
|
|
2016-09-04 15:35:12 +08:00
|
|
|
public function delete(Request $request)
|
2016-07-21 22:01:57 +08:00
|
|
|
{
|
2016-09-04 15:35:12 +08:00
|
|
|
$player_name = $this->player->player_name;
|
2016-07-21 22:01:57 +08:00
|
|
|
|
2016-09-04 15:35:12 +08:00
|
|
|
if ($this->player->delete()) {
|
|
|
|
$this->user->setScore(Option::get('score_per_player'), 'plus');
|
2016-07-21 22:01:57 +08:00
|
|
|
|
2016-09-24 22:49:20 +08:00
|
|
|
return json("trans('user.player.delete.success', ['name' => $player_name])", 0);
|
2016-09-04 15:35:12 +08:00
|
|
|
}
|
2016-07-21 22:01:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public function show()
|
|
|
|
{
|
2016-08-29 15:28:20 +08:00
|
|
|
return json_encode($this->player->model->toArray(), JSON_NUMERIC_CHECK);
|
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, [
|
|
|
|
'new_player_name' => 'required|'.(Option::get('allow_chinese_playername') == "1") ? 'pname_chinese' : 'player_name'
|
|
|
|
]);
|
2016-07-21 22:01:57 +08:00
|
|
|
|
2016-09-04 15:35:12 +08:00
|
|
|
$new_player_name = $request->input('new_player_name');
|
2016-07-21 22:01:57 +08:00
|
|
|
|
|
|
|
if (!PlayerModel::where('player_name', $new_player_name)->get()->isEmpty())
|
2016-09-24 22:49:20 +08:00
|
|
|
return json(trans('user.player.rename.repeated'), 6);
|
2016-07-21 22:01:57 +08:00
|
|
|
|
2016-09-03 21:06:01 +08:00
|
|
|
$old_player_name = $this->player->player_name;
|
2016-08-30 08:55:02 +08:00
|
|
|
$this->player->rename($new_player_name);
|
2016-07-21 22:01:57 +08:00
|
|
|
|
2016-09-24 22:49:20 +08:00
|
|
|
return json(trans('user.player.rename.success', ['old' => $old_player_name, 'new' => $new_player_name]), 0);
|
2016-07-21 22:01:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A wrapper of Player::setTexture()
|
|
|
|
*/
|
2016-09-04 15:35:12 +08:00
|
|
|
public function setTexture(Request $request)
|
2016-07-21 22:01:57 +08:00
|
|
|
{
|
2016-09-04 15:35:12 +08:00
|
|
|
$this->validate($request, [
|
|
|
|
'tid' => 'required|integer'
|
|
|
|
]);
|
2016-07-21 22:01:57 +08:00
|
|
|
|
2016-09-10 22:06:43 +08:00
|
|
|
if (!($texture = Texture::find($request->tid)))
|
2016-09-24 22:49:20 +08:00
|
|
|
return json(trans('skinlib.un-existent'), 6);
|
2016-07-21 22:01:57 +08:00
|
|
|
|
2016-09-10 22:06:43 +08:00
|
|
|
$field_name = "tid_{$texture->type}";
|
2016-07-21 22:01:57 +08:00
|
|
|
|
2016-09-10 22:06:43 +08:00
|
|
|
$this->player->setTexture([$field_name => $request->tid]);
|
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()
|
|
|
|
{
|
2016-08-25 21:42:09 +08:00
|
|
|
$this->player->clearTexture();
|
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
|
|
|
}
|
|
|
|
|
|
|
|
}
|