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-08-28 10:05:21 +08:00
|
|
|
use Illuminate\Routing\Controller as BaseController;
|
2016-07-21 22:01:57 +08:00
|
|
|
use App\Models\User;
|
|
|
|
use App\Models\Player;
|
|
|
|
use App\Models\PlayerModel;
|
|
|
|
use App\Models\Texture;
|
|
|
|
use App\Exceptions\E;
|
2016-08-06 21:53:55 +08:00
|
|
|
use Validate;
|
2016-07-21 22:01:57 +08:00
|
|
|
use Utils;
|
|
|
|
use Option;
|
|
|
|
use View;
|
|
|
|
|
|
|
|
class PlayerController extends BaseController
|
|
|
|
{
|
|
|
|
private $player = null;
|
|
|
|
|
2016-08-16 22:52:00 +08:00
|
|
|
private $user = null;
|
|
|
|
|
|
|
|
public function __construct()
|
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-07-21 22:01:57 +08:00
|
|
|
if (isset($_POST['pid'])) {
|
|
|
|
$this->player = new Player($_POST['pid']);
|
|
|
|
if (!$this->player)
|
|
|
|
\Http::abort(404, '角色不存在');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function index()
|
|
|
|
{
|
2016-08-28 10:05:21 +08:00
|
|
|
return View::make('user.player')->with('players', $this->user->getPlayers()->toArray())->with('user', $this->user);
|
2016-07-21 22:01:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public function add()
|
|
|
|
{
|
|
|
|
$player_name = $_POST['player_name'];
|
|
|
|
|
|
|
|
if (!isset($player_name))
|
|
|
|
View::json('你还没有填写要添加的角色名哦', 1);
|
|
|
|
|
2016-08-06 21:53:55 +08:00
|
|
|
if (!Validate::playerName($player_name))
|
2016-07-21 22:01:57 +08:00
|
|
|
{
|
|
|
|
$msg = "无效的角色名。角色名只能包含" . ((Option::get('allow_chinese_playername') == "1") ? "汉字、" : "")."字母、数字以及下划线";
|
|
|
|
View::json($msg, 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!PlayerModel::where('player_name', $player_name)->get()->isEmpty())
|
|
|
|
View::json('该角色名已经被其他人注册掉啦', 6);
|
|
|
|
|
2016-08-16 22:52:00 +08:00
|
|
|
if ($this->user->getScore() < Option::get('score_per_player'))
|
2016-07-21 22:01:57 +08:00
|
|
|
View::json('积分不够添加角色啦', 7);
|
|
|
|
|
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-16 22:52:00 +08:00
|
|
|
$this->user->setScore(Option::get('score_per_player'), 'minus');
|
2016-07-21 22:01:57 +08:00
|
|
|
|
|
|
|
View::json('成功添加了角色 '.$player_name.'', 0);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public function delete()
|
|
|
|
{
|
2016-08-16 22:52:00 +08:00
|
|
|
$player_name = $this->player->model->player_name;
|
|
|
|
$this->player->model->delete();
|
2016-07-21 22:01:57 +08:00
|
|
|
|
2016-08-16 22:52:00 +08:00
|
|
|
$this->user->setScore(Option::get('score_per_player'), 'plus');
|
2016-07-21 22:01:57 +08:00
|
|
|
|
|
|
|
View::json('角色 '.$player_name.' 已被删除', 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
public function rename()
|
|
|
|
{
|
|
|
|
$new_player_name = Utils::getValue('new_player_name', $_POST);
|
|
|
|
|
|
|
|
if (!$new_player_name)
|
2016-08-16 22:52:00 +08:00
|
|
|
throw new E('非法参数', 1);
|
2016-07-21 22:01:57 +08:00
|
|
|
|
2016-08-06 21:53:55 +08:00
|
|
|
if (!Validate::playerName($new_player_name))
|
2016-07-21 22:01:57 +08:00
|
|
|
{
|
|
|
|
$msg = "无效的角色名。角色名只能包含" . ((Option::get('allow_chinese_playername') == "1") ? "汉字、" : "")."字母、数字以及下划线";
|
|
|
|
View::json($msg, 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!PlayerModel::where('player_name', $new_player_name)->get()->isEmpty())
|
|
|
|
View::json('此角色名已被他人使用,换一个吧~', 6);
|
|
|
|
|
2016-08-16 22:52:00 +08:00
|
|
|
$old_player_name = $this->player->model->player_name;
|
|
|
|
$this->player->model->player_name = $new_player_name;
|
|
|
|
$this->player->model->last_modified = Utils::getTimeFormatted();
|
|
|
|
$this->player->model->save();
|
2016-07-21 22:01:57 +08:00
|
|
|
|
|
|
|
View::json('角色 '.$old_player_name.' 已更名为 '.$_POST['new_player_name'], 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A wrapper of Player::setTexture()
|
|
|
|
*/
|
|
|
|
public function setTexture()
|
|
|
|
{
|
|
|
|
$tid = Utils::getValue('tid', $_POST);
|
|
|
|
|
|
|
|
if (!is_numeric($tid))
|
2016-08-16 22:52:00 +08:00
|
|
|
throw new E('非法参数', 1);
|
2016-07-21 22:01:57 +08:00
|
|
|
|
|
|
|
if (!($texture = Texture::find($tid)))
|
|
|
|
View::json('Unexistent texture.', 6);
|
|
|
|
|
|
|
|
$field_name = "tid_".$texture->type;
|
|
|
|
|
2016-08-29 15:28:20 +08:00
|
|
|
$this->player->setTexture([$field_name => $tid]);
|
2016-07-21 22:01:57 +08:00
|
|
|
|
2016-08-16 22:52:00 +08:00
|
|
|
View::json('材质已成功应用至角色 '.$this->player->model->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-08-16 22:52:00 +08:00
|
|
|
View::json('角色 '.$this->player->model->player_name.' 的材质已被成功重置', 0);
|
2016-07-21 22:01:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public function setPreference()
|
|
|
|
{
|
|
|
|
if (!isset($_POST['preference']) ||
|
|
|
|
($_POST['preference'] != "default" && $_POST['preference'] != "slim"))
|
|
|
|
{
|
2016-08-16 22:52:00 +08:00
|
|
|
throw new E('非法参数', 1);
|
2016-07-21 22:01:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->player->setPreference($_POST['preference']);
|
|
|
|
|
|
|
|
View::json('角色 '.$this->player->player_name.' 的优先模型已更改至 '.$_POST['preference'], 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|