SowHandler.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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\Services\LandService;
  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\RequestLandSow;
  11. use Uraus\Kku\Response\ResponseLandSow;
  12. use UCore\Exception\LogicException;
  13. /**
  14. * 种植操作请求处理器
  15. */
  16. class SowHandler extends BaseHandler
  17. {
  18. /**
  19. * 是否需要登录
  20. * @var bool
  21. */
  22. protected bool $need_login = true;
  23. /**
  24. * 处理种植操作请求
  25. *
  26. * @param RequestLandSow $data 种植操作请求数据
  27. * @return ResponseLandSow 种植操作响应
  28. */
  29. public function handle(Message $data): Message
  30. {
  31. // 创建响应对象
  32. $response = new ResponseLandSow();
  33. try {
  34. // 获取请求参数
  35. $landId = $data->getLandId();
  36. $itemId = $data->getItemId();
  37. $itemInstanceId = $data->getItemInstanceId();
  38. $userId = $this->user_id;
  39. // 验证土地是否存在且属于当前用户
  40. // 获取用户所有土地
  41. $userLands = LandService::getUserLands($userId);
  42. $landInfo = null;
  43. // 查找指定ID的土地
  44. foreach ($userLands as $land) {
  45. if ($land->id == $landId) {
  46. $landInfo = $land;
  47. break;
  48. }
  49. }
  50. if (!$landInfo) {
  51. throw new LogicException("土地不存在或不属于当前用户");
  52. }
  53. // 在事务开始前检查土地状态
  54. if ($landInfo->status !== \App\Module\Farm\Enums\LAND_STATUS::IDLE->value) {
  55. throw new LogicException("土地状态不允许种植");
  56. }
  57. // 验证用户是否拥有该种子物品
  58. $hasItem = ItemService::getUserItems($userId, ['item_id' => $itemId]);
  59. if ($hasItem->isEmpty()) {
  60. throw new LogicException("您没有该种子");
  61. }
  62. // 开启数据库事务
  63. DB::beginTransaction();
  64. // 调用Farm模块的CropService种植作物
  65. $plantResult = CropService::plantCrop($userId, $landId, $itemId);
  66. if (!$plantResult) {
  67. throw new LogicException("种植失败,请检查土地状态");
  68. }
  69. // 获取种植日志ID
  70. $sowLogId = $plantResult['log_id'] ?? 0;
  71. // 消耗种子物品
  72. ItemService::consumeItem($userId, $itemId, $itemInstanceId, 1, [
  73. 'source_type' => 'land_sow',
  74. 'source_id' => $landId,
  75. 'details' => [
  76. 'land_id' => $landId,
  77. 'sow_id' => $sowLogId
  78. ]
  79. ]);
  80. // 提交事务
  81. DB::commit();
  82. Log::info('用户种植成功', [
  83. 'user_id' => $userId,
  84. 'land_id' => $landId,
  85. 'seed_id' => $itemId,
  86. 'item_instance_id' => $itemInstanceId,
  87. 'crop_id' => $plantResult['crop']->id ?? 0,
  88. 'sow_log_id' => $sowLogId
  89. ]);
  90. } catch (LogicException $e) {
  91. // 回滚事务
  92. DB::rollBack();
  93. // 设置错误响应
  94. $this->response->setCode(400);
  95. $this->response->setMsg($e->getMessage());
  96. Log::warning('用户种植失败', [
  97. 'user_id' => $this->user_id,
  98. 'error' => $e->getMessage(),
  99. 'trace' => $e->getTraceAsString()
  100. ]);
  101. }
  102. return $response;
  103. }
  104. }