| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309 |
- <?php
- namespace App\Module\AppGame\Handler\Public;
- use App\Module\AppGame\Handler\BaseHandler;
- use App\Module\Farm\Services\BuffService;
- use App\Module\Farm\Services\LandService;
- use App\Module\Fund\Services\AccountService;
- use App\Module\Game\Services\SkinService;
- use App\Module\GameItems\Services\ItemService;
- use App\Module\Pet\Services\PetService;
- use App\Module\User\Services\UserService;
- use Google\Protobuf\Internal\Message;
- use Uraus\Kku\Request\RequestPublicPlayerData;
- use Uraus\Kku\Response\ResponsePublicPlayerData;
- use Uraus\Kku\Response\PublicUserInfo;
- use Uraus\Kku\Response\PublicGod;
- use Uraus\Kku\Response\PublicLand;
- use Uraus\Kku\Response\PublicPet;
- use Uraus\Kku\Response\PublicZichan;
- /**
- * 处理玩家数据请求(公共数据)
- *
- */
- class PlayerDataHandler extends BaseHandler
- {
- /**
- * 是否需要登录
- * @var bool
- */
- protected bool $need_login = true;
- /**
- * 处理玩家数据请求
- *
- * @param RequestPublicPlayerData $data 玩家数据请求数据
- * @return ResponsePublicPlayerData 玩家数据响应
- */
- public function handle(Message $data): Message
- {
- // 获取请求的用户ID
- $targetUserId = $data->getUserId();
- // 创建响应对象
- $response = new ResponsePublicPlayerData();
- // 获取用户基本信息
- $this->setUserInfo($response, $targetUserId);
- // 获取神像数据
- $this->setGodsInfo($response, $targetUserId);
- // 获取土地数据
- $this->setLandInfo($response, $targetUserId);
- // 获取宠物数据
- $this->setPetInfo($response, $targetUserId);
- // 获取资产数据
- $this->setZichanInfo($response, $targetUserId);
- return $response;
- }
- /**
- * 设置用户基本信息
- *
- * @param ResponsePublicPlayerData $response
- * @param int $userId
- * @return void
- */
- private function setUserInfo(ResponsePublicPlayerData $response, int $userId): void
- {
- // 获取用户基本信息
- $userInfo = UserService::info($userId);
- $userDetailInfo = UserService::infoinfo($userId);
- if (!$userInfo || !$userDetailInfo) {
- return;
- }
- // 获取用户皮肤信息
- $currentSkinId = SkinService::getCurrentSkinId($userId);
- // 创建PublicUserInfo对象
- $publicUserInfo = new PublicUserInfo();
- $publicUserInfo->setUserD($userId); // 注意:protobuf中字段名是user_d
- $publicUserInfo->setSkinId($currentSkinId);
- $publicUserInfo->setAvatar($userDetailInfo->avatar ?? 0);
- $publicUserInfo->setNickName($userDetailInfo->nickname ?? '');
- $response->setUserInfo($publicUserInfo);
- }
- /**
- * 设置神像数据
- *
- * @param ResponsePublicPlayerData $response
- * @param int $userId
- * @return void
- */
- private function setGodsInfo(ResponsePublicPlayerData $response, int $userId): void
- {
- $godsInfo = [];
- // 遍历所有可能的神像类型(1-4)
- for ($godId = 1; $godId <= 4; $godId++) {
- $publicGod = new PublicGod();
- $publicGod->setId($godId);
- // 检查用户是否有该类型的有效神灵加持
- $activeBuff = BuffService::getActiveUserBuff($userId, $godId);
- $publicGod->setStatus($activeBuff !== null);
- $godsInfo[] = $publicGod;
- }
- $response->setGodsInfo($godsInfo);
- }
- /**
- * 设置土地数据
- *
- * @param ResponsePublicPlayerData $response
- * @param int $userId
- * @return void
- */
- private function setLandInfo(ResponsePublicPlayerData $response, int $userId): void
- {
- // 获取用户的所有土地
- $userLands = LandService::getUserLands($userId);
- $landInfo = [];
- foreach ($userLands as $landDto) {
- $publicLand = new PublicLand();
- $publicLand->setId($landDto->id);
- $publicLand->setIndex($landDto->position); // 使用position作为index
- $publicLand->setLevel($landDto->landType); // 使用landType作为level
- $publicLand->setStatus($landDto->status); // status已经是int类型
- // 设置种植相关信息
- if ($landDto->crop) {
- // 种植时间(使用plantTime字段)
- $plantTime = 0;
- if ($landDto->crop->plantTime) {
- if (is_string($landDto->crop->plantTime)) {
- $plantTime = strtotime($landDto->crop->plantTime);
- } elseif ($landDto->crop->plantTime instanceof \Carbon\Carbon) {
- $plantTime = $landDto->crop->plantTime->timestamp;
- } else {
- $plantTime = $landDto->crop->plantTime;
- }
- }
- $publicLand->setSeedPlantingTimes($plantTime);
- $publicLand->setSeedId($landDto->crop->seedId ?? 0);
- $publicLand->setFruitId($landDto->crop->finalOutputItemId ?? 0);
- $publicLand->setPlantId($landDto->crop->id ?? 0); // 使用作物ID作为植株ID
- $publicLand->setSeedStatus($landDto->crop->growthStage ?? 0);
- // 阶段开始时间
- $stageStartTime = 0;
- if ($landDto->crop->stageStartTime) {
- $stageStartTime = $landDto->crop->stageStartTime->timestamp ?? 0;
- }
- $publicLand->setStageStartTimes($stageStartTime);
- // 阶段结束时间
- $stageEndTime = 0;
- if ($landDto->crop->stageEndTime) {
- $stageEndTime = $landDto->crop->stageEndTime->timestamp ?? 0;
- }
- $publicLand->setStageNextTimes($stageEndTime);
- } else {
- $publicLand->setSeedPlantingTimes(0);
- $publicLand->setSeedId(0);
- $publicLand->setFruitId(0);
- $publicLand->setPlantId(0);
- $publicLand->setSeedStatus(0);
- $publicLand->setStageStartTimes(0);
- $publicLand->setStageNextTimes(0);
- }
- // 设置灾害信息(暂时为空数组)
- $publicLand->setDisasters([]);
- $landInfo[] = $publicLand;
- }
- $response->setLandInfo($landInfo);
- }
- /**
- * 设置宠物数据
- *
- * @param ResponsePublicPlayerData $response
- * @param int $userId
- * @return void
- */
- private function setPetInfo(ResponsePublicPlayerData $response, int $userId): void
- {
- try {
- // 获取用户的宠物列表
- $userPets = PetService::getUserPets($userId);
- // 如果用户没有宠物,则不设置宠物信息
- if ($userPets->isEmpty()) {
- return;
- }
- // 获取第一个宠物作为公共展示的宠物(通常是主宠物)
- $mainPet = $userPets->first();
- // 创建PublicPet对象
- $publicPet = new PublicPet();
- // 宠物种族ID:由于当前数据库设计中PetUser表没有type_id字段,
- // 我们使用默认值1(对应松狮),或者可以根据业务需求进行调整
- $typeId = 1; // 默认为松狮(ID=1)
- $publicPet->setTypeId($typeId);
- $publicPet->setName($mainPet->name ?? '');
- $publicPet->setLevel($mainPet->level ?? 1);
- $publicPet->setPower($mainPet->stamina ?? 0);
- $response->setPetInfo($publicPet);
- } catch (\Exception $e) {
- // 如果获取宠物信息失败,记录日志但不影响其他数据的返回
- \Illuminate\Support\Facades\Log::warning('获取用户宠物公共信息失败', [
- 'user_id' => $userId,
- 'error' => $e->getMessage()
- ]);
- }
- }
- /**
- * 设置资产数据
- *
- * @param ResponsePublicPlayerData $response
- * @param int $userId
- * @return void
- */
- private function setZichanInfo(ResponsePublicPlayerData $response, int $userId): void
- {
- try {
- // 创建PublicZichan对象
- $publicZichan = new PublicZichan();
- // 获取钻石余额(fund_id = 2)
- $diamondBalance = 0;
- try {
- $accounts = AccountService::list4user($userId);
- foreach ($accounts as $account) {
- if ($account->getFundId() == 2) { // 钻石的fund_id是2
- $diamondBalance = $account->balance;
- break;
- }
- }
- } catch (\Exception $e) {
- \Illuminate\Support\Facades\Log::warning('获取用户钻石余额失败', [
- 'user_id' => $userId,
- 'error' => $e->getMessage()
- ]);
- }
- // 获取建筑材料数量
- $woodQuantity = 0; // 木材 (item_id = 33)
- $stoneQuantity = 0; // 石材 (item_id = 34)
- $steelQuantity = 0; // 钢材 (item_id = 35)
- try {
- // 获取木材数量
- $woodItems = ItemService::getUserItems($userId, ['item_id' => 33]);
- $woodQuantity = $woodItems->sum('quantity');
- // 获取石材数量
- $stoneItems = ItemService::getUserItems($userId, ['item_id' => 34]);
- $stoneQuantity = $stoneItems->sum('quantity');
- // 获取钢材数量
- $steelItems = ItemService::getUserItems($userId, ['item_id' => 35]);
- $steelQuantity = $steelItems->sum('quantity');
- } catch (\Exception $e) {
- \Illuminate\Support\Facades\Log::warning('获取用户建筑材料数量失败', [
- 'user_id' => $userId,
- 'error' => $e->getMessage()
- ]);
- }
- // 设置资产信息
- $publicZichan->setZuanshi((float)$diamondBalance);
- $publicZichan->setMucai((float)$woodQuantity);
- $publicZichan->setShicai((float)$stoneQuantity);
- $publicZichan->setGangcai((float)$steelQuantity);
- $response->setZichan($publicZichan);
- } catch (\Exception $e) {
- // 如果获取资产信息失败,记录日志但不影响其他数据的返回
- \Illuminate\Support\Facades\Log::warning('获取用户资产公共信息失败', [
- 'user_id' => $userId,
- 'error' => $e->getMessage()
- ]);
- }
- }
- }
|