2016-07-21 22:01:57 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
2016-08-29 15:28:20 +08:00
|
|
|
use Event;
|
2016-09-04 15:35:12 +08:00
|
|
|
use App\Events\GetPlayerJson;
|
|
|
|
use App\Events\PlayerProfileUpdated;
|
2016-10-23 11:41:52 +08:00
|
|
|
use Illuminate\Database\Eloquent\Model;
|
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
|
|
|
{
|
2019-03-02 23:47:51 +08:00
|
|
|
public const CREATED_AT = null;
|
|
|
|
public const UPDATED_AT = 'last_modified';
|
|
|
|
|
2016-10-23 11:41:52 +08:00
|
|
|
/**
|
|
|
|
* Json APIs.
|
|
|
|
*/
|
|
|
|
const CSL_API = 0;
|
|
|
|
const USM_API = 1;
|
|
|
|
|
2019-03-02 21:13:17 +08:00
|
|
|
protected static $types = ['skin', 'cape'];
|
2016-10-23 11:41:52 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Properties for Eloquent Model.
|
|
|
|
*/
|
2019-03-02 22:58:37 +08:00
|
|
|
public $primaryKey = 'pid';
|
2019-03-13 13:16:51 +08:00
|
|
|
protected $fillable = ['uid', 'name', 'last_modified'];
|
2016-07-21 22:01:57 +08:00
|
|
|
|
2018-02-23 09:51:23 +08:00
|
|
|
/**
|
|
|
|
* The attributes that should be cast to native types.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $casts = [
|
|
|
|
'pid' => 'integer',
|
|
|
|
'uid' => 'integer',
|
2019-03-02 21:13:17 +08:00
|
|
|
'tid_skin' => 'integer',
|
2018-02-23 09:51:23 +08:00
|
|
|
'tid_cape' => 'integer',
|
|
|
|
];
|
|
|
|
|
2016-09-04 15:35:12 +08:00
|
|
|
/**
|
2016-10-16 18:16:15 +08:00
|
|
|
* Check if the player is banned.
|
2016-09-04 15:35:12 +08:00
|
|
|
*
|
2016-10-16 18:16:15 +08:00
|
|
|
* @return bool
|
2016-09-04 15:35:12 +08:00
|
|
|
*/
|
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.
|
|
|
|
*
|
2017-11-14 23:25:04 +08:00
|
|
|
* @return \App\Models\User
|
2016-10-23 11:41:52 +08:00
|
|
|
*/
|
|
|
|
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-08-16 22:52:00 +08:00
|
|
|
*
|
2019-03-02 21:13:17 +08:00
|
|
|
* @param string $type "skin" or "cape".
|
2018-02-16 17:31:04 +08:00
|
|
|
* @return string The sha256 hash of texture file.
|
2016-07-21 22:01:57 +08:00
|
|
|
*/
|
|
|
|
public function getTexture($type)
|
|
|
|
{
|
2019-03-02 21:13:17 +08:00
|
|
|
if (in_array($type, self::$types)) {
|
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
|
2017-11-14 23:25:04 +08:00
|
|
|
* @return $this
|
2016-10-16 18:16:15 +08:00
|
|
|
*/
|
2019-03-02 22:58:37 +08:00
|
|
|
public function setTexture(array $tids)
|
2016-07-21 22:01:57 +08:00
|
|
|
{
|
2019-03-02 21:13:17 +08:00
|
|
|
foreach (self::$types as $type) {
|
|
|
|
$property = "tid_$type";
|
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->save();
|
2016-08-29 15:28:20 +08:00
|
|
|
|
2017-11-14 23:25:04 +08:00
|
|
|
event(new PlayerProfileUpdated($this));
|
|
|
|
|
|
|
|
return $this;
|
2016-07-21 22:01:57 +08:00
|
|
|
}
|
|
|
|
|
2017-06-28 17:13:07 +08:00
|
|
|
/**
|
|
|
|
* Check and delete invalid textures from player profile.
|
|
|
|
*
|
2018-02-16 17:31:04 +08:00
|
|
|
* @return $this
|
2017-06-28 17:13:07 +08:00
|
|
|
*/
|
|
|
|
public function checkForInvalidTextures()
|
|
|
|
{
|
2019-03-02 21:13:17 +08:00
|
|
|
foreach (self::$types as $type) {
|
|
|
|
$property = "tid_$type";
|
2017-06-28 17:13:07 +08:00
|
|
|
|
2018-02-16 17:31:04 +08:00
|
|
|
if (! Texture::find($this->$property)) {
|
2017-06-28 17:13:07 +08:00
|
|
|
// reset texture
|
|
|
|
$this->$property = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-16 17:31:04 +08:00
|
|
|
$this->save();
|
|
|
|
|
|
|
|
return $this;
|
2017-06-28 17:13:07 +08:00
|
|
|
}
|
|
|
|
|
2016-10-16 18:16:15 +08:00
|
|
|
/**
|
|
|
|
* Clear the textures of player.
|
|
|
|
*
|
2017-04-26 23:39:10 +08:00
|
|
|
* @param array|string $types
|
|
|
|
* @return $this
|
2016-10-16 18:16:15 +08:00
|
|
|
*/
|
2017-04-26 23:39:10 +08:00
|
|
|
public function clearTexture($types)
|
2016-08-25 21:42:09 +08:00
|
|
|
{
|
2017-04-26 23:39:10 +08:00
|
|
|
$types = (array) $types;
|
|
|
|
|
|
|
|
$map = [];
|
|
|
|
|
|
|
|
foreach ($types as $type) {
|
|
|
|
$map["tid_$type"] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->setTexture($map);
|
2016-10-16 18:16:15 +08:00
|
|
|
|
2017-04-26 23:39:10 +08:00
|
|
|
return $this;
|
2016-08-25 21:42:09 +08:00
|
|
|
}
|
|
|
|
|
2016-10-07 16:06:38 +08:00
|
|
|
/**
|
|
|
|
* Rename the player.
|
|
|
|
*
|
2018-02-16 17:31:04 +08:00
|
|
|
* @param string $newName
|
|
|
|
* @return $this
|
2016-10-07 16:06:38 +08:00
|
|
|
*/
|
2018-02-16 17:31:04 +08:00
|
|
|
public function rename($newName)
|
2016-08-30 08:55:02 +08:00
|
|
|
{
|
2016-10-16 18:16:15 +08:00
|
|
|
$this->update([
|
2019-03-13 13:16:51 +08:00
|
|
|
'name' => $newName,
|
2016-08-30 08:55:02 +08:00
|
|
|
]);
|
|
|
|
|
2019-03-13 13:16:51 +08:00
|
|
|
$this->name = $newName;
|
2016-08-30 08:55:02 +08:00
|
|
|
|
2017-11-14 23:25:04 +08:00
|
|
|
event(new PlayerProfileUpdated($this));
|
|
|
|
|
|
|
|
return $this;
|
2016-08-30 08:55:02 +08:00
|
|
|
}
|
|
|
|
|
2016-10-07 16:06:38 +08:00
|
|
|
/**
|
|
|
|
* Set a new owner for the player.
|
|
|
|
*
|
2018-02-16 17:31:04 +08:00
|
|
|
* @param int $uid
|
2017-11-14 23:25:04 +08:00
|
|
|
* @return $this
|
2016-10-07 16:06:38 +08:00
|
|
|
*/
|
2019-03-02 22:58:37 +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
|
|
|
|
2017-11-14 23:25:04 +08:00
|
|
|
event(new PlayerProfileUpdated($this));
|
|
|
|
|
|
|
|
return $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
|
|
|
*/
|
2016-09-04 15:35:12 +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) {
|
2019-02-27 23:44:50 +08:00
|
|
|
$responses = Event::dispatch(new GetPlayerJson($this, $api_type));
|
2016-11-21 21:50:24 +08:00
|
|
|
|
2018-02-16 17:31:04 +08:00
|
|
|
// If listeners return nothing
|
2016-08-30 10:10:11 +08:00
|
|
|
if (isset($responses[0]) && $responses[0] !== null) {
|
2017-12-26 20:07:26 +08:00
|
|
|
return $responses[0]; // @codeCoverageIgnore
|
2016-08-30 10:10:11 +08:00
|
|
|
} else {
|
|
|
|
return $this->generateJsonProfile($api_type);
|
|
|
|
}
|
2016-07-21 22:01:57 +08:00
|
|
|
} else {
|
2017-12-26 20:07:26 +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)
|
|
|
|
{
|
2019-03-13 13:16:51 +08:00
|
|
|
$json[($api_type == self::CSL_API) ? 'username' : 'player_name'] = $this->name;
|
2016-08-29 15:28:20 +08:00
|
|
|
|
2019-03-02 21:13:17 +08:00
|
|
|
$texture = Texture::find($this->tid_skin);
|
|
|
|
$model = empty($texture) ? 'default' : ($texture->type === 'steve' ? 'default' : 'slim');
|
2016-08-29 15:28:20 +08:00
|
|
|
|
|
|
|
if ($api_type == self::USM_API) {
|
2019-03-02 22:58:37 +08:00
|
|
|
$json['last_update'] = strtotime($this->last_modified);
|
2019-03-02 21:13:17 +08:00
|
|
|
$json['model_preference'] = [$model];
|
2016-08-29 15:28:20 +08:00
|
|
|
}
|
|
|
|
|
2019-03-02 21:13:17 +08:00
|
|
|
$skinHash = $this->getTexture('skin');
|
|
|
|
if ($model == 'slim') {
|
|
|
|
$json['skins']['slim'] = $skinHash;
|
2016-08-29 15:28:20 +08:00
|
|
|
}
|
2019-03-02 21:13:17 +08:00
|
|
|
$json['skins']['default'] = $skinHash;
|
2016-08-29 15:28:20 +08:00
|
|
|
$json['cape'] = $this->getTexture('cape');
|
2016-07-21 22:01:57 +08:00
|
|
|
|
2016-11-11 21:19:45 +08:00
|
|
|
return json_encode($json, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
|
2016-07-21 22:01:57 +08:00
|
|
|
}
|
|
|
|
}
|