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

247 lines
6.8 KiB
PHP
Raw Normal View History

2016-07-21 22:01:57 +08:00
<?php
namespace App\Models;
use View;
2016-08-29 15:28:20 +08:00
use Event;
2016-07-21 22:01:57 +08:00
use Utils;
use App\Events\GetPlayerJson;
use App\Events\PlayerWasDeleted;
use App\Events\PlayerProfileUpdated;
use App\Exceptions\PrettyPageException;
2016-07-21 22:01:57 +08:00
class Player
{
public $pid;
public $player_name;
public $is_banned = false;
2016-07-21 22:01:57 +08:00
public $model;
/**
* User Instance.
*
* @var \App\Models\User
*/
private $owner;
2016-08-29 15:28:20 +08:00
const CSL_API = 0;
const USM_API = 1;
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) {
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;
$this->owner = new User($this->model->uid);
if ($this->owner->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)
{
2016-09-10 21:39:45 +08:00
$map = ['steve', 'alex', 'cape'];
2016-07-21 22:01:57 +08:00
2016-09-10 21:39:45 +08:00
foreach ($map as $model) {
$property = "tid_$model";
if (isset($tids[$property])) {
$this->model->$property = $tids[$property];
}
}
2016-07-21 22:01:57 +08:00
2016-08-16 13:27:06 +08:00
$this->model->last_modified = Utils::getTimeFormatted();
2016-08-29 15:28:20 +08:00
$this->model->save();
return Event::fire(new PlayerProfileUpdated($this));
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 {
abort(404, '请求的贴图已被删除。');
2016-07-21 22:01:57 +08:00
}
} else {
abort(404, '该用户尚未上传请求的贴图类型 '.$type);
2016-07-21 22:01:57 +08:00
}
}
/**
* Set preferred model
* @param string $type, 'slim' or 'default'
*/
public function setPreference($type)
{
2016-08-29 15:28:20 +08:00
$this->model->update([
2016-07-21 22:01:57 +08:00
'preference' => $type,
'last_modified' => Utils::getTimeFormatted()
]);
2016-08-29 15:28:20 +08:00
return Event::fire(new PlayerProfileUpdated($this));
2016-07-21 22:01:57 +08:00
}
public function getPreference()
{
2016-08-16 13:27:06 +08:00
return $this->model['preference'];
2016-07-21 22:01:57 +08:00
}
public function rename($new_name)
{
$this->model->update([
'player_name' => $new_name,
'last_modified' => Utils::getTimeFormatted()
]);
$this->player_name = $new_name;
return Event::fire(new PlayerProfileUpdated($this));
}
2016-07-23 15:20:10 +08:00
public function setOwner($uid) {
2016-08-29 15:28:20 +08:00
$this->model->update(['uid' => $uid]);
return Event::fire(new PlayerProfileUpdated($this));
2016-07-23 15:20:10 +08:00
}
2016-07-21 22:01:57 +08:00
/**
* Get JSON profile
2016-08-29 15:28:20 +08:00
*
* @param int $api_type Which API to use, 0 for CustomSkinAPI, 1 for UniSkinAPI
* @return string User profile in json format
2016-07-21 22:01:57 +08:00
*/
public function getJsonProfile($api_type)
{
2016-07-21 22:01:57 +08:00
// Support both CustomSkinLoader API & UniSkinAPI
2016-08-29 15:28:20 +08:00
if ($api_type == self::CSL_API || $api_type == self::USM_API) {
2016-08-30 10:10:11 +08:00
$responses = Event::fire(new GetPlayerJson($this, $api_type));
// if listeners return nothing
if (isset($responses[0]) && $responses[0] !== null) {
return $responses[0];
} else {
return $this->generateJsonProfile($api_type);
}
2016-07-21 22:01:57 +08:00
} else {
throw new PrettyPageException('不支持的 API_TYPE。', -1);
2016-07-21 22:01:57 +08:00
}
2016-08-29 15:28:20 +08:00
}
2016-08-30 10:10:11 +08:00
/**
* Generate player profile in json string
*
* @param int $api_type
* @return string
*/
2016-08-29 15:28:20 +08:00
public function generateJsonProfile($api_type)
{
$json[($api_type == self::CSL_API) ? 'username' : 'player_name'] = $this->player_name;
$model = $this->getPreference();
$sec_model = ($model == 'default') ? 'slim' : 'default';
if ($api_type == self::USM_API) {
$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');
2016-07-21 22:01:57 +08:00
return json_encode($json, JSON_PRETTY_PRINT);
}
public function updateLastModified()
{
2016-07-21 22:01:57 +08:00
// @see http://stackoverflow.com/questions/2215354/php-date-format-when-inserting-into-datetime-in-mysql
2016-08-29 15:28:20 +08:00
$this->model->update(['last_modified' => Utils::getTimeFormatted()]);
return Event::fire(new PlayerProfileUpdated($this));
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
}
public function delete()
{
// Event::fire(new PlayerWasDeleted($this));
return $this->model->delete();
}
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 = ['player_name', '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
}