| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259 |
- <?php
- namespace App\Module\AppGame\Handler\User;
- use App\Module\AppGame\Handler\BaseHandler;
- use App\Module\AppGame\Proto\FarmInfoDto;
- use App\Module\AppGame\Proto\GodBuffDto;
- use App\Module\AppGame\Proto\LandInfoDto;
- use App\Module\AppGame\Proto\PetDataDto;
- use App\Module\Farm\Dtos\GodBuffDto as FarmGodBuffDto;
- use App\Module\Farm\Services\BuffService;
- use App\Module\Farm\Services\FarmService;
- use App\Module\Farm\Services\LandService;
- use App\Module\Fund\Services\AccountService;
- use App\Module\GameItems\Services\ItemService;
- use App\Module\Pet\Services\PetService;
- use App\Module\User\Services\UserService;
- use Google\Protobuf\Internal\Message;
- use Illuminate\Support\Facades\Log;
- use Uraus\Kku\Common\DataCoin;
- use Uraus\Kku\Common\DataGod;
- use Uraus\Kku\Common\DataItem;
- use Uraus\Kku\Common\DataLand;
- use Uraus\Kku\Common\DataPet;
- use Uraus\Kku\Common\LastData;
- use Uraus\Kku\Request\RequestUserData;
- use Uraus\Kku\Response\ResponseUserData;
- use Uraus\Kku\Response\UserInfo;
- /**
- * 处理用户信息请求
- */
- class DataHandler extends BaseHandler
- {
- /**
- * 是否需要登录
- *
- * @var bool
- */
- protected bool $need_login = true;
- /**
- * 处理用户信息请求
- *
- * @param RequestUserData $data 用户信息请求数据
- * @return ResponseUserData 用户信息响应
- */
- public function handle(Message $data): Message
- {
- $lastData = new LastData();
- $this->response->setLastData($lastData);
- // 创建响应对象
- $uinfo = UserService::info($this->user_id);
- $uinfo2 = UserService::infoinfo($this->user_id);
- $response = new ResponseUserData();
- $info = new UserInfo();
- $info->setMobile($uinfo->username);
- $info->setUserId($uinfo->id);
- $info->setSkinId($uinfo2->avatar);
- $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::getOrInitializeFarm($this->user_id);
- // 或者使用getOrInitializeFarm方法,如果用户没有农场信息,会自动初始化
- // 直接转换为DataHourse对象
- $dataHourse = FarmInfoDto::toDataHourse($farmInfo);
- $this->response->getLastData()->setHourse($dataHourse);
- }
- /**
- * 用户土地数据
- *
- * @return void
- */
- private function getUserLands()
- {
- // 调用Farm模块的LandService获取用户的所有土地
- $lands = LandService::getUserLands($this->user_id);
- // 将土地数据转换为响应格式
- $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->instance_id) {
- $li->setInstanceId($item->instance_id);
- }
- $li->setItemId($item->item_id);
- $li->setQuantity($item->quantity);
- $itemLs[] = $li;
- }
- $this->response->getLastData()->setItems($itemLs);
- }
- /**
- * 用户货币的数据
- *
- * @return void
- */
- private function getUserCoins()
- {
- $lsc = [];
- $coins = AccountService::list4user($this->user_id);
- foreach ($coins as $coin) {
- $dC = new DataCoin();
- $dC->setType($coin->fund_id);
- $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;
- $godBuffDto->expireTime = $buff->expire_time ? $buff->expire_time->toDateTimeString() : 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()
- ]);
- }
- }
- }
|