RemoveCropHandler.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. namespace App\Module\AppGame\Handler\Land;
  3. use App\Module\AppGame\Handler\BaseHandler;
  4. use App\Module\Farm\Services\CropService;
  5. use App\Module\Farm\Services\LandService;
  6. use App\Module\GameItems\Services\ItemService;
  7. use Google\Protobuf\Internal\Message;
  8. use Illuminate\Support\Facades\Log;
  9. use Uraus\Kku\Request\RequestLandRemoveCrop;
  10. use Uraus\Kku\Response\ResponseLandRemoveCrop;
  11. use UCore\Exception\LogicException;
  12. /**
  13. * 处理铲除作物操作请求
  14. */
  15. class RemoveCropHandler extends BaseHandler
  16. {
  17. /**
  18. * 是否需要登录
  19. * @var bool
  20. */
  21. protected bool $need_login = true;
  22. /**
  23. * 处理铲除作物操作请求
  24. *
  25. * @param RequestLandRemoveCrop $data 铲除作物操作请求数据
  26. * @return ResponseLandRemoveCrop 铲除作物操作响应
  27. */
  28. public function handle(Message $data): Message
  29. {
  30. // 创建响应对象
  31. $response = new ResponseLandRemoveCrop();
  32. try {
  33. // 获取请求参数
  34. $landId = $data->getLandId();
  35. $userItemId = $data->getUserItemId();
  36. $userId = $this->user_id;
  37. // 验证土地是否存在且属于当前用户
  38. // 获取用户所有土地
  39. $userLands = LandService::getUserLands($userId);
  40. $landInfo = null;
  41. // 查找指定ID的土地
  42. foreach ($userLands as $land) {
  43. if ($land->id == $landId) {
  44. $landInfo = $land;
  45. break;
  46. }
  47. }
  48. if (!$landInfo) {
  49. throw new LogicException("土地不存在或不属于当前用户");
  50. }
  51. // 如果提供了物品ID,验证用户是否拥有该物品
  52. if ($userItemId > 0) {
  53. $hasItem = ItemService::getUserItems($userId, ['item_id' => $userItemId]);
  54. if ($hasItem->isEmpty()) {
  55. throw new LogicException("您没有该铲除工具");
  56. }
  57. }
  58. // 调用铲除作物服务
  59. $result = CropService::removeCrop($userId, $landId);
  60. if (!$result) {
  61. throw new LogicException("铲除作物失败,请检查土地状态");
  62. }
  63. // 如果使用了物品,消耗物品
  64. if ($userItemId > 0) {
  65. ItemService::consumeItem($userId, $userItemId, null, 1, [
  66. 'source_type' => 'land_remove_crop',
  67. 'source_id' => $landId,
  68. 'details' => ['land_id' => $landId]
  69. ]);
  70. }
  71. // 设置响应状态
  72. $this->response->setCode(0);
  73. $this->response->setMsg('铲除作物成功');
  74. Log::info('用户铲除作物成功', [
  75. 'user_id' => $userId,
  76. 'land_id' => $landId,
  77. 'item_id' => $userItemId
  78. ]);
  79. } catch (LogicException $e) {
  80. // 设置错误响应
  81. $this->response->setCode(400);
  82. $this->response->setMsg($e->getMessage());
  83. Log::warning('用户铲除作物失败', [
  84. 'user_id' => $this->user_id,
  85. 'error' => $e->getMessage(),
  86. 'trace' => $e->getTraceAsString()
  87. ]);
  88. } catch (\Exception $e) {
  89. // 设置错误响应
  90. $this->response->setCode(500);
  91. $this->response->setMsg('系统错误,请稍后再试');
  92. Log::error('铲除作物操作异常', [
  93. 'user_id' => $this->user_id,
  94. 'error' => $e->getMessage(),
  95. 'trace' => $e->getTraceAsString()
  96. ]);
  97. }
  98. return $response;
  99. }
  100. }