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 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;
|
2017-11-15 14:00:11 +08:00
|
|
|
use App\Http\Middleware\CheckPlayerExist;
|
|
|
|
use App\Http\Middleware\CheckPlayerOwner;
|
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
|
|
|
{
|
2018-07-20 14:42:43 +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], [
|
2019-03-02 22:58:37 +08:00
|
|
|
'only' => ['delete', 'rename', 'setTexture', 'clearTexture'],
|
2018-02-16 17:31:04 +08:00
|
|
|
]);
|
2016-07-21 22:01:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public function index()
|
|
|
|
{
|
2018-07-20 14:42:43 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
|
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|player_name|min:'.option('player_name_length_min').'|max:'.option('player_name_length_max'),
|
|
|
|
])['name'];
|
2016-07-21 22:01:57 +08:00
|
|
|
|
2019-04-23 23:03:12 +08:00
|
|
|
event(new CheckPlayerExists($name));
|
2016-10-30 11:57:26 +08:00
|
|
|
|
2019-04-23 23:03:12 +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
|
|
|
|
2019-04-23 23:03:12 +08:00
|
|
|
event(new PlayerWillBeAdded($name));
|
2016-10-30 14:12:22 +08:00
|
|
|
|
2016-10-16 18:16:15 +08:00
|
|
|
$player = new Player;
|
|
|
|
|
2019-03-02 22:58:37 +08:00
|
|
|
$player->uid = $user->uid;
|
2019-04-23 23:03:12 +08:00
|
|
|
$player->name = $name;
|
2019-03-02 22:58:37 +08:00
|
|
|
$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();
|
|
|
|
|
2017-11-14 23:25:04 +08:00
|
|
|
event(new PlayerWasAdded($player));
|
2016-10-16 18:16:15 +08:00
|
|
|
|
2019-07-30 14:29:02 +08:00
|
|
|
$user->score -= option('score_per_player');
|
|
|
|
$user->save();
|
2016-07-21 22:01:57 +08:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-04-24 13:10:03 +08:00
|
|
|
public function delete($pid)
|
2016-07-21 22:01:57 +08:00
|
|
|
{
|
2019-04-24 13:10:03 +08:00
|
|
|
$player = Player::find($pid);
|
|
|
|
$playerName = $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);
|
|
|
|
}
|
|
|
|
|
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();
|
2016-12-21 22:53:53 +08:00
|
|
|
|
2018-02-16 17:31:04 +08:00
|
|
|
if (option('return_score')) {
|
2019-07-30 14:29:02 +08:00
|
|
|
$user = auth()->user();
|
|
|
|
$user->score += option('score_per_player');
|
|
|
|
$user->save();
|
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
|
|
|
}
|
|
|
|
|
2019-04-24 13:10:03 +08:00
|
|
|
public function rename(Request $request, $pid)
|
2016-07-21 22:01:57 +08:00
|
|
|
{
|
2019-04-24 13:10:03 +08:00
|
|
|
$newName = $this->validate($request, [
|
|
|
|
'name' => 'required|player_name|min:'.option('player_name_length_min').'|max:'.option('player_name_length_max'),
|
|
|
|
])['name'];
|
|
|
|
$player = Player::find($pid);
|
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-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-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
|
|
|
}
|
|
|
|
|
2019-04-24 13:10:03 +08:00
|
|
|
public function setTexture(Request $request, $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) {
|
|
|
|
if ($tid = $request->input($type)) {
|
|
|
|
$texture = Texture::find($tid);
|
|
|
|
if (! $texture) {
|
|
|
|
return json(trans('skinlib.non-existent'), 1);
|
|
|
|
}
|
2018-02-16 17:31:04 +08:00
|
|
|
|
2019-04-24 13:10:03 +08:00
|
|
|
$field = "tid_$type";
|
|
|
|
$player->$field = $tid;
|
|
|
|
$player->save();
|
2018-02-16 17:31:04 +08:00
|
|
|
}
|
2016-11-13 12:20:30 +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
|
|
|
}
|
|
|
|
|
2019-04-24 13:10:03 +08:00
|
|
|
public function clearTexture(Request $request, $pid)
|
2016-07-21 22:01:57 +08:00
|
|
|
{
|
2019-04-24 13:10:03 +08:00
|
|
|
$player = Player::find($pid);
|
|
|
|
array_map(function ($type) use ($request, $player) {
|
2019-04-27 20:08:41 +08:00
|
|
|
if (
|
|
|
|
$request->has($type) ||
|
|
|
|
($request->has('type') && in_array($type, $request->input('type')))
|
|
|
|
) {
|
2019-03-23 00:20:28 +08:00
|
|
|
$field = "tid_$type";
|
2019-04-24 13:10:03 +08:00
|
|
|
$player->$field = 0;
|
2019-03-23 00:20:28 +08:00
|
|
|
}
|
2019-04-27 20:08:41 +08:00
|
|
|
}, ['skin', 'cape']);
|
2019-04-24 13:10:03 +08:00
|
|
|
$player->save();
|
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
|
|
|
|
|
|
|
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
|
|
|
}
|