| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199 |
- <?php
- namespace App\Module\AppGame\Handler\God;
- use App\Module\AppGame\Handler\BaseHandler;
- use App\Module\Farm\Enums\BUFF_TYPE;
- use App\Module\Farm\Services\BuffService;
- use App\Module\GameItems\Services\ItemService;
- use Google\Protobuf\Internal\Message;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Log;
- use Uraus\Kku\Request\RequestGodOpen;
- use Uraus\Kku\Response\ResponseGodOpen;
- use UCore\Exception\LogicException;
- /**
- * 处理神像开启请求
- */
- class OpenHandler extends BaseHandler
- {
- /**
- * 是否需要登录
- * @var bool
- */
- protected bool $need_login = true;
- /**
- * 处理神像开启请求
- *
- * @param RequestGodOpen $data 神像开启请求数据
- * @return ResponseGodOpen 神像开启响应
- */
- public function handle(Message $data): Message
- {
- // 创建响应对象
- $response = new ResponseGodOpen();
- try {
- // 获取请求参数
- $godId = $data->getGodId();
- $itemId = $data->getItemId();
- $userId = $this->user_id;
- // 如果客户端没有指定物品ID,则自动查找
- if (!$itemId || $itemId <= 0) {
- $itemId = $this->findGodItem($userId, $godId);
- }
- // 先进行验证,避免不必要的事务开销
- $validation = new \App\Module\Farm\Validations\GodActivationValidation([
- 'user_id' => $userId,
- 'god_id' => $godId,
- 'item_id' => $itemId
- ]);
- // 验证数据
- $validation->validated();
- // 验证通过后,开启事务
- DB::beginTransaction();
- // 执行业务逻辑(不再需要验证)
- $result = $this->activateGodBuff($userId, $godId, $itemId);
- // 提交事务
- DB::commit();
- // 设置响应状态
- $this->response->setCode(0);
- $this->response->setMsg($result['message']);
- } catch (\UCore\Exception\ValidateException $e) {
- // 验证失败,此时可能还没有开启事务
- $this->response->setCode(400);
- $this->response->setMsg($e->getMessage());
- Log::warning('用户神像激活验证失败', [
- 'user_id' => $this->user_id,
- 'god_id' => $godId ?? null,
- 'item_id' => $itemId ?? null,
- 'error' => $e->getMessage()
- ]);
- } catch (LogicException $e) {
- // 业务逻辑异常,需要回滚事务
- if (DB::transactionLevel() > 0) {
- DB::rollBack();
- }
- // 设置错误响应
- $this->response->setCode(400);
- $this->response->setMsg($e->getMessage());
- Log::warning('用户神像激活失败', [
- 'user_id' => $this->user_id,
- 'god_id' => $godId ?? null,
- 'item_id' => $itemId ?? null,
- 'error' => $e->getMessage(),
- 'trace' => $e->getTraceAsString()
- ]);
- } catch (\Exception $e) {
- // 系统异常,需要回滚事务
- if (DB::transactionLevel() > 0) {
- DB::rollBack();
- }
- // 设置错误响应
- $this->response->setCode(500);
- $this->response->setMsg('系统错误,请稍后再试');
- Log::error('神像激活操作异常', [
- 'user_id' => $this->user_id,
- 'god_id' => $godId ?? null,
- 'item_id' => $itemId ?? null,
- 'error' => $e->getMessage(),
- 'trace' => $e->getTraceAsString()
- ]);
- }
- return $response;
- }
- /**
- * 查找用户背包中具有指定神像种类的物品(备用方法)
- * 当客户端没有指定物品ID时使用此方法自动查找
- *
- * @param int $userId 用户ID
- * @param int $godId 神像ID
- * @return int 物品ID
- * @throws LogicException
- */
- private function findGodItem(int $userId, int $godId): int
- {
- // 获取用户所有物品
- $userItems = ItemService::getUserItems($userId);
- foreach ($userItems as $userItem) {
- // 检查物品是否具有神像时间属性
- $godDuration = ItemService::getItemNumericAttribute($userItem->itemId, 'god_duration_seconds');
- // 检查物品的神像种类是否匹配
- $godType = ItemService::getItemNumericAttribute($userItem->itemId, 'god_type');
- if ($godDuration > 0 && $godType === $godId) {
- return $userItem->itemId;
- }
- }
- throw new LogicException("您没有对应的神像物品");
- }
- /**
- * 激活神像buff
- *
- * @param int $userId 用户ID
- * @param int $godId 神像ID
- * @param int $itemId 物品ID
- * @return array 激活结果
- * @throws LogicException
- */
- private function activateGodBuff(int $userId, int $godId, int $itemId): array
- {
- // 获取物品的神像时间属性
- $godDurationSeconds = ItemService::getItemNumericAttribute($itemId, 'god_duration_seconds');
- $durationHours = $godDurationSeconds > 0 ? ceil($godDurationSeconds / 3600) : 24; // 转换为小时,默认24小时
- // 消耗神像物品
- ItemService::consumeItem($userId, $itemId, null, 1, [
- 'source_type' => 'god_activate',
- 'source_id' => $godId,
- 'details' => [
- 'god_id' => $godId,
- 'god_name' => BUFF_TYPE::getName($godId),
- 'duration_seconds' => $godDurationSeconds
- ]
- ]);
- // 激活神像加持
- $buff = BuffService::activateBuff($userId, $godId, $durationHours);
- if (!$buff) {
- throw new LogicException("神像激活失败");
- }
- // 记录日志
- Log::info('用户激活神像成功', [
- 'user_id' => $userId,
- 'god_id' => $godId,
- 'item_id' => $itemId,
- 'duration_seconds' => $godDurationSeconds,
- 'duration_hours' => $durationHours,
- 'expire_time' => $buff->expire_time ? $buff->expire_time->toDateTimeString() : null
- ]);
- return [
- 'message' => '神像激活成功',
- 'buff' => $buff
- ];
- }
- }
|