123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664 |
- using System;
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- using Facebook.Unity;
- using Facebook.Unity.Example;
- using Prime31;
- public class WrapperFB : MonoBehaviour
- {
- public GameObject LogoutGop;
- public GameObject LoginGop;
- public static GameObject LoginGo;
- public static GameObject LogoutGo;
- public static string Name { get; private set; }
- public static string Id { get; private set; }
- public static string Email { get; private set; }
- public static bool InitDone { get; private set; }
- public delegate void EventHandler(object target);
-
- public static event EventHandler OnLoggedIn;
- public static event EventHandler LoginFailedHandler;
- public static GameObject Go;
- // Use this for initialization
- // public void Start()
- // {
-
- // //Go = gameObject;
- // //Name = PlayerPrefs.GetString("FB_USER_NAME", "");
- //#if UNITY_EDITOR
- //#endif
- // }
- public void LoginFacebook()
- {
- if (!FB.IsLoggedIn)
- {
- // Continue with Facebook SDK
- var perms = new List<string>() { "public_profile", "email", "user_friends" };
- FB.LogInWithReadPermissions(perms, FBAuthCallback);
- }
- else
- {
- FBAuthCallback(null);
- }
- }
- private void FBAuthCallback(ILoginResult result)
- {
- if (FB.IsLoggedIn)
- {
- // AccessToken class will have session details
- var aToken = Facebook.Unity.AccessToken.CurrentAccessToken;
- // Print current access token's User ID
- Debug.Log(aToken.UserId);
- // Print current access token's granted permissions
- foreach (string perm in aToken.Permissions)
- {
- Debug.Log(perm);
- }
- }
- else
- {
- Debug.Log("User cancelled login");
- }
- }
- public static void UpdateLoginButton()
- {
- if (PlayerPrefs.GetInt("Login") == 1)
- {
- LogoutGo.SetActive(true);
- LoginGo.SetActive(false);
- }
- else
- {
- LogoutGo.SetActive(false);
- LoginGo.SetActive(true);
- }
- }
- public static void Init()
- {
- FB.Init(() =>
- {
- InitDone = true;
- });
- }
- void Update()
- {
- //if (WrapperFB.IsLoggedIn)
- //{
- // LogoutGo.SetActive(true);
- // LoginGo.SetActive(false);
- //}
- //else
- //{
- // LogoutGo.SetActive(false);
- // LoginGo.SetActive(true);
- //}
- }
- public static void LoggedIn(GameObject unit)
- {
- if (OnLoggedIn != null)
- {
- OnLoggedIn(unit);
- }
- }
- private IEnumerator ShowLoginPopup()
- {
- yield return new WaitForSeconds(1);
- //LoginManager.Instance.WrongMessages.Show();
- }
- public static bool IsLoggedIn
- {
- get { return UserTokenInfo.ExpirationTime > DateTime.Now; }
- }
- public static string UserID
- {
- get { return UserTokenInfo.UserID; }
- }
- public static string AccessToken
- {
- get { return UserTokenInfo.AccessToken; }
- }
- private static void DispNAme(IGraphResult result)
- {
- if (result.Error != null)
- {
-
- }
- else
- {
- if (result.ResultDictionary != null)
- {
- foreach (string key in result.ResultDictionary.Keys)
- {
- Debug.Log(key + " : " + result.ResultDictionary[key].ToString());
- // first_name : Chris
- // id : 12345678901234567
- }
- }
- PlayerPrefs.SetString("FB_USER_NAME", result.ResultDictionary["name"].ToString());
- PlayerPrefs.SetString("FB_ID", result.ResultDictionary["id"].ToString());
- PlayerPrefs.Save();
- //SocialManager.Instance.GraphRequestCallback(result);
- }
- }
- public void CallFBLogout()
- {
- FB.LogOut();
- if (WrapperFB.IsLoggedIn)
- {
- LogoutGo.SetActive(true);
- LoginGo.SetActive(false);
- }
- else
- {
- LogoutGo.SetActive(false);
- LoginGo.SetActive(true);
- }
- }
- public static void Login()
- {
- Debug.Log("Login");
- #if UNITY_EDITOR
- //PlayerPrefs.SetInt("Login", 1);
- //LoginManager.Instance.WrongMessages.Hide();
- #endif
- FB.LogInWithReadPermissions(new List<string> { "email", "public_profile", "user_friends" }, result =>
- {
- if (result != null && !result.Cancelled && string.IsNullOrEmpty(result.Error))
- {
- Debug.Log("LOGIN");
- UserTokenInfo.LoginToken(result.AccessToken);
-
-
- //Name = result.ResultDictionary["first_name"].ToString() + " " + result.ResultDictionary["last_name"].ToString();
- PlayerPrefs.SetInt("Login",1);
- if (PlayerPrefs.GetInt("Login") == 1)
- {
- LogoutGo.SetActive(true);
- LoginGo.SetActive(false);
- }
- else
- {
- LogoutGo.SetActive(false);
- LoginGo.SetActive(true);
- }
- //LoginManager.Instance.WrongMessages.Hide();
-
- LoggedIn(Go);
- FB.API("/me",
- HttpMethod.GET,
- DispNAme)
- ;
- }
- else if (!string.IsNullOrEmpty(result.Error))
- {
- LoginFailedHandler(result.Error);
- }
-
- });
- }
- // public static void Logout()
- // {
- //#if UNITY_EDITOR
- // PlayerPrefs.SetInt("Login", 0);
- //#endif
- // PlayerPrefs.SetInt("Login", 0);
-
- // if (PlayerPrefs.GetInt("Login") == 1)
- // {
- // LogoutGo.SetActive(true);
- // LoginGo.SetActive(false);
- // }
- // else
- // {
- // LogoutGo.SetActive(false);
- // LoginGo.SetActive(true);
- // }
- // UserTokenInfo.LogOut();
- // FB.LogOut();
- // }
- public static void HandleResult(IResult result)
- {
- if (result == null)
- {
- LastResponse = "Null Response\n";
- LogView.AddLog(LastResponse);
- Debug.Log(LastResponse);
- return;
- }
- LastResponseTexture = null;
- // Some platforms return the empty string instead of null.
- if (!string.IsNullOrEmpty(result.Error))
- {
- Status = "Error - Check log for details";
- LastResponse = "Error Response:\n" + result.Error;
- LogView.AddLog(result.Error);
- Debug.Log(LastResponse);
- }
- else if (result.Cancelled)
- {
- Status = "Cancelled - Check log for details";
- LastResponse = "Cancelled Response:\n" + result.RawResult;
- LogView.AddLog(result.RawResult);
- Debug.Log(LastResponse);
-
- //if (MenuManager._instance.gameOverSummaryScreen.isActiveAndEnabled && !MenuManager._instance.IsLevelEnd)
- //{
- // MenuManager._instance.gameOverSummaryScreen.Hide();
- //}
-
- //NoLivesLeftDetector.Instance.NoLivesAddFriendForOneLiveGameOver.Hide();
- //GameDataManager.Instance.HasStartedSpawningEnemies = false;
- //MenuManager._instance.IsLevelEnd = false;
- //NotificationCenter.Post(NotificationType.GameOver);
- //NotificationCenter.Post(NotificationType.MenuTransition, MenuManager.MenuTransition.ExitToMainMenu);
- //MenuManager._instance.playScreen.SetActive(false);
- }
- else if (!string.IsNullOrEmpty(result.RawResult))
- {
- Status = "Success - Check log for details";
- LastResponse = "Success Response:\n" + result.RawResult;
- LogView.AddLog(result.RawResult);
- Debug.Log(LastResponse);
- var countLifeInDay = PlayerPrefs.GetInt("CounterLifeInDay");
-
- if (countLifeInDay == 0)
- {
- //NotificationCenter.Post(NotificationType.GetLife);
- //if (MenuManager._instance.gameOverSummaryScreen.isActiveAndEnabled && !MenuManager._instance.IsLevelEnd)
- //{
- // MenuManager._instance.gameOverSummaryScreen.Hide();
- //}
- }
- else
- {
- }
- PlayerPrefs.SetInt("CounterLifeInDay", countLifeInDay + 1);
- PlayerPrefs.Save();
-
- //LivesManager.Instance.AddLife();
- //NoLivesLeftDetector.Instance.NoLivesAddFriendForOneLiveGameOver.Hide();
- }
- else
- {
- LastResponse = "Empty Response\n";
- LogView.AddLog(LastResponse);
- Debug.Log(LastResponse);
- }
- }
- public static string Status { get; set; }
- public static object LastResponseTexture { get; set; }
- public static string LastResponse { get; set; }
- public static void PostFB()
- {
- Facebook.Unity.FB.
- FeedShare(
- string.Empty,
- new Uri("https://game.gamatic.com/app_condom_dk/Kondomspillet4.html"),
- "",
- "",
- "",
- new Uri("https://game.gamatic.com/RubIt/facebookShareWonChallenge2.png"),
- string.Empty,
- HandleResultPost);
- }
- public void Post()
- {
- PostFB();
- }
- private static void HandleResultPost(IResult result)
- {
- if (result == null)
- {
- LastResponse = "Null Response\n";
- LogView.AddLog(LastResponse);
- Debug.Log(LastResponse);
- return;
- }
- LastResponseTexture = null;
- // Some platforms return the empty string instead of null.
- if (!string.IsNullOrEmpty(result.Error))
- {
- Status = "Error - Check log for details";
- LastResponse = "Error Response:\n" + result.Error;
- LogView.AddLog(result.Error);
- Debug.Log(LastResponse);
- }
- else if (result.Cancelled)
- {
- Status = "Cancelled - Check log for details";
- LastResponse = "Cancelled Response:\n" + result.RawResult;
- LogView.AddLog(result.RawResult);
- Debug.Log(LastResponse);
- }
- else if (!string.IsNullOrEmpty(result.RawResult))
- {
- Status = "Success - Check log for details";
- LastResponse = "Success Response:\n" + result.RawResult;
- LogView.AddLog(result.RawResult);
- //LoginManager.Instance.WrongMessages.Hide();
- Debug.Log(LastResponse);
- //MenuManager._instance.FBShareDialog.Hide();
- }
- else
- {
- LastResponse = "Empty Response\n";
- LogView.AddLog(LastResponse);
- Debug.Log(LastResponse);
- }
- }
- public static void InviteFriends()
- {
- FB.Mobile.AppInvite(new Uri("https://fb.me/126548434714783"), callback: HandleResult);
- }
- //public static string Name { get; private set; }
- //public static bool InitDone { get; private set; }
- private static WrapperFB _instance;
- // Use this for initialization
- void Start()
- {
- LoginGo = LoginGop;
- LogoutGo = LogoutGop;
- if (PlayerPrefs.GetInt("Login") == 1)
- {
- LogoutGo.SetActive(true);
- LoginGo.SetActive(false);
- }
- else
- {
- LogoutGo.SetActive(false);
- LoginGo.SetActive(true);
- }
- //ServerGiftManager.Instance.BaseRegistration((states, s) =>
- //{
- // // Debug.LogError("ServerGiftManager.Instance.BaseRegistration: " + states);
- //});
- if (_instance)
- {
- Destroy(gameObject);
- return;
- }
- _instance = this;
- Name = PlayerPrefs.GetString("FB_USER_NAME", "");
- FB.Init(() =>
- {
- InitDone = true;
- //Invite(i =>
- //{
- //});
- });
- DontDestroyOnLoad(gameObject);
- }
- //private void Update()
- //{
- // if (Input.GetKeyDown(KeyCode.F2) && InitDone && IsLoggedIn)
- // {
- // Logout();
- // }
- //}
- //public static bool IsLoggedIn
- //{
- // get { return UserTokenInfo.ExpirationTime > DateTime.Now; }
- //}
- //public static string UserID
- //{
- // get { return UserTokenInfo.UserID; }
- //}
- //public static string AccessToken
- //{
- // get { return UserTokenInfo.AccessToken; }
- //}
- public static void Login(Action onSuccess, Action onError)
- {
- FB.LogInWithReadPermissions(new List<string> { "email", "public_profile", "user_friends" }, result =>
- {
- if (result != null && !result.Cancelled && string.IsNullOrEmpty(result.Error))
- {
- UserTokenInfo.LoginToken(result.AccessToken);
- if (onSuccess != null)
- {
- onSuccess();
- }
- var aToken = Facebook.Unity.AccessToken.CurrentAccessToken;
- // Print current access token's User ID
- Debug.Log(aToken.UserId);
- // Print current access token's granted permissions
- bool email = false;
- foreach (string perm in aToken.Permissions)
- {
- if (perm == "email")
- {
- email = true;
- }
- Debug.Log(perm);
- }
- FB.API("/me?fields=id,name,email", HttpMethod.GET, resultMe =>
- {
- if (resultMe != null && !resultMe.Cancelled && string.IsNullOrEmpty(resultMe.Error))
- {
- Name = resultMe.ResultDictionary["name"].ToString();
- if (email)
- {
- Email = resultMe.ResultDictionary["email"].ToString();
- }
- Id = resultMe.ResultDictionary["id"].ToString();
- PlayerPrefs.SetString("FB_USER_NAME", Name);
- PlayerPrefs.Save();
- ServerGiftManager.Instance.FullRegistrationFB(Name, Email, "FB", Id, states =>
- {
- switch (states)
- {
- case ServerGiftManager.RegistrationStates.UpdateDone:
- RegistrationManager.Instance.ChangeRegistration.Hide();
- Debug.Log("new");
- //todo: handle - user info was updated (everything goes good)
- break;
- case ServerGiftManager.RegistrationStates.Old:
- Debug.Log("Old");
- //todo: handle - old user returns (everything goes good)
- break;
- case ServerGiftManager.RegistrationStates.SantaNameNotUnique:
- //todo: handle - user should guess another name for santa (no data was updated!)
- break;
- case ServerGiftManager.RegistrationStates.IdentificatorNotIsset:
- //todo: handle - you must run base registration first! (no data was updated!)
- break;
- case ServerGiftManager.RegistrationStates.ContactsIsNotEnough:
- //todo: handle - santaName or phone or e-mail is empty (no data was updated!)
- break;
- case ServerGiftManager.RegistrationStates.Error:
- //todo: handle - unknown error happens - maybe internet connection errors (no data was updated!)
- break;
- }
- });
- }
- });
- }
- else
- {
- Debug.LogError(result);
- Debug.LogError(result.Error);
- Debug.LogError(result.Cancelled);
- Debug.LogError(result.RawResult);
- if (onError != null)
- {
- onError();
- }
- }
- });
- }
- public static void ShareWin(int distance)
- {
- FB.FeedShare("", new Uri("http://www.kringlan.is/"), "Viltu vinna páskaegg?", "Ég náði " + distance + " stigum í Páskaleik Kringlunnar -– þúsundir vinninga og Páskaegg frá Góu í boði!", "", new Uri(GameConstants.SERVER_BASE_URL + "FB.png"), "",
- result =>
- {
- if (!string.IsNullOrEmpty(result.Error))
- {
- Debug.LogError(result.Error);
- }
- else if (result.Cancelled)
- {
- }
- else if (!string.IsNullOrEmpty(result.RawResult))
- {
- //NGUIMenuScript.Instance.AddLife();
- }
- Debug.LogError(result.RawResult);
- });
- }
- public static void ShareWeekWin()
- {
- FB.FeedShare("", new Uri("http://www.kringlan.is/"), "download and run", "I am a winner!", "", new Uri(GameConstants.SERVER_BASE_URL + "FB.png"), "",
- result =>
- {
- if (!string.IsNullOrEmpty(result.Error))
- {
- Debug.LogError(result.Error);
- }
- Debug.LogError(result.RawResult);
- });
- }
- public static void Logout(Action onSuccess)
- {
- PlayerPrefs.SetString("REGISTRATION", "FB");
- PlayerPrefs.SetString("FB_USER_NAME", "");
- PlayerPrefs.Save();
- UserTokenInfo.LogOut();
- FB.LogOut();
- if (onSuccess != null)
- {
- onSuccess();
- }
- PlayerPrefs.SetInt("Login", 0);
- if (PlayerPrefs.GetInt("Login") == 1)
- {
- LogoutGo.SetActive(true);
- LoginGo.SetActive(false);
- }
- else
- {
- LogoutGo.SetActive(false);
- LoginGo.SetActive(true);
- }
- }
- #region INVITE
- private static string FriendSelectorTitle = "Join the Easter Marathon";
- private static string FriendSelectorMessage = "Help me to collect more eggs";
- // private static string [] FriendSelectorFilters = {"all", "app_users", "app_non_users"};
- private static string[] FriendSelectorFilters = { "app_non_users" };
- private static string FriendSelectorData = "{}";
- private static int maxRecipients = 5;
- private static void CallAppRequestAsFriendSelector()
- {
- string[] excludeIds = null;
- //FB.AppRequest(
- // FriendSelectorMessage,
- // null,
- // FriendSelectorFilters,
- // excludeIds,
- // maxRecipients,
- // FriendSelectorData,
- // FriendSelectorTitle,
- // Callback
- //);
- FB.AppRequest(FriendSelectorMessage, null, null, null, 5, "", FriendSelectorTitle, Callback);
- }
- public static void Invite(Action<int> onDone)
- {
- CallAppRequestAsFriendSelector();
- }
- private static Action<int> _onInviteDone;
- private static void Callback(IAppRequestResult result)
- {
- Debug.LogWarning(result);
- if (_onInviteDone != null)
- {
- if (result != null && !result.Cancelled && string.IsNullOrEmpty(result.Error))
- {
- var friends = 0;
- foreach (var v in result.To)
- {
- friends++;
- }
- _onInviteDone(friends);
- }
- else
- {
- _onInviteDone(0);
- }
- }
- }
- #endregion
- }
|