PetConfig.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. /**
  7. * 宠物配置模型
  8. *
  9. * field start
  10. * @property int $id
  11. * @property string $pet_type 宠物类型
  12. * @property object|array $grade_probability 品阶概率配置
  13. * @property object|array $feed_effect 喂养效果配置
  14. * @property int $max_level 最大等级
  15. * @property int $stamina_recovery 体力恢复值/分钟
  16. * @property object|array $display_attributes 显示属性配置
  17. * @property object|array $numeric_attributes 数值属性配置
  18. * @property \Carbon\Carbon $created_at
  19. * @property \Carbon\Carbon $updated_at
  20. * field end
  21. */
  22. class PetConfig extends ModelCore
  23. {
  24. /**
  25. * 与模型关联的表名
  26. *
  27. * @var string
  28. */
  29. protected $table = 'pet_configs';
  30. // attrlist start
  31. protected $fillable = [
  32. 'id',
  33. 'pet_type',
  34. 'grade_probability',
  35. 'feed_effect',
  36. 'max_level',
  37. 'stamina_recovery',
  38. 'display_attributes',
  39. 'numeric_attributes',
  40. ];
  41. // attrlist end
  42. /**
  43. * 应该被转换为原生类型的属性
  44. *
  45. * @var array
  46. */
  47. protected $casts = [
  48. 'grade_probability' => 'json',
  49. 'feed_effect' => 'json',
  50. 'max_level' => 'integer',
  51. 'stamina_recovery' => 'integer',
  52. 'display_attributes' => DisplayAttributesCast::class,
  53. 'numeric_attributes' => NumericAttributesCast::class,
  54. ];
  55. /**
  56. * 获取指定宠物类型的配置
  57. *
  58. * @param string $petType
  59. * @return self|null
  60. */
  61. public static function getByType(string $petType): ?self
  62. {
  63. return self::where('pet_type', $petType)->first();
  64. }
  65. /**
  66. * 获取品阶概率
  67. *
  68. * @param string $grade
  69. * @return float
  70. */
  71. public function getGradeProbability(string $grade): float
  72. {
  73. $probabilities = json_decode($this->grade_probability, true);
  74. return $probabilities[$grade] ?? 0;
  75. }
  76. /**
  77. * 获取喂养效果
  78. *
  79. * @param string $foodType
  80. * @return array
  81. */
  82. public function getFeedEffect(string $foodType): array
  83. {
  84. $effects = json_decode($this->feed_effect, true);
  85. return $effects[$foodType] ?? [];
  86. }
  87. }