Advertisement
Guest User

logincontroller

a guest
Feb 6th, 2017
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 18.62 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Text.RegularExpressions;
  5. using Facebook.MiniJSON;
  6. using SimpleJSON;
  7. using UnityEngine.UI;
  8. using ExitGames.Client.Photon;
  9.  
  10. public class LoginController : MonoBehaviour {
  11.  
  12.     bool isLoginUser;
  13.     bool isSignin;
  14.     public AudioSource audioSource;
  15.     public TextController[] listText;
  16.     public Texture2D defaultProfileImage;
  17.     public InputField username;
  18.     public InputField password;
  19.     public Toggle rememberMe;
  20.    
  21.  
  22.     IEnumerator ChangeScene(int index){
  23.         Loading.self.Open();
  24.         yield return null;
  25.         Application.LoadLevelAsync(index);
  26. //      Application.LoadLevelAsync(index);
  27. //      while(!asyncOp.isDone){
  28. //          Loading.self.SetProgress(asyncOp.progress);
  29. //          yield return null;
  30. //      }
  31.     }
  32.  
  33.     public void LoginFacebook(){
  34.         audioSource.Play();
  35.         Loading.self.Open();
  36.         #if UNITY_EDITOR
  37.         App.userId = "U015060000000001";
  38.         StartCoroutine(DoMockUpSignIn());
  39. //      StartCoroutine(ChangeScene(App.SCENE_MENU));
  40.         #else
  41. //      if(FB.IsLoggedIn){
  42. //          OnLoggedIn();
  43. //      }else{
  44. //          FB.Login("email", LoginCallback);
  45. //      }
  46. //      FB.Login("email", LoginCallback);
  47.         FB.Login("email", LoginCallback);
  48.         #endif
  49.     }
  50.  
  51.     public void LoginGuest(){
  52.         audioSource.Play();
  53.         if(username.text!=null && username.text.Length>0){
  54.             if(password.text!=null && password.text.Length>0){
  55.                 StartCoroutine(DoGuestSignIn());
  56.             }else{
  57.                 // Toast.self.Show("Please insert password");
  58.                 Toast.self.Show("请把密码写下");
  59.             }
  60.         }else{
  61.             // Toast.self.Show("Please insert username");
  62.             Toast.self.Show("请把用户名写下");
  63.         }
  64. //      StartCoroutine(ChangeScene(App.SCENE_MENU));
  65.     }
  66.  
  67.     public void Setting(){
  68.         audioSource.Play();
  69.         SettingController.fromGame = false;
  70.         SettingController.ROOM_ID = null;
  71.         StartCoroutine(ChangeScene(App.SCENE_SETTING));
  72.     }
  73.  
  74.     public void Support(){
  75.         audioSource.Play();
  76.         StartCoroutine(ChangeScene(App.SCENE_SUPPORT));
  77.     }              
  78.  
  79.     public void Register(){
  80.         audioSource.Play ();
  81.         StartCoroutine (ChangeScene (App.SCENE_REGISTER));
  82.     }
  83.  
  84.     void Awake(){
  85.         App.profileImage = null;
  86.         FB.Init(SetInit, OnHideUnity);
  87.         PhotonNetwork.Disconnect();
  88.         PhotonNetwork.ConnectUsingSettings("1.0");
  89.  
  90.         App.LoadBonus();
  91.         App.LoadRoulete();
  92.         App.LoadLoginData();
  93.         if(App.loginData.rememberMe){
  94.             username.text = App.loginData.username;
  95.             password.text = App.loginData.password;
  96.             rememberMe.isOn = true;
  97.         }
  98. //      App.LoadDailyMission();
  99.         SetLanguage (2);
  100.     }
  101.     void Update()
  102.     {
  103.         //TexasClient.Service();
  104.         //Debug.Log("Server availability : " + PhotonNetwork.networkingPeer.server);
  105.         // Contoh Penggunaan TexasClient Operation
  106.         // Contoh OperationRequest ke Master Server
  107.         //TexasClient.OpTestRequest();
  108.     }
  109.  
  110.  
  111.  
  112.     private void SetInit()                                                                      
  113.     {
  114.         try{
  115.             Debug.Log("SetInit");                                                                  
  116.             enabled = true; // "enabled" is a property inherited from MonoBehaviour                  
  117.             if(FB.IsLoggedIn){  
  118.                 //          OnLoggedIn();    
  119.                
  120.             }  
  121.             Loading.self.Close();
  122.         }catch(UnityException ex){
  123.             Debug.Log(ex.ToString());
  124.         }
  125.     }                                                                                            
  126.    
  127.     private void OnHideUnity(bool isGameShown)                                                  
  128.     {
  129.         if (!isGameShown)                                                                        
  130.         {                                                                                        
  131.             // pause the game - we will need to hide                                            
  132.             Time.timeScale = 0;                                                                  
  133.         }                                                                                        
  134.         else                                                                                    
  135.         {                                                                                        
  136.             // start the game back up - we're getting focus again                                
  137.             Time.timeScale = 1;                                                                  
  138.         }                                                                                        
  139.     }    
  140.    
  141.     void LoginCallback(FBResult result)                                                  
  142.     {
  143.         if (FB.IsLoggedIn){                                                                                      
  144.             OnLoggedIn();                                                                      
  145.         }else{
  146.             Loading.self.Close();
  147.             if(result.Error!=null){
  148.                 Debug.Log("Login facebook failed"+result.Error);
  149.                 Toast.self.Show("Login facebook failed");
  150.             }else{
  151. //              Toast.self.Show("Login facebook failed "+result.Text);
  152.             }
  153.         }                                                                    
  154.     }                                                                                          
  155.    
  156.     void OnLoggedIn()                                                                          
  157.     {              
  158.         Loading.self.Open();
  159.         FB.API("/me?fields=id,email,name", Facebook.HttpMethod.GET, APICallback);
  160.     }
  161.    
  162.     void APICallback(FBResult result)                                                                                              
  163.     {                                                                                                          
  164.         if (result.Error != null)                                                                                                  
  165.         {                                                                                                                                                                                                                
  166.             // Let's just try again                                                                                                
  167.             FB.API("/me?fields=id,email,name", Facebook.HttpMethod.GET, APICallback);    
  168.             return;                                                                                                                
  169.         }else{
  170.             //
  171.         }
  172.         Dictionary<string,object> profile = (Dictionary<string, object>)Json.Deserialize(result.Text) ;    
  173.         App.emailAddress = profile["email"].ToString();
  174.         App.userName = profile["name"].ToString();
  175. //      Toast.self.Show("facebook id static : "+FB.UserId+","+"facebook id result : "+profile["id"].ToString());
  176.         App.facebookId = FB.UserId;
  177.         StartCoroutine(DoSignInFacebook());
  178.     }    
  179.  
  180.     public void SetLanguage(int index){
  181.         Camera.main.GetComponent<AudioSource>().Play();
  182.         App.language = index;
  183.         foreach(TextController tc in listText){
  184.             tc.SetLanguage();
  185.         }
  186.  
  187.     }  
  188.  
  189.     IEnumerator DoImage(string url){
  190.         WWW www;
  191. //      www = new WWW(url);
  192. //      yield return www;
  193. //      if(www.error == null){
  194. //          if(www.texture != null){
  195. //              App.imageUrl = App.BASIC_URL_IMAGE+App.userId+".png";
  196. //              App.profileImage = www.texture;
  197. //          }
  198. //      }
  199.         if(App.profileImage == null){
  200.             Texture2D texture;
  201. //          if(App.facebookId!=""){
  202. //              App.imageUrl = "https://graph.facebook.com/" + App.facebookId + "/picture?type=square&return_ssl_resources=1" + "&access_token=" + FB.AccessToken;
  203.                 www = new WWW(url);
  204.                 yield return www;
  205.                 texture = www.texture;
  206.             if(texture!=null){
  207. //              Toast.self.Show("texture isn't null");
  208.                 App.profileImage = texture;
  209.                 byte[] bytes = texture.EncodeToPNG();
  210. //                  WWWForm form = new WWWForm();
  211. //                  form.AddField("user_id",App.userId);
  212. //                  form.AddBinaryData("file",bytes,App.userId+".png","image/png");
  213. //                  www = new WWW(App.BASIC_URL_USER+"upload_photo_profile",form);
  214. //                  yield return www;
  215. //                  if(www.error == null){
  216. //                      App.imageUrl = App.BASIC_URL_IMAGE+App.userId+".png";
  217. //                  }
  218.             }else{
  219. //              Toast.self.Show("texture is null");
  220.             }
  221. //          }
  222.         }
  223.         StartCoroutine(ChangeScene(App.SCENE_MENU));
  224.     }
  225.    
  226.     // Done Migrated
  227.     IEnumerator DoProfile(){
  228.  
  229.         TexasClient.Instance.OpProfileRequest(App.userId);
  230.         yield return new WaitUntil(() => TexasClient.Instance.WWWTextProfileResponse != "");
  231.  
  232.         if(TexasClient.Instance.WWWErrorProfileResponse == null){
  233.             string result = TexasClient.Instance.WWWTextProfileResponse;
  234.             Debug.Log("Operation Get Profile Result : "+result);
  235.             //          var jsonObject = JSON.Parse(result);
  236.         }else{
  237.             //
  238.         }
  239.         StartCoroutine(ChangeScene(App.SCENE_MENU));
  240.     }
  241.    
  242.     // Done Migrated
  243.     IEnumerator DoProfileFacebook(){
  244.  
  245.         TexasClient.Instance.OpProfileRequest(App.userId);
  246.         yield return new WaitUntil(() => TexasClient.Instance.WWWTextProfileResponse != "");
  247.  
  248.         if (TexasClient.Instance.WWWErrorProfileResponse == null){
  249.             string result = TexasClient.Instance.WWWTextProfileResponse;
  250.             Debug.Log("Operation Get Facebook Profile Result : " + result);
  251.         }
  252.         else{
  253.             //
  254.         }
  255.  
  256.         WWW www;
  257.         WWWForm form = new WWWForm();
  258.         www = null;
  259.         form = null;
  260.         if(App.facebookId!=""){
  261.             App.imageUrl = "https://graph.facebook.com/" + App.facebookId + "/picture?type=square&return_ssl_resources=1" + "&access_token=" + FB.AccessToken;
  262.             www = new WWW(App.imageUrl);
  263.             yield return www;
  264.             App.profileImage = www.texture;
  265.         }
  266.         StartCoroutine(ChangeScene(App.SCENE_MENU));
  267.     }
  268.     // Done Migrated
  269.     IEnumerator DoMockUpSignIn(){
  270.  
  271.         TexasClient.Instance.OpFacebookSignInRequest("U15060000000002", "andrianHerlino");
  272.         // Wait until response is coming through TexasClient HandleResponse
  273.         yield return new WaitUntil(() => TexasClient.Instance.WWWTextFacebookSignInResponse != "");
  274.  
  275.  
  276.         if(TexasClient.Instance.WWWErrorFacebookSignInResponse == null){
  277.             string result = TexasClient.Instance.WWWTextFacebookSignInResponse;
  278.             Debug.Log("mock up "+result);
  279.             try{
  280.                 var jsonObject = JSON.Parse(result);
  281.                 if(!jsonObject["return"].AsBool){
  282.                     StartSignUpFacebook();
  283.                 }else{
  284.                     jsonObject = jsonObject["data"].AsObject;
  285.                     App.userName = jsonObject["USER_NAME"];
  286.                     App.status = jsonObject["USER_STATUS"];
  287.                     App.userId = jsonObject["USER_ID"];
  288.                     Debug.Log("USER ID : "+App.userId);
  289.                     App.chip = jsonObject["USER_CHIPS"].AsLong;
  290.                     App.coin = jsonObject["USER_MONEY"].AsLong;
  291.                     App.point = jsonObject["POINTS_USER"].AsInt;
  292.                     Brightness.self.SetBrightness(jsonObject["BRIGHTNESS_SETTING"].AsInt);
  293.                     App.SetVolume(jsonObject["VOLUME_SETTING"].AsInt);
  294.                     if(jsonObject["SIT_AUTOMATIC_FLAG"].AsInt==2){
  295.                         SettingController.isSitAutomatic = true;
  296.                     }else{
  297.                         SettingController.isSitAutomatic = false;
  298.                     }
  299.                     App.level = jsonObject["USER_LEVEL"].AsInt;
  300. //                  StartCoroutine(DoImage());
  301.                 }
  302.             }catch(UnityException ex){
  303.                 Debug.Log(ex.ToString());
  304. //              Toast.self.Show(ex.ToString());
  305.                 Loading.self.Close();
  306.             }
  307.         }else{
  308.             Debug.Log("mock up "+ TexasClient.Instance.WWWErrorFacebookSignInResponse);
  309. //          Toast.self.Show(www.error);
  310.             Loading.self.Close();
  311.         }
  312.     }
  313.  
  314.     // Done Migrated
  315.     IEnumerator DoGuestSignIn(){
  316.         Loading.self.Open();
  317.         // Call Operation Guest Sign In from TexasClient
  318.         TexasClient.Instance.OpGuestSignInRequest(username.text, password.text);
  319.  
  320.         // I dont know what this is for (code dari fungsi yang dulu)
  321.         Debug.Log(App.BASIC_URL_USER);
  322.  
  323.         // Wait until response is coming through TexasClient HandleResponse
  324.         yield return new WaitUntil(() => TexasClient.Instance.WWWTextGuestSignInResponse != "");
  325.  
  326.         // Masuk ke fungsi bawaan dan mengganti WWW.text menjadi string response (more light)
  327.         // selain itu tidak ada yg dirubah
  328.         if (TexasClient.Instance.WWWErrorGuestSignInResponse == null){
  329.             try{
  330.                 Debug.Log(TexasClient.Instance.WWWTextGuestSignInResponse);
  331.                 string result = TexasClient.Instance.WWWTextGuestSignInResponse;
  332.                 var jsonObject = JSON.Parse(result);
  333.                 if(jsonObject["return"].AsBool){
  334.                     jsonObject = jsonObject["data"].AsObject;
  335. //                  if(jsonObject["USER_BANNED"].AsInt == 1){
  336. //                      Toast.self.Show("You are banned, contact admin!");
  337. //                  }else{
  338. //                      if(jsonObject["IS_ONLINE"].AsInt == 1){
  339. //                          Toast.self.Show("You already log in in another device!");
  340. //                      }else{
  341.                     App.userId = jsonObject["USER_ID"];
  342.                     Debug.Log("USER ID : "+App.userId);
  343.                     App.userName = jsonObject["USER_NAME"];
  344.                     App.status = jsonObject["USER_STATUS"];
  345.                     App.chip = jsonObject["USER_CHIPS"].AsInt;
  346.                     App.coin = jsonObject["USER_MONEY"].AsInt;
  347.                     App.point = jsonObject["POINTS_USER"].AsInt;
  348.                     App.userTimer = jsonObject["TIMER_SETTING"].AsFloat; //faisal 13012017
  349.                     Brightness.self.SetBrightness(jsonObject["BRIGHTNESS_SETTING"].AsInt);
  350.                     App.SetVolume(jsonObject["VOLUME_SETTING"].AsInt);
  351.                     App.address = jsonObject["USER_ADDRESS"];
  352.                     if(jsonObject["SIT_AUTOMATIC_FLAG"].AsInt==2){
  353.                         SettingController.isSitAutomatic = true;
  354.                     }else{
  355.                         SettingController.isSitAutomatic = false;
  356.                     }
  357.                     if(jsonObject["VIBRATE_SETTING"].AsInt==2){
  358.                         SettingController.isVibrating = true;
  359.                     }else{
  360.                         SettingController.isVibrating = false;
  361.                     }
  362.                     App.loginData.rememberMe = rememberMe.isOn;
  363.                     if(rememberMe.isOn){
  364.                         App.loginData.username = App.userName;
  365.                         App.loginData.password = password.text;
  366.                         App.SaveLoginData();
  367.                     }
  368.                     if(jsonObject["PHOTO_URL"] == null){
  369.                         App.profileImage = defaultProfileImage;
  370.                         App.imageUrl = null;
  371.                     }else{
  372.                         App.imageUrl = App.BASIC_URL_IMAGE + jsonObject["PHOTO_URL"];
  373.                         //For Testing only, need response 3
  374.                         StartCoroutine(DoProfile());
  375.  
  376.  
  377.                         StartCoroutine(DoImage(App.imageUrl));
  378.                     }
  379.  
  380.                     //faisal 01012017 set name player = user id + client ip
  381.                     PhotonNetwork.player.name = App.userId+"|"+App.remoteIP;
  382.                     Debug.Log("Photon Player name is "+PhotonNetwork.player.name);
  383.  
  384. //                  StartCoroutine(ChangeScene(App.SCENE_MENU));
  385. //                  StartCoroutine(DoImage());
  386. //                      }
  387. //                  }
  388.                 }else{
  389.                     Toast.self.Show(jsonObject["messages"]);
  390.                 }
  391.             }catch(UnityException ex){
  392.                 Debug.Log(ex.ToString());
  393.                 Loading.self.Close();
  394.                 Toast.self.Show(ex.ToString());
  395.             }
  396.         }else{
  397.             Debug.Log(TexasClient.Instance.WWWErrorGuestSignInResponse);
  398. //          Toast.self.Show(www.error);
  399.         }
  400.         Loading.self.Close();
  401.     }
  402.     // Done Migrated
  403.     IEnumerator DoSignInFacebook(){
  404.  
  405.         TexasClient.Instance.OpFacebookSignInRequest(App.userName, App.facebookId);
  406.         // Wait until response is coming through TexasClient HandleResponse
  407.         yield return new WaitUntil(() => TexasClient.Instance.WWWTextFacebookSignInResponse != "");
  408.  
  409.  
  410. //      if(www.responseHeaders.Count > 0) {
  411. //          foreach(KeyValuePair<string, string> entry in www.responseHeaders) {
  412. //              debugText.text += entry.Value + "=" + entry.Key;
  413. ////                Debug.Log(entry.Value + "=" + entry.Key);
  414. //          }
  415. //      }else{
  416. //          debugText.text ="response headers is 0";
  417. //      }
  418.         if(TexasClient.Instance.WWWErrorFacebookSignInResponse == null){
  419.             string result = TexasClient.Instance.WWWTextFacebookSignInResponse;
  420.             try{
  421.                 var jsonObject = JSON.Parse(result);
  422.                 if(!jsonObject["return"].AsBool){
  423.                     StartSignUpFacebook();
  424.                 }else{
  425.                     jsonObject = jsonObject["data"].AsObject;
  426.                     App.userName = jsonObject["USER_NAME"];
  427.                     App.status = jsonObject["USER_STATUS"];
  428.                     App.userId = jsonObject["USER_ID"];
  429.                     App.chip = jsonObject["USER_CHIPS"].AsInt;
  430.                     App.coin = jsonObject["USER_MONEY"].AsInt;
  431.                     App.point = jsonObject["POINTS_USER"].AsInt;
  432.                     Brightness.self.SetBrightness(jsonObject["BRIGHTNESS_SETTING"].AsInt);
  433.                     App.SetVolume(jsonObject["VOLUME_SETTING"].AsInt);
  434.                     App.address = jsonObject["USER_ADDRESS"];
  435.                     App.userTimer = jsonObject["TIMER_SETTING"].AsFloat; //faisal 13012017
  436.                     if(jsonObject["SIT_AUTOMATIC_FLAG"].AsInt==2){
  437.                         SettingController.isSitAutomatic = true;
  438.                     }else{
  439.                         SettingController.isSitAutomatic = false;
  440.                     }
  441.                     if(jsonObject["VIBRATE_SETTING"].AsInt==2){
  442.                         SettingController.isVibrating = true;
  443.                     }else{
  444.                         SettingController.isVibrating = false;
  445.                     }
  446. //                  if(jsonObject["NEWS_FLAG"].AsInt==2){
  447. //                      SettingController.isNews = true;
  448. //                  }else{
  449. //                      SettingController.isNews = false;
  450. //                  }
  451.                     App.level = jsonObject["USER_LEVEL"].AsInt;
  452.                    
  453. //                  StartCoroutine(DoImage());
  454.                 }
  455.             }catch(UnityException ex){
  456. //              Toast.self.Show("server error : "+ex.ToString());
  457.                 Debug.Log("server error : "+ex.ToString());
  458.                 Loading.self.Close();
  459.             }
  460.         }else{
  461.             Debug.Log("server error : "+ TexasClient.Instance.WWWErrorFacebookSignInResponse);
  462. //          Toast.self.Show("server error : "+www.error);
  463.             Loading.self.Close();
  464.         }
  465.     }
  466.    
  467.     void StartSignUpFacebook(){
  468.         StartCoroutine(DoSignUpFacebook());
  469.     }  
  470.    
  471.     // Done Migrated
  472.     IEnumerator DoSignUpFacebook(){
  473.  
  474.         TexasClient.Instance.OpFacebookSignUpRequest(App.userName, App.facebookId, App.emailAddress, App.facebookId);
  475.         yield return new WaitUntil(() => TexasClient.Instance.WWWTextFacebookSignUpResponse != "");
  476.  
  477.         if(TexasClient.Instance.WWWErrorFacebookSignUpResponse == null){
  478.             string result = TexasClient.Instance.WWWTextFacebookSignUpResponse;
  479.             try{
  480.                 var jsonObject = JSON.Parse(result);
  481.                 if(jsonObject["return"].AsBool){
  482.                     StartCoroutine(DoSignInFacebook());
  483.                 }else{
  484.                     result = jsonObject["error"];
  485.                     Debug.Log("server error : "+result+" "+App.userName+" "+App.facebookId+" "+App.emailAddress);
  486.                     Toast.self.Show("server error");
  487.                     Loading.self.Close();
  488.                 }
  489.             }catch(UnityException ex){
  490.                 result = ex.ToString();
  491.                 Debug.Log(result);
  492.                 Toast.self.Show("server error");
  493.                 Loading.self.Close();
  494.             }
  495.         }else{
  496.             Debug.Log("server error : "+ TexasClient.Instance.WWWErrorFacebookSignUpResponse);
  497.             Toast.self.Show("server error");
  498.             Loading.self.Close();
  499.         }
  500.     }
  501.  
  502.     public void ForgotPassword(){
  503.         if(username.text!=null && username.text.Length>0){
  504.             StartCoroutine(DoForgotPassword());
  505.         }else{
  506.             Toast.self.Show("Please input username first");
  507.         }
  508.     }
  509.  
  510.     // Done Migrated
  511.     IEnumerator DoForgotPassword(){
  512.         Loading.self.Open();
  513.  
  514.         TexasClient.Instance.OpForgotPasswordRequest(username.text);
  515.         yield return new WaitUntil(() => TexasClient.Instance.WWWTextForgotPasswordResponse != "");
  516.  
  517.         if(TexasClient.Instance.WWWErrorForgotPasswordResponse == null){
  518.             string result = TexasClient.Instance.WWWTextForgotPasswordResponse;
  519.             Debug.Log("Forgot Password Operation result : " + result);
  520.             var jsonObject = JSON.Parse(result);
  521.             Toast.self.Show(jsonObject["messages"]);
  522.         }else{
  523.             Toast.self.Show(TexasClient.Instance.WWWErrorForgotPasswordResponse);
  524.         }
  525.         Loading.self.Close();
  526.     }
  527.  
  528. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement