OpenHandler.php 6.9 KB

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