Unity-Firebase实时数据库-在页首横幅中排名 [英] Unity - Firebase realtime database - Get my rank in leaderboard

查看:319
本文介绍了Unity-Firebase实时数据库-在页首横幅中排名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有排行榜的迷你游戏,使用的是实时Firebase数据库.

I have a mini-game with a leaderboard using firebase database realtime.

从firebase获得用户得分列表之后,我想获得不在列表中的当前用户的分数.

After I got list of user-score from firebase, I would like to get the score of the current user who was out of the list.

很容易获得当前用户的得分,但是如何知道列表中的排名为OrderByChild("score").

It's easy to get the score of the current user but how to know the rank in list which was OrderByChild("score").

这是获得排行榜的代码.

This is the code to get the leaderboard.

List<UserScore> leaderBoard = new List<UserScore>();
FirebaseDatabase.DefaultInstance
                .GetReference("user-scores")
                .OrderByChild("score")
                .LimitToLast(10)
                .GetValueAsync()
                .ContinueWith(task =>
        {
            if (task.IsFaulted)
            {
                Debug.Log("Fail To Load");
            }
            else if (task.IsCompleted)
            {
                DataSnapshot snapshot = task.Result;
                foreach (DataSnapshot h in snapshot.Children)
                {
                    UserScore userScore = new UserScore(h.Child("uid").Value.ToString(), h.Child("name").Value.ToString(), h.Child("photo").Value.ToString(),int.Parse(h.Child("score").Value.ToString()));
                    leaderBoard.Add(userScore);
                }
            }
        });

推荐答案

如何知道我的排名

how to know my rank

这取决于谁是我的".但是,请说您在变量uid中具有当前用户的UID.然后,您可以通过以下方法确定他们在前10名中的排名:

That depends a bit on who "my" is. But say you have the UID of the current user in a variable uid. You can then determine their rank in this top 10 with:

int rank = 0;
foreach (DataSnapshot h in snapshot.Children)
{
    rank = rank + 1;
    UserScore userScore = new UserScore(
      h.Child("uid").Value.ToString(), 
      h.Child("name").Value.ToString(), 
      h.Child("photo").Value.ToString(),
      int.Parse(h.Child("score").Value.ToString()));
    leaderBoard.Add(userScore);
    if (h.Child("uid").Value.ToString() == uid) {
      Debug.Log("I'm number "+rank+" in the rankings");
    }
}

这篇关于Unity-Firebase实时数据库-在页首横幅中排名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆