SowHandler.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. namespace App\Module\AppGame\Handler\Land;
  3. use App\Module\AppGame\Handler\BaseHandler;
  4. use App\Module\AppGame\Proto\LandInfoDto;
  5. use App\Module\Farm\Services\CropService;
  6. use App\Module\Farm\Services\LandService;
  7. use App\Module\Farm\Validations\CropPlantValidation;
  8. use App\Module\GameItems\Services\ItemService;
  9. use Google\Protobuf\Internal\Message;
  10. use Illuminate\Support\Facades\DB;
  11. use Illuminate\Support\Facades\Log;
  12. use Uraus\Kku\Common\LastData;
  13. use Uraus\Kku\Request\RequestLandSow;
  14. use Uraus\Kku\Response\ResponseLandSow;
  15. use UCore\Exception\LogicException;
  16. /**
  17. * 种植操作请求处理器
  18. */
  19. class SowHandler extends BaseHandler
  20. {
  21. /**
  22. * 是否需要登录
  23. * @var bool
  24. */
  25. protected bool $need_login = true;
  26. /**
  27. * 处理种植操作请求
  28. *
  29. * @param RequestLandSow $data 种植操作请求数据
  30. * @return ResponseLandSow 种植操作响应
  31. */
  32. public function handle(Message $data): Message
  33. {
  34. // 创建响应对象
  35. $response = new ResponseLandSow();
  36. // 获取请求参数
  37. $landId = $data->getLandId();
  38. $itemId = $data->getItemId();
  39. $itemInstanceId = $data->getItemInstanceId();
  40. $userId = $this->user_id;
  41. // 先进行验证,避免不必要的事务开销
  42. $validation = new CropPlantValidation([
  43. 'user_id' => $userId,
  44. 'land_id' => $landId,
  45. 'item_id' => $itemId,
  46. 'item_instance_id' => $itemInstanceId
  47. ]);
  48. // 验证数据
  49. $validation->validated();
  50. try {
  51. // 验证通过后,开启事务
  52. DB::beginTransaction();
  53. // 调用Farm模块的CropService种植作物
  54. $plantResult = CropService::plantCrop($userId, $landId, $itemId);
  55. if (!$plantResult) {
  56. throw new LogicException("种植失败,请检查土地状态");
  57. }
  58. // 获取种植日志ID
  59. $sowLogId = $plantResult['log_id'] ?? 0;
  60. // 消耗种子物品
  61. ItemService::consumeItem($userId, $itemId, $itemInstanceId, 1, [
  62. 'source_type' => 'land_sow',
  63. 'source_id' => $landId,
  64. 'details' => [
  65. 'land_id' => $landId,
  66. 'sow_id' => $sowLogId
  67. ]
  68. ]);
  69. // 提交事务
  70. DB::commit();
  71. // LastData 由中间件处理
  72. Log::info('用户种植成功', [
  73. 'user_id' => $userId,
  74. 'land_id' => $landId,
  75. 'seed_id' => $itemId,
  76. 'item_instance_id' => $itemInstanceId,
  77. 'crop_id' => $plantResult['crop']->id ?? 0,
  78. 'sow_log_id' => $sowLogId
  79. ]);
  80. } catch (\Exception $e) {
  81. // 系统异常,需要回滚事务
  82. if (DB::transactionLevel() > 0) {
  83. DB::rollBack();
  84. }
  85. $this->response->setCode(500);
  86. $this->response->setMsg('系统错误,请稍后再试');
  87. Log::error('种植操作异常', [
  88. 'user_id' => $userId ?? null,
  89. 'land_id' => $landId ?? null,
  90. 'item_id' => $itemId ?? null,
  91. 'error' => $e->getMessage(),
  92. 'trace' => $e->getTraceAsString()
  93. ]);
  94. }
  95. return $response;
  96. }
  97. }