| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205 |
- <?php
- namespace App\Module\Farm\Services;
- use App\Module\Farm\Logics\HouseLogic;
- use App\Module\Farm\Models\FarmHouseConfig;
- use Illuminate\Support\Facades\Log;
- use UCore\Exception\LogicException;
- use UCore\Exception\ValidateException;
- /**
- * 房屋管理服务
- */
- class HouseService
- {
- /**
- * 获取房屋配置
- *
- * @param int $level
- * @return FarmHouseConfig|null
- */
- public static function getHouseConfig(int $level): ?FarmHouseConfig
- {
- try {
- $houseLogic = new HouseLogic();
- return $houseLogic->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['success']){
- throw new LogicException($checkResult['message']);
- }
- return true;
- }
- /**
- * 执行房屋升级
- *
- * @param int $userId 用户ID
- * @return array 升级结果,包含success字段表示是否成功,message字段表示错误信息,以及其他详细信息
- */
- public static function executeHouseUpgrade(int $userId): true
- {
- // 获取用户当前房屋等级
- $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;
- // 开始事务
- \Illuminate\Support\Facades\DB::beginTransaction();
- try {
- // 执行消耗
- $consumeResult = \App\Module\Game\Services\ConsumeService::executeConsume($userId, $consumeGroupId, 'house_upgrade', $farmUser->id);
- if (!$consumeResult['success']) {
- \Illuminate\Support\Facades\DB::rollBack();
- return [
- 'success' => false,
- 'message' => $consumeResult['message'] ?? '消耗材料失败',
- 'code' => 'CONSUME_FAILED'
- ];
- }
- // 构建消耗的材料数组
- $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('升级房屋失败');
- }
- // 提交事务
- \Illuminate\Support\Facades\DB::commit();
- return true;
- } catch (\Exception $e) {
- // 回滚事务
- \Illuminate\Support\Facades\DB::rollBack();
- throw $e;
- }
- }
- }
|