FarmFruitGrowthCycle.php 2.3 KB

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