273 lines
6.9 KiB
PHP
273 lines
6.9 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Event;
|
|
use Utils;
|
|
use Storage;
|
|
use Response;
|
|
use App\Models\User;
|
|
use Illuminate\Support\Arr;
|
|
use App\Events\GetPlayerJson;
|
|
use App\Events\PlayerProfileUpdated;
|
|
use App\Exceptions\PrettyPageException;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
|
|
|
class Player extends Model
|
|
{
|
|
/**
|
|
* Json APIs.
|
|
*/
|
|
const CSL_API = 0;
|
|
const USM_API = 1;
|
|
|
|
/**
|
|
* Set of models.
|
|
*/
|
|
const MODELS = ['steve', 'alex', 'cape'];
|
|
|
|
/**
|
|
* Properties for Eloquent Model.
|
|
*/
|
|
public $primaryKey = 'pid';
|
|
public $timestamps = false;
|
|
protected $fillable = ['uid', 'player_name', 'preference', 'last_modified'];
|
|
|
|
/**
|
|
* Check if the player is banned.
|
|
*
|
|
* @return bool
|
|
*/
|
|
public function isBanned()
|
|
{
|
|
return $this->user->getPermission() == User::BANNED;
|
|
}
|
|
|
|
/**
|
|
* Return the owner of the player.
|
|
*
|
|
* @return App\Models\User
|
|
*/
|
|
public function user()
|
|
{
|
|
return $this->belongsTo('App\Models\User', 'uid');
|
|
}
|
|
|
|
/**
|
|
* Get specific texture of player.
|
|
*
|
|
* @param string $type steve|alex|cape
|
|
* @return string Sha256-hash of texture file.
|
|
*/
|
|
public function getTexture($type)
|
|
{
|
|
if ($type == "skin")
|
|
$type = ($this->getPreference() == "default") ? "steve" : "alex";
|
|
|
|
if (in_array($type, self::MODELS)) {
|
|
return Texture::find($this["tid_$type"])['hash'];
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* Set textures for the player.
|
|
*
|
|
* @param array $tids
|
|
* @return mixed
|
|
*/
|
|
public function setTexture(Array $tids)
|
|
{
|
|
foreach (self::MODELS as $model) {
|
|
$property = "tid_$model";
|
|
|
|
if (isset($tids[$property])) {
|
|
$this->$property = $tids[$property];
|
|
}
|
|
}
|
|
|
|
$this->last_modified = Utils::getTimeFormatted();
|
|
|
|
$this->save();
|
|
|
|
return Event::fire(new PlayerProfileUpdated($this));
|
|
}
|
|
|
|
/**
|
|
* Clear the textures of player.
|
|
*
|
|
* @return mixed
|
|
*/
|
|
public function clearTexture()
|
|
{
|
|
$this->setPreference('default');
|
|
|
|
return $this->setTexture([
|
|
'tid_steve' => 0,
|
|
'tid_alex' => 0,
|
|
'tid_cape' => 0
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Get binary texture by type.
|
|
*
|
|
* @param string $type steve|alex|cape
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function getBinaryTexture($type)
|
|
{
|
|
if ($this->getTexture($type)) {
|
|
$hash = $this->getTexture($type);
|
|
$path = BASE_DIR."/storage/textures/".$hash;
|
|
|
|
if (Storage::disk('textures')->has($hash)) {
|
|
// Cache friendly
|
|
return Response::png(Storage::disk('textures')->get($hash), 200, [
|
|
'Last-Modified' => $this->getLastModified(),
|
|
'Accept-Ranges' => 'bytes',
|
|
'Content-Length' => Storage::disk('textures')->size($hash),
|
|
]);
|
|
} else {
|
|
throw new NotFoundHttpException(trans('general.texture-deleted'));
|
|
}
|
|
} else {
|
|
throw new NotFoundHttpException(trans('general.texture-not-uploaded', ['type' => $type]));
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Set preferred model for the player.
|
|
*
|
|
* @param string $type slim|default
|
|
*/
|
|
public function setPreference($type)
|
|
{
|
|
$this->update([
|
|
'preference' => $type,
|
|
'last_modified' => Utils::getTimeFormatted()
|
|
]);
|
|
|
|
return Event::fire(new PlayerProfileUpdated($this));
|
|
}
|
|
|
|
/**
|
|
* Get model preference of the player.
|
|
*
|
|
* @return string
|
|
*/
|
|
public function getPreference()
|
|
{
|
|
return $this['preference'];
|
|
}
|
|
|
|
/**
|
|
* Rename the player.
|
|
*
|
|
* @param string $new_name
|
|
* @return mixed
|
|
*/
|
|
public function rename($new_name)
|
|
{
|
|
$this->update([
|
|
'player_name' => $new_name,
|
|
'last_modified' => Utils::getTimeFormatted()
|
|
]);
|
|
|
|
$this->player_name = $new_name;
|
|
|
|
return Event::fire(new PlayerProfileUpdated($this));
|
|
}
|
|
|
|
/**
|
|
* Set a new owner for the player.
|
|
*
|
|
* @param int $uid
|
|
*/
|
|
public function setOwner($uid) {
|
|
$this->update(['uid' => $uid]);
|
|
|
|
return Event::fire(new PlayerProfileUpdated($this));
|
|
}
|
|
|
|
/**
|
|
* Get Json profile of player.
|
|
*
|
|
* @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)
|
|
{
|
|
// Support both CustomSkinLoader API & UniSkinAPI
|
|
if ($api_type == self::CSL_API || $api_type == self::USM_API) {
|
|
$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);
|
|
}
|
|
} else {
|
|
throw new InvalidArgumentException('The given api type should be Player::CSL_API or Player::USM_API.');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Generate player profile in json format.
|
|
*
|
|
* @param int $api_type
|
|
* @return string
|
|
*/
|
|
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');
|
|
|
|
return json_encode($json, JSON_PRETTY_PRINT);
|
|
}
|
|
|
|
/**
|
|
* Update the date of last modified.
|
|
*
|
|
* @return mixed
|
|
*/
|
|
public function updateLastModified()
|
|
{
|
|
// @see http://stackoverflow.com/questions/2215354/php-date-format-when-inserting-into-datetime-in-mysql
|
|
$this->update(['last_modified' => Utils::getTimeFormatted()]);
|
|
return Event::fire(new PlayerProfileUpdated($this));
|
|
}
|
|
|
|
/**
|
|
* Get time of last modified.
|
|
*
|
|
* @return timestamp
|
|
*/
|
|
public function getLastModified()
|
|
{
|
|
return strtotime($this['last_modified']);
|
|
}
|
|
|
|
public function scopeLike($query, $field, $value)
|
|
{
|
|
return $query->where($field, 'LIKE', "%$value%");
|
|
}
|
|
}
|