Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public bool loginListenersAdded = false;
- public override void Awake()
- {
- base.Awake();
- }
- public void AddAllLoginListeners()
- {
- loginListenersAdded = false;
- // TODO: check if LoginEvents singleton exists and do not create a new one if it doesn't
- KGameObjectSingleton<LoginEvents>.Instance.OnLoginEvent += OnLogin;
- KGameObjectSingleton<LoginEvents>.Instance.OnLogoutEvent += OnLogout;
- KGameObjectSingleton<LoginEvents>.Instance.OnLoginCancelledEvent += TryAutologin;
- KGameObjectSingleton<LoginEvents>.Instance.OnLoginFailedEvent += OnLoginFailed;
- KGameObjectSingleton<LoginEvents>.Instance.OnLoginFinishedEvent += UpdateInformation;
- KGameObjectSingleton<LoginEvents>.Instance.OnFBLoginFinishedEvent += LoginWithFB;
- KGameObjectSingleton<LoginEvents>.Instance.OnGPLoginFinishedEvent += LoginWithGP;
- KGameObjectSingleton<LoginEvents>.Instance.OnTWLoginFinishedEvent += LoginWithTW;
- KGameObjectSingleton<LoginEvents>.Instance.OnAccountLoginEvent += LoginWithAccount;
- KGameObjectSingleton<LoginEvents>.Instance.OnRegisterEvent += RegisterWithEmail;
- KGameObjectSingleton<LoginEvents>.Instance.OnApplicationQuitEvent += RemoveAllLoginListeners;
- //KGameObjectSingleton<GcmEvents>.Instance.GCMPushRegistered += Instance_GCMPushRegistered;
- }
- public void RemoveAllLoginListeners()
- {
- loginListenersAdded = true;
- // TODO: check if LoginEvents singleton exists and do not create a new one if it doesn't
- KGameObjectSingleton<LoginEvents>.Instance.OnLoginEvent -= OnLogin;
- KGameObjectSingleton<LoginEvents>.Instance.OnLogoutEvent -= OnLogout;
- KGameObjectSingleton<LoginEvents>.Instance.OnLoginCancelledEvent -= TryAutologin;
- KGameObjectSingleton<LoginEvents>.Instance.OnLoginFailedEvent -= OnLoginFailed;
- KGameObjectSingleton<LoginEvents>.Instance.OnLoginFinishedEvent -= UpdateInformation;
- KGameObjectSingleton<LoginEvents>.Instance.OnFBLoginFinishedEvent -= LoginWithFB;
- KGameObjectSingleton<LoginEvents>.Instance.OnGPLoginFinishedEvent -= LoginWithGP;
- KGameObjectSingleton<LoginEvents>.Instance.OnTWLoginFinishedEvent -= LoginWithTW;
- KGameObjectSingleton<LoginEvents>.Instance.OnAccountLoginEvent -= LoginWithAccount;
- KGameObjectSingleton<LoginEvents>.Instance.OnRegisterEvent -= RegisterWithEmail;
- KGameObjectSingleton<LoginEvents>.Instance.OnApplicationQuitEvent -= RemoveAllLoginListeners;
- }
- public void OnSceneClosed()
- {
- RemoveAllLoginListeners();
- }
- public void RefreshGSAvailability()
- {
- if (GS.Available && GSAvailabilityChangedCallBack != null)
- {
- GSAvailabilityChangedCallBack(GS.Available);
- GSAvailabilityChangedCallBack = null;
- }
- }
- public void TryAutologin()
- {
- if (CanAutologin())
- {
- KGameObjectSingleton<LoginEvents>.Instance.OnLogin((LoginProvider)PlayerPrefs.GetInt(LAST_LOGIN_PROVIDER));
- }
- else
- {
- KGameObjectSingleton<LoginEvents>.Instance.OnLogin(LoginProvider.Guest);
- }
- }
- public void EnableNotifications()
- {
- UTNotifications.Manager.Instance.NotificationsEnabled();
- UTNotifications.Manager notificationsManager = UTNotifications.Manager.Instance;
- notificationsManager.OnNotificationClicked += (notification) =>
- {
- Debug.Log(notification.text + " clicked");
- };
- notificationsManager.OnNotificationsReceived += (receivedNotifications) =>
- {
- foreach (UTNotifications.ReceivedNotification notification in receivedNotifications)
- {
- Debug.Log(notification.text + " received");
- }
- };
- notificationsManager.OnSendRegistrationId += (providerName, registrationId) =>
- {
- if (providerName == "GooglePlay")
- {
- registrationIdGoogle = registrationId;
- RegisterForPushNotification();
- }
- };
- notificationsManager.Initialize(true);
- }
- public void DisableNotifications()
- {
- UnregisterForPushNotification();
- UTNotifications.Manager.Instance.SetNotificationsEnabled(false);
- }
- public void RegisterForPushNotification()
- {
- new PushRegistrationRequest().SetPushId(registrationIdGoogle).Send((response) =>
- {
- if (response.HasErrors)
- {
- Debug.Log("Something failed when registering push notification Id " + response.Errors);
- }
- else
- {
- PlayerPrefs.SetString(PUSH_REGISTRATION_ID_GOOGLE, response.RegistrationId);
- Debug.Log("Gamesparks Push Register Successful");
- }
- });
- }
- public void UnregisterForPushNotification()
- {
- new LogEventRequest().SetEventKey("UNREGISTER_FROM_PUSH").SetEventAttribute("Registration_Id", PlayerPrefs.GetString(PUSH_REGISTRATION_ID_GOOGLE)).Send((response) =>
- {
- if (!response.HasErrors)
- {
- PlayerPrefs.DeleteKey(PUSH_REGISTRATION_ID_GOOGLE);
- Debug.Log("Gamesparks Push Unregister Successful");
- }
- else
- {
- Debug.Log("Gamesparks Push Register Unsuccessful : " + response.Errors);
- }
- });
- }
- public bool IsPushNotificationIdSet()
- {
- return PlayerPrefs.GetString(PUSH_REGISTRATION_ID_GOOGLE) != "";
- }
- public bool CanAutologin()
- {
- return PlayerPrefs.HasKey(LAST_LOGIN_PROVIDER);
- }
- public void OnLogin(LoginProvider provider)
- {
- if (provider == LoginProvider.Mail)
- {
- LoginWithAccount(PlayerPrefs.GetString(ACCOUNT_LOGIN_USERNAME), PlayerPrefs.GetString(ACCOUNT_LOGIN_PASSWORD), false);
- }
- else if (provider == LoginProvider.Guest)
- {
- LoginWithAccount(GUEST_USERNAME, GUEST_PASSWORD, true);
- }
- }
- void RegisterGuest()
- {
- Debug.Log("Gamesparks Guest Register");
- string username = "Guest_" + Guid.NewGuid().ToString();
- string password = Guid.NewGuid().ToString();
- GSRequestData requestData = new GSRequestData().AddString("action", "RegisterGuest").AddBoolean("isGuest", true);
- new RegistrationRequest().SetUserName(username).SetPassword(password).SetDisplayName("Guest").SetScriptData(requestData).Send((response) =>
- {
- if (response.HasErrors)
- {
- Debug.Log("Something failed when registering as Guest " + response.Errors);
- }
- else
- {
- Debug.Log("Gamesparks Guest Register Successful");
- LoginWithAccount(username, password, true);
- }
- });
- }
- public void LoginWithFB(string FBAccessToken)
- {
- Debug.Log("Gamesparks Facebook login with token : " + FBAccessToken);
- OnVerifyingAccount();
- attemptedLoginProvider = LoginProvider.Facebook;
- new FacebookConnectRequest().SetAccessToken(FBAccessToken).SetDoNotLinkToCurrentPlayer(true).SetSyncDisplayName(true).Send((response) =>
- {
- if (response.HasErrors)
- {
- Debug.LogError("Something failed when connecting with Facebook " + response.Errors);
- KGameObjectSingleton<LoginEvents>.Instance.OnLoginFailed();
- }
- else
- {
- Debug.Log("Gamesparks Facebook Login Successful");
- JSONNode data = JSONNode.Parse(response.JSONString);
- newPlayer = data["newPlayer"].AsBool;
- UpdateInformation();
- previousLoginProvider = loginProvider;
- loginProvider = LoginProvider.Facebook;
- PlayerPrefs.SetInt(LAST_LOGIN_PROVIDER, Convert.ToInt32(loginProvider));
- }
- OnVerifyingAccountFinished();
- });
- }
- public void LoginWithGP(string token)
- {
- Debug.Log("Gamesparks Google login with token : " + token);
- OnVerifyingAccount();
- attemptedLoginProvider = LoginProvider.Google;
- new GooglePlusConnectRequest().SetAccessToken(token).SetDoNotLinkToCurrentPlayer(true).SetSyncDisplayName(true).Send((response) =>
- {
- if (response.HasErrors)
- {
- Debug.LogError("Something failed when connecting with Google Plus " + response.Errors);
- KGameObjectSingleton<LoginEvents>.Instance.OnLoginFailed();
- }
- else
- {
- Debug.Log("Gamesparks Google Plus Login Successful");
- JSONNode data = JSONNode.Parse(response.JSONString);
- newPlayer = data["newPlayer"].AsBool;
- UpdateInformation();
- previousLoginProvider = loginProvider;
- loginProvider = LoginProvider.Google;
- PlayerPrefs.SetInt(LAST_LOGIN_PROVIDER, Convert.ToInt32(loginProvider));
- }
- OnVerifyingAccountFinished();
- });
- }
- public void LoginWithTW(string token, string secret)
- {
- Debug.Log("Gamesparks Twitter login with token : " + token + Environment.NewLine + "secret " + secret);
- OnVerifyingAccount();
- attemptedLoginProvider = LoginProvider.Twitter;
- new TwitterConnectRequest().SetAccessToken(token).SetDoNotLinkToCurrentPlayer(true).SetAccessSecret(secret).SetSyncDisplayName(true).Send((response) =>
- {
- if (response.HasErrors)
- {
- Debug.Log("Something failed when connecting with Twitter " + response.Errors);
- KGameObjectSingleton<LoginEvents>.Instance.OnLoginFailed();
- }
- else
- {
- Debug.Log("Gamesparks Twitter Login Successful");
- JSONNode data = JSONNode.Parse(response.JSONString);
- newPlayer = data["newPlayer"].AsBool;
- UpdateInformation();
- previousLoginProvider = loginProvider;
- loginProvider = LoginProvider.Twitter;
- PlayerPrefs.SetInt(LAST_LOGIN_PROVIDER, Convert.ToInt32(loginProvider));
- }
- OnVerifyingAccountFinished();
- });
- }
- public void LoginWithAccount(string username, string password, bool isGuest)
- {
- Debug.Log("Gamesparks Begin Account Login with : " + username);
- if (!isGuest)
- OnVerifyingAccount();
- GSRequestData requestData = new GSRequestData().AddString("action", "AuthenticateGuest").AddBoolean("isGuest", isGuest);
- attemptedLoginProvider = LoginProvider.Mail;
- new AuthenticationRequest().SetUserName(username).SetPassword(password).SetScriptData(requestData).Send((response) =>
- {
- if (response.HasErrors)
- {
- Debug.Log("Something failed when connecting with Account " + response.Errors);
- if (!isGuest)
- KGameObjectSingleton<LoginEvents>.Instance.OnLoginFailed();
- }
- else
- {
- Debug.Log("Gamesparks Account Login Successful");
- JSONNode data = JSONNode.Parse(response.JSONString);
- newPlayer = data["newPlayer"].AsBool;
- UpdateInformation();
- if (!isGuest)
- {
- previousLoginProvider = loginProvider;
- loginProvider = LoginProvider.Mail;
- }
- else
- {
- previousLoginProvider = loginProvider;
- loginProvider = LoginProvider.Guest;
- }
- // save user credentials for autologin
- PlayerPrefs.SetInt(LAST_LOGIN_PROVIDER, Convert.ToInt32(loginProvider));
- PlayerPrefs.SetString(ACCOUNT_LOGIN_USERNAME, username);
- PlayerPrefs.SetString(ACCOUNT_LOGIN_PASSWORD, password);
- }
- if (!isGuest)
- OnVerifyingAccountFinished();
- });
- }
- public void RegisterWithEmail(string email, string password, string displayName)
- {
- new RegistrationRequest().SetUserName(email).SetPassword(password).SetDisplayName(displayName).Send((response) =>
- {
- if (response.HasErrors)
- {
- Debug.Log("Something failed when registering with Email " + response.Errors);
- KGameObjectSingleton<LoginEvents>.Instance.OnLoginFailed();
- }
- else
- {
- Debug.Log("Gamesparks Email Register Successful");
- AttachProfilePictureLink(true);
- LoginWithAccount(email, password, false);
- }
- });
- }
- void OnLogout(LoginProvider _loginProvider)
- {
- if (_loginProvider == LoginProvider.Mail)
- {
- PlayerPrefs.DeleteKey(ACCOUNT_LOGIN_USERNAME);
- PlayerPrefs.DeleteKey(ACCOUNT_LOGIN_PASSWORD);
- }
- PlayerPrefs.DeleteKey(LAST_LOGIN_PROVIDER);
- new EndSessionRequest().Send((response) =>
- {
- if (response.HasErrors)
- {
- Debug.Log("Something failed when logging out " + response.Errors);
- }
- else
- {
- Debug.Log("Gamesparks Logout Successful");
- GS.GSPlatform.AuthToken = "";
- GS.GSPlatform.UserId = "";
- previousLoginProvider = loginProvider;
- loginProvider = LoginProvider.None;
- if (UsersAllController.Instance != null)
- {
- UsersAllController.Instance.RefreshFollowingUsers(false);
- }
- KGameObjectSingleton<LoginEvents>.Instance.OnLogoutFinished();
- KGameObjectSingleton<LoginEvents>.Instance.OnLogin(LoginProvider.Guest);
- }
- });
- }
- public void UpdateInformation()
- {
- new AccountDetailsRequest().Send((response) =>
- {
- accountResponse = response;
- if (response.HasErrors)
- {
- Debug.Log("Something failed when connecting " + response.Errors);
- if (loginProvider != LoginProvider.None && loginProvider != LoginProvider.Guest)
- KGameObjectSingleton<LoginEvents>.Instance.OnLoginFailed();
- }
- else
- {
- Debug.Log("Gamesparks Login Successful");
- currentPlayerData = new PlayerData();
- JSONNode data = JSONNode.Parse(response.JSONString);
- currentPlayerData = new PlayerData();
- currentPlayerData.Id = data["userId"].Value;
- currentPlayerData.Name = data["displayName"].Value;
- currentPlayerData.ProfilePictureLink = data["scriptData"]["profilePictureLink"].Value;
- currentPlayerData.NewPlayer = newPlayer;
- if (loginProvider != LoginProvider.None && loginProvider != LoginProvider.Guest)
- {
- AttachLastLoginProvider();
- if (loginProvider != LoginProvider.Mail)
- {
- AttachProfilePictureLink(false);
- }
- ManageSpentTimeInsideApp();
- if (UsersAllController.Instance != null)
- {
- UsersAllController.Instance.RefreshFollowingUsers(true);
- }
- }
- KGameObjectSingleton<LoginEvents>.Instance.OnUserProfileUpdated();
- }
- });
- }
- public bool soomlaSecondAttempt;
- public void OnLoginFailed()
- {
- if ((SoomlaLoginManager.previousLoginRequest == LoginProvider.Google || SoomlaLoginManager.previousLoginRequest == LoginProvider.Twitter) && !soomlaSecondAttempt)
- {
- if (SoomlaLoginManager.previousLoginRequest == LoginProvider.Twitter)
- {
- AllWelcomeScreen.Instance.LoginWithTwitter ();
- }
- if (SoomlaLoginManager.previousLoginRequest == LoginProvider.Google)
- {
- AllWelcomeScreen.Instance.LoginWithGooglePlus ();
- }
- soomlaSecondAttempt = true;
- }
- else
- {
- if (attemptedLoginProvider == LoginProvider.Mail)
- {
- loginFailedPopUp.ShowPopUp();
- }
- KGameObjectSingleton<LoginEvents>.Instance.OnLogin(LoginProvider.Guest);
- soomlaSecondAttempt = false;
- }
- }
- public void OnVerifyingAccount()
- {
- if (verifyingAccountPopUp != null)
- {
- verifyingAccountPopUp.ShowPopUp();
- }
- }
- public void OnVerifyingAccountFinished()
- {
- if (verifyingAccountPopUp != null)
- {
- verifyingAccountPopUp.HidePopUp();
- }
- }
- private void AttachLastLoginProvider()
- {
- new LogEventRequest().SetEventKey("ATTACH_LAST_LOGIN_PROVIDER").SetEventAttribute("LAST_LOGIN_PROVIDER", loginProvider.ToString()).Send((response) => {
- if (!response.HasErrors)
- {
- Debug.Log("Succeded to attach last login provider");
- }
- else
- {
- Debug.Log("Fail to attach last login providerk" + response.Errors);
- }
- });
- }
- private void AttachProfilePictureLink(bool newMailRegistered)
- {
- string link = "";
- if (loginProvider == LoginProvider.Facebook)
- {
- link = accountResponse.ExternalIds.GetString("FB").ToString();
- }
- else if (loginProvider == LoginProvider.Twitter)
- {
- link = SoomlaProfile.GetStoredUserProfile(Provider.TWITTER).AvatarLink;
- }
- else if (loginProvider == LoginProvider.Google)
- {
- link = SoomlaProfile.GetStoredUserProfile(Provider.GOOGLE).AvatarLink;
- }
- else if (newMailRegistered)
- {
- int randomColor = UnityEngine.Random.Range(0, mailBgColors.Length);
- link = mailBgColors[randomColor].r + " " + mailBgColors[randomColor].g + " " + mailBgColors[randomColor].b;
- }
- if (!link.Equals(""))
- {
- new LogEventRequest().SetEventKey("ATTACH_PROFILE_PICTURE_LINK").SetEventAttribute("LINK", link).Send((response) =>
- {
- if (!response.HasErrors)
- {
- Debug.Log("Succeded to attach profile picture link");
- }
- else
- {
- Debug.Log("Fail to attach profile picture link" + response.Errors);
- }
- });
- }
- }
- private void ManageSpentTimeInsideApp()
- {
- showReward = false;
- new LogEventRequest().SetEventKey("MANAGE_SPENT_TIME_INSIDE_APP").Send((response) =>
- {
- if (!response.HasErrors)
- {
- Debug.Log("Succeded to manage spent time inside app: " + response.JSONString);
- showReward = JSONNode.Parse(response.JSONString)["scriptData"]["giveReward"].AsBool;
- daysInGame = JSONNode.Parse(response.JSONString)["scriptData"]["daysInGame"].AsInt;
- }
- else
- {
- Debug.Log("Fail to manage spent time inside app" + response.Errors);
- }
- });
- }
- public bool isLoggedIn()
- {
- if (loginProvider == LoginProvider.None)
- {
- return false;
- }
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement