PetLevelConfig.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. namespace App\Module\Pet\Models;
  3. use UCore\ModelCore;
  4. use App\Module\Pet\Casts\DisplayAttributesCast;
  5. use App\Module\Pet\Casts\NumericAttributesCast;
  6. use App\Module\Pet\Models\PetSkill;
  7. use App\Module\Pet\Models\PetUser;
  8. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  9. use Illuminate\Database\Eloquent\Relations\BelongsToMany;
  10. /**
  11. * 宠物等级配置模型
  12. *
  13. * field start
  14. * @property int $id
  15. * @property int $pet_id 宠物 ID
  16. * @property int $level 等级
  17. * @property int $exp_required 升级所需经验值
  18. * @property object|array $skills 可用技能
  19. * @property object|array $display_attributes 等级显示属性配置
  20. * @property object|array $numeric_attributes 等级数值属性配置
  21. * @property \Carbon\Carbon $created_at
  22. * @property \Carbon\Carbon $updated_at
  23. * field end
  24. */
  25. class PetLevelConfig extends ModelCore
  26. {
  27. /**
  28. * 与模型关联的表名
  29. *
  30. * @var string
  31. */
  32. protected $table = 'pet_level_configs';
  33. // attrlist start
  34. protected $fillable = [
  35. 'id',
  36. 'pet_id',
  37. 'level',
  38. 'exp_required',
  39. 'skills',
  40. 'display_attributes',
  41. 'numeric_attributes',
  42. ];
  43. // attrlist end
  44. /**
  45. * 应该被转换为原生类型的属性
  46. *
  47. * @var array
  48. */
  49. protected $casts = [
  50. 'level' => 'integer',
  51. 'exp_required' => 'integer',
  52. 'skills' => 'array',
  53. 'display_attributes' => DisplayAttributesCast::class,
  54. 'numeric_attributes' => NumericAttributesCast::class,
  55. ];
  56. /**
  57. * 获取关联的宠物
  58. *
  59. * @return BelongsTo
  60. */
  61. public function pet(): BelongsTo
  62. {
  63. return $this->belongsTo(PetUser::class, 'pet_id');
  64. }
  65. /**
  66. * 获取该等级可用的技能
  67. *
  68. * 通过 skills 字段中存储的技能ID数组关联到 PetSkill 模型
  69. *
  70. * @return \Illuminate\Database\Eloquent\Collection
  71. */
  72. public function getAvailableSkills()
  73. {
  74. if (empty($this->skills)) {
  75. return collect();
  76. }
  77. return PetSkill::whereIn('id', $this->skills)->get();
  78. }
  79. /**
  80. * 获取技能列表的访问器
  81. *
  82. * @return string
  83. */
  84. public function getSkillsListAttribute()
  85. {
  86. $skills = $this->getAvailableSkills();
  87. if ($skills->isEmpty()) {
  88. return '无可用技能';
  89. }
  90. $skillNames = [];
  91. foreach ($skills as $skill) {
  92. $skillNames[] = "{$skill->skill_name} (ID: {$skill->id})";
  93. }
  94. return implode('<br>', $skillNames);
  95. }
  96. /**
  97. * 检查指定技能是否在该等级可用
  98. *
  99. * @param int $skillId 技能ID
  100. * @return bool
  101. */
  102. public function hasSkill(int $skillId): bool
  103. {
  104. return !empty($this->skills) && in_array($skillId, $this->skills);
  105. }
  106. }