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

157 lines
4.8 KiB
PHP
Raw Normal View History

2016-07-21 22:01:57 +08:00
<?php
namespace App\Controllers;
use App\Models\User;
use App\Models\Player;
use App\Models\PlayerModel;
use App\Models\Texture;
use App\Exceptions\E;
use Utils;
use Option;
use View;
class PlayerController extends BaseController
{
private $player = null;
function __construct()
{
if (isset($_POST['pid'])) {
$this->player = new Player($_POST['pid']);
if (!$this->player)
\Http::abort(404, '角色不存在');
}
}
public function index()
{
echo View::make('user.player')->with('players', (new User($_SESSION['email']))->getPlayers()->toArray())->with('user', new User($_SESSION['email']));
}
public function add()
{
$player_name = $_POST['player_name'];
if (!isset($player_name))
View::json('你还没有填写要添加的角色名哦', 1);
2016-08-06 19:38:37 +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);
$user = new User($_SESSION['email']);
2016-07-29 12:46:19 +08:00
if ($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();
$player->uid = $user->uid;
$player->player_name = $player_name;
$player->preference = "default";
$player->last_modified = Utils::getTimeFormatted();
2016-07-21 22:01:57 +08:00
$player->save();
2016-07-29 12:46:19 +08:00
$user->setScore(Option::get('score_per_player'), 'minus');
2016-07-21 22:01:57 +08:00
View::json('成功添加了角色 '.$player_name.'', 0);
}
public function delete()
{
$player_name = $this->player->eloquent_model->player_name;
$this->player->eloquent_model->delete();
2016-07-29 12:46:19 +08:00
(new User($_SESSION['email']))->setScore(Option::get('score_per_player'), 'plus');
2016-07-21 22:01:57 +08:00
View::json('角色 '.$player_name.' 已被删除', 0);
}
public function show()
{
echo json_encode($this->player->eloquent_model->toArray(), JSON_NUMERIC_CHECK);
}
public function rename()
{
$new_player_name = Utils::getValue('new_player_name', $_POST);
if (!$new_player_name)
throw new E('Invalid parameters', 1);
2016-08-06 19:38:37 +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);
$old_player_name = $this->player->eloquent_model->player_name;
$this->player->eloquent_model->player_name = $new_player_name;
$this->player->eloquent_model->last_modified = Utils::getTimeFormatted();
$this->player->eloquent_model->save();
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))
throw new E('Invalid parameters.', 1);
if (!($texture = Texture::find($tid)))
View::json('Unexistent texture.', 6);
$field_name = "tid_".$texture->type;
$this->player->eloquent_model->$field_name = $tid;
2016-07-23 15:20:10 +08:00
$this->player->eloquent_model->last_modified = Utils::getTimeFormatted();
2016-07-21 22:01:57 +08:00
$this->player->eloquent_model->save();
View::json('材质已成功应用至角色 '.$this->player->eloquent_model->player_name.'', 0);
}
public function clearTexture()
{
$this->player->eloquent_model->preference = "default";
$this->player->eloquent_model->tid_steve = "";
$this->player->eloquent_model->tid_alex = "";
$this->player->eloquent_model->tid_cape = "";
2016-07-23 15:20:10 +08:00
$this->player->eloquent_model->last_modified = Utils::getTimeFormatted();
2016-07-21 22:01:57 +08:00
$this->player->eloquent_model->save();
View::json('角色 '.$this->player->eloquent_model->player_name.' 的材质已被成功重置', 0);
}
public function setPreference()
{
if (!isset($_POST['preference']) ||
($_POST['preference'] != "default" && $_POST['preference'] != "slim"))
{
throw new E('Invalid parameters.', 1);
}
$this->player->setPreference($_POST['preference']);
View::json('角色 '.$this->player->player_name.' 的优先模型已更改至 '.$_POST['preference'], 0);
}
}