SowHandler.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. try {
  37. // 获取请求参数
  38. $landId = $data->getLandId();
  39. $itemId = $data->getItemId();
  40. $itemInstanceId = $data->getItemInstanceId();
  41. $userId = $this->user_id;
  42. // 先进行验证,避免不必要的事务开销
  43. $validation = new CropPlantValidation([
  44. 'user_id' => $userId,
  45. 'land_id' => $landId,
  46. 'item_id' => $itemId,
  47. 'item_instance_id' => $itemInstanceId
  48. ]);
  49. // 验证数据
  50. $validation->validated();
  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. // 获取更新后的土地信息并返回
  72. $updatedLand = LandService::getUserLand($userId, $landId);
  73. if ($updatedLand) {
  74. // 创建LastData对象
  75. $lastData = new LastData();
  76. $landData = LandInfoDto::toDataLand($updatedLand);
  77. $lastData->setLands([$landData]);
  78. // 设置LastData到响应
  79. $this->response->setLastData($lastData);
  80. }
  81. // 设置响应状态
  82. $this->response->setCode(0);
  83. $this->response->setMsg('种植成功');
  84. Log::info('用户种植成功', [
  85. 'user_id' => $userId,
  86. 'land_id' => $landId,
  87. 'seed_id' => $itemId,
  88. 'item_instance_id' => $itemInstanceId,
  89. 'crop_id' => $plantResult['crop']->id ?? 0,
  90. 'sow_log_id' => $sowLogId
  91. ]);
  92. } catch (\UCore\Exception\ValidateException $e) {
  93. // 验证失败,此时可能还没有开启事务
  94. $this->response->setCode(400);
  95. $this->response->setMsg($e->getMessage());
  96. Log::warning('种植验证失败', [
  97. 'user_id' => $userId ?? null,
  98. 'land_id' => $landId ?? null,
  99. 'item_id' => $itemId ?? null,
  100. 'error' => $e->getMessage()
  101. ]);
  102. } catch (LogicException $e) {
  103. // 业务逻辑异常,需要回滚事务
  104. if (DB::transactionLevel() > 0) {
  105. DB::rollBack();
  106. }
  107. $this->response->setCode(400);
  108. $this->response->setMsg($e->getMessage());
  109. Log::warning('用户种植失败', [
  110. 'user_id' => $userId ?? null,
  111. 'land_id' => $landId ?? null,
  112. 'item_id' => $itemId ?? null,
  113. 'error' => $e->getMessage()
  114. ]);
  115. } catch (\Exception $e) {
  116. // 系统异常,需要回滚事务
  117. if (DB::transactionLevel() > 0) {
  118. DB::rollBack();
  119. }
  120. $this->response->setCode(500);
  121. $this->response->setMsg('系统错误,请稍后再试');
  122. Log::error('种植操作异常', [
  123. 'user_id' => $userId ?? null,
  124. 'land_id' => $landId ?? null,
  125. 'item_id' => $itemId ?? null,
  126. 'error' => $e->getMessage(),
  127. 'trace' => $e->getTraceAsString()
  128. ]);
  129. }
  130. return $response;
  131. }
  132. }