Skip to content
This repository has been archived by the owner on Dec 15, 2017. It is now read-only.

Commit

Permalink
Using friends and invitable_friends to populate friend list.
Browse files Browse the repository at this point in the history
  • Loading branch information
andyborrell committed Jul 31, 2014
1 parent 4bb2253 commit b2b085e
Show file tree
Hide file tree
Showing 10 changed files with 114 additions and 45 deletions.
23 changes: 15 additions & 8 deletions friendsmash_advanced/Assets/Scripts/MainMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -173,14 +173,15 @@ void LoginCallback(FBResult result)
OnLoggedIn();
}
}
string meQueryString = "/v2.0/me?fields=id,first_name,friends.limit(100).fields(first_name,id,picture.width(128).height(128)),invitable_friends.limit(100).fields(first_name,id,picture.width(128).height(128))";

void OnLoggedIn()
{
Util.Log("Logged in. ID: " + FB.UserId);

// Reqest player info and profile picture
FB.API("/me?fields=id,first_name,friends.limit(100).fields(first_name,id)", Facebook.HttpMethod.GET, APICallback);
LoadPicture(Util.GetPictureURL("me", 128, 128),MyPictureCallback);
FB.API(meQueryString, Facebook.HttpMethod.GET, APICallback);
LoadPictureAPI(Util.GetPictureURL("me", 128, 128),MyPictureCallback);

// Load high scores
QueryScores();
Expand All @@ -198,7 +199,7 @@ void APICallback(FBResult result)
{
Util.LogError(result.Error);
// Let's just try again
FB.API("/me?fields=id,first_name,friends.limit(100).fields(first_name,id)", Facebook.HttpMethod.GET, APICallback);
FB.API(meQueryString, Facebook.HttpMethod.GET, APICallback);
return;
}

Expand All @@ -215,7 +216,7 @@ void MyPictureCallback(Texture texture)
if (texture == null)
{
// Let's just try again
LoadPicture(Util.GetPictureURL("me", 128, 128),MyPictureCallback);
LoadPictureAPI(Util.GetPictureURL("me", 128, 128),MyPictureCallback);

return;
}
Expand Down Expand Up @@ -269,7 +270,7 @@ void ScoresCallback(FBResult result)
if (!friendImages.ContainsKey(userId))
{
// We don't have this players image yet, request it now
LoadPicture(Util.GetPictureURL(userId, 128, 128),pictureTexture =>
LoadPictureAPI(Util.GetPictureURL(userId, 128, 128),pictureTexture =>
{
if (pictureTexture != null)
{
Expand Down Expand Up @@ -418,7 +419,7 @@ void OnGUI()
GUI.Label( (new Rect(179 , 11, 287, 160)), "Login to Facebook", MenuSkin.GetStyle("text_only"));
if (GUI.Button(LoginButtonRect, "", MenuSkin.GetStyle("button_login")))
{
FB.Login("email,publish_actions", LoginCallback);
FB.Login("public_profile,user_friends,email,publish_actions", LoginCallback);
}
}

Expand Down Expand Up @@ -595,7 +596,7 @@ private void onPlayClicked()
GameStateManager.FriendName = friend["first_name"];
GameStateManager.FriendID = friend["id"];
GameStateManager.CelebFriend = -1;
LoadPicture(Util.GetPictureURL((string)friend["id"], 128, 128),FriendPictureCallback);
LoadPictureURL(friend["image_url"],FriendPictureCallback);
}
else
{
Expand Down Expand Up @@ -693,7 +694,7 @@ IEnumerator LoadPictureEnumerator(string url, LoadPictureCallback callback)
yield return www;
callback(www.texture);
}
void LoadPicture (string url, LoadPictureCallback callback)
void LoadPictureAPI (string url, LoadPictureCallback callback)
{
FB.API(url,Facebook.HttpMethod.GET,result =>
{
Expand All @@ -708,4 +709,10 @@ void LoadPicture (string url, LoadPictureCallback callback)
StartCoroutine(LoadPictureEnumerator(imageUrl,callback));
});
}
void LoadPictureURL (string url, LoadPictureCallback callback)
{
StartCoroutine(LoadPictureEnumerator(url,callback));

}

}
11 changes: 9 additions & 2 deletions friendsmash_advanced/Assets/Scripts/Util.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@ public static string GetPictureURL(string facebookID, int? width = null, int? he

public static Dictionary<string, string> RandomFriend(List<object> friends)
{
var fd = ((Dictionary<string, object>)(friends[Random.Range(0, friends.Count - 1)]));
var fd = ((Dictionary<string, object>)(friends[Random.Range(0, friends.Count)]));
var friend = new Dictionary<string, string>();
friend["id"] = (string)fd["id"];
friend["first_name"] = (string)fd["first_name"];
var pictureDict = ((Dictionary<string, object>)(fd["picture"]));
var pictureDataDict = ((Dictionary<string, object>)(pictureDict["data"]));
friend["image_url"] = (string)pictureDataDict["url"];
return friend;
}

Expand Down Expand Up @@ -57,10 +60,14 @@ public static List<object> DeserializeJSONFriends(string response)
var responseObject = Json.Deserialize(response) as Dictionary<string, object>;
object friendsH;
var friends = new List<object>();
if (responseObject.TryGetValue("friends", out friendsH))
if (responseObject.TryGetValue("invitable_friends", out friendsH))
{
friends = (List<object>)(((Dictionary<string, object>)friendsH)["data"]);
}
if (responseObject.TryGetValue("friends", out friendsH))
{
friends.AddRange((List<object>)(((Dictionary<string, object>)friendsH)["data"]));
}
return friends;
}

Expand Down
23 changes: 15 additions & 8 deletions friendsmash_complete/Assets/Scripts/MainMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,14 +160,15 @@ void LoginCallback(FBResult result)
OnLoggedIn();
}
}
string meQueryString = "/v2.0/me?fields=id,first_name,friends.limit(100).fields(first_name,id,picture.width(128).height(128)),invitable_friends.limit(100).fields(first_name,id,picture.width(128).height(128))";

void OnLoggedIn()
{
Util.Log("Logged in. ID: " + FB.UserId);

// Reqest player info and profile picture
FB.API("/me?fields=id,first_name,friends.limit(100).fields(first_name,id)", Facebook.HttpMethod.GET, APICallback);
LoadPicture(Util.GetPictureURL("me", 128, 128),MyPictureCallback);
FB.API(meQueryString, Facebook.HttpMethod.GET, APICallback);
LoadPictureAPI(Util.GetPictureURL("me", 128, 128),MyPictureCallback);

}

Expand All @@ -183,7 +184,7 @@ void APICallback(FBResult result)
{
Util.LogError(result.Error);
// Let's just try again
FB.API("/me?fields=id,first_name,friends.limit(100).fields(first_name,id)", Facebook.HttpMethod.GET, APICallback);
FB.API(meQueryString, Facebook.HttpMethod.GET, APICallback);
return;
}

Expand All @@ -199,7 +200,7 @@ void MyPictureCallback(Texture texture)
if (texture == null)
{
// Let's just try again
LoadPicture(Util.GetPictureURL("me", 128, 128),MyPictureCallback);
LoadPictureAPI(Util.GetPictureURL("me", 128, 128),MyPictureCallback);

return;
}
Expand Down Expand Up @@ -251,7 +252,7 @@ void ScoresCallback(FBResult result)
if (!friendImages.ContainsKey(userId))
{
// We don't have this players image yet, request it now
LoadPicture(Util.GetPictureURL(userId, 128, 128),pictureTexture =>
LoadPictureAPI(Util.GetPictureURL(userId, 128, 128),pictureTexture =>
{
if (pictureTexture != null)
{
Expand Down Expand Up @@ -372,7 +373,7 @@ void OnGUI()
GUI.Label( (new Rect(179 , 11, 287, 160)), "Login to Facebook", MenuSkin.GetStyle("text_only"));
if (GUI.Button(LoginButtonRect, "", MenuSkin.GetStyle("button_login")))
{
FB.Login("email,publish_actions", LoginCallback);
FB.Login("public_profile,user_friends,email,publish_actions", LoginCallback);
}
}

Expand Down Expand Up @@ -544,7 +545,7 @@ private void onPlayClicked()
GameStateManager.FriendName = friend["first_name"];
GameStateManager.FriendID = friend["id"];
GameStateManager.CelebFriend = -1;
LoadPicture(Util.GetPictureURL((string)friend["id"], 128, 128),FriendPictureCallback);
LoadPictureURL(friend["image_url"],FriendPictureCallback);
}
else
{
Expand Down Expand Up @@ -642,7 +643,7 @@ IEnumerator LoadPictureEnumerator(string url, LoadPictureCallback callback)
yield return www;
callback(www.texture);
}
void LoadPicture (string url, LoadPictureCallback callback)
void LoadPictureAPI (string url, LoadPictureCallback callback)
{
FB.API(url,Facebook.HttpMethod.GET,result =>
{
Expand All @@ -657,4 +658,10 @@ void LoadPicture (string url, LoadPictureCallback callback)
StartCoroutine(LoadPictureEnumerator(imageUrl,callback));
});
}
void LoadPictureURL (string url, LoadPictureCallback callback)
{
StartCoroutine(LoadPictureEnumerator(url,callback));

}

}
11 changes: 9 additions & 2 deletions friendsmash_complete/Assets/Scripts/Util.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@ public static string GetPictureURL(string facebookID, int? width = null, int? he

public static Dictionary<string, string> RandomFriend(List<object> friends)
{
var fd = ((Dictionary<string, object>)(friends[Random.Range(0, friends.Count - 1)]));
var fd = ((Dictionary<string, object>)(friends[Random.Range(0, friends.Count)]));
var friend = new Dictionary<string, string>();
friend["id"] = (string)fd["id"];
friend["first_name"] = (string)fd["first_name"];
var pictureDict = ((Dictionary<string, object>)(fd["picture"]));
var pictureDataDict = ((Dictionary<string, object>)(pictureDict["data"]));
friend["image_url"] = (string)pictureDataDict["url"];
return friend;
}

Expand Down Expand Up @@ -57,10 +60,14 @@ public static List<object> DeserializeJSONFriends(string response)
var responseObject = Json.Deserialize(response) as Dictionary<string, object>;
object friendsH;
var friends = new List<object>();
if (responseObject.TryGetValue("friends", out friendsH))
if (responseObject.TryGetValue("invitable_friends", out friendsH))
{
friends = (List<object>)(((Dictionary<string, object>)friendsH)["data"]);
}
if (responseObject.TryGetValue("friends", out friendsH))
{
friends.AddRange((List<object>)(((Dictionary<string, object>)friendsH)["data"]));
}
return friends;
}

Expand Down
23 changes: 15 additions & 8 deletions friendsmash_payments_complete/Assets/Scripts/MainMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -173,14 +173,15 @@ void LoginCallback(FBResult result)
OnLoggedIn();
}
}
string meQueryString = "/v2.0/me?fields=id,first_name,friends.limit(100).fields(first_name,id,picture.width(128).height(128)),invitable_friends.limit(100).fields(first_name,id,picture.width(128).height(128))";

void OnLoggedIn()
{
Util.Log("Logged in. ID: " + FB.UserId);

// Reqest player info and profile picture
FB.API("/me?fields=id,first_name,friends.limit(100).fields(first_name,id)", Facebook.HttpMethod.GET, APICallback);
LoadPicture(Util.GetPictureURL("me", 128, 128),MyPictureCallback);
FB.API(meQueryString, Facebook.HttpMethod.GET, APICallback);
LoadPictureAPI(Util.GetPictureURL("me", 128, 128),MyPictureCallback);

// Load high scores
QueryScores();
Expand All @@ -199,7 +200,7 @@ void APICallback(FBResult result)
{
Util.LogError(result.Error);
// Let's just try again
FB.API("/me?fields=id,first_name,friends.limit(100).fields(first_name,id)", Facebook.HttpMethod.GET, APICallback);
FB.API(meQueryString, Facebook.HttpMethod.GET, APICallback);
return;
}

Expand All @@ -216,7 +217,7 @@ void MyPictureCallback(Texture texture)
if (texture == null)
{
// Let's just try again
LoadPicture(Util.GetPictureURL("me", 128, 128),MyPictureCallback);
LoadPictureAPI(Util.GetPictureURL("me", 128, 128),MyPictureCallback);

return;
}
Expand Down Expand Up @@ -270,7 +271,7 @@ void ScoresCallback(FBResult result)
if (!friendImages.ContainsKey(userId))
{
// We don't have this players image yet, request it now
LoadPicture(Util.GetPictureURL(userId, 128, 128),pictureTexture =>
LoadPictureAPI(Util.GetPictureURL(userId, 128, 128),pictureTexture =>
{
if (pictureTexture != null)
{
Expand Down Expand Up @@ -419,7 +420,7 @@ void OnGUI()
GUI.Label( (new Rect(179 , 11, 287, 160)), "Login to Facebook", MenuSkin.GetStyle("text_only"));
if (GUI.Button(LoginButtonRect, "", MenuSkin.GetStyle("button_login")))
{
FB.Login("email,publish_actions", LoginCallback);
FB.Login("public_profile,user_friends,email,publish_actions", LoginCallback);
}
}

Expand Down Expand Up @@ -601,7 +602,7 @@ private void onPlayClicked()
GameStateManager.FriendName = friend["first_name"];
GameStateManager.FriendID = friend["id"];
GameStateManager.CelebFriend = -1;
LoadPicture(Util.GetPictureURL((string)friend["id"], 128, 128),FriendPictureCallback);
LoadPictureURL(friend["image_url"],FriendPictureCallback);
}
else
{
Expand Down Expand Up @@ -699,7 +700,7 @@ IEnumerator LoadPictureEnumerator(string url, LoadPictureCallback callback)
yield return www;
callback(www.texture);
}
void LoadPicture (string url, LoadPictureCallback callback)
void LoadPictureAPI (string url, LoadPictureCallback callback)
{
FB.API(url,Facebook.HttpMethod.GET,result =>
{
Expand All @@ -714,4 +715,10 @@ void LoadPicture (string url, LoadPictureCallback callback)
StartCoroutine(LoadPictureEnumerator(imageUrl,callback));
});
}
void LoadPictureURL (string url, LoadPictureCallback callback)
{
StartCoroutine(LoadPictureEnumerator(url,callback));

}

}
11 changes: 9 additions & 2 deletions friendsmash_payments_complete/Assets/Scripts/Util.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@ public static string GetPictureURL(string facebookID, int? width = null, int? he

public static Dictionary<string, string> RandomFriend(List<object> friends)
{
var fd = ((Dictionary<string, object>)(friends[Random.Range(0, friends.Count - 1)]));
var fd = ((Dictionary<string, object>)(friends[Random.Range(0, friends.Count)]));
var friend = new Dictionary<string, string>();
friend["id"] = (string)fd["id"];
friend["first_name"] = (string)fd["first_name"];
var pictureDict = ((Dictionary<string, object>)(fd["picture"]));
var pictureDataDict = ((Dictionary<string, object>)(pictureDict["data"]));
friend["image_url"] = (string)pictureDataDict["url"];
return friend;
}

Expand Down Expand Up @@ -57,10 +60,14 @@ public static List<object> DeserializeJSONFriends(string response)
var responseObject = Json.Deserialize(response) as Dictionary<string, object>;
object friendsH;
var friends = new List<object>();
if (responseObject.TryGetValue("friends", out friendsH))
if (responseObject.TryGetValue("invitable_friends", out friendsH))
{
friends = (List<object>)(((Dictionary<string, object>)friendsH)["data"]);
}
if (responseObject.TryGetValue("friends", out friendsH))
{
friends.AddRange((List<object>)(((Dictionary<string, object>)friendsH)["data"]));
}
return friends;
}

Expand Down
Loading

0 comments on commit b2b085e

Please sign in to comment.