PetSkill.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. namespace App\Module\Pet\Models;
  3. use UCore\ModelCore;
  4. use Illuminate\Database\Eloquent\Relations\HasMany;
  5. /**
  6. * 宠物技能模型
  7. *
  8. * field start
  9. * @property int $id
  10. * @property string $skill_name 技能名称
  11. * @property int $stamina_cost 体力消耗
  12. * @property int $cool_down 冷却时间(秒)
  13. * @property int $duration_time 持续时间(秒)
  14. * @property string $effect_desc 效果描述
  15. * @property int $min_level 最低等级要求
  16. * @property \Carbon\Carbon $created_at
  17. * @property \Carbon\Carbon $updated_at
  18. * field end
  19. */
  20. class PetSkill extends ModelCore
  21. {
  22. /**
  23. * 与模型关联的表名
  24. *
  25. * @var string
  26. */
  27. protected $table = 'pet_skills';
  28. /**
  29. * 可批量赋值的属性
  30. *
  31. * @var array
  32. */
  33. // attrlist start
  34. protected $fillable = [
  35. 'id',
  36. 'skill_name',
  37. 'stamina_cost',
  38. 'cool_down',
  39. 'duration_time',
  40. 'effect_desc',
  41. 'min_level',
  42. ];
  43. // attrlist end
  44. /**
  45. * 属性类型转换
  46. *
  47. * @var array
  48. */
  49. protected $casts = [
  50. 'stamina_cost' => 'integer',
  51. 'cool_down' => 'integer',
  52. 'min_level' => 'integer',
  53. 'created_at' => 'datetime',
  54. 'updated_at' => 'datetime',
  55. ];
  56. /**
  57. * 获取技能的使用记录
  58. *
  59. * @return HasMany
  60. */
  61. public function usageLogs(): HasMany
  62. {
  63. return $this->hasMany(PetSkillLog::class, 'skill_id');
  64. }
  65. /**
  66. * 格式化持续时间显示
  67. *
  68. * @param int $seconds 秒数
  69. * @return string 友好的时间显示
  70. */
  71. public static function formatDuration(int $seconds): string
  72. {
  73. if ($seconds <= 0) {
  74. return '<span class="text-muted">无持续时间</span>';
  75. }
  76. $days = floor($seconds / 86400);
  77. $hours = floor(($seconds % 86400) / 3600);
  78. $minutes = floor(($seconds % 3600) / 60);
  79. $remainingSeconds = $seconds % 60;
  80. $parts = [];
  81. if ($days > 0) {
  82. $parts[] = "<span class=\"badge badge-primary\">{$days}天</span>";
  83. }
  84. if ($hours > 0) {
  85. $parts[] = "<span class=\"badge badge-info\">{$hours}小时</span>";
  86. }
  87. if ($minutes > 0) {
  88. $parts[] = "<span class=\"badge badge-success\">{$minutes}分钟</span>";
  89. }
  90. if ($remainingSeconds > 0 && empty($parts)) {
  91. // 只有在没有更大单位时才显示秒数
  92. $parts[] = "<span class=\"badge badge-secondary\">{$remainingSeconds}秒</span>";
  93. }
  94. if (empty($parts)) {
  95. return '<span class="text-muted">瞬间</span>';
  96. }
  97. $result = implode(' ', $parts);
  98. // 添加原始秒数的提示
  99. $result .= "<br><small class=\"text-muted\">({$seconds}秒)</small>";
  100. return $result;
  101. }
  102. /**
  103. * 获取格式化的持续时间
  104. *
  105. * @return string
  106. */
  107. public function getFormattedDurationTimeAttribute(): string
  108. {
  109. return self::formatDuration($this->duration_time);
  110. }
  111. /**
  112. * 获取格式化的冷却时间
  113. *
  114. * @return string
  115. */
  116. public function getFormattedCoolDownAttribute(): string
  117. {
  118. return self::formatDuration($this->cool_down);
  119. }
  120. }