playgameservices / play-games-plugin-for-unity

Google Play Games plugin for Unity
Other
3.43k stars 954 forks source link

rank for PlayGamesPlatform.Instance.LoadScores + LeaderboardTimeSpan.Weekly returns all-time #3090

Open frazerbw opened 2 years ago

frazerbw commented 2 years ago

This seems broken. I'm using the following code.

I want to get the players rank within the weekly leaderboard, but everything I try only gives me the all-time rank. Any ideas?

PlayGamesPlatform.Instance.LoadScores( WORLDWIDE_EXP_RANK_WEEKLY, LeaderboardStart.TopScores, 1, LeaderboardCollection.Public, LeaderboardTimeSpan.Weekly, (data) => { expWeeklyRank = data.PlayerScore.rank; });

frazerbw commented 2 years ago

The only idea I have is I need to loop down through every single player in the Scores and count until I find the player I am looking for. But if I had +1 mil users on the highscores, that would be very very inefficient.

frolky commented 2 years ago

Solution: https://github.com/playgameservices/play-games-plugin-for-unity/issues/2829#issuecomment-573395075

frazerbw commented 2 years ago

@frolky thanks, so your solution is to change the playgameservices code? Did that work, it sounds like you ended up with issues call ReportScore after making that change?

frolky commented 2 years ago

@frazerbw yes, you need to make changes to the plugin code. Everything works well, the problem with ReportScore was different. New code:

            using (var leaderboard = leaderboardScoresJava.Call<AndroidJavaObject>("getLeaderboard"))
            {
                using (var variants = leaderboard.Call<AndroidJavaObject>("getVariants"))
                {
                    var arrayListIterator = variants.Call<AndroidJavaObject>("iterator");

                    while (arrayListIterator.Call<bool>("hasNext"))
                    {
                        using (var variant = arrayListIterator.Call<AndroidJavaObject>("next"))
                        {
                            if (variant.Call<int>("getCollection") == (int)collection - 1 && variant.Call<int>("getTimeSpan") == (int)timespan - 1)
                            {
                                leaderboardScoreData.Title = leaderboard.Call<string>("getDisplayName");

                                if (variant.Call<bool>("hasPlayerInfo"))
                                {
                                    var date = AndroidJavaConverter.ToDateTime(0);
                                    var rank = (ulong)variant.Call<long>("getPlayerRank");
                                    var score = (ulong)variant.Call<long>("getRawPlayerScore");
                                    var metadata = variant.Call<string>("getPlayerScoreTag");
                                    leaderboardScoreData.PlayerScore = new PlayGamesScore(date, leaderboardId,
                                        rank, mUser.id, score, metadata);
                                }

                                leaderboardScoreData.ApproximateCount = (ulong)variant.Call<long>("getNumScores");
                            }
                        }
                    }
                }
            }
frazerbw commented 2 years ago

@frolky you're a legend! Thanks, will try this and report back if it works :)

frazerbw commented 2 years ago

@frolky this worked in my test build! People who find this thread in the future can assume it fully worked unless I am back again to say otherwise :)

EDIT: small comment to any Google maintainers, why not use this code and make the time-span feature work?