*/ protected $fillable = [ 'stealer_user_id', 'stealer_pet_id', 'target_user_id', 'target_pet_id', 'land_id', 'crop_id', 'success', 'defended', 'steal_amount', 'stealer_stamina_cost', 'target_stamina_cost', 'pick_log_id', 'fail_reason', ]; /** * 属性类型转换 * * @var array */ protected $casts = [ 'success' => 'boolean', 'defended' => 'boolean', 'steal_amount' => 'integer', 'stealer_stamina_cost' => 'integer', 'target_stamina_cost' => 'integer', 'created_at' => 'datetime', ]; /** * 关联偷菜者宠物 * * @return BelongsTo */ public function stealerPet(): BelongsTo { return $this->belongsTo(PetUser::class, 'stealer_pet_id'); } /** * 关联被偷者宠物 * * @return BelongsTo */ public function targetPet(): BelongsTo { return $this->belongsTo(PetUser::class, 'target_pet_id'); } /** * 关联土地 * * @return BelongsTo */ public function land(): BelongsTo { return $this->belongsTo(FarmLand::class, 'land_id'); } /** * 关联作物 * * @return BelongsTo */ public function crop(): BelongsTo { return $this->belongsTo(FarmCrop::class, 'crop_id'); } /** * 获取指定土地的被偷成功次数 * * @param int $landId 土地ID * @return int 被偷成功次数 */ public static function getSuccessfulStealCount(int $landId): int { return static::where('land_id', $landId) ->where('success', true) ->count(); } /** * 检查土地是否还能被偷 * * @param int $landId 土地ID * @param int $maxStealTimes 最大被偷次数,默认5次 * @return bool 是否还能被偷 */ public static function canStealLand(int $landId, int $maxStealTimes = 5): bool { return static::getSuccessfulStealCount($landId) < $maxStealTimes; } /** * 获取用户今日偷菜次数 * * @param int $userId 用户ID * @return int 今日偷菜次数 */ public static function getTodayStealCount(int $userId): int { return static::where('stealer_user_id', $userId) ->whereDate('created_at', today()) ->count(); } /** * 获取用户今日被偷次数 * * @param int $userId 用户ID * @return int 今日被偷次数 */ public static function getTodayBeingStolenCount(int $userId): int { return static::where('target_user_id', $userId) ->whereDate('created_at', today()) ->count(); } }