OpenHandler.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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\Common\DataGod;
  11. use Uraus\Kku\Common\LastData;
  12. use Uraus\Kku\Request\RequestGodOpen;
  13. use Uraus\Kku\Response\ResponseGodOpen;
  14. use UCore\Exception\LogicException;
  15. /**
  16. * 处理神像开启请求
  17. */
  18. class OpenHandler extends BaseHandler
  19. {
  20. /**
  21. * 是否需要登录
  22. * @var bool
  23. */
  24. protected bool $need_login = true;
  25. /**
  26. * 处理神像开启请求
  27. *
  28. * @param RequestGodOpen $data 神像开启请求数据
  29. * @return ResponseGodOpen 神像开启响应
  30. */
  31. public function handle(Message $data): Message
  32. {
  33. // 创建响应对象
  34. $response = new ResponseGodOpen();
  35. try {
  36. // 获取请求参数
  37. $godId = $data->getGodId();
  38. $userId = $this->user_id;
  39. // 参数验证
  40. if ($godId <= 0) {
  41. throw new LogicException("神像ID无效");
  42. }
  43. // 检查神像ID是否有效
  44. if (!in_array($godId, [
  45. BUFF_TYPE::HARVEST_GOD->value,
  46. BUFF_TYPE::RAIN_GOD->value,
  47. BUFF_TYPE::WEED_KILLER_GOD->value,
  48. BUFF_TYPE::PEST_CLEANER_GOD->value
  49. ])) {
  50. throw new LogicException("无效的神像类型");
  51. }
  52. // 检查用户是否已有该神像的有效加持
  53. $existingBuff = BuffService::getActiveUserBuff($userId, $godId);
  54. if ($existingBuff) {
  55. throw new LogicException("该神像已激活,有效期至:" . $existingBuff->expire_time);
  56. }
  57. // 查找用户背包中的神像物品
  58. $godItemId = 3000 + $godId; // 假设神像物品ID为3001-3004,对应神像类型1-4
  59. $userItems = ItemService::getUserItems($userId, ['item_id' => $godItemId]);
  60. if ($userItems->isEmpty()) {
  61. throw new LogicException("您没有该神像物品");
  62. }
  63. // 开始事务
  64. DB::beginTransaction();
  65. // 消耗神像物品
  66. ItemService::consumeItem($userId, $godItemId, null, 1, [
  67. 'source_type' => 'god_activate',
  68. 'source_id' => $godId,
  69. 'details' => [
  70. 'god_id' => $godId,
  71. 'god_name' => BUFF_TYPE::getName($godId)
  72. ]
  73. ]);
  74. // 激活神像加持(默认24小时)
  75. $durationHours = 24;
  76. $buff = BuffService::activateBuff($userId, $godId, $durationHours);
  77. if (!$buff) {
  78. throw new LogicException("神像激活失败");
  79. }
  80. // 提交事务
  81. DB::commit();
  82. // 创建LastData对象,用于返回神像信息
  83. $lastData = new LastData();
  84. $godList = [];
  85. // 创建神像数据
  86. $dataGod = new DataGod();
  87. $dataGod->setId($godId);
  88. $dataGod->setStatus(true);
  89. $dataGod->setVaidTime($buff->expire_time->timestamp);
  90. $godList[] = $dataGod;
  91. // 设置神像列表到LastData
  92. $lastData->setGods($godList);
  93. // 设置LastData到响应
  94. $this->response->setLastData($lastData);
  95. // 设置响应状态
  96. $this->response->setCode(0);
  97. $this->response->setMsg('神像激活成功');
  98. // 记录日志
  99. Log::info('用户激活神像成功', [
  100. 'user_id' => $userId,
  101. 'god_id' => $godId,
  102. 'expire_time' => $buff->expire_time->toDateTimeString()
  103. ]);
  104. } catch (LogicException $e) {
  105. // 回滚事务
  106. if (DB::transactionLevel() > 0) {
  107. DB::rollBack();
  108. }
  109. // 设置错误响应
  110. $this->response->setCode(400);
  111. $this->response->setMsg($e->getMessage());
  112. Log::warning('用户激活神像失败', [
  113. 'user_id' => $this->user_id,
  114. 'error' => $e->getMessage(),
  115. 'trace' => $e->getTraceAsString()
  116. ]);
  117. } catch (\Exception $e) {
  118. // 回滚事务
  119. if (DB::transactionLevel() > 0) {
  120. DB::rollBack();
  121. }
  122. // 设置错误响应
  123. $this->response->setCode(500);
  124. $this->response->setMsg('系统错误,请稍后再试');
  125. Log::error('激活神像操作异常', [
  126. 'user_id' => $this->user_id,
  127. 'error' => $e->getMessage(),
  128. 'trace' => $e->getTraceAsString()
  129. ]);
  130. }
  131. return $response;
  132. }
  133. }