| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- <?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\Common\DataGod;
- use Uraus\Kku\Common\LastData;
- 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();
- $userId = $this->user_id;
- // 参数验证
- if ($godId <= 0) {
- throw new LogicException("神像ID无效");
- }
- // 检查神像ID是否有效
- if (!in_array($godId, [
- BUFF_TYPE::HARVEST_GOD->value,
- BUFF_TYPE::RAIN_GOD->value,
- BUFF_TYPE::WEED_KILLER_GOD->value,
- BUFF_TYPE::PEST_CLEANER_GOD->value
- ])) {
- throw new LogicException("无效的神像类型");
- }
- // 检查用户是否已有该神像的有效加持
- $existingBuff = BuffService::getActiveUserBuff($userId, $godId);
- if ($existingBuff) {
- throw new LogicException("该神像已激活,有效期至:" . $existingBuff->expire_time);
- }
- // 查找用户背包中的神像物品
- $godItemId = 3000 + $godId; // 假设神像物品ID为3001-3004,对应神像类型1-4
- $userItems = ItemService::getUserItems($userId, ['item_id' => $godItemId]);
- if ($userItems->isEmpty()) {
- throw new LogicException("您没有该神像物品");
- }
- // 开始事务
- DB::beginTransaction();
- // 消耗神像物品
- ItemService::consumeItem($userId, $godItemId, null, 1, [
- 'source_type' => 'god_activate',
- 'source_id' => $godId,
- 'details' => [
- 'god_id' => $godId,
- 'god_name' => BUFF_TYPE::getName($godId)
- ]
- ]);
- // 激活神像加持(默认24小时)
- $durationHours = 24;
- $buff = BuffService::activateBuff($userId, $godId, $durationHours);
- if (!$buff) {
- throw new LogicException("神像激活失败");
- }
- // 提交事务
- DB::commit();
- // 创建LastData对象,用于返回神像信息
- $lastData = new LastData();
- $godList = [];
- // 创建神像数据
- $dataGod = new DataGod();
- $dataGod->setId($godId);
- $dataGod->setStatus(true);
- $dataGod->setVaidTime($buff->expire_time->timestamp);
- $godList[] = $dataGod;
- // 设置神像列表到LastData
- $lastData->setGods($godList);
- // 设置LastData到响应
- $this->response->setLastData($lastData);
- // 设置响应状态
- $this->response->setCode(0);
- $this->response->setMsg('神像激活成功');
- // 记录日志
- Log::info('用户激活神像成功', [
- 'user_id' => $userId,
- 'god_id' => $godId,
- 'expire_time' => $buff->expire_time->toDateTimeString()
- ]);
- } 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,
- '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,
- 'error' => $e->getMessage(),
- 'trace' => $e->getTraceAsString()
- ]);
- }
- return $response;
- }
- }
|