PetUser.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. namespace App\Module\Pet\Models;
  3. use UCore\ModelCore;
  4. use App\Module\Pet\Enums\PetStatus;
  5. use App\Module\Pet\Models\PetLevelConfig;
  6. use Illuminate\Database\Eloquent\Relations\HasMany;
  7. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  8. use Illuminate\Database\Eloquent\SoftDeletes;
  9. /**
  10. * 宠物模型
  11. *
  12. * field start
  13. * @property int $id
  14. * @property int $user_id 用户ID
  15. * @property string $name 宠物名称
  16. * @property int $level 等级
  17. * @property int $experience 经验
  18. * @property int $stamina 体力
  19. * @property \App\Module\Pet\Enums\PetStatus $status 宠物状态:0未知,1正常,2战斗中,3死亡
  20. * @property \Carbon\Carbon $created_at
  21. * @property \Carbon\Carbon $updated_at
  22. * @property \Carbon\Carbon $deleted_at 删除时间
  23. * field end
  24. */
  25. class PetUser extends ModelCore
  26. {
  27. use SoftDeletes;
  28. /**
  29. * 与模型关联的表名
  30. *
  31. * @var string
  32. */
  33. protected $table = 'pet_users';
  34. /**
  35. * 可批量赋值的属性
  36. *
  37. * @var array
  38. */
  39. // attrlist start
  40. protected $fillable = [
  41. 'id',
  42. 'user_id',
  43. 'name',
  44. 'level',
  45. 'experience',
  46. 'stamina',
  47. 'status',
  48. ];
  49. // attrlist end
  50. /**
  51. * 属性类型转换
  52. *
  53. * @var array
  54. */
  55. protected $casts = [
  56. 'user_id' => 'integer',
  57. 'level' => 'integer',
  58. 'experience' => 'integer',
  59. 'stamina' => 'integer',
  60. 'status' => PetStatus::class,
  61. 'created_at' => 'datetime',
  62. 'updated_at' => 'datetime',
  63. 'deleted_at' => 'datetime',
  64. ];
  65. /**
  66. * 获取关联的用户
  67. *
  68. * @return BelongsTo
  69. */
  70. public function user(): BelongsTo
  71. {
  72. return $this->belongsTo(\App\Module\User\Models\User::class, 'user_id');
  73. }
  74. /**
  75. * 获取宠物的技能使用记录
  76. *
  77. * @return HasMany
  78. */
  79. public function skillLogs(): HasMany
  80. {
  81. return $this->hasMany(PetSkillLog::class, 'pet_id');
  82. }
  83. /**
  84. * 获取当前等级的最大经验值(升级所需经验)
  85. *
  86. * @return int
  87. */
  88. public function getMaxExperienceAttribute(): int
  89. {
  90. // 获取下一级配置
  91. $nextLevelConfig = PetLevelConfig::where('level', $this->level + 1)->first();
  92. if (!$nextLevelConfig) {
  93. return 0; // 已达到最高等级
  94. }
  95. return $nextLevelConfig->exp_required;
  96. }
  97. /**
  98. * 获取当前等级的最大体力值
  99. *
  100. * @return int
  101. */
  102. public function getMaxStaminaAttribute(): int
  103. {
  104. // 获取当前等级配置
  105. $levelConfig = PetLevelConfig::where('level', $this->level)->first();
  106. if (!$levelConfig) {
  107. return 100; // 默认体力上限
  108. }
  109. return $levelConfig->numeric_attributes->stamina_max ?? 100;
  110. }
  111. /**
  112. * 获取宠物的当前等级配置
  113. *
  114. * @return \Illuminate\Database\Eloquent\Relations\HasOne
  115. */
  116. public function levelConfig()
  117. {
  118. return $this->hasOne(PetLevelConfig::class, 'level', 'level');
  119. }
  120. }