FertilizerHandler.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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\Validations\FertilizerValidation;
  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\RequestLandFertilizer;
  11. use Uraus\Kku\Response\ResponseLandFertilizer;
  12. use UCore\Exception\LogicException;
  13. /**
  14. * 处理施肥操作请求
  15. */
  16. class FertilizerHandler extends BaseHandler
  17. {
  18. /**
  19. * 是否需要登录
  20. *
  21. * @var bool
  22. */
  23. protected bool $need_login = true;
  24. /**
  25. * 处理施肥操作请求
  26. *
  27. * @param RequestLandFertilizer $data 施肥操作请求数据
  28. * @return Message 施肥操作响应
  29. */
  30. public function handle(Message $data): Message
  31. {
  32. // 获取请求参数
  33. $landId = $data->getLandId();
  34. $itemId = $data->getItemId();
  35. $userId = $this->user_id;
  36. Log::info('施肥操作开始', [
  37. 'user_id' => $userId,
  38. 'land_id' => $landId,
  39. 'item_id' => $itemId,
  40. ]);
  41. // 使用FertilizerValidation进行数据验证
  42. Log::info('FertilizerHandler: 开始验证', [
  43. 'user_id' => $userId,
  44. 'land_id' => $landId,
  45. 'item_id' => $itemId,
  46. ]);
  47. $validation = new FertilizerValidation();
  48. $validationData = [
  49. 'user_id' => $userId,
  50. 'land_id' => $landId,
  51. 'item_id' => $itemId,
  52. ];
  53. Log::info('FertilizerHandler: 调用validation->validate()');
  54. $validationResult = $validation->validate($validationData);
  55. Log::info('FertilizerHandler: 验证结果', [
  56. 'result' => $validationResult,
  57. 'errors' => $validation->getErrors()
  58. ]);
  59. if (!$validationResult) {
  60. $errors = $validation->getErrors();
  61. $errorMessage = implode(', ', $errors);
  62. Log::warning('施肥验证失败', [
  63. 'user_id' => $userId,
  64. 'land_id' => $landId,
  65. 'item_id' => $itemId,
  66. 'errors' => $errors,
  67. ]);
  68. throw new LogicException($errorMessage);
  69. }
  70. try {
  71. DB::beginTransaction();
  72. // 从验证结果中获取数据,避免重复查询
  73. $cropGrowthTime = $validation->crop_growth_time;
  74. // 使用肥料(使用已验证的数据)
  75. $result = CropService::useFertilizer($userId, $landId, $cropGrowthTime);
  76. if ($result->error) {
  77. throw new LogicException("施肥失败:" . $result->message);
  78. }
  79. // 消耗物品
  80. ItemService::consumeItem($userId, $itemId, null, 1, [
  81. 'source_type' => 'land_fertilizer',
  82. 'source_id' => $landId,
  83. 'details' => ['land_id' => $landId]
  84. ]);
  85. DB::commit();
  86. Log::info('施肥操作成功', [
  87. 'user_id' => $userId,
  88. 'land_id' => $landId,
  89. 'item_id' => $itemId,
  90. 'crop_growth_time' => $cropGrowthTime,
  91. ]);
  92. // 更新作物生长阶段
  93. CropService::updateGrowthStage($result->data['crop_id']);
  94. } catch (\Exception $e) {
  95. DB::rollBack();
  96. Log::error('施肥操作失败', [
  97. 'user_id' => $userId,
  98. 'land_id' => $landId,
  99. 'item_id' => $itemId,
  100. 'error' => $e->getMessage(),
  101. ]);
  102. throw $e;
  103. }
  104. // 创建响应对象
  105. $response = new ResponseLandFertilizer();
  106. return $response;
  107. }
  108. }