middleware([CheckPlayerExist::class, CheckPlayerOwner::class], [ 'only' => ['delete', 'rename', 'setTexture', 'clearTexture'], ]); } public function index() { return view('user.player') ->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')] ), ]); } public function listAll() { return json( '', 0, Auth::user() ->players() ->select('pid', 'name', 'tid_skin', 'tid_cape') ->get() ->toArray() ); } public function add(Request $request) { $user = Auth::user(); if (option('single_player', false)) { return json(trans('user.player.add.single'), 1); } $name = $this->validate($request, [ 'name' => 'required|player_name|min:'.option('player_name_length_min').'|max:'.option('player_name_length_max'), ])['name']; event(new CheckPlayerExists($name)); if (! Player::where('name', $name)->get()->isEmpty()) { return json(trans('user.player.add.repeated'), 6); } if ($user->score < Option::get('score_per_player')) { return json(trans('user.player.add.lack-score'), 7); } event(new PlayerWillBeAdded($name)); $player = new Player; $player->uid = $user->uid; $player->name = $name; $player->tid_skin = 0; $player->tid_cape = 0; $player->save(); event(new PlayerWasAdded($player)); $user->setScore(option('score_per_player'), 'minus'); return json(trans('user.player.add.success', ['name' => $name]), 0, $player->toArray()); } public function delete($pid) { $player = Player::find($pid); $playerName = $player->name; if (option('single_player', false)) { return json(trans('user.player.delete.single'), 1); } event(new PlayerWillBeDeleted($player)); $player->delete(); if (option('return_score')) { Auth::user()->setScore(Option::get('score_per_player'), 'plus'); } event(new PlayerWasDeleted($playerName)); return json(trans('user.player.delete.success', ['name' => $playerName]), 0); } public function rename(Request $request, $pid) { $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); if (! Player::where('name', $newName)->get()->isEmpty()) { return json(trans('user.player.rename.repeated'), 6); } $oldName = $player->name; $player->name = $newName; $player->save(); if (option('single_player', false)) { $user = auth()->user(); $user->nickname = $newName; $user->save(); } return json(trans('user.player.rename.success', ['old' => $oldName, 'new' => $newName]), 0, $player->toArray()); } public function setTexture(Request $request, $pid) { $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); } $field = "tid_$type"; $player->$field = $tid; $player->save(); } } return json(trans('user.player.set.success', ['name' => $player->name]), 0, $player->toArray()); } public function clearTexture(Request $request, $pid) { $player = Player::find($pid); array_map(function ($type) use ($request, $player) { if ( $request->has($type) || ($request->has('type') && in_array($type, $request->input('type'))) ) { $field = "tid_$type"; $player->$field = 0; } }, ['skin', 'cape']); $player->save(); return json(trans('user.player.clear.success', ['name' => $player->name]), 0, $player->toArray()); } 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); } }