OpenHandler.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. namespace App\Module\AppGame\Handler\God;
  3. use App\Module\AppGame\Handler\BaseHandler;
  4. use App\Module\Farm\Enums\BUFF_TYPE;
  5. use App\Module\Farm\Services\BuffService;
  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\RequestGodOpen;
  11. use Uraus\Kku\Response\ResponseGodOpen;
  12. use Uraus\Kku\Common\RESPONSE_CODE;
  13. use UCore\Exception\LogicException;
  14. /**
  15. * 处理神像开启请求
  16. */
  17. class OpenHandler extends BaseHandler
  18. {
  19. /**
  20. * 是否需要登录
  21. * @var bool
  22. */
  23. protected bool $need_login = true;
  24. /**
  25. * 处理神像开启请求
  26. *
  27. * @param RequestGodOpen $data 神像开启请求数据
  28. * @return ResponseGodOpen 神像开启响应
  29. */
  30. public function handle(Message $data): Message
  31. {
  32. // 创建响应对象
  33. $response = new ResponseGodOpen();
  34. try {
  35. // 获取请求参数
  36. $godId = $data->getGodId();
  37. $itemId = $data->getItemId();
  38. $userId = $this->user_id;
  39. // 如果客户端没有指定物品ID,则自动查找
  40. if (!$itemId || $itemId <= 0) {
  41. $itemId = $this->findGodItem($userId, $godId);
  42. }
  43. // 先进行验证,避免不必要的事务开销
  44. $validation = new \App\Module\Farm\Validations\GodActivationValidation([
  45. 'user_id' => $userId,
  46. 'god_id' => $godId,
  47. 'item_id' => $itemId
  48. ]);
  49. // 验证数据
  50. $validation->validated();
  51. // 验证通过后,开启事务
  52. DB::beginTransaction();
  53. // 执行业务逻辑(不再需要验证)
  54. $result = $this->activateGodBuff($userId, $godId, $itemId);
  55. // 提交事务
  56. DB::commit();
  57. // 设置响应状态
  58. $this->response->setCode(RESPONSE_CODE::OK);
  59. $this->response->setMsg($result['message']);
  60. } catch (\Exception $e) {
  61. // 系统异常,需要回滚事务
  62. if (DB::transactionLevel() > 0) {
  63. DB::rollBack();
  64. }
  65. Log::error('神像激活操作异常', [
  66. 'user_id' => $this->user_id,
  67. 'god_id' => $godId ?? null,
  68. 'item_id' => $itemId ?? null,
  69. 'error' => $e->getMessage(),
  70. 'trace' => $e->getTraceAsString()
  71. ]);
  72. // 重新抛出异常,交由框架处理
  73. throw $e;
  74. }
  75. return $response;
  76. }
  77. /**
  78. * 查找用户背包中具有指定神像种类的物品(备用方法)
  79. * 当客户端没有指定物品ID时使用此方法自动查找
  80. *
  81. * @param int $userId 用户ID
  82. * @param int $godId 神像ID
  83. * @return int 物品ID
  84. * @throws LogicException
  85. */
  86. private function findGodItem(int $userId, int $godId): int
  87. {
  88. // 获取用户所有物品
  89. $userItems = ItemService::getUserItems($userId);
  90. foreach ($userItems as $userItem) {
  91. // 检查物品是否具有神像时间属性
  92. $godDuration = ItemService::getItemNumericAttribute($userItem->itemId, 'god_duration_seconds');
  93. // 检查物品的神像种类是否匹配
  94. $godType = ItemService::getItemNumericAttribute($userItem->itemId, 'god_type');
  95. if ($godDuration > 0 && $godType === $godId) {
  96. return $userItem->itemId;
  97. }
  98. }
  99. throw new LogicException("您没有对应的神像物品");
  100. }
  101. /**
  102. * 激活神像buff
  103. *
  104. * @param int $userId 用户ID
  105. * @param int $godId 神像ID
  106. * @param int $itemId 物品ID
  107. * @return array 激活结果
  108. * @throws LogicException
  109. */
  110. private function activateGodBuff(int $userId, int $godId, int $itemId): array
  111. {
  112. // 获取物品的神像时间属性
  113. $godDurationSeconds = ItemService::getItemNumericAttribute($itemId, 'god_duration_seconds');
  114. $durationHours = $godDurationSeconds > 0 ? ceil($godDurationSeconds / 3600) : 24; // 转换为小时,默认24小时
  115. // 消耗神像物品
  116. ItemService::consumeItem($userId, $itemId, null, 1, [
  117. 'source_type' => 'god_activate',
  118. 'source_id' => $godId,
  119. 'details' => [
  120. 'god_id' => $godId,
  121. 'god_name' => BUFF_TYPE::getName($godId),
  122. 'duration_seconds' => $godDurationSeconds
  123. ]
  124. ]);
  125. // 激活神像加持
  126. $buff = BuffService::activateBuff($userId, $godId, $durationHours);
  127. if (!$buff) {
  128. throw new LogicException("神像激活失败");
  129. }
  130. // 记录日志
  131. Log::info('用户激活神像成功', [
  132. 'user_id' => $userId,
  133. 'god_id' => $godId,
  134. 'item_id' => $itemId,
  135. 'duration_seconds' => $godDurationSeconds,
  136. 'duration_hours' => $durationHours,
  137. 'expire_time' => $buff->expire_time ? $buff->expire_time->toDateTimeString() : null
  138. ]);
  139. return [
  140. 'message' => '神像激活成功',
  141. 'buff' => $buff
  142. ];
  143. }
  144. }