OpenHandler.php 6.3 KB

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