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

273 lines
6.9 KiB
PHP
Raw Normal View History

2016-07-21 22:01:57 +08:00
<?php
namespace App\Models;
2016-08-29 15:28:20 +08:00
use Event;
2016-07-21 22:01:57 +08:00
use Utils;
2016-09-25 11:40:50 +08:00
use Storage;
use Response;
2016-10-07 16:06:38 +08:00
use App\Models\User;
2016-10-16 18:16:15 +08:00
use Illuminate\Support\Arr;
use App\Events\GetPlayerJson;
use App\Events\PlayerProfileUpdated;
use App\Exceptions\PrettyPageException;
2016-10-23 11:41:52 +08:00
use Illuminate\Database\Eloquent\Model;
2016-10-16 18:16:15 +08:00
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
2016-07-21 22:01:57 +08:00
2016-10-23 11:41:52 +08:00
class Player extends Model
2016-07-21 22:01:57 +08:00
{
2016-10-23 11:41:52 +08:00
/**
* Json APIs.
*/
const CSL_API = 0;
const USM_API = 1;
/**
* Set of models.
*/
2016-10-23 12:19:19 +08:00
protected static $models = ['steve', 'alex', 'cape'];
2016-10-23 11:41:52 +08:00
/**
* Properties for Eloquent Model.
*/
2016-10-16 18:16:15 +08:00
public $primaryKey = 'pid';
public $timestamps = false;
protected $fillable = ['uid', 'player_name', 'preference', 'last_modified'];
2016-07-21 22:01:57 +08:00
/**
2016-10-16 18:16:15 +08:00
* Check if the player is banned.
*
2016-10-16 18:16:15 +08:00
* @return bool
*/
2016-10-16 18:16:15 +08:00
public function isBanned()
2016-07-21 22:01:57 +08:00
{
2016-10-23 11:41:52 +08:00
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');
2016-07-21 22:01:57 +08:00
}
/**
2016-10-16 18:16:15 +08:00
* Get specific texture of player.
*
2016-10-16 18:16:15 +08:00
* @param string $type steve|alex|cape
2016-10-23 11:41:52 +08:00
* @return string Sha256-hash of texture file.
2016-07-21 22:01:57 +08:00
*/
public function getTexture($type)
{
if ($type == "skin")
$type = ($this->getPreference() == "default") ? "steve" : "alex";
2016-10-16 18:16:15 +08:00
2016-10-23 12:19:19 +08:00
if (in_array($type, self::$models)) {
2016-10-16 18:16:15 +08:00
return Texture::find($this["tid_$type"])['hash'];
2016-07-21 22:01:57 +08:00
}
2016-10-16 18:16:15 +08:00
2016-07-21 22:01:57 +08:00
return false;
}
2016-10-16 18:16:15 +08:00
/**
* Set textures for the player.
*
* @param array $tids
* @return mixed
*/
2016-07-21 22:01:57 +08:00
public function setTexture(Array $tids)
{
2016-10-23 12:19:19 +08:00
foreach (self::$models as $model) {
2016-09-10 21:39:45 +08:00
$property = "tid_$model";
2016-10-16 18:16:15 +08:00
2016-09-10 21:39:45 +08:00
if (isset($tids[$property])) {
2016-10-16 18:16:15 +08:00
$this->$property = $tids[$property];
2016-09-10 21:39:45 +08:00
}
}
2016-07-21 22:01:57 +08:00
2016-10-16 18:16:15 +08:00
$this->last_modified = Utils::getTimeFormatted();
2016-08-29 15:28:20 +08:00
2016-10-16 18:16:15 +08:00
$this->save();
2016-08-29 15:28:20 +08:00
return Event::fire(new PlayerProfileUpdated($this));
2016-07-21 22:01:57 +08:00
}
2016-10-16 18:16:15 +08:00
/**
* Clear the textures of player.
*
* @return mixed
*/
public function clearTexture()
{
$this->setPreference('default');
2016-10-16 18:16:15 +08:00
return $this->setTexture([
'tid_steve' => 0,
'tid_alex' => 0,
'tid_cape' => 0
]);
}
2016-10-23 11:41:52 +08:00
/**
* Get binary texture by type.
*
* @param string $type steve|alex|cape
* @return \Illuminate\Http\Response
*/
2016-07-21 22:01:57 +08:00
public function getBinaryTexture($type)
{
2016-10-16 18:16:15 +08:00
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-09-25 11:40:50 +08:00
if (Storage::disk('textures')->has($hash)) {
2016-07-21 22:01:57 +08:00
// Cache friendly
2016-09-25 11:40:50 +08:00
return Response::png(Storage::disk('textures')->get($hash), 200, [
'Last-Modified' => $this->getLastModified(),
'Accept-Ranges' => 'bytes',
'Content-Length' => Storage::disk('textures')->size($hash),
]);
2016-07-21 22:01:57 +08:00
} else {
2016-10-16 18:16:15 +08:00
throw new NotFoundHttpException(trans('general.texture-deleted'));
2016-07-21 22:01:57 +08:00
}
} else {
2016-10-16 18:16:15 +08:00
throw new NotFoundHttpException(trans('general.texture-not-uploaded', ['type' => $type]));
2016-07-21 22:01:57 +08:00
}
}
/**
2016-10-16 18:16:15 +08:00
* Set preferred model for the player.
2016-10-07 16:06:38 +08:00
*
2016-10-16 18:16:15 +08:00
* @param string $type slim|default
2016-07-21 22:01:57 +08:00
*/
public function setPreference($type)
{
2016-10-16 18:16:15 +08:00
$this->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
}
2016-10-23 11:41:52 +08:00
/**
* Get model preference of the player.
*
* @return string
*/
public function getPreference()
{
2016-10-16 18:16:15 +08:00
return $this['preference'];
2016-10-07 16:06:38 +08:00
}
/**
* Rename the player.
*
* @param string $new_name
* @return mixed
*/
public function rename($new_name)
{
2016-10-16 18:16:15 +08:00
$this->update([
'player_name' => $new_name,
'last_modified' => Utils::getTimeFormatted()
]);
$this->player_name = $new_name;
return Event::fire(new PlayerProfileUpdated($this));
}
2016-10-07 16:06:38 +08:00
/**
* Set a new owner for the player.
*
* @param int $uid
*/
2016-07-23 15:20:10 +08:00
public function setOwner($uid) {
2016-10-16 18:16:15 +08:00
$this->update(['uid' => $uid]);
2016-08-29 15:28:20 +08:00
return Event::fire(new PlayerProfileUpdated($this));
2016-07-23 15:20:10 +08:00
}
2016-07-21 22:01:57 +08:00
/**
2016-10-16 18:16:15 +08:00
* Get Json profile of player.
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 {
2016-10-23 11:41:52 +08:00
throw new InvalidArgumentException('The given api type should be Player::CSL_API or Player::USM_API.');
2016-07-21 22:01:57 +08:00
}
2016-08-29 15:28:20 +08:00
}
2016-08-30 10:10:11 +08:00
/**
2016-10-16 18:16:15 +08:00
* Generate player profile in json format.
2016-08-30 10:10:11 +08:00
*
* @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);
}
2016-10-16 18:16:15 +08:00
/**
* Update the date of last modified.
*
* @return mixed
*/
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-10-16 18:16:15 +08:00
$this->update(['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
}
/**
2016-10-16 18:16:15 +08:00
* Get time of last modified.
*
2016-07-21 22:01:57 +08:00
* @return timestamp
*/
public function getLastModified()
{
2016-10-16 18:16:15 +08:00
return strtotime($this['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
}