0

我正在尝试根据数据库中的分数获取前 5 名球员的数据。问题是,我不能将它们存储在一个数组中,我没有收集所有信息......当我检查这些步骤时,我发现输出顺序不正确。原因可能是每个请求的服务器响应时间不同,但我该怎么办?

另外,为什么它不按分值排序?我做错了什么?

数据库结构:UID/[分数、昵称、胜利、失败等...]

提前致谢。

public void ReadTopScore(string[,] Top5, int index)
    {
        string uid = Top5[index, 0];
        FirebaseDatabase.DefaultInstance.GetReference("Players").Child(uid).GetValueAsync().ContinueWith(task =>
        {
            if (task.IsFaulted)
            {
                Debug.LogError("DataManager: read ReadTopScore is faulted with error: " + task.Exception.ToString());
                return;
            }

            if (task.IsCompleted)
            {
                if (task.Result == null)
                {
                    Debug.Log("DataManager: ReadTopScore ==> task result is null.");
                }

                DataSnapshot snapshot = task.Result;
                Dictionary<string, System.Object> attributes = (Dictionary<string, System.Object>)snapshot.Value;
                StartCoroutine(WaitForSeconds());
                if (snapshot.Exists)
                {
                    Top5[index, 1] = attributes["nick"].ToString();
                    Top5[index, 2] = attributes["score"].ToString();
                    Top5[index, 3] = attributes["level"].ToString();
                    Top5[index, 4] = attributes["victory"].ToString();
                    Top5[index, 5] = attributes["defeat"].ToString();
                    Debug.Log("For Player" + index + " Nick: " +  Top5[index, 1]);
                    Debug.Log("For Player" + index + " Score: " + Top5[index, 2]);
                    Debug.Log("For Player" + index + " Level: " + Top5[index, 3]);
                    Debug.Log("For Player" + index + " Victory: " + Top5[index, 4]);
                    Debug.Log("For Player" + index + " Defeat: " + Top5[index, 5]);
                }
            }
        });        
    }

    public void UpdateLeaderBoard()
    {
        FirebaseDatabase.DefaultInstance.GetReference("Players").OrderByChild("score").LimitToLast(5).GetValueAsync().ContinueWith(task =>
        {
            if (task.IsFaulted)
            {
                Debug.LogError("LeaderBoardManager: read database is faulted with error: " + task.Exception.ToString());
                return;
            }

            if (task.IsCompleted)
            {
                if (task.Result == null)
                {
                    Debug.Log("LeaderBoardManager: ReadData ==> task result is null.");
                }

                DataSnapshot snapshot = task.Result;
                Dictionary<string, System.Object> attributes = (Dictionary<string, System.Object>)snapshot.Value;
                string[,] Top5 = new string[5, 6];

                if (snapshot.Exists)
                {
                    int i = 0;
                    foreach (KeyValuePair<string, object> kvp in attributes)
                    {
                        Top5[i, 0] = kvp.Key;
                        i++;
                    }
                }

                for (int i = 0; i < 5; i++)
                {
                    ReadTopScore(Top5, i);
                }

                for (int i = 0; i < 5; i++)
                {
                    Debug.Log("Data0 " + Top5[i, 0]);
                }
                for (int i = 0; i < 5; i++)
                {
                    Debug.Log("Data1 " + Top5[i, 1]);
                }
                for (int i = 0; i < 5; i++)
                {
                    Debug.Log("Data2 " + Top5[i, 2]);
                }
            }
        });        
    }

: Data Set 1
: Data0 8xbehHaKF5gdyVgixgtIeFHA3cS2
: Data0 9NCXibtdXkNkS1WrIguLm5Q44p92
: Data0 O27iHeGPrJMt5rFKvfyV1uLt56J3
: Data0 vpOrKmTi3lcAHr6S3VjyaVlCr0T2
: Data0 ymQXyG9hRpSvmK1bgru6N0FwlvD3
: Data1 
: Data1 
: Data2 
: Data2 
: For Player1 Nick: Player99
: For Player3 Nick: Player3
: For Player0 Nick: Player1
: For Player1 Score: 1780
: For Player3 Score: 1630
: For Player0 Score: 1030
: For Player1 Level: 3
: For Player3 Level: 4
: For Player0 Level: 1
: For Player1 Victory: 5
: For Player3 Victory: 7
: For Player0 Victory: 0
: For Player1 Defeat: 1
: For Player3 Defeat: 9
: For Player0 Defeat: 1
: For Player2 Nick: StormEmu
: For Player4 Nick: Player4
: For Player2 Score: 4930
: For Player4 Score: 1310
: For Player2 Level: 8
: For Player4 Level: 2
: For Player2 Victory: 26
: For Player4 Victory: 2
: For Player2 Defeat: 25
: For Player4 Defeat: 3
4

1 回答 1

0

经过我的尝试和搜索,我找到了答案。需要使用子操作进行管理以进行正确排序。

public void UpdateLeaderBoard()
{
        FirebaseDatabase.DefaultInstance.GetReference("Players").OrderByChild("score").LimitToLast(5).GetValueAsync().ContinueWith(task =>
    {
        if (task.IsFaulted)
        {
            Debug.LogError("LeaderBoardManager: read database is faulted with error: " + task.Exception.ToString());
            return;
        }

        if (task.IsCompleted)
        {
            if (task.Result == null)
            {
                Debug.Log("LeaderBoardManager: ReadData ==> task result is null.");
            }

            DataSnapshot snapshot = task.Result;
            Dictionary<string, System.Object> attributes = (Dictionary<string, System.Object>)snapshot.Value;                

            if (snapshot.Exists)
            {
                int i = 0;
                foreach (DataSnapshot childSnapshot in snapshot.Children)
                {
                    Top5[i, 0] = childSnapshot.Key;
                    Dictionary<string, System.Object> attributes2 = (Dictionary<string, System.Object>)childSnapshot.Value;
                    Top5[i, 1] = attributes2["nick"].ToString();
                    Top5[i, 2] = attributes2["score"].ToString();
                    Top5[i, 3] = attributes2["level"].ToString();
                    Top5[i, 4] = attributes2["victory"].ToString();
                    Top5[i, 5] = attributes2["defeat"].ToString();

                    i++;
                }
            }
        }
    });        
}
于 2019-08-17T23:50:49.427 回答