PetStealLog.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. namespace App\Module\Pet\Models;
  3. use UCore\ModelCore;
  4. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  5. use App\Module\Pet\Models\PetUser;
  6. use App\Module\Farm\Models\FarmLand;
  7. use App\Module\Farm\Models\FarmCrop;
  8. /**
  9. * 宠物偷菜日志模型
  10. *
  11. * @property int $id 日志ID
  12. * @property int $stealer_user_id 偷菜者用户ID
  13. * @property int|null $stealer_pet_id 偷菜者宠物ID
  14. * @property int $target_user_id 被偷者用户ID
  15. * @property int|null $target_pet_id 被偷者宠物ID
  16. * @property int $land_id 土地ID
  17. * @property int|null $crop_id 作物ID
  18. * @property bool $success 是否偷取成功
  19. * @property bool $defended 是否被防御
  20. * @property int $steal_amount 偷取数量
  21. * @property int $stealer_stamina_cost 偷菜者体力消耗
  22. * @property int $target_stamina_cost 被偷者体力消耗
  23. * @property int|null $pick_log_id 关联的Farm模块摘取日志ID
  24. * @property string|null $fail_reason 失败原因
  25. * @property \Carbon\Carbon $created_at 创建时间
  26. */
  27. class PetStealLog extends ModelCore
  28. {
  29. /**
  30. * 数据表名
  31. *
  32. * @var string
  33. */
  34. protected $table = 'pet_steal_logs';
  35. /**
  36. * 主键
  37. *
  38. * @var string
  39. */
  40. protected $primaryKey = 'id';
  41. /**
  42. * 是否自动维护时间戳
  43. *
  44. * @var bool
  45. */
  46. public $timestamps = false;
  47. /**
  48. * 可批量赋值的属性
  49. *
  50. * @var array<string>
  51. */
  52. protected $fillable = [
  53. 'stealer_user_id',
  54. 'stealer_pet_id',
  55. 'target_user_id',
  56. 'target_pet_id',
  57. 'land_id',
  58. 'crop_id',
  59. 'success',
  60. 'defended',
  61. 'steal_amount',
  62. 'stealer_stamina_cost',
  63. 'target_stamina_cost',
  64. 'pick_log_id',
  65. 'fail_reason',
  66. ];
  67. /**
  68. * 属性类型转换
  69. *
  70. * @var array<string, string>
  71. */
  72. protected $casts = [
  73. 'success' => 'boolean',
  74. 'defended' => 'boolean',
  75. 'steal_amount' => 'integer',
  76. 'stealer_stamina_cost' => 'integer',
  77. 'target_stamina_cost' => 'integer',
  78. 'created_at' => 'datetime',
  79. ];
  80. /**
  81. * 关联偷菜者宠物
  82. *
  83. * @return BelongsTo
  84. */
  85. public function stealerPet(): BelongsTo
  86. {
  87. return $this->belongsTo(PetUser::class, 'stealer_pet_id');
  88. }
  89. /**
  90. * 关联被偷者宠物
  91. *
  92. * @return BelongsTo
  93. */
  94. public function targetPet(): BelongsTo
  95. {
  96. return $this->belongsTo(PetUser::class, 'target_pet_id');
  97. }
  98. /**
  99. * 关联土地
  100. *
  101. * @return BelongsTo
  102. */
  103. public function land(): BelongsTo
  104. {
  105. return $this->belongsTo(FarmLand::class, 'land_id');
  106. }
  107. /**
  108. * 关联作物
  109. *
  110. * @return BelongsTo
  111. */
  112. public function crop(): BelongsTo
  113. {
  114. return $this->belongsTo(FarmCrop::class, 'crop_id');
  115. }
  116. /**
  117. * 获取指定土地的被偷成功次数
  118. *
  119. * @param int $landId 土地ID
  120. * @return int 被偷成功次数
  121. */
  122. public static function getSuccessfulStealCount(int $landId): int
  123. {
  124. return static::where('land_id', $landId)
  125. ->where('success', true)
  126. ->count();
  127. }
  128. /**
  129. * 检查土地是否还能被偷
  130. *
  131. * @param int $landId 土地ID
  132. * @param int $maxStealTimes 最大被偷次数,默认5次
  133. * @return bool 是否还能被偷
  134. */
  135. public static function canStealLand(int $landId, int $maxStealTimes = 5): bool
  136. {
  137. return static::getSuccessfulStealCount($landId) < $maxStealTimes;
  138. }
  139. /**
  140. * 获取用户今日偷菜次数
  141. *
  142. * @param int $userId 用户ID
  143. * @return int 今日偷菜次数
  144. */
  145. public static function getTodayStealCount(int $userId): int
  146. {
  147. return static::where('stealer_user_id', $userId)
  148. ->whereDate('created_at', today())
  149. ->count();
  150. }
  151. /**
  152. * 获取用户今日被偷次数
  153. *
  154. * @param int $userId 用户ID
  155. * @return int 今日被偷次数
  156. */
  157. public static function getTodayBeingStolenCount(int $userId): int
  158. {
  159. return static::where('target_user_id', $userId)
  160. ->whereDate('created_at', today())
  161. ->count();
  162. }
  163. }