FarmFruitGrowthCycle.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. namespace App\Module\Farm\Models;
  3. use Illuminate\Database\Eloquent\Model;
  4. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  5. /**
  6. * 果实生长周期配置模型
  7. * field start
  8. * @property int $id 主键ID
  9. * @property int $fruit_item_id 果实物品ID
  10. * @property int $sprout_time 发芽期时间(秒)
  11. * @property int $growth_time 成长期时间(秒)
  12. * @property int $fruit_time 果实期时间(秒)
  13. * @property int $mature_time 成熟期时间(秒,0表示无限)
  14. * @property int $wither_time 枯萎期时间(秒,0表示无限)
  15. * @property int $pick_enabled 是否允许摘取:0否,1是
  16. * @property int $pick_start_stage 允许摘取的最早阶段:40成熟期
  17. * @property float $pick_efficiency 摘取效率
  18. * @property \Carbon\Carbon $created_at 创建时间
  19. * @property \Carbon\Carbon $updated_at 更新时间
  20. * field end
  21. */
  22. class FarmFruitGrowthCycle extends Model
  23. {
  24. /**
  25. * 与模型关联的表名
  26. *
  27. * @var string
  28. */
  29. protected $table = 'farm_fruit_growth_cycles';
  30. /**
  31. * 可批量赋值的属性
  32. *
  33. * @var array
  34. */
  35. protected $fillable = [
  36. 'fruit_item_id',
  37. 'sprout_time',
  38. 'growth_time',
  39. 'fruit_time',
  40. 'mature_time',
  41. 'wither_time',
  42. ];
  43. /**
  44. * 应该被转换为原生类型的属性
  45. *
  46. * @var array
  47. */
  48. protected $casts = [
  49. 'sprout_time' => 'integer',
  50. 'growth_time' => 'integer',
  51. 'fruit_time' => 'integer',
  52. 'mature_time' => 'integer',
  53. 'wither_time' => 'integer',
  54. ];
  55. /**
  56. * 获取关联的果实物品
  57. *
  58. * @return BelongsTo
  59. */
  60. public function fruitItem(): BelongsTo
  61. {
  62. return $this->belongsTo(\App\Module\GameItems\Models\Item::class, 'fruit_item_id', 'id');
  63. }
  64. /**
  65. * 检查果实期是否无限
  66. *
  67. * @return bool
  68. */
  69. public function isFruitTimeInfinite(): bool
  70. {
  71. return $this->fruit_time === 0;
  72. }
  73. /**
  74. * 检查成熟期是否无限
  75. *
  76. * @return bool
  77. */
  78. public function isMatureTimeInfinite(): bool
  79. {
  80. return $this->mature_time === 0;
  81. }
  82. /**
  83. * 检查枯萎期是否无限
  84. *
  85. * @return bool
  86. */
  87. public function isWitherTimeInfinite(): bool
  88. {
  89. return $this->wither_time === 0;
  90. }
  91. /**
  92. * 获取总生长时间(不包括无限期)
  93. *
  94. * @return int
  95. */
  96. public function getTotalGrowthTime(): int
  97. {
  98. $total = $this->sprout_time + $this->growth_time;
  99. // 添加果实期时间(如果不是无限)
  100. if (!$this->isFruitTimeInfinite()) {
  101. $total += $this->fruit_time;
  102. }
  103. if (!$this->isMatureTimeInfinite()) {
  104. $total += $this->mature_time;
  105. }
  106. if (!$this->isWitherTimeInfinite()) {
  107. $total += $this->wither_time;
  108. }
  109. return $total;
  110. }
  111. }