Advertisement
Guest User

Untitled

a guest
Jun 21st, 2016
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.52 KB | None | 0 0
  1. public bool loginListenersAdded = false;
  2.  
  3. public override void Awake()
  4. {
  5. base.Awake();
  6. }
  7.  
  8. public void AddAllLoginListeners()
  9. {
  10. loginListenersAdded = false;
  11. // TODO: check if LoginEvents singleton exists and do not create a new one if it doesn't
  12. KGameObjectSingleton<LoginEvents>.Instance.OnLoginEvent += OnLogin;
  13. KGameObjectSingleton<LoginEvents>.Instance.OnLogoutEvent += OnLogout;
  14. KGameObjectSingleton<LoginEvents>.Instance.OnLoginCancelledEvent += TryAutologin;
  15. KGameObjectSingleton<LoginEvents>.Instance.OnLoginFailedEvent += OnLoginFailed;
  16. KGameObjectSingleton<LoginEvents>.Instance.OnLoginFinishedEvent += UpdateInformation;
  17. KGameObjectSingleton<LoginEvents>.Instance.OnFBLoginFinishedEvent += LoginWithFB;
  18. KGameObjectSingleton<LoginEvents>.Instance.OnGPLoginFinishedEvent += LoginWithGP;
  19. KGameObjectSingleton<LoginEvents>.Instance.OnTWLoginFinishedEvent += LoginWithTW;
  20. KGameObjectSingleton<LoginEvents>.Instance.OnAccountLoginEvent += LoginWithAccount;
  21. KGameObjectSingleton<LoginEvents>.Instance.OnRegisterEvent += RegisterWithEmail;
  22. KGameObjectSingleton<LoginEvents>.Instance.OnApplicationQuitEvent += RemoveAllLoginListeners;
  23. //KGameObjectSingleton<GcmEvents>.Instance.GCMPushRegistered += Instance_GCMPushRegistered;
  24. }
  25.  
  26. public void RemoveAllLoginListeners()
  27. {
  28. loginListenersAdded = true;
  29. // TODO: check if LoginEvents singleton exists and do not create a new one if it doesn't
  30. KGameObjectSingleton<LoginEvents>.Instance.OnLoginEvent -= OnLogin;
  31. KGameObjectSingleton<LoginEvents>.Instance.OnLogoutEvent -= OnLogout;
  32. KGameObjectSingleton<LoginEvents>.Instance.OnLoginCancelledEvent -= TryAutologin;
  33. KGameObjectSingleton<LoginEvents>.Instance.OnLoginFailedEvent -= OnLoginFailed;
  34. KGameObjectSingleton<LoginEvents>.Instance.OnLoginFinishedEvent -= UpdateInformation;
  35. KGameObjectSingleton<LoginEvents>.Instance.OnFBLoginFinishedEvent -= LoginWithFB;
  36. KGameObjectSingleton<LoginEvents>.Instance.OnGPLoginFinishedEvent -= LoginWithGP;
  37. KGameObjectSingleton<LoginEvents>.Instance.OnTWLoginFinishedEvent -= LoginWithTW;
  38. KGameObjectSingleton<LoginEvents>.Instance.OnAccountLoginEvent -= LoginWithAccount;
  39. KGameObjectSingleton<LoginEvents>.Instance.OnRegisterEvent -= RegisterWithEmail;
  40. KGameObjectSingleton<LoginEvents>.Instance.OnApplicationQuitEvent -= RemoveAllLoginListeners;
  41. }
  42.  
  43. public void OnSceneClosed()
  44. {
  45. RemoveAllLoginListeners();
  46. }
  47.  
  48. public void RefreshGSAvailability()
  49. {
  50. if (GS.Available && GSAvailabilityChangedCallBack != null)
  51. {
  52. GSAvailabilityChangedCallBack(GS.Available);
  53. GSAvailabilityChangedCallBack = null;
  54. }
  55. }
  56.  
  57. public void TryAutologin()
  58. {
  59. if (CanAutologin())
  60. {
  61. KGameObjectSingleton<LoginEvents>.Instance.OnLogin((LoginProvider)PlayerPrefs.GetInt(LAST_LOGIN_PROVIDER));
  62. }
  63. else
  64. {
  65. KGameObjectSingleton<LoginEvents>.Instance.OnLogin(LoginProvider.Guest);
  66. }
  67. }
  68.  
  69. public void EnableNotifications()
  70. {
  71. UTNotifications.Manager.Instance.NotificationsEnabled();
  72. UTNotifications.Manager notificationsManager = UTNotifications.Manager.Instance;
  73.  
  74. notificationsManager.OnNotificationClicked += (notification) =>
  75. {
  76. Debug.Log(notification.text + " clicked");
  77. };
  78.  
  79. notificationsManager.OnNotificationsReceived += (receivedNotifications) =>
  80. {
  81. foreach (UTNotifications.ReceivedNotification notification in receivedNotifications)
  82. {
  83. Debug.Log(notification.text + " received");
  84. }
  85. };
  86.  
  87. notificationsManager.OnSendRegistrationId += (providerName, registrationId) =>
  88. {
  89. if (providerName == "GooglePlay")
  90. {
  91. registrationIdGoogle = registrationId;
  92. RegisterForPushNotification();
  93. }
  94. };
  95.  
  96. notificationsManager.Initialize(true);
  97. }
  98.  
  99. public void DisableNotifications()
  100. {
  101. UnregisterForPushNotification();
  102. UTNotifications.Manager.Instance.SetNotificationsEnabled(false);
  103. }
  104.  
  105. public void RegisterForPushNotification()
  106. {
  107. new PushRegistrationRequest().SetPushId(registrationIdGoogle).Send((response) =>
  108. {
  109. if (response.HasErrors)
  110. {
  111. Debug.Log("Something failed when registering push notification Id " + response.Errors);
  112. }
  113. else
  114. {
  115. PlayerPrefs.SetString(PUSH_REGISTRATION_ID_GOOGLE, response.RegistrationId);
  116. Debug.Log("Gamesparks Push Register Successful");
  117. }
  118. });
  119. }
  120.  
  121. public void UnregisterForPushNotification()
  122. {
  123. new LogEventRequest().SetEventKey("UNREGISTER_FROM_PUSH").SetEventAttribute("Registration_Id", PlayerPrefs.GetString(PUSH_REGISTRATION_ID_GOOGLE)).Send((response) =>
  124. {
  125. if (!response.HasErrors)
  126. {
  127. PlayerPrefs.DeleteKey(PUSH_REGISTRATION_ID_GOOGLE);
  128. Debug.Log("Gamesparks Push Unregister Successful");
  129. }
  130. else
  131. {
  132. Debug.Log("Gamesparks Push Register Unsuccessful : " + response.Errors);
  133. }
  134. });
  135. }
  136.  
  137. public bool IsPushNotificationIdSet()
  138. {
  139. return PlayerPrefs.GetString(PUSH_REGISTRATION_ID_GOOGLE) != "";
  140. }
  141.  
  142. public bool CanAutologin()
  143. {
  144. return PlayerPrefs.HasKey(LAST_LOGIN_PROVIDER);
  145. }
  146.  
  147. public void OnLogin(LoginProvider provider)
  148. {
  149. if (provider == LoginProvider.Mail)
  150. {
  151. LoginWithAccount(PlayerPrefs.GetString(ACCOUNT_LOGIN_USERNAME), PlayerPrefs.GetString(ACCOUNT_LOGIN_PASSWORD), false);
  152. }
  153. else if (provider == LoginProvider.Guest)
  154. {
  155. LoginWithAccount(GUEST_USERNAME, GUEST_PASSWORD, true);
  156. }
  157. }
  158.  
  159. void RegisterGuest()
  160. {
  161. Debug.Log("Gamesparks Guest Register");
  162.  
  163. string username = "Guest_" + Guid.NewGuid().ToString();
  164. string password = Guid.NewGuid().ToString();
  165.  
  166. GSRequestData requestData = new GSRequestData().AddString("action", "RegisterGuest").AddBoolean("isGuest", true);
  167.  
  168. new RegistrationRequest().SetUserName(username).SetPassword(password).SetDisplayName("Guest").SetScriptData(requestData).Send((response) =>
  169. {
  170. if (response.HasErrors)
  171. {
  172. Debug.Log("Something failed when registering as Guest " + response.Errors);
  173. }
  174. else
  175. {
  176. Debug.Log("Gamesparks Guest Register Successful");
  177.  
  178. LoginWithAccount(username, password, true);
  179. }
  180. });
  181. }
  182.  
  183. public void LoginWithFB(string FBAccessToken)
  184. {
  185. Debug.Log("Gamesparks Facebook login with token : " + FBAccessToken);
  186.  
  187. OnVerifyingAccount();
  188. attemptedLoginProvider = LoginProvider.Facebook;
  189. new FacebookConnectRequest().SetAccessToken(FBAccessToken).SetDoNotLinkToCurrentPlayer(true).SetSyncDisplayName(true).Send((response) =>
  190. {
  191. if (response.HasErrors)
  192. {
  193. Debug.LogError("Something failed when connecting with Facebook " + response.Errors);
  194.  
  195. KGameObjectSingleton<LoginEvents>.Instance.OnLoginFailed();
  196. }
  197. else
  198. {
  199. Debug.Log("Gamesparks Facebook Login Successful");
  200.  
  201. JSONNode data = JSONNode.Parse(response.JSONString);
  202. newPlayer = data["newPlayer"].AsBool;
  203.  
  204. UpdateInformation();
  205.  
  206. previousLoginProvider = loginProvider;
  207. loginProvider = LoginProvider.Facebook;
  208. PlayerPrefs.SetInt(LAST_LOGIN_PROVIDER, Convert.ToInt32(loginProvider));
  209. }
  210. OnVerifyingAccountFinished();
  211. });
  212. }
  213.  
  214. public void LoginWithGP(string token)
  215. {
  216. Debug.Log("Gamesparks Google login with token : " + token);
  217.  
  218. OnVerifyingAccount();
  219. attemptedLoginProvider = LoginProvider.Google;
  220. new GooglePlusConnectRequest().SetAccessToken(token).SetDoNotLinkToCurrentPlayer(true).SetSyncDisplayName(true).Send((response) =>
  221. {
  222. if (response.HasErrors)
  223. {
  224. Debug.LogError("Something failed when connecting with Google Plus " + response.Errors);
  225.  
  226. KGameObjectSingleton<LoginEvents>.Instance.OnLoginFailed();
  227. }
  228. else
  229. {
  230. Debug.Log("Gamesparks Google Plus Login Successful");
  231.  
  232. JSONNode data = JSONNode.Parse(response.JSONString);
  233. newPlayer = data["newPlayer"].AsBool;
  234.  
  235. UpdateInformation();
  236.  
  237. previousLoginProvider = loginProvider;
  238. loginProvider = LoginProvider.Google;
  239. PlayerPrefs.SetInt(LAST_LOGIN_PROVIDER, Convert.ToInt32(loginProvider));
  240. }
  241. OnVerifyingAccountFinished();
  242. });
  243. }
  244.  
  245. public void LoginWithTW(string token, string secret)
  246. {
  247. Debug.Log("Gamesparks Twitter login with token : " + token + Environment.NewLine + "secret " + secret);
  248.  
  249. OnVerifyingAccount();
  250. attemptedLoginProvider = LoginProvider.Twitter;
  251. new TwitterConnectRequest().SetAccessToken(token).SetDoNotLinkToCurrentPlayer(true).SetAccessSecret(secret).SetSyncDisplayName(true).Send((response) =>
  252. {
  253. if (response.HasErrors)
  254. {
  255. Debug.Log("Something failed when connecting with Twitter " + response.Errors);
  256.  
  257. KGameObjectSingleton<LoginEvents>.Instance.OnLoginFailed();
  258. }
  259. else
  260. {
  261. Debug.Log("Gamesparks Twitter Login Successful");
  262.  
  263. JSONNode data = JSONNode.Parse(response.JSONString);
  264. newPlayer = data["newPlayer"].AsBool;
  265.  
  266. UpdateInformation();
  267.  
  268. previousLoginProvider = loginProvider;
  269. loginProvider = LoginProvider.Twitter;
  270. PlayerPrefs.SetInt(LAST_LOGIN_PROVIDER, Convert.ToInt32(loginProvider));
  271. }
  272. OnVerifyingAccountFinished();
  273. });
  274. }
  275.  
  276. public void LoginWithAccount(string username, string password, bool isGuest)
  277. {
  278. Debug.Log("Gamesparks Begin Account Login with : " + username);
  279.  
  280. if (!isGuest)
  281. OnVerifyingAccount();
  282.  
  283. GSRequestData requestData = new GSRequestData().AddString("action", "AuthenticateGuest").AddBoolean("isGuest", isGuest);
  284. attemptedLoginProvider = LoginProvider.Mail;
  285. new AuthenticationRequest().SetUserName(username).SetPassword(password).SetScriptData(requestData).Send((response) =>
  286. {
  287. if (response.HasErrors)
  288. {
  289. Debug.Log("Something failed when connecting with Account " + response.Errors);
  290.  
  291. if (!isGuest)
  292. KGameObjectSingleton<LoginEvents>.Instance.OnLoginFailed();
  293. }
  294. else
  295. {
  296. Debug.Log("Gamesparks Account Login Successful");
  297.  
  298. JSONNode data = JSONNode.Parse(response.JSONString);
  299. newPlayer = data["newPlayer"].AsBool;
  300.  
  301. UpdateInformation();
  302.  
  303. if (!isGuest)
  304. {
  305. previousLoginProvider = loginProvider;
  306. loginProvider = LoginProvider.Mail;
  307. }
  308. else
  309. {
  310. previousLoginProvider = loginProvider;
  311. loginProvider = LoginProvider.Guest;
  312. }
  313.  
  314. // save user credentials for autologin
  315. PlayerPrefs.SetInt(LAST_LOGIN_PROVIDER, Convert.ToInt32(loginProvider));
  316. PlayerPrefs.SetString(ACCOUNT_LOGIN_USERNAME, username);
  317. PlayerPrefs.SetString(ACCOUNT_LOGIN_PASSWORD, password);
  318. }
  319. if (!isGuest)
  320. OnVerifyingAccountFinished();
  321. });
  322. }
  323.  
  324. public void RegisterWithEmail(string email, string password, string displayName)
  325. {
  326. new RegistrationRequest().SetUserName(email).SetPassword(password).SetDisplayName(displayName).Send((response) =>
  327. {
  328. if (response.HasErrors)
  329. {
  330. Debug.Log("Something failed when registering with Email " + response.Errors);
  331.  
  332. KGameObjectSingleton<LoginEvents>.Instance.OnLoginFailed();
  333. }
  334. else
  335. {
  336. Debug.Log("Gamesparks Email Register Successful");
  337. AttachProfilePictureLink(true);
  338. LoginWithAccount(email, password, false);
  339. }
  340. });
  341. }
  342.  
  343. void OnLogout(LoginProvider _loginProvider)
  344. {
  345. if (_loginProvider == LoginProvider.Mail)
  346. {
  347. PlayerPrefs.DeleteKey(ACCOUNT_LOGIN_USERNAME);
  348. PlayerPrefs.DeleteKey(ACCOUNT_LOGIN_PASSWORD);
  349. }
  350. PlayerPrefs.DeleteKey(LAST_LOGIN_PROVIDER);
  351.  
  352. new EndSessionRequest().Send((response) =>
  353. {
  354. if (response.HasErrors)
  355. {
  356. Debug.Log("Something failed when logging out " + response.Errors);
  357. }
  358. else
  359. {
  360. Debug.Log("Gamesparks Logout Successful");
  361.  
  362. GS.GSPlatform.AuthToken = "";
  363. GS.GSPlatform.UserId = "";
  364.  
  365. previousLoginProvider = loginProvider;
  366. loginProvider = LoginProvider.None;
  367.  
  368. if (UsersAllController.Instance != null)
  369. {
  370. UsersAllController.Instance.RefreshFollowingUsers(false);
  371. }
  372.  
  373. KGameObjectSingleton<LoginEvents>.Instance.OnLogoutFinished();
  374.  
  375. KGameObjectSingleton<LoginEvents>.Instance.OnLogin(LoginProvider.Guest);
  376. }
  377. });
  378. }
  379.  
  380. public void UpdateInformation()
  381. {
  382. new AccountDetailsRequest().Send((response) =>
  383. {
  384. accountResponse = response;
  385. if (response.HasErrors)
  386. {
  387. Debug.Log("Something failed when connecting " + response.Errors);
  388.  
  389. if (loginProvider != LoginProvider.None && loginProvider != LoginProvider.Guest)
  390. KGameObjectSingleton<LoginEvents>.Instance.OnLoginFailed();
  391. }
  392. else
  393. {
  394. Debug.Log("Gamesparks Login Successful");
  395.  
  396. currentPlayerData = new PlayerData();
  397. JSONNode data = JSONNode.Parse(response.JSONString);
  398. currentPlayerData = new PlayerData();
  399. currentPlayerData.Id = data["userId"].Value;
  400. currentPlayerData.Name = data["displayName"].Value;
  401. currentPlayerData.ProfilePictureLink = data["scriptData"]["profilePictureLink"].Value;
  402. currentPlayerData.NewPlayer = newPlayer;
  403.  
  404. if (loginProvider != LoginProvider.None && loginProvider != LoginProvider.Guest)
  405. {
  406. AttachLastLoginProvider();
  407. if (loginProvider != LoginProvider.Mail)
  408. {
  409. AttachProfilePictureLink(false);
  410. }
  411. ManageSpentTimeInsideApp();
  412. if (UsersAllController.Instance != null)
  413. {
  414. UsersAllController.Instance.RefreshFollowingUsers(true);
  415. }
  416. }
  417.  
  418. KGameObjectSingleton<LoginEvents>.Instance.OnUserProfileUpdated();
  419. }
  420. });
  421. }
  422.  
  423. public bool soomlaSecondAttempt;
  424. public void OnLoginFailed()
  425. {
  426. if ((SoomlaLoginManager.previousLoginRequest == LoginProvider.Google || SoomlaLoginManager.previousLoginRequest == LoginProvider.Twitter) && !soomlaSecondAttempt)
  427. {
  428. if (SoomlaLoginManager.previousLoginRequest == LoginProvider.Twitter)
  429. {
  430. AllWelcomeScreen.Instance.LoginWithTwitter ();
  431. }
  432.  
  433. if (SoomlaLoginManager.previousLoginRequest == LoginProvider.Google)
  434. {
  435. AllWelcomeScreen.Instance.LoginWithGooglePlus ();
  436. }
  437.  
  438. soomlaSecondAttempt = true;
  439. }
  440. else
  441. {
  442. if (attemptedLoginProvider == LoginProvider.Mail)
  443. {
  444. loginFailedPopUp.ShowPopUp();
  445. }
  446.  
  447. KGameObjectSingleton<LoginEvents>.Instance.OnLogin(LoginProvider.Guest);
  448. soomlaSecondAttempt = false;
  449. }
  450. }
  451.  
  452. public void OnVerifyingAccount()
  453. {
  454. if (verifyingAccountPopUp != null)
  455. {
  456. verifyingAccountPopUp.ShowPopUp();
  457. }
  458. }
  459.  
  460. public void OnVerifyingAccountFinished()
  461. {
  462. if (verifyingAccountPopUp != null)
  463. {
  464. verifyingAccountPopUp.HidePopUp();
  465. }
  466. }
  467.  
  468. private void AttachLastLoginProvider()
  469. {
  470. new LogEventRequest().SetEventKey("ATTACH_LAST_LOGIN_PROVIDER").SetEventAttribute("LAST_LOGIN_PROVIDER", loginProvider.ToString()).Send((response) => {
  471. if (!response.HasErrors)
  472. {
  473. Debug.Log("Succeded to attach last login provider");
  474. }
  475. else
  476. {
  477. Debug.Log("Fail to attach last login providerk" + response.Errors);
  478. }
  479. });
  480. }
  481.  
  482. private void AttachProfilePictureLink(bool newMailRegistered)
  483. {
  484. string link = "";
  485. if (loginProvider == LoginProvider.Facebook)
  486. {
  487. link = accountResponse.ExternalIds.GetString("FB").ToString();
  488. }
  489. else if (loginProvider == LoginProvider.Twitter)
  490. {
  491. link = SoomlaProfile.GetStoredUserProfile(Provider.TWITTER).AvatarLink;
  492. }
  493. else if (loginProvider == LoginProvider.Google)
  494. {
  495. link = SoomlaProfile.GetStoredUserProfile(Provider.GOOGLE).AvatarLink;
  496. }
  497. else if (newMailRegistered)
  498. {
  499. int randomColor = UnityEngine.Random.Range(0, mailBgColors.Length);
  500. link = mailBgColors[randomColor].r + " " + mailBgColors[randomColor].g + " " + mailBgColors[randomColor].b;
  501. }
  502. if (!link.Equals(""))
  503. {
  504. new LogEventRequest().SetEventKey("ATTACH_PROFILE_PICTURE_LINK").SetEventAttribute("LINK", link).Send((response) =>
  505. {
  506. if (!response.HasErrors)
  507. {
  508. Debug.Log("Succeded to attach profile picture link");
  509. }
  510. else
  511. {
  512. Debug.Log("Fail to attach profile picture link" + response.Errors);
  513. }
  514. });
  515. }
  516. }
  517.  
  518. private void ManageSpentTimeInsideApp()
  519. {
  520. showReward = false;
  521. new LogEventRequest().SetEventKey("MANAGE_SPENT_TIME_INSIDE_APP").Send((response) =>
  522. {
  523. if (!response.HasErrors)
  524. {
  525. Debug.Log("Succeded to manage spent time inside app: " + response.JSONString);
  526. showReward = JSONNode.Parse(response.JSONString)["scriptData"]["giveReward"].AsBool;
  527. daysInGame = JSONNode.Parse(response.JSONString)["scriptData"]["daysInGame"].AsInt;
  528. }
  529. else
  530. {
  531. Debug.Log("Fail to manage spent time inside app" + response.Errors);
  532. }
  533. });
  534. }
  535.  
  536. public bool isLoggedIn()
  537. {
  538. if (loginProvider == LoginProvider.None)
  539. {
  540. return false;
  541. }
  542. return true;
  543. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement