Google Play Games C++ API - 无法以编程方式访问好友

Posted

技术标签:

【中文标题】Google Play Games C++ API - 无法以编程方式访问好友【英文标题】:Google Play Games C++ API - Unable to Access Friends Programatically 【发布时间】:2017-06-20 04:08:38 【问题描述】:

Google Play Games C++ 似乎没有为我返回正确的朋友数据。

这里的基本想法是,我们在游戏中有一个页面,显示朋友得分列表。此外,在您玩游戏时,当您接近和超过朋友的最高分时,它会在屏幕上显示一个指示器。

游戏是用 Cocos2d-x 编写的,因此我们使用了 Google Play Games C++ 库。 GPG 验证成功,其他功能(例如解锁成就和实际提交分数)正常工作。顺便说一句,如果 Google 的任何人正在阅读,网站上有 2.2 版的发行说明,但下载页面只有 2.1,所以这就是我们正在使用的。

无论如何,我们有两台设备具有不同的 Google Play 帐户,它们是朋友。我们可以选择打开默认的原生游戏服务排行榜 UI,如果我进入社交排行榜,那么两台设备都会在此页面上看到对方玩家的高分 - 所以看起来我们已经成功地让这两个帐户成为朋友了。

很遗憾,我无法使用 C++ API 以编程方式获取此朋友数据。

这里是相关代码。

void GameCenterSession::fetchFriends()

    __android_log_print(ANDROID_LOG_INFO, "MyGame!", "Fetching friends!");

    //GET INVITABLE FRIENDS
    game_services_->Players().FetchInvitable(gpg::DataSource::CACHE_OR_NETWORK, [] (gpg::PlayerManager::FetchListResponse response) 
        __android_log_print(ANDROID_LOG_INFO, "MyGame!", "Got invitable friends response: %d", response.status);
        __android_log_print(ANDROID_LOG_INFO, "MyGame!", "Got invitable players info! Num players: %d", response.data.size());
        if (gpg::IsSuccess(response.status)) 
            //PROCESS PLAYERS
            GameCenterSession::getInstance()->onPlayersInfoReceived(kRequestFriendsInfo, response.data);
        
    );

    //GET CONNECTED FRIENDS
    game_services_->Players().FetchConnected(gpg::DataSource::CACHE_OR_NETWORK, [] (gpg::PlayerManager::FetchListResponse response) 
        __android_log_print(ANDROID_LOG_INFO, "MyGame!", "Got connected friends response: %d", response.status);
        __android_log_print(ANDROID_LOG_INFO, "MyGame!", "Got connected players info! Num players: %d", response.data.size());
    );


void GameCenterSession::onPlayersInfoReceived(const int requestId, std::vector<gpg::Player> playersInfo) 

    __android_log_print(ANDROID_LOG_INFO, "MyGame!", "onPlayersInfoReceived num players: %d", playersInfo.size());
    const gpg::ScorePage::ScorePageToken distanceToken = game_services_->Leaderboards().ScorePageToken(
        kLeaderboardBestDistance,
        gpg::LeaderboardStart::TOP_SCORES,
        gpg::LeaderboardTimeSpan::ALL_TIME,
        gpg::LeaderboardCollection::SOCIAL
    );

    //FETCH ALL TIME SOCIAL SCORES FOR DISTANCE
    game_services_->Leaderboards().FetchScorePage(gpg::DataSource::CACHE_OR_NETWORK, distanceToken, 1000, [] (gpg::LeaderboardManager::FetchScorePageResponse response) 

        if (gpg::IsSuccess(response.status) && response.data.Valid()) 
            __android_log_print(ANDROID_LOG_INFO, "MyGame!", "Got social leaderboard! Num players: %d", response.data.Entries().size());
            gpg::ScorePage::Entry myEntry = gpg::ScorePage::Entry();

            //search through and find my score!
            for (auto score : response.data.Entries()) 
                __android_log_print(ANDROID_LOG_INFO, "MyGame!", "%s got distance %d", score.PlayerId().c_str(), score.Score().Value());


                if (score.PlayerId().compare(GameCenterSession::getInstance()->myPlayer.Id()) == 0) 
                    __android_log_print(ANDROID_LOG_INFO, "MyGame!", "Distance - Yup that's me");
                    myEntry = score;
                 else 
                    __android_log_print(ANDROID_LOG_INFO, "MyGame!", "Distance - It's another player!");
                
            

            GameCenterSession::getInstance()->onScoresReceived(kRequestFriendsDistancesInfo, myEntry, response.data.Entries());
        
    );


    const gpg::ScorePage::ScorePageToken scoreToken = game_services_->Leaderboards().ScorePageToken(
        kLeaderboardBestScore,
        gpg::LeaderboardStart::TOP_SCORES,
        gpg::LeaderboardTimeSpan::ALL_TIME,
        gpg::LeaderboardCollection::SOCIAL
    );

    game_services_->Leaderboards().FetchScorePage(gpg::DataSource::CACHE_OR_NETWORK, scoreToken, 1000, [] (gpg::LeaderboardManager::FetchScorePageResponse response) 
        if (gpg::IsSuccess(response.status) && response.data.Valid()) 

            gpg::ScorePage::Entry myEntry = gpg::ScorePage::Entry();

            //search through and find my score!
            for (auto score : response.data.Entries()) 
                __android_log_print(ANDROID_LOG_INFO, "MyGame!", "%s got score %d", score.PlayerId().c_str(), score.Score().Value());


                if (score.PlayerId().compare(GameCenterSession::getInstance()->myPlayer.Id()) == 0) 
                    __android_log_print(ANDROID_LOG_INFO, "MyGame!", "Score - Yup that's me");
                    myEntry = score;
                 else 
                    __android_log_print(ANDROID_LOG_INFO, "MyGame!", "Score - It's another player!");
                
            

            GameCenterSession::getInstance()->onScoresReceived(kRequestFriendsScoresInfo, myEntry, response.data.Entries());
        
    );

    if (requestId == kRequestFriendsInfo)
    
        friends = playersInfo;
    

查看代码,您会看到我首先获取好友列表并检查该列表中有多少玩家。我尝试访问邀请的朋友(根据文档判断,这是我想要的)和已连接的朋友(只是作为测试)。我继续抓取两个不同的历史社交排行榜——距离和得分,然后检查结果中有多少玩家。这是我的日志输出。

//设备1

I/MyGame!( 1510): Google Play Games authenticated successfully!
I/MyGame!( 1510): Fetching friends!
I/MyGame!( 1510): Got invitable friends response: 1
I/MyGame!( 1510): Got invitable players info! Num players: 0
I/MyGame!( 1510): onPlayersInfoReceived num players: 0
I/MyGame!( 1510): Got connected friends response: 1
I/MyGame!( 1510): Got connected players info! Num players: 0
I/MyGame!( 1510): Got social leaderboard! Num players: 1
I/MyGame!( 1510): g1703186947466536XXXX got distance 310
I/MyGame!( 1510): Distance - Yup that's me
I/MyGame!( 1510): g1703186947466536XXXX got score 2510
I/MyGame!( 1510): Score - Yup that's me

//设备2

01-23 17:11:27.227 17187 17234 I MyGame!: Google Play Games authenticated successfully!
01-23 17:11:27.250 17187 17234 I MyGame!: Fetching friends!
01-23 17:11:27.451 17187 17234 I MyGame!: Got invitable friends response: 1
01-23 17:11:27.451 17187 17234 I MyGame!: Got invitable players info! Num players: 0
01-23 17:11:27.451 17187 17234 I MyGame!: onPlayersInfoReceived num players: 0
01-23 17:11:27.581 17187 17234 I MyGame!: Got connected friends response: 1
01-23 17:11:27.581 17187 17234 I MyGame!: Got connected players info! Num players: 0
01-23 17:11:27.973 17187 17234 I MyGame!: Got social leaderboard! Num players: 1
01-23 17:11:27.973 17187 17234 I MyGame!: g0152008166550356XXXX got distance 712
01-23 17:11:27.973 17187 17234 I MyGame!: Distance - Yup that's me
01-23 17:11:28.444 17187 17234 I MyGame!: g0152008166550356XXXX got score 2142
01-23 17:11:28.444 17187 17234 I MyGame!: Score - Yup that's me

如您所见,我的回调都返回成功的结果代码。不幸的是,这两个设备都没有返回任何朋友,并且社交排行榜回调只包含设备上的玩家,而不是他们朋友的分数。

我尽我所能地遵循了文档,并且我已经查看了代码,但没有发现任何问题,但如果这只是一个语义问题,我很乐意听到我的错误。我在这里做错了什么,还是 C++ API 本身有问题?

提前致谢。

【问题讨论】:

【参考方案1】:

API 的行为符合预期。来自:https://android-developers.googleblog.com/2016/12/games-authentication-adopting-google.html

Google+ 不再集成 Announced last year,在此过渡期间,游戏与 Google+ 分离。结果,用于通过圈子连接玩家的公共 API 停止工作,但多人邀请和社交排行榜的标准 UI 继续工作。从 2017 年 2 月开始,标准 UI 也将不再显示社交图结果,因为 Google+ 数据变得无法访问。这将影响 Android 上的多人游戏、社交排行榜和礼物 API。效果是这些 API 将成功返回,但玩家列表为空。

【讨论】:

感谢您的回复。我看到您是这篇博文的作者,所以我恳请您在相关文档中添加一条说明,说明此功能不再有效。我们花了很多时间来实现和调试它,在看到链接的博客文章之前,我没有看到任何提到这些功能不再起作用的消息。再次感谢。

以上是关于Google Play Games C++ API - 无法以编程方式访问好友的主要内容,如果未能解决你的问题,请参考以下文章

限制 Google Play Games API 调用

如何从 Android 中的 Google Play Games API 中提取多个排行榜?

在哪里提交有关 Google Play Games API 的错误报告?

从 Google Play Games API 获取用户 ID?

Google Play Games Services Leaderboard API,看不到其他玩家的分数

100% “google.play.games.games.v1.Applications.RecordApplicationActivity” 一个月以来