FertilizerHandler.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. $validation = new FertilizerValidation();
  43. $validationData = [
  44. 'user_id' => $userId,
  45. 'land_id' => $landId,
  46. 'item_id' => $itemId,
  47. ];
  48. if (!$validation->validate($validationData)) {
  49. $errors = $validation->getErrors();
  50. $errorMessage = implode(', ', $errors);
  51. Log::warning('施肥验证失败', [
  52. 'user_id' => $userId,
  53. 'land_id' => $landId,
  54. 'item_id' => $itemId,
  55. 'errors' => $errors,
  56. ]);
  57. throw new LogicException($errorMessage);
  58. }
  59. try {
  60. DB::beginTransaction();
  61. // 从验证结果中获取数据,避免重复查询
  62. $cropGrowthTime = $validation->crop_growth_time;
  63. // 使用肥料(使用已验证的数据)
  64. $result = CropService::useFertilizer($userId, $landId, $cropGrowthTime);
  65. if ($result->error) {
  66. throw new LogicException("施肥失败:" . $result->message);
  67. }
  68. // 消耗物品
  69. ItemService::consumeItem($userId, $itemId, null, 1, [
  70. 'source_type' => 'land_fertilizer',
  71. 'source_id' => $landId,
  72. 'details' => ['land_id' => $landId]
  73. ]);
  74. DB::commit();
  75. Log::info('施肥操作成功', [
  76. 'user_id' => $userId,
  77. 'land_id' => $landId,
  78. 'item_id' => $itemId,
  79. 'crop_growth_time' => $cropGrowthTime,
  80. ]);
  81. // 更新作物生长阶段
  82. CropService::updateGrowthStage($result->data['crop_id']);
  83. } catch (\Exception $e) {
  84. DB::rollBack();
  85. Log::error('施肥操作失败', [
  86. 'user_id' => $userId,
  87. 'land_id' => $landId,
  88. 'item_id' => $itemId,
  89. 'error' => $e->getMessage(),
  90. ]);
  91. throw $e;
  92. }
  93. // 创建响应对象
  94. $response = new ResponseLandFertilizer();
  95. return $response;
  96. }
  97. }