FertilizerHandler.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 UCore\Helper\Logger;
  11. use Uraus\Kku\Request\RequestLandFertilizer;
  12. use Uraus\Kku\Response\ResponseLandFertilizer;
  13. use UCore\Exception\LogicException;
  14. /**
  15. * 处理施肥操作请求
  16. */
  17. class FertilizerHandler extends BaseHandler
  18. {
  19. /**
  20. * 是否需要登录
  21. *
  22. * @var bool
  23. */
  24. protected bool $need_login = true;
  25. /**
  26. * 处理施肥操作请求
  27. *
  28. * @param RequestLandFertilizer $data 施肥操作请求数据
  29. * @return Message 施肥操作响应
  30. */
  31. public function handle(Message $data): Message
  32. {
  33. // 创建响应对象
  34. $response = new ResponseLandFertilizer();
  35. // 获取请求参数
  36. $landId = $data->getLandId();
  37. $itemId = $data->getItemId();
  38. $userId = $this->user_id;
  39. Log::info('施肥操作开始', [
  40. 'user_id' => $userId,
  41. 'land_id' => $landId,
  42. 'item_id' => $itemId,
  43. ]);
  44. // 先进行验证,避免不必要的事务开销
  45. Log::info('FertilizerHandler: 开始验证', [
  46. 'user_id' => $userId,
  47. 'land_id' => $landId,
  48. 'item_id' => $itemId,
  49. ]);
  50. $validationData = [
  51. 'user_id' => $userId,
  52. 'land_id' => $landId,
  53. 'item_id' => $itemId,
  54. ];
  55. $validation = new FertilizerValidation($validationData);
  56. $validation->validated(); // 这个方法会在验证失败时抛出异常
  57. try {
  58. // 验证通过后,开启事务
  59. DB::beginTransaction();
  60. // 从验证结果中获取数据,避免重复查询
  61. $cropGrowthTime = $validation->crop_growth_time;
  62. $crop = $validation->crop; // 从验证结果中获取作物对象
  63. $cropId = $crop->id; // 获取作物ID
  64. Log::info('FertilizerHandler: 验证通过,获取到作物信息', [
  65. 'user_id' => $userId,
  66. 'land_id' => $landId,
  67. 'crop_id' => $cropId,
  68. 'crop_growth_time' => $cropGrowthTime,
  69. ]);
  70. // 使用肥料(直接传入作物ID)
  71. $result = CropService::useFertilizer($cropId, $cropGrowthTime);
  72. if ($result->error) {
  73. throw new LogicException("施肥失败:" . $result->message);
  74. }
  75. // 消耗物品
  76. ItemService::consumeItem($userId, $itemId, null, 1, [
  77. 'source_type' => 'land_fertilizer',
  78. 'source_id' => $landId,
  79. 'details' => ['land_id' => $landId, 'crop_id' => $cropId]
  80. ]);
  81. // 提交事务
  82. DB::commit();
  83. Log::info('施肥操作成功', [
  84. 'user_id' => $userId,
  85. 'land_id' => $landId,
  86. 'item_id' => $itemId,
  87. 'crop_id' => $cropId,
  88. 'crop_growth_time' => $cropGrowthTime,
  89. ]);
  90. // 更新作物生长阶段(在事务外执行)
  91. CropService::updateGrowthStage($cropId);
  92. } catch (\Exception $e) {
  93. // 系统异常,需要回滚事务
  94. if (DB::transactionLevel() > 0) {
  95. DB::rollBack();
  96. }
  97. Logger::error('施肥操作异常', [
  98. 'user_id' => $userId,
  99. 'land_id' => $landId,
  100. 'item_id' => $itemId,
  101. 'error' => $e->getMessage(),
  102. 'trace' => $e->getTraceAsString()
  103. ]);
  104. // 重新抛出异常,交由框架处理
  105. throw $e;
  106. }
  107. return $response;
  108. }
  109. }