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

96 lines
2.2 KiB
PHP
Raw Normal View History

2016-07-21 22:01:57 +08:00
<?php
namespace App\Models;
use App\Events\PlayerProfileUpdated;
2019-12-30 23:29:44 +08:00
use App\Models;
2020-03-09 12:29:00 +08:00
use DateTimeInterface;
2016-10-23 11:41:52 +08:00
use Illuminate\Database\Eloquent\Model;
2020-04-19 19:36:39 +08:00
use Illuminate\Support\Carbon;
use Lorisleiva\LaravelSearchString\Concerns\SearchString;
2016-07-21 22:01:57 +08:00
2020-04-19 19:36:39 +08:00
/**
* @property int $pid
* @property int $uid
* @property string $name
* @property int $tid_skin
* @property int $tid_cape
* @property Carbon $last_modified
* @property User $user
* @property Texture $skin
* @property Texture $cape
* @property string $model
*/
2016-10-23 11:41:52 +08:00
class Player extends Model
2016-07-21 22:01:57 +08:00
{
use SearchString;
2019-03-02 23:47:51 +08:00
public const CREATED_AT = null;
public const UPDATED_AT = 'last_modified';
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
protected $casts = [
'pid' => 'integer',
'uid' => 'integer',
'tid_skin' => 'integer',
2018-02-23 09:51:23 +08:00
'tid_cape' => 'integer',
];
2019-03-23 00:20:28 +08:00
protected $dispatchesEvents = [
'retrieved' => \App\Events\PlayerRetrieved::class,
2019-03-23 00:20:28 +08:00
'updated' => PlayerProfileUpdated::class,
];
protected $searchStringColumns = [
'pid', 'uid',
'tid_skin' => '/^(?:tid_)?skin$/',
'tid_cape' => '/^(?:tid_)?cape$/',
'name' => ['searchable' => true],
'last_modified' => ['date' => true],
];
2016-10-23 11:41:52 +08:00
public function user()
{
2019-12-30 23:29:44 +08:00
return $this->belongsTo(Models\User::class, 'uid');
}
public function skin()
{
return $this->belongsTo(Models\Texture::class, 'tid_skin');
}
public function cape()
{
return $this->belongsTo(Models\Texture::class, 'tid_cape');
}
public function getModelAttribute()
{
return optional($this->skin)->model ?? 'default';
}
/**
* CustomSkinAPI R1.
*/
public function toJson($options = 0)
{
$model = $this->model;
$profile = [
'username' => $this->name,
'skins' => [
$model => optional($this->skin)->hash,
],
'cape' => optional($this->cape)->hash,
];
return json_encode($profile, $options | JSON_UNESCAPED_UNICODE);
2016-07-21 22:01:57 +08:00
}
2020-03-09 12:29:00 +08:00
protected function serializeDate(DateTimeInterface $date)
{
return $date->format('Y-m-d H:i:s');
}
2016-07-21 22:01:57 +08:00
}