blessing-skin-server/app/Models/Player.php

179 lines
5.6 KiB
PHP
Raw Normal View History

2016-07-21 22:01:57 +08:00
<?php
namespace App\Models;
use App\Exceptions\E;
use Utils;
class Player
{
public $pid = "";
public $player_name = "";
public $is_banned = false;
2016-08-16 13:27:06 +08:00
public $model = null;
2016-07-21 22:01:57 +08:00
/**
* Construct player with pid or playername
*
* @param int $pid
2016-07-21 22:01:57 +08:00
* @param string $player_name
*/
public function __construct($pid, $player_name = "")
{
if ($player_name == "") {
$this->pid = $pid;
2016-08-16 13:27:06 +08:00
$this->model = PlayerModel::find($pid);
2016-07-21 22:01:57 +08:00
} else {
2016-08-16 13:27:06 +08:00
$this->model = PlayerModel::where('player_name', $player_name)->first();
2016-07-21 22:01:57 +08:00
}
if (!$this->model) {
2016-07-21 22:01:57 +08:00
\Http::abort(404, '角色不存在');
} else {
$this->pid = $this->model->pid;
}
2016-07-21 22:01:57 +08:00
2016-08-16 13:27:06 +08:00
$this->player_name = $this->model->player_name;
2016-08-16 13:27:06 +08:00
if ((new User($this->model->uid))->getPermission() == "-1")
$this->is_banned = true;
2016-07-21 22:01:57 +08:00
}
/**
* Get textures of player
*
2016-07-21 22:01:57 +08:00
* @param string $type steve|alex|cape, 'skin' for texture of preferred model
* @return string sha256-hash of texture file
*/
public function getTexture($type)
{
if ($type == "skin")
$type = ($this->getPreference() == "default") ? "steve" : "alex";
if ($type == "steve" | $type == "alex" | $type == "cape") {
2016-08-16 13:27:06 +08:00
$tid = $this->model['tid_'.$type];
2016-07-21 22:01:57 +08:00
return Texture::find($tid)['hash'];
}
return false;
}
public function setTexture(Array $tids)
{
if (!isset($tids['tid_steve']) && !isset($tids['tid_alex']) && !isset($tids['tid_cape']))
{
throw new E('非法参数', 1);
2016-07-21 22:01:57 +08:00
}
2016-08-16 13:27:06 +08:00
$this->model->tid_steve = isset($tids['tid_steve']) ? $tids['tid_steve'] : $this->model['tid_steve'];
$this->model->tid_alex = isset($tids['tid_alex']) ? $tids['tid_alex'] : $this->model['tid_alex'];
$this->model->tid_cape = isset($tids['tid_cape']) ? $tids['tid_cape'] : $this->model['tid_cape'];
2016-07-21 22:01:57 +08:00
2016-08-16 13:27:06 +08:00
$this->model->last_modified = Utils::getTimeFormatted();
return $this->model->save();
2016-07-21 22:01:57 +08:00
}
public function clearTexture()
{
$this->setPreference('default');
$this->setTexture(['tid_steve' => 0, 'tid_alex' => 0, 'tid_cape' => 0]);
}
2016-07-21 22:01:57 +08:00
public function getBinaryTexture($type)
{
if ($this->getTexture($type) != "") {
2016-08-28 20:33:35 +08:00
$hash = $this->getTexture($type);
$path = BASE_DIR."/storage/textures/".$hash;
2016-07-21 22:01:57 +08:00
2016-08-28 20:33:35 +08:00
if (\Storage::disk('textures')->has($hash)) {
2016-07-21 22:01:57 +08:00
// Cache friendly
2016-08-28 20:33:35 +08:00
return response(\Storage::disk('textures')->get($hash))
->header('Content-Type', 'image/png')
->header('Last-Modified', gmdate('D, d M Y H:i:s', $this->getLastModified()).' GMT')
->header('Content-Length', filesize($path));
2016-07-21 22:01:57 +08:00
} else {
\Http::abort(404, '请求的贴图已被删除。');
}
} else {
\Http::abort(404, '该用户尚未上传请求的贴图类型 '.$type);
}
}
/**
* Set preferred model
* @param string $type, 'slim' or 'default'
*/
public function setPreference($type) {
2016-08-16 13:27:06 +08:00
return $this->model->update([
2016-07-21 22:01:57 +08:00
'preference' => $type,
'last_modified' => Utils::getTimeFormatted()
]);
}
public function getPreference() {
2016-08-16 13:27:06 +08:00
return $this->model['preference'];
2016-07-21 22:01:57 +08:00
}
2016-07-23 15:20:10 +08:00
public function setOwner($uid) {
2016-08-16 13:27:06 +08:00
return $this->model->update(['uid' => $uid]);
2016-07-23 15:20:10 +08:00
}
2016-07-21 22:01:57 +08:00
/**
* Get JSON profile
* @param int $api_type, which API to use, 0 for CustomSkinAPI, 1 for UniSkinAPI
* @return string, user profile in json format
*/
public function getJsonProfile($api_type) {
header('Content-type: application/json');
// Support both CustomSkinLoader API & UniSkinAPI
if ($api_type == 0 || $api_type == 1) {
$json[($api_type == 0) ? 'username' : 'player_name'] = $this->player_name;
$model = $this->getPreference();
$sec_model = ($model == 'default') ? 'slim' : 'default';
if ($api_type == 1) {
$json['last_update'] = $this->getLastModified();
$json['model_preference'] = [$model, $sec_model];
}
if ($this->getTexture('steve') || $this->getTexture('alex')) {
// Skins dict order by preference model
$json['skins'][$model] = $this->getTexture($model == "default" ? "steve" : "alex");
$json['skins'][$sec_model] = $this->getTexture($sec_model == "default" ? "steve" : "alex");
}
$json['cape'] = $this->getTexture('cape');
} else {
throw new E('不支持的 API_TYPE。', -1, true);
}
return json_encode($json, JSON_PRETTY_PRINT);
}
public function updateLastModified() {
// @see http://stackoverflow.com/questions/2215354/php-date-format-when-inserting-into-datetime-in-mysql
2016-08-16 13:27:06 +08:00
return $this->model->update(['last_modified' => Utils::getTimeFormatted()]);
2016-07-21 22:01:57 +08:00
}
/**
* Get last modified time
* @return timestamp
*/
public function getLastModified() {
2016-08-16 13:27:06 +08:00
return strtotime($this->model['last_modified']);
2016-07-21 22:01:57 +08:00
}
}
class PlayerModel extends \Illuminate\Database\Eloquent\Model
{
2016-07-23 15:20:10 +08:00
public $primaryKey = 'pid';
protected $table = 'players';
public $timestamps = false;
2016-07-21 22:01:57 +08:00
protected $fillable = ['preference', 'last_modified'];
2016-07-23 15:20:10 +08:00
public function scopeLike($query, $field, $value)
{
return $query->where($field, 'LIKE', "%$value%");
}
2016-07-21 22:01:57 +08:00
}