response->setLastData($lastData); // 创建响应对象 $uinfo = UserService::info($this->user_id); $uinfo2 = UserService::infoinfo($this->user_id,true); $response = new ResponseUserData(); $info = new UserInfo(); $info->setMobile($uinfo->username); $info->setUserId($uinfo->id); // 获取用户皮肤信息 $currentSkinId = SkinService::getCurrentSkinId($this->user_id); $ownedSkins = SkinService::getOwnedSkins($this->user_id); $info->setSkinId($currentSkinId); $info->setSkinIds($ownedSkins); $info->setAvatar($uinfo2->avatar); $info->setNickName($uinfo2->nickname); $response->setInfo($info); // 激活 物品 lastData $this->getUserItems(); // 激活 货币 lastData $this->getUserCoins(); // 房屋 的 lastData $this->getUserHouse(); // 土地 的 lastData $this->getUserLands(); // 神像数据 $this->getUserGods(); // 宠物数据 $this->getUserPets(); return $response; } /** * 用户房屋数据 * * @return void */ private function getUserHouse() { $farmInfo = FarmService::getFarmInfo($this->user_id); if(!$farmInfo){ DB::beginTransaction(); $farmInfo = FarmService::initializeFarm($this->user_id); if(!$farmInfo){ DB::rollBack(); return ; } DB::commit(); } // 直接转换为DataHourse对象 $dataHourse = FarmInfoDto::toDataHourse($farmInfo); $this->response->getLastData()->setHourse($dataHourse); } /** * 用户土地数据 * * @return void */ private function getUserLands() { // 调用Farm模块的LandService获取用户的所有土地 $lands = LandService::getUserLands($this->user_id); // dd($lands); // 将土地数据转换为响应格式 $landDataList = []; foreach ($lands as $land) { $landData = LandInfoDto::toDataLand($land); $landDataList[] = $landData; } $this->response->getLastData()->setLands($landDataList); } /** * 用户物品的数据 * * @return void */ private function getUserItems() { $itemService = new ItemService(); $items = $itemService->getUserItems($this->user_id); $itemLs = []; foreach ($items as $item) { $li = new DataItem(); if ($item->instanceId) { $li->setInstanceId($item->instanceId); } $li->setItemId($item->itemId); if($item->quantity <= 0){ continue; } $li->setQuantity($item->quantity); // 设置堆id(item_user表的id) $li->setIuId($item->id); // 设置过期时间 if ($item->expireAt) { $expireTime = strtotime($item->expireAt); $li->setExpireTime($expireTime); } // 设置冻结状态 $li->setIsFrozen($item->isFrozen); $itemLs[] = $li; } $this->response->getLastData()->setItems($itemLs); } /** * 用户货币的数据 * * @return void */ private function getUserCoins() { $lsc = []; $coins = AccountService::list4user($this->user_id); foreach ($coins as $coin) { // dump($coin->fund_id); $dC = new DataCoin(); $dC->setType($coin->getFundId()); $dC->setQuantity($coin->balance); $lsc[] = $dC; } $this->response->getLastData()->setCoins($lsc); } /** * 获取用户神像数据 * * @return void */ private function getUserGods() { try { // 调用Farm模块的BuffService获取用户的所有神灵加持 $buffs = BuffService::getUserBuffs($this->user_id); // 将神灵加持数据转换为响应格式 $godDataList = []; foreach ($buffs as $buff) { // 创建Farm模块的GodBuffDto $godBuffDto = new FarmGodBuffDto(); $godBuffDto->id = $buff->id; $godBuffDto->userId = $buff->user_id; $godBuffDto->buffType = $buff->buff_type; // 安全地处理过期时间,确保正确的类型转换 if ($buff->expire_time) { if (is_string($buff->expire_time)) { $godBuffDto->expireTime = $buff->expire_time; } elseif ($buff->expire_time instanceof \Carbon\Carbon) { $godBuffDto->expireTime = $buff->expire_time->toDateTimeString(); } else { $godBuffDto->expireTime = (string)$buff->expire_time; } } else { $godBuffDto->expireTime = null; } // 转换为Protobuf的DataGod对象 $dataGod = new DataGod(); $dataGod->setId($godBuffDto->buffType); // 设置激活状态(如果有过期时间且未过期,则为激活状态) $isActive = !empty($godBuffDto->expireTime) && strtotime($godBuffDto->expireTime) > time(); $dataGod->setStatus($isActive); // 设置有效期(转换为时间戳) if (!empty($godBuffDto->expireTime)) { $expireTime = strtotime($godBuffDto->expireTime); $dataGod->setVaidTime($expireTime); } $godDataList[] = $dataGod; } // 设置神灵加持数据 $this->response->getLastData()->setGods($godDataList); } catch (\Exception $e) { // 记录错误日志 Log::error('获取用户神灵加持数据失败', [ 'user_id' => $this->user_id, 'error' => $e->getMessage(), 'trace' => $e->getTraceAsString() ]); } } /** * 获取用户宠物数据 * * @return void */ private function getUserPets() { try { // 调用Pet模块的PetService获取用户的所有宠物 $pets = PetService::getUserPets($this->user_id); // 将宠物数据转换为响应格式 $petDataList = []; foreach ($pets as $pet) { try { // 获取宠物详细信息 $petDataDto = PetService::getPetStatus($this->user_id, $pet->id); // 转换为Protobuf的DataPet对象 $dataPet = PetDataDto::toDataPet($petDataDto); $petDataList[] = $dataPet; } catch (\Exception $e) { // 记录错误日志 Log::error('获取宠物详细信息失败', [ 'user_id' => $this->user_id, 'pet_id' => $pet->id, 'error' => $e->getMessage() ]); continue; } } // 设置宠物数据 $this->response->getLastData()->setPets($petDataList); } catch (\Exception $e) { // 记录错误日志 Log::error('获取用户宠物数据失败', [ 'user_id' => $this->user_id, 'error' => $e->getMessage(), 'trace' => $e->getTraceAsString() ]); } } }