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(); } }
// ================================================================================================================= // === public interface ============================================================================================ public override void FetchAndUpdate(string inLocalPrimaryKey, MonoBehaviour inCorutineOwner, FetchFinished inFinishDelegate) { if (Mathf.Abs((float)(m_LastSyncTime - CloudDateTime.UtcNow).TotalMinutes) < SKIP_UPDATE_TIMEOUT) { return; // don't update mailbox from cloud } m_LastSyncTime = CloudDateTime.UtcNow; m_Rows = new List <Row>(); m_Rows.Add(new Row(TEXT_FETCHING)); inCorutineOwner.StartCoroutine(FetchFacebookFriendsLeaderBoard_Corutine(leaderBoardName, inCorutineOwner, inLocalPrimaryKey, maxRows, inFinishDelegate)); }
// ================================================================================================================= // === public interface ============================================================================================ public override void FetchAndUpdate(string inLocalUserName, MonoBehaviour inCorutineOwner, FetchFinished inFinishDelegate) { // Fetch score info from cloud. // ................................................................................... if (Mathf.Abs((float)(m_LastSyncTime - CloudDateTime.UtcNow).TotalMinutes) < SKIP_UPDATE_TIMEOUT) { return; // don't update mailbox from cloud } m_LastSyncTime = CloudDateTime.UtcNow; m_Rows = new List <Row>(); m_Rows.Add(new Row(TEXT_FETCHING)); inCorutineOwner.StartCoroutine(FetchFriendsLeaderBoard_Corutine(leaderBoardName, inLocalUserName, maxRows, inFinishDelegate)); // debug... //inCorutineOwner.StartCoroutine( FetchFriendsLeaderBoard_Corutine("Default", "xxx", 8, inFinishDelegate) ); }
// ================================================================================================================= // === 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(); } }
// ================================================================================================================= // === internal ==================================================================================================== IEnumerator FetchLeaderBoard_Corutine(string inLeaderBoardName, string inPrimaryKey, int inMaxRecords, FetchFinished inFinishDelegate) { // show first 3 users, then two before, local user, two after. In total 8 people... // ................................................................................... bool leaderboardError = false; int localUserRank = -1; { string[] names = new string[] { inPrimaryKey }; QueryLeaderBoardRank action = new QueryLeaderBoardRank(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 rank of user: "******" Name: " + names[i] + " Score: " + action.retRanks[i]); * }*/ localUserRank = action.retRanks[0]; //Debug.Log("Rank: " + localUserRank); } } List <Row> rows = new List <Row>(); if (leaderboardError != true) { QueryLeaderBoardScore action = new QueryLeaderBoardScore(CloudUser.instance.authenticatedUserID, inLeaderBoardName, 0); 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 score info from leaderboard: " + inLeaderBoardName); leaderboardError |= true; } else { for (int i = 0; i < action.retScores.Length && i < inMaxRecords; i++) { QueryLeaderBoardScore.ScoreInfo score = action.retScores[i]; //Debug.Log("ID: " + (i+1) + " Name: " + action.retScores[i].userName + " Score: " + action.retScores[i].score); rows.Add(new Row(i + 1, score.userName, score.displayName, score.score, score.experience, score.userName == inPrimaryKey)); } } } if (leaderboardError != true && localUserRank > 5) { //static int cfg_startOffset = 4; // wa want users which are near, so we set // start index little smaller... int startRankIndex = localUserRank - cfg_startOffset; // request for leaderbord records... QueryLeaderBoardScore action = new QueryLeaderBoardScore(CloudUser.instance.authenticatedUserID, inLeaderBoardName, startRankIndex); GameCloudManager.AddAction(action); // wait for finish... while (action.isDone == false) { yield return(new WaitForSeconds(0.2f)); } // processing result... if (action.isFailed == true) { Debug.LogWarning("Can't retrive score info from leaderboard: " + inLeaderBoardName); // leaderboardError |= true; ALEX:: we ignore this error. At least first users will be shown... } else { int startScoreIndex = Mathf.Clamp(((action.retScores.Length - 1) - cfg_startOffset), 0, 2); for (int rowIndex = 3, userIndex = startScoreIndex; rowIndex < inMaxRecords && userIndex < action.retScores.Length; rowIndex++, userIndex++) { //Debug.Log("ID: " + (i) + " Name: " + action.retScores[i].userName + " Score: " + action.retScores[i].score); QueryLeaderBoardScore.ScoreInfo scoreInfo = action.retScores[userIndex]; int userOrder = startRankIndex + userIndex + 1; // Create leaderbord row with user score info Row userRow = new Row(userOrder, scoreInfo.userName, scoreInfo.displayName, scoreInfo.score, scoreInfo.experience, scoreInfo.userName == inPrimaryKey); // set user row into leaderboard rows... if (rowIndex < rows.Count) { rows[rowIndex] = userRow; } else { rows.Add(userRow); } } } } if (leaderboardError == true) { m_Rows = new List <Row>(); m_Rows.Add(new Row(TEXT_ERROR)); } else { m_Rows = rows; } if (inFinishDelegate != null) { inFinishDelegate(); } }