| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- <?php
- namespace App\Module\Pet\Models;
- use UCore\ModelCore;
- use Illuminate\Database\Eloquent\Relations\HasMany;
- /**
- * 宠物技能模型
- *
- * field start
- * @property int $id
- * @property string $skill_name 技能名称
- * @property int $stamina_cost 体力消耗
- * @property int $cool_down 冷却时间(秒)
- * @property int $duration_time 持续时间(秒)
- * @property string $effect_desc 效果描述
- * @property int $min_level 最低等级要求
- * @property \Carbon\Carbon $created_at
- * @property \Carbon\Carbon $updated_at
- * field end
- */
- class PetSkill extends ModelCore
- {
- /**
- * 与模型关联的表名
- *
- * @var string
- */
- protected $table = 'pet_skills';
- /**
- * 可批量赋值的属性
- *
- * @var array
- */
- // attrlist start
- protected $fillable = [
- 'id',
- 'skill_name',
- 'stamina_cost',
- 'cool_down',
- 'duration_time',
- 'effect_desc',
- 'min_level',
- ];
- // attrlist end
- /**
- * 属性类型转换
- *
- * @var array
- */
- protected $casts = [
- 'stamina_cost' => 'integer',
- 'cool_down' => 'integer',
- 'min_level' => 'integer',
- 'created_at' => 'datetime',
- 'updated_at' => 'datetime',
- ];
- /**
- * 获取技能的使用记录
- *
- * @return HasMany
- */
- public function usageLogs(): HasMany
- {
- return $this->hasMany(PetSkillLog::class, 'skill_id');
- }
- /**
- * 格式化持续时间显示
- *
- * @param int $seconds 秒数
- * @return string 友好的时间显示
- */
- public static function formatDuration(int $seconds): string
- {
- if ($seconds <= 0) {
- return '<span class="text-muted">无持续时间</span>';
- }
- $days = floor($seconds / 86400);
- $hours = floor(($seconds % 86400) / 3600);
- $minutes = floor(($seconds % 3600) / 60);
- $remainingSeconds = $seconds % 60;
- $parts = [];
- if ($days > 0) {
- $parts[] = "<span class=\"badge badge-primary\">{$days}天</span>";
- }
- if ($hours > 0) {
- $parts[] = "<span class=\"badge badge-info\">{$hours}小时</span>";
- }
- if ($minutes > 0) {
- $parts[] = "<span class=\"badge badge-success\">{$minutes}分钟</span>";
- }
- if ($remainingSeconds > 0 && empty($parts)) {
- // 只有在没有更大单位时才显示秒数
- $parts[] = "<span class=\"badge badge-secondary\">{$remainingSeconds}秒</span>";
- }
- if (empty($parts)) {
- return '<span class="text-muted">瞬间</span>';
- }
- $result = implode(' ', $parts);
- // 添加原始秒数的提示
- $result .= "<br><small class=\"text-muted\">({$seconds}秒)</small>";
- return $result;
- }
- /**
- * 获取格式化的持续时间
- *
- * @return string
- */
- public function getFormattedDurationTimeAttribute(): string
- {
- return self::formatDuration($this->duration_time);
- }
- /**
- * 获取格式化的冷却时间
- *
- * @return string
- */
- public function getFormattedCoolDownAttribute(): string
- {
- return self::formatDuration($this->cool_down);
- }
- }
|