FarmFruitGrowthCycle.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 \Carbon\Carbon $created_at 创建时间
  16. * @property \Carbon\Carbon $updated_at 更新时间
  17. * field end
  18. */
  19. class FarmFruitGrowthCycle extends Model
  20. {
  21. /**
  22. * 与模型关联的表名
  23. *
  24. * @var string
  25. */
  26. protected $table = 'farm_fruit_growth_cycles';
  27. /**
  28. * 可批量赋值的属性
  29. *
  30. * @var array
  31. */
  32. protected $fillable = [
  33. 'fruit_item_id',
  34. 'sprout_time',
  35. 'growth_time',
  36. 'fruit_time',
  37. 'mature_time',
  38. 'wither_time',
  39. ];
  40. /**
  41. * 应该被转换为原生类型的属性
  42. *
  43. * @var array
  44. */
  45. protected $casts = [
  46. 'sprout_time' => 'integer',
  47. 'growth_time' => 'integer',
  48. 'fruit_time' => 'integer',
  49. 'mature_time' => 'integer',
  50. 'wither_time' => 'integer',
  51. ];
  52. /**
  53. * 获取关联的果实物品
  54. *
  55. * @return BelongsTo
  56. */
  57. public function fruitItem(): BelongsTo
  58. {
  59. return $this->belongsTo(\App\Module\GameItems\Models\Item::class, 'fruit_item_id', 'id');
  60. }
  61. /**
  62. * 检查果实期是否无限
  63. *
  64. * @return bool
  65. */
  66. public function isFruitTimeInfinite(): bool
  67. {
  68. return $this->fruit_time === 0;
  69. }
  70. /**
  71. * 检查成熟期是否无限
  72. *
  73. * @return bool
  74. */
  75. public function isMatureTimeInfinite(): bool
  76. {
  77. return $this->mature_time === 0;
  78. }
  79. /**
  80. * 检查枯萎期是否无限
  81. *
  82. * @return bool
  83. */
  84. public function isWitherTimeInfinite(): bool
  85. {
  86. return $this->wither_time === 0;
  87. }
  88. /**
  89. * 获取总生长时间(不包括无限期)
  90. *
  91. * @return int
  92. */
  93. public function getTotalGrowthTime(): int
  94. {
  95. $total = $this->sprout_time + $this->growth_time;
  96. // 添加果实期时间(如果不是无限)
  97. if (!$this->isFruitTimeInfinite()) {
  98. $total += $this->fruit_time;
  99. }
  100. if (!$this->isMatureTimeInfinite()) {
  101. $total += $this->mature_time;
  102. }
  103. if (!$this->isWitherTimeInfinite()) {
  104. $total += $this->wither_time;
  105. }
  106. return $total;
  107. }
  108. }