| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- <?php
- namespace App\Module\AppGame\Handler\Land;
- use App\Module\AppGame\Handler\BaseHandler;
- use App\Module\Farm\Services\LandService;
- use App\Module\Game\Services\ConsumeService;
- use App\Module\GameItems\Services\ItemService;
- use Google\Protobuf\Internal\Message;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Log;
- use Uraus\Kku\Request\RequestLandUp;
- use Uraus\Kku\Response\ResponseLandUp;
- use UCore\Exception\LogicException;
- /**
- * 处理土地升级请求
- */
- class UpHandler extends BaseHandler
- {
- /**
- * 是否需要登录
- * @var bool
- */
- protected bool $need_login = true;
- /**
- * 处理土地升级请求
- *
- * @param RequestLandUp $data 土地升级请求数据
- * @return ResponseLandUp 土地升级响应
- */
- public function handle(Message $data): Message
- {
- // 创建响应对象
- $response = new ResponseLandUp();
- try {
- // 获取请求参数
- $landId = $data->getLandId();
- $userId = $this->user_id;
- // 验证土地是否存在且属于当前用户
- // 获取用户所有土地
- $landInfo = LandService::getUserLand($userId,$landId);
- if (!$landInfo) {
- throw new LogicException("土地不存在或不属于当前用户1");
- }
- // 获取可用的升级路径
- $upgradePaths = LandService::getAvailableUpgradePaths($userId, $landId);
- if (empty($upgradePaths)) {
- throw new LogicException("当前没有可用的升级路径");
- }
- // 记录日志,帮助调试
- Log::info('获取到的升级路径', [
- 'user_id' => $userId,
- 'land_id' => $landId,
- 'paths_count' => count($upgradePaths),
- 'first_path' => $upgradePaths[0] ?? null
- ]);
- // 选择第一个可用的升级路径
- $upgradePath = $upgradePaths[0];
- $targetType = $upgradePath['to_type_id'];
- // 升级土地
- $result = LandService::upgradeLand($userId, $landId, $targetType);
- if (!$result) {
- throw new LogicException("升级土地失败");
- }
- Log::info('用户土地升级成功', [
- 'user_id' => $userId,
- 'land_id' => $landId,
- 'old_type' => $landInfo->landType,
- 'new_type' => $targetType
- ]);
- } catch (LogicException $e) {
- // 设置错误响应
- $this->response->setCode(400);
- $this->response->setMsg($e->getMessage());
- Log::warning('用户土地升级失败', [
- 'user_id' => $this->user_id,
- 'error' => $e->getMessage(),
- 'trace' => $e->getTraceAsString()
- ]);
- }
- return $response;
- }
- }
|