belongsTo(FarmLandType::class, 'from_type_id', 'id'); } /** * 获取目标土地类型 * * @return BelongsTo */ public function toType(): BelongsTo { return $this->belongsTo(FarmLandType::class, 'to_type_id', 'id'); } /** * 获取关联的消耗组 * * @return BelongsTo */ public function materialsGroup(): BelongsTo { return $this->belongsTo(GameConsumeGroup::class, 'materials_group_id', 'id'); } /** * 获取关联的条件组 * * @return BelongsTo */ public function conditionsGroup(): BelongsTo { return $this->belongsTo(GameConditionGroup::class, 'conditions_group_id', 'id'); } /** * 获取关联的消耗组(兼容旧代码) * * @return BelongsTo * @deprecated 使用 materialsGroup() 替代 */ public function consumeGroup(): BelongsTo { return $this->materialsGroup(); } /** * 获取关联的条件组(兼容旧代码) * * @return BelongsTo * @deprecated 使用 conditionsGroup() 替代 */ public function conditionGroup(): BelongsTo { return $this->conditionsGroup(); } /** * 获取升级所需材料 * * 优先使用消耗组,如果没有则使用 materials 字段 * * @return array */ public function getUpgradeMaterials(): array { // 如果有关联的消耗组,则使用消耗组服务获取材料 if ($this->materials && is_numeric($this->materials)) { return \App\Module\Game\Services\ConsumeGroupService::getConsumeMaterials($this->materials); } // 否则使用 materials 字段 if (is_string($this->materials)) { $decoded = json_decode($this->materials, true); return $decoded['materials'] ?? []; } elseif (is_array($this->materials)) { return $this->materials['materials'] ?? []; } return []; } /** * 获取升级条件 * * 优先使用条件组,如果没有则使用 conditions 字段 * * @return array */ public function getUpgradeConditions(): array { // 如果有关联的条件组,则使用条件组服务获取条件 if ($this->conditions && is_numeric($this->conditions)) { return \App\Module\Game\Services\ConditionGroupService::getConditionItems($this->conditions); } // 否则使用 conditions 字段 if (is_string($this->conditions)) { return json_decode($this->conditions, true) ?? []; } elseif (is_array($this->conditions)) { return $this->conditions ?? []; } return []; } /** * 检查用户是否满足升级条件 * * @param int $userId 用户ID * @return array 检查结果,包含success字段表示是否满足条件,message字段表示错误信息 */ public function checkUpgradeConditions(int $userId): array { // 如果有关联的条件组,则使用条件组检查 if ($this->conditions && is_numeric($this->conditions)) { return \App\Module\Game\Services\ConditionService::checkCondition($userId, $this->conditions); } // 否则使用旧版本的条件检查逻辑 $conditions = $this->getUpgradeConditions(); // 如果没有条件,则默认满足 if (empty($conditions)) { return [ 'success' => true, 'message' => '没有条件限制' ]; } // 检查房屋等级条件 if (isset($conditions['house_level_min'])) { $farmUser = \App\Module\Farm\Models\FarmUser::where('user_id', $userId)->first(); if (!$farmUser || $farmUser->house_level < $conditions['house_level_min']) { return [ 'success' => false, 'message' => "房屋等级不足,需要 {$conditions['house_level_min']} 级,实际 " . ($farmUser ? $farmUser->house_level : 0) . " 级" ]; } } // 检查特殊土地数量限制 if (isset($conditions['special_land_check']) && $conditions['special_land_check']) { $farmUser = \App\Module\Farm\Models\FarmUser::where('user_id', $userId)->first(); if (!$farmUser) { return [ 'success' => false, 'message' => "用户信息不存在" ]; } $specialLandCount = \App\Module\Farm\Models\FarmLand::where('user_id', $userId) ->whereIn('land_type', [ \App\Module\Farm\Enums\LAND_TYPE::GOLD->value, \App\Module\Farm\Enums\LAND_TYPE::BLUE->value, \App\Module\Farm\Enums\LAND_TYPE::PURPLE->value ]) ->count(); $houseConfig = $farmUser->houseConfig; if ($specialLandCount >= $houseConfig->special_land_limit) { return [ 'success' => false, 'message' => "特殊土地数量已达上限 {$houseConfig->special_land_limit}" ]; } } // 其他条件检查... return [ 'success' => true, 'message' => '满足所有条件' ]; } }