示例#1
0
    IEnumerator FetchFacebookFriendsLeaderBoard_Corutine(string inLeaderBoardName,
                                                         MonoBehaviour inCorutineOwner,
                                                         string inLocalPrimaryKey,
                                                         int inMaxRecords,
                                                         FetchFinished inFinishDelegate)
    {
        yield return(inCorutineOwner.StartCoroutine(GameCloudManager.facebookFriendList.WaitForLoading()));

        bool leaderboardError = false;

        List <Row> rows = new List <Row>();

        {
            List <string> names = new List <string>();
            names.Add(inLocalPrimaryKey);

            if (GameCloudManager.facebookFriendList.Friends != null)
            {
                foreach (FacebookFriendList.FacebookFriend friend in GameCloudManager.facebookFriendList.Friends)
                {
                    foreach (string primaryKey in friend.PrimaryKeys)
                    {
                        if (inLocalPrimaryKey != primaryKey)
                        {
                            names.Add(primaryKey);
                        }
                    }
                }
            }

            QueryLeaderBoardRankAndScores action = new QueryLeaderBoardRankAndScores(CloudUser.instance.authenticatedUserID,
                                                                                     inLeaderBoardName,
                                                                                     names.ToArray());
            GameCloudManager.AddAction(action);

            while (action.isDone == false)
            {
                yield return(new WaitForSeconds(0.2f));
            }

            if (action.isFailed == true)
            {
                Debug.LogWarning("Can't retrive friends record: " + inLocalPrimaryKey);
                leaderboardError |= true;
            }
            else
            {
                for (int i = 0; i < action.retRecords.Length; i++)
                {
                    QueryLeaderBoardRankAndScores.UserRecord record = action.retRecords[i];

                    int userOrder = record.rank < 0 ? record.rank : record.rank + 1;
                    rows.Add(new Row(userOrder, record.userName, record.displayName, record.score, record.experience, record.userName == inLocalPrimaryKey));
                }
            }
        }

        if (leaderboardError == true)
        {
            m_Rows = new List <Row>();
            m_Rows.Add(new Row(TEXT_ERROR));
        }
        else
        {
            // sort by rank...
            rows.Sort((x, y) =>
            {
                if (x == y)
                {
                    return(0);
                }
                else if (x.Order < 0 && y.Order < 0)
                {
                    if (x.PrimaryKey == inLocalPrimaryKey)
                    {
                        return(-1);
                    }
                    if (y.PrimaryKey == inLocalPrimaryKey)
                    {
                        return(1);
                    }

                    return(1);
                }
                else
                {
                    return((x.Order < 0) ? 1 : (y.Order < 0) ? -1 : x.Order.CompareTo(y.Order));
                }
            });

            int localUserIndex = rows.FindIndex(x => x.PrimaryKey == inLocalPrimaryKey);

            if (rows.Count <= maxRows)
            {
                m_Rows = rows;
            }
            else
            {
                if (localUserIndex < maxRows)
                {
                    m_Rows = rows.GetRange(0, maxRows);
                }
                else                 //local user didn't get into the list
                {
                    m_Rows = rows.GetRange(0, maxRows - 1);
                    m_Rows.Add(rows[localUserIndex]);
                }
            }
        }

        if (inFinishDelegate != null)
        {
            inFinishDelegate();
        }
    }
示例#2
0
    // =================================================================================================================
    // === internal ====================================================================================================
    IEnumerator FetchFriendsLeaderBoard_Corutine(string inLeaderBoardName,
                                                 string inLocalPrimaryKey,
                                                 int inMaxRecords,
                                                 FetchFinished inFinishDelegate)
    {
        // show first 3 users, then two before, local user, two after. In total 8 people...
        // ...................................................................................

        bool leaderboardError = false;

        List <Row> rows = new List <Row>();

        {
            // Get friends from friend list...
            string[] names = new string[GameCloudManager.friendList.friends.Count + 1];
            names[0] = inLocalPrimaryKey;
            for (int index = 0; index < GameCloudManager.friendList.friends.Count; index++)
            {
                names[index + 1] = GameCloudManager.friendList.friends[index].PrimaryKey;
            }

            // debug...
            //string[] names = new string[] {inLocalUserName, "User_2141855", "User_1139861", "User_780626", "User_1232916", "Vykuk", "alexdebug", "janko-hrasko", "xxx", "yyy", "01", "02"};
            //string[] names = new string[] {inLocalUserName, "User_2141855", "User_1139861", "User_780626", "User_1232916", "Vykuk", "alex", "janko-hrasko", "xxx", "yyy", "01", "02"};
            //names = names.Distinct().ToArray();

            QueryLeaderBoardRankAndScores action = new QueryLeaderBoardRankAndScores(CloudUser.instance.authenticatedUserID, inLeaderBoardName, names);
            GameCloudManager.AddAction(action);

            // wait for authentication...
            while (action.isDone == false)
            {
                yield return(new WaitForSeconds(0.2f));
            }

            if (action.isFailed == true)
            {
                Debug.LogWarning("Can't retrive friends record: " + inLocalPrimaryKey);
                leaderboardError |= true;
            }
            else
            {
                for (int i = 0; i < action.retRecords.Length; i++)
                {
                    QueryLeaderBoardRankAndScores.UserRecord record = action.retRecords[i];

                    int userOrder = record.rank < 0 ? record.rank : record.rank + 1;
                    //Debug.Log(" Name: " + names[i] + " Score: " + action.retRanks[i]);
                    rows.Add(new Row(userOrder, record.userName, record.displayName, record.score, record.experience, record.userName == inLocalPrimaryKey));
                }
            }
        }

        if (leaderboardError == true)
        {
            m_Rows = new List <Row>();
            m_Rows.Add(new Row(TEXT_ERROR));
        }
        else
        {
            // sort by rank...
            rows.Sort((x, y) =>
            {
                if (x == y)
                {
                    return(0);
                }
                else if (x.Order < 0 && y.Order < 0)
                {
                    if (x.PrimaryKey == inLocalPrimaryKey)
                    {
                        return(-1);
                    }
                    if (y.PrimaryKey == inLocalPrimaryKey)
                    {
                        return(1);
                    }

                    return(1);
                }
                else
                {
                    return((x.Order < 0) ? 1 : (y.Order < 0) ? -1 : x.Order.CompareTo(y.Order));
                }
            });

            int localUserIndex = rows.FindIndex(x => x.PrimaryKey == inLocalPrimaryKey);
            if (localUserIndex >= 5)
            {
                // check if there are at least two other friend behind me...
                int rest          = Mathf.Clamp((rows.Count - 1) - localUserIndex, 0, 2);
                int userViewIndex = localUserIndex - (4 - rest);

                m_Rows = rows.GetRange(0, 3);
                m_Rows.AddRange(rows.GetRange(userViewIndex, 5));
            }
            else
            {
                // nothing...
                m_Rows = rows;
            }
        }

        if (inFinishDelegate != null)
        {
            inFinishDelegate();
        }
    }