UpHandler.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. namespace App\Module\AppGame\Handler\Land;
  3. use App\Module\AppGame\Handler\BaseHandler;
  4. use App\Module\Farm\Services\LandService;
  5. use App\Module\Game\Services\ConsumeService;
  6. use App\Module\GameItems\Services\ItemService;
  7. use Google\Protobuf\Internal\Message;
  8. use Illuminate\Support\Facades\DB;
  9. use Illuminate\Support\Facades\Log;
  10. use Uraus\Kku\Request\RequestLandUp;
  11. use Uraus\Kku\Response\ResponseLandUp;
  12. use UCore\Exception\LogicException;
  13. /**
  14. * 处理土地升级请求
  15. */
  16. class UpHandler extends BaseHandler
  17. {
  18. /**
  19. * 是否需要登录
  20. * @var bool
  21. */
  22. protected bool $need_login = true;
  23. /**
  24. * 处理土地升级请求
  25. *
  26. * @param RequestLandUp $data 土地升级请求数据
  27. * @return ResponseLandUp 土地升级响应
  28. */
  29. public function handle(Message $data): Message
  30. {
  31. // 创建响应对象
  32. $response = new ResponseLandUp();
  33. try {
  34. // 获取请求参数
  35. $landId = $data->getLandId();
  36. $userId = $this->user_id;
  37. // 验证土地是否存在且属于当前用户
  38. // 获取用户所有土地
  39. $landInfo = LandService::getUserLand($userId,$landId);
  40. if (!$landInfo) {
  41. throw new LogicException("土地不存在或不属于当前用户1");
  42. }
  43. // 获取可用的升级路径
  44. $upgradePaths = LandService::getAvailableUpgradePaths($userId, $landId);
  45. if (empty($upgradePaths)) {
  46. throw new LogicException("当前没有可用的升级路径");
  47. }
  48. // 记录日志,帮助调试
  49. Log::info('获取到的升级路径', [
  50. 'user_id' => $userId,
  51. 'land_id' => $landId,
  52. 'paths_count' => count($upgradePaths),
  53. 'first_path' => $upgradePaths[0] ?? null
  54. ]);
  55. // 选择第一个可用的升级路径
  56. $upgradePath = $upgradePaths[0];
  57. $targetType = $upgradePath['to_type_id'];
  58. // 升级土地
  59. $result = LandService::upgradeLand($userId, $landId, $targetType);
  60. if (!$result) {
  61. throw new LogicException("升级土地失败");
  62. }
  63. Log::info('用户土地升级成功', [
  64. 'user_id' => $userId,
  65. 'land_id' => $landId,
  66. 'old_type' => $landInfo->landType,
  67. 'new_type' => $targetType
  68. ]);
  69. } catch (LogicException $e) {
  70. // 设置错误响应
  71. $this->response->setCode(400);
  72. $this->response->setMsg($e->getMessage());
  73. Log::warning('用户土地升级失败', [
  74. 'user_id' => $this->user_id,
  75. 'error' => $e->getMessage(),
  76. 'trace' => $e->getTraceAsString()
  77. ]);
  78. }
  79. return $response;
  80. }
  81. }