GetHandler.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. namespace App\Module\AppGame\Handler\Pet;
  3. use App\Module\AppGame\Handler\BaseHandler;
  4. use App\Module\GameItems\Services\ItemService;
  5. use App\Module\Pet\Services\PetService;
  6. use Google\Protobuf\Internal\Message;
  7. use Illuminate\Support\Facades\DB;
  8. use Illuminate\Support\Facades\Log;
  9. use Uraus\Kku\Request\RequestPetGet;
  10. use Uraus\Kku\Response\ResponsePetGet;
  11. use UCore\Exception\LogicException;
  12. /**
  13. * 处理通过物品获取宠物请求
  14. */
  15. class GetHandler extends BaseHandler
  16. {
  17. /**
  18. * 是否需要登录
  19. *
  20. * @var bool
  21. */
  22. protected bool $need_login = true;
  23. /**
  24. * 处理通过物品获取宠物请求
  25. *
  26. * @param RequestPetGet $data 获取宠物请求数据
  27. * @return ResponsePetGet 获取宠物响应
  28. */
  29. public function handle(Message $data): Message
  30. {
  31. // 创建响应对象
  32. $response = new ResponsePetGet();
  33. // 获取请求参数
  34. $itemId = $data->getItemId();
  35. $userId = $this->user_id;
  36. // 验证参数
  37. if (!$itemId || $itemId <= 0) {
  38. throw new LogicException("物品ID无效");
  39. }
  40. // 先进行验证,避免不必要的事务开销
  41. $validation = new \App\Module\Pet\Validations\PetGetValidation([
  42. 'user_id' => $userId,
  43. 'item_id' => $itemId
  44. ]);
  45. // 验证数据
  46. $validation->validated();
  47. try {
  48. // 验证通过后,开启事务
  49. DB::beginTransaction();
  50. // 执行业务逻辑(不再需要验证)
  51. $result = $this->createPetFromItem($userId, $itemId);
  52. // 提交事务
  53. DB::commit();
  54. } catch (\Exception $e) {
  55. // 系统异常,需要回滚事务
  56. if (DB::transactionLevel() > 0) {
  57. DB::rollBack();
  58. }
  59. // 设置错误响应
  60. $this->response->setCode(500);
  61. $this->response->setMsg('系统错误,请稍后再试');
  62. Log::error('获取宠物操作异常', [
  63. 'user_id' => $this->user_id,
  64. 'item_id' => $itemId ?? null,
  65. 'error' => $e->getMessage(),
  66. 'trace' => $e->getTraceAsString()
  67. ]);
  68. }
  69. return $response;
  70. }
  71. /**
  72. * 通过物品创建宠物
  73. *
  74. * @param int $userId 用户ID
  75. * @param int $itemId 物品ID
  76. * @return array 创建结果
  77. * @throws LogicException
  78. */
  79. private function createPetFromItem(int $userId, int $itemId): array
  80. {
  81. // 获取物品的宠物种类属性
  82. $petType = ItemService::getItemNumericAttribute($itemId, 'pet_type');
  83. if (empty($petType)) {
  84. throw new LogicException("该物品不能获取宠物");
  85. }
  86. // 消耗物品
  87. ItemService::consumeItem($userId, $itemId, null, 1, [
  88. 'source_type' => 'pet_get',
  89. 'source_id' => 0,
  90. 'details' => [
  91. 'pet_type' => $petType,
  92. 'action' => 'create_pet_from_item'
  93. ]
  94. ]);
  95. // 生成宠物名称(可以根据宠物种类生成默认名称)
  96. $petName = $this->generatePetName($petType);
  97. // 创建宠物
  98. $result = PetService::createPet($userId, $petName, null, [
  99. 'pet_type' => $petType,
  100. 'source' => 'item_use',
  101. 'source_item_id' => $itemId
  102. ]);
  103. if (!$result['success']) {
  104. throw new LogicException("宠物创建失败: " . ($result['message'] ?? '未知错误'));
  105. }
  106. // 创建LastData对象,用于返回宠物信息
  107. $lastData = new \Uraus\Kku\Common\LastData();
  108. // 获取创建的宠物详细信息
  109. $petData = PetService::getPetStatus($userId, $result['pet_id']);
  110. // 设置宠物数据到LastData(这里需要根据实际的LastData结构调整)
  111. // $lastData->setPets([$petData]);
  112. // 记录日志
  113. Log::info('用户通过物品获取宠物成功', [
  114. 'user_id' => $userId,
  115. 'item_id' => $itemId,
  116. 'pet_type' => $petType,
  117. 'pet_id' => $result['pet_id'],
  118. 'pet_name' => $petName,
  119. 'pet_grade' => $result['grade']
  120. ]);
  121. return [
  122. 'message' => '恭喜您获得了新宠物:' . $petName,
  123. 'last_data' => $lastData,
  124. 'pet_id' => $result['pet_id'],
  125. 'pet_name' => $petName,
  126. 'pet_type' => $petType,
  127. 'pet_grade' => $result['grade']
  128. ];
  129. }
  130. /**
  131. * 生成宠物名称
  132. *
  133. * @param string $petType 宠物种类
  134. * @return string 宠物名称
  135. */
  136. private function generatePetName(string $petType): string
  137. {
  138. // 根据宠物种类生成默认名称
  139. $nameMap = [
  140. 'dog' => '小狗',
  141. 'cat' => '小猫',
  142. 'bird' => '小鸟',
  143. 'fish' => '小鱼',
  144. 'rabbit' => '小兔',
  145. 'hamster' => '小仓鼠',
  146. ];
  147. $baseName = $nameMap[$petType] ?? '小宠物';
  148. // 添加随机后缀以避免重名
  149. $suffix = rand(1000, 9999);
  150. return $baseName . $suffix;
  151. }
  152. }