public RequestData(string id, string fromId, string fromName, FBActionType actionType, FBObjectType objectType) { _id = id; _fromId = fromId; _fromName = fromName; _actionType = actionType; _objectType = objectType; }
/// <summary> /// Get ASK requests. /// Return callback(requests, error). /// </summary> public static void GetAskRequests(Action <List <RequestData>, string> callback) { FB.API("/me/apprequests?fields=id,from,data,action_type", HttpMethod.GET, (result) => { if (!string.IsNullOrEmpty(result.Error)) { callback(null, result.Error); } else { if (!string.IsNullOrEmpty(result.RawResult)) { var pendingRequests = Json.Deserialize(result.RawResult) as Dictionary <string, object>; var pendingRequestsData = pendingRequests["data"] as List <object>; if (pendingRequestsData != null && pendingRequestsData.Count > 0) { var list = new List <RequestData>(); object idH; object fromH; object dataH; object actionTypeH; foreach (var entry in pendingRequestsData) { var requestItem = entry as Dictionary <string, object>; string id = ""; string fromId = ""; string fromName = ""; FBObjectType objectType = FBObjectType.None; if (requestItem.TryGetValue("id", out idH)) { id = idH.ToString(); } if (requestItem.TryGetValue("from", out fromH)) { var from = (Dictionary <string, object>)fromH; fromId = from["id"].ToString(); fromName = from["name"].ToString(); } if (requestItem.TryGetValue("data", out dataH)) { string item = dataH.ToString(); if (item == Coin) { objectType = FBObjectType.Coin; } else if (item == Mana) { objectType = FBObjectType.Mana; } // else if (item == InviteToGetCoin) // { // // Send coin // SendObject(null, InviteToGetCoin, Settings.SendCoinByInviteTitle, string.Format(Settings.SendCoinByInviteMessage, Settings.CoinByInvite), (error) => { // if (!string.IsNullOrEmpty(error)) // { // //Log.Debug("Invite error: " + error); // } // }, fromId); // // DeleteRequest(id); // // callback(null, null); // // return; // } // else if (item == InviteToGetMana) // { // // Send mana // SendObject(null, InviteToGetMana, Settings.SendManaByInviteTitle, string.Format(Settings.SendManaByInviteMessage, Settings.ManaByInvite), (error) => { // if (!string.IsNullOrEmpty(error)) // { // //Log.Debug("Invite error: " + error); // } // }, fromId); // // DeleteRequest(id); // // callback(null, null); // // return; // } else { objectType = FBObjectType.Unknown; } } if (requestItem.TryGetValue("action_type", out actionTypeH)) { if (actionTypeH.ToString() == "askfor") { list.Add(new RequestData(id, fromId, fromName, FBActionType.AskFor, objectType)); } } } callback(list, null); } else { callback(null, null); } } else { callback(null, null); } } }); }
static void InviteInternal(string title, string message, FriendType friendType = FriendType.Invitable, FBObjectType objectType = FBObjectType.Coin, Action <string> callback = null) { #if UNITY_EDITOR if (callback != null) { callback("Not supported!"); } #else FB.AppRequest( message, null, GetFriendFilters(friendType), null, null, objectType == FBObjectType.Coin ? Coin : Mana, title, (result) => { if (string.IsNullOrEmpty(result.Error)) { var dictionary = result.ResultDictionary; // foreach (var key in dictionary.Keys) // { // Log.Debug(string.Format("{0}:{1}", key, dictionary[key])); // } if (dictionary.ContainsKey("to")) { var to = dictionary["to"] as List <object>; if (to == null) { string[] tos = dictionary["to"].ToString().Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); // Coin if (objectType == FBObjectType.Coin) { for (int i = 0; i < tos.Length; i++) { FriendData.Instance.AddCoinInvitedFriend(tos[i]); } } // Mana else { for (int i = 0; i < tos.Length; i++) { FriendData.Instance.AddManaInvitedFriend(tos[i]); } } } else { // Coin if (objectType == FBObjectType.Coin) { for (int i = 0; i < to.Count; i++) { FriendData.Instance.AddCoinInvitedFriend(to[i].ToString()); } } // Mana else { for (int i = 0; i < to.Count; i++) { FriendData.Instance.AddManaInvitedFriend(to[i].ToString()); } } } Log.Debug(FriendData.Instance.ToString()); } } else { Log.Debug("Invite error: " + result.Error); } if (callback != null) { callback(result.Error); } } ); #endif }
/// <summary> /// Get SEND requests. /// Return callback(requests, error). /// </summary> public static void GetSendRequests(Action <List <RequestData>, string> callback) { FB.API("/me/apprequests?fields=id,from,data,action_type", HttpMethod.GET, (result) => { if (!string.IsNullOrEmpty(result.Error)) { callback(null, result.Error); } else { if (!string.IsNullOrEmpty(result.RawResult)) { var pendingRequests = Json.Deserialize(result.RawResult) as Dictionary <string, object>; var pendingRequestsData = pendingRequests["data"] as List <object>; if (pendingRequestsData != null && pendingRequestsData.Count > 0) { var list = new List <RequestData>(); object idH; object fromH; object dataH; object actionTypeH; foreach (var entry in pendingRequestsData) { var requestItem = entry as Dictionary <string, object>; string id = ""; string fromId = ""; string fromName = ""; FBObjectType objectType = FBObjectType.None; if (requestItem.TryGetValue("id", out idH)) { id = idH.ToString(); } if (requestItem.TryGetValue("from", out fromH)) { var from = (Dictionary <string, object>)fromH; fromId = from["id"].ToString(); fromName = from["name"].ToString(); } if (requestItem.TryGetValue("data", out dataH)) { string item = dataH.ToString(); if (item == Coin) { objectType = FBObjectType.Coin; } else if (item == Mana) { objectType = FBObjectType.Mana; } // else if (item == InviteToGetCoin) objectType = FBObjectType.InviteCoin; // else if (item == InviteToGetMana) objectType = FBObjectType.InviteMana; else { objectType = FBObjectType.Unknown; } } if (requestItem.TryGetValue("action_type", out actionTypeH)) { if (actionTypeH.ToString() == "send") { list.Add(new RequestData(id, fromId, fromName, FBActionType.Send, objectType)); } } } callback(list, null); } else { callback(null, null); } } else { callback(null, null); } } }); }
// Invite friends // Return callback(error) public static void Invite(string title, string message, FriendType friendType = FriendType.Invitable, FBObjectType objectType = FBObjectType.Coin, Action <string> callback = null) { #if UNITY_EDITOR if (callback != null) { callback("Not supported!"); } #else if (FB.IsLoggedIn) { InviteInternal(title, message, friendType, objectType, callback); } else { LogIn((error) => { if (!string.IsNullOrEmpty(error)) { if (callback != null) { callback(error); } } else { InviteInternal(title, message, friendType, objectType, callback); } }); } #endif }