getHouseConfig($level); } catch (\Exception $e) { Log::error('获取房屋配置失败', [ 'level' => $level, 'error' => $e->getMessage(), 'trace' => $e->getTraceAsString() ]); return null; } } /** * 获取所有房屋配置 * * @return array */ public static function getAllHouseConfigs(): array { try { $houseLogic = new HouseLogic(); return $houseLogic->getAllHouseConfigs(); } catch (\Exception $e) { Log::error('获取所有房屋配置失败', [ 'error' => $e->getMessage(), 'trace' => $e->getTraceAsString() ]); return []; } } /** * 获取下一级房屋配置 * * @param int $currentLevel * @return FarmHouseConfig|null */ public static function getNextLevelConfig(int $currentLevel): ?FarmHouseConfig { try { $houseLogic = new HouseLogic(); return $houseLogic->getNextLevelConfig($currentLevel); } catch (\Exception $e) { Log::error('获取下一级房屋配置失败', [ 'current_level' => $currentLevel, 'error' => $e->getMessage(), 'trace' => $e->getTraceAsString() ]); return null; } } /** * 检查房屋升级条件 * * @param int $userId 用户ID * @return array 检查结果,包含success字段表示是否满足条件,message字段表示错误信息,以及其他详细信息 */ public static function checkUpgradeRequirements(int $userId): true { // 获取用户当前房屋等级 $farmUser = \App\Module\Farm\Models\FarmUser::where('user_id', $userId)->first(); if (!$farmUser) { throw new LogicException('用户农场不存在'); } $currentLevel = $farmUser->house_level; $nextLevel = $currentLevel + 1; // 获取下一级房屋配置 $nextLevelConfig = self::getHouseConfig($nextLevel); if (!$nextLevelConfig) { Log::error('获取下一级房屋配置失败', [ 'current_level' => $currentLevel, 'error' => '下一级房屋配置不存在' ]); throw new LogicException('已达到最高等级'); } // 获取升级所需消耗组 $consumeGroupId = $nextLevelConfig->upgrade_materials; if (!$consumeGroupId) { throw new LogicException('升级条件组不存在'); } // 检查消耗条件 $checkResult = \App\Module\Game\Services\ConsumeService::checkConsume($userId, $consumeGroupId); if($checkResult->error){ throw new LogicException($checkResult->message); } return true; } /** * 获取房屋排行榜数据 * * @param int $userId 当前用户ID * @param int $page 页码 * @param int $pageSize 每页数量 * @return HouseRankDto */ public static function getHouseRankList(int $userId, int $page = 1, int $pageSize = 10): HouseRankDto { try { $houseLogic = new HouseLogic(); return $houseLogic->getHouseRankList($userId, $page, $pageSize); } catch (\Exception $e) { Log::error('获取房屋排行榜失败', [ 'user_id' => $userId, 'page' => $page, 'page_size' => $pageSize, 'error' => $e->getMessage(), 'trace' => $e->getTraceAsString() ]); return new HouseRankDto(); } } /** * 获取财富排行榜数据 * * @param int $userId 当前用户ID * @param int $page 页码 * @param int $pageSize 每页数量 * @return WealthRankDto */ public static function getWealthRankList(int $userId, int $page = 1, int $pageSize = 10): WealthRankDto { try { $houseLogic = new HouseLogic(); return $houseLogic->getWealthRankList($userId, $page, $pageSize); } catch (\Exception $e) { Log::error('获取财富排行榜失败', [ 'user_id' => $userId, 'page' => $page, 'page_size' => $pageSize, 'error' => $e->getMessage(), 'trace' => $e->getTraceAsString() ]); return new WealthRankDto(); } } /** * 执行房屋升级 * 要求调用者已开启事务 * * @param int $userId 用户ID * @return bool 升级结果 */ public static function executeHouseUpgrade(int $userId): bool { // 验证事务是否已开启 \UCore\Db\Helper::check_tr(); // 获取用户当前房屋等级 $farmUser = \App\Module\Farm\Models\FarmUser::where('user_id', $userId)->first(); $currentLevel = $farmUser->house_level; $nextLevel = $currentLevel + 1; // 获取下一级房屋配置 $nextLevelConfig = self::getHouseConfig($nextLevel); $consumeGroupId = $nextLevelConfig->upgrade_materials; // 执行消耗 $consumeResult = \App\Module\Game\Services\ConsumeService::executeConsume( $userId, $consumeGroupId, REWARD_SOURCE_TYPE::FARM_UPGRADE, $farmUser->id); if ($consumeResult->error) { throw new LogicException($consumeResult->message ?? '消耗材料失败'); } // 构建消耗的材料数组 $consumedItems = []; $consumeGroup = \App\Module\Game\Models\GameConsumeGroup::find($consumeGroupId); if ($consumeGroup && $consumeGroup->consumeItems) { foreach ($consumeGroup->consumeItems as $item) { if ($item->consume_type == \App\Module\Game\Enums\CONSUME_TYPE::ITEM->value) { $consumedItems[] = [ 'item_id' => $item->target_id, 'amount' => $item->quantity ]; } } } // 执行升级 $houseLogic = new HouseLogic(); $upgradeResult = $houseLogic->upgradeHouse($userId, $consumedItems); if (!$upgradeResult) { throw new LogicException('升级房屋失败'); } return true; } }