FertilizerHandler.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. $response = new ResponseLandFertilizer();
  34. // 获取请求参数
  35. $landId = $data->getLandId();
  36. $itemId = $data->getItemId();
  37. $userId = $this->user_id;
  38. Log::info('施肥操作开始', [
  39. 'user_id' => $userId,
  40. 'land_id' => $landId,
  41. 'item_id' => $itemId,
  42. ]);
  43. // 先进行验证,避免不必要的事务开销
  44. Log::info('FertilizerHandler: 开始验证', [
  45. 'user_id' => $userId,
  46. 'land_id' => $landId,
  47. 'item_id' => $itemId,
  48. ]);
  49. $validationData = [
  50. 'user_id' => $userId,
  51. 'land_id' => $landId,
  52. 'item_id' => $itemId,
  53. ];
  54. $validation = new FertilizerValidation($validationData);
  55. $validation->validated(); // 这个方法会在验证失败时抛出异常
  56. try {
  57. // 验证通过后,开启事务
  58. DB::beginTransaction();
  59. // 从验证结果中获取数据,避免重复查询
  60. $cropGrowthTime = $validation->crop_growth_time;
  61. // 使用肥料(使用已验证的数据)
  62. $result = CropService::useFertilizer($userId, $landId, $cropGrowthTime);
  63. if ($result->error) {
  64. throw new LogicException("施肥失败:" . $result->message);
  65. }
  66. // 消耗物品
  67. ItemService::consumeItem($userId, $itemId, null, 1, [
  68. 'source_type' => 'land_fertilizer',
  69. 'source_id' => $landId,
  70. 'details' => ['land_id' => $landId]
  71. ]);
  72. // 提交事务
  73. DB::commit();
  74. Log::info('施肥操作成功', [
  75. 'user_id' => $userId,
  76. 'land_id' => $landId,
  77. 'item_id' => $itemId,
  78. 'crop_growth_time' => $cropGrowthTime,
  79. ]);
  80. // 更新作物生长阶段(在事务外执行)
  81. CropService::updateGrowthStage($result->data['crop_id']);
  82. } catch (\Exception $e) {
  83. // 系统异常,需要回滚事务
  84. if (DB::transactionLevel() > 0) {
  85. DB::rollBack();
  86. }
  87. // 设置错误响应
  88. $this->response->setCode(500);
  89. $this->response->setMsg('系统错误,请稍后再试');
  90. Log::error('施肥操作异常', [
  91. 'user_id' => $userId,
  92. 'land_id' => $landId,
  93. 'item_id' => $itemId,
  94. 'error' => $e->getMessage(),
  95. 'trace' => $e->getTraceAsString()
  96. ]);
  97. }
  98. return $response;
  99. }
  100. }