PesticideHandler.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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\RequestLandPesticide;
  10. use Uraus\Kku\Response\ResponseLandPesticide;
  11. use UCore\Exception\LogicException;
  12. /**
  13. * 处理除虫操作请求
  14. * Pesticide
  15. *
  16. */
  17. class PesticideHandler extends BaseHandler
  18. {
  19. /**
  20. * 是否需要登录
  21. * @var bool
  22. */
  23. protected bool $need_login = true;
  24. /**
  25. * 处理除虫操作请求
  26. *
  27. * @param RequestLandPesticide $data 除虫操作请求数据
  28. * @return ResponseLandPesticide 除虫操作响应
  29. */
  30. public function handle(Message $data): Message
  31. {
  32. // 创建响应对象
  33. $response = new ResponseLandPesticide();
  34. try {
  35. // 获取请求参数
  36. $landId = $data->getLandId();
  37. $userItemId = $data->getItemId();
  38. $userId = $this->user_id;
  39. // 验证土地是否存在且属于当前用户
  40. // 获取用户所有土地
  41. $landInfo = LandService::getUserLand($userId,$landId);
  42. if (!$landInfo) {
  43. throw new LogicException("土地不存在或不属于当前用户");
  44. }
  45. // 验证用户是否拥有该物品
  46. $hasItem = ItemService::getUserItems($userId, ['item_id' => $userItemId]);
  47. if ($hasItem->isEmpty()) {
  48. throw new LogicException("您没有该除虫物品");
  49. }
  50. // 除虫概率 百分比 ,100 = 100%
  51. $rate =ItemService::getItemNumericAttribute($userItemId,'fram_pesticide_rate');
  52. if($rate<=0){
  53. throw new LogicException("不是除虫物品");
  54. }
  55. // 调用除虫服务
  56. $result = CropService::clearDisaster($userId, $landId, 2); // 2表示虫害灾害类型
  57. if (!$result) {
  58. throw new LogicException("除虫失败,请检查土地状态或作物生长阶段");
  59. }
  60. // 消耗物品
  61. ItemService::consumeItem($userId, $userItemId, null, 1, [
  62. 'source_type' => 'land_pesticide',
  63. 'source_id' => $landId,
  64. 'details' => ['land_id' => $landId]
  65. ]);
  66. // 设置响应状态
  67. $this->response->setCode(0);
  68. $this->response->setMsg('除虫成功');
  69. Log::info('用户除虫成功', [
  70. 'user_id' => $userId,
  71. 'land_id' => $landId,
  72. 'item_id' => $userItemId
  73. ]);
  74. } catch (LogicException $e) {
  75. // 设置错误响应
  76. $this->response->setCode(400);
  77. $this->response->setMsg($e->getMessage());
  78. Log::warning('用户除虫失败', [
  79. 'user_id' => $this->user_id,
  80. 'error' => $e->getMessage(),
  81. 'trace' => $e->getTraceAsString()
  82. ]);
  83. } catch (\Exception $e) {
  84. // 设置错误响应
  85. $this->response->setCode(500);
  86. $this->response->setMsg('系统错误,请稍后再试');
  87. Log::error('除虫操作异常', [
  88. 'user_id' => $this->user_id,
  89. 'error' => $e->getMessage(),
  90. 'trace' => $e->getTraceAsString()
  91. ]);
  92. }
  93. return $response;
  94. }
  95. }