| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178 |
- <?php
- namespace App\Module\Pet\Models;
- use UCore\ModelCore;
- use Illuminate\Database\Eloquent\Relations\BelongsTo;
- use App\Module\Pet\Models\PetUser;
- use App\Module\Farm\Models\FarmLand;
- use App\Module\Farm\Models\FarmCrop;
- /**
- * 宠物偷菜日志模型
- *
- * @property int $id 日志ID
- * @property int $stealer_user_id 偷菜者用户ID
- * @property int|null $stealer_pet_id 偷菜者宠物ID
- * @property int $target_user_id 被偷者用户ID
- * @property int|null $target_pet_id 被偷者宠物ID
- * @property int $land_id 土地ID
- * @property int|null $crop_id 作物ID
- * @property bool $success 是否偷取成功
- * @property bool $defended 是否被防御
- * @property int $steal_amount 偷取数量
- * @property int $stealer_stamina_cost 偷菜者体力消耗
- * @property int $target_stamina_cost 被偷者体力消耗
- * @property int|null $pick_log_id 关联的Farm模块摘取日志ID
- * @property string|null $fail_reason 失败原因
- * @property \Carbon\Carbon $created_at 创建时间
- */
- class PetStealLog extends ModelCore
- {
- /**
- * 数据表名
- *
- * @var string
- */
- protected $table = 'pet_steal_logs';
- /**
- * 主键
- *
- * @var string
- */
- protected $primaryKey = 'id';
- /**
- * 是否自动维护时间戳
- *
- * @var bool
- */
- public $timestamps = false;
- /**
- * 可批量赋值的属性
- *
- * @var array<string>
- */
- 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<string, string>
- */
- 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();
- }
- }
|