Advertisement
Guest User

Untitled

a guest
Jan 4th, 2018
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.70 KB | None | 0 0
  1.     public void Login(Action doneCallback)
  2.     {
  3.         new LoginTask((result) =>
  4.         {
  5.             playFabId = result.PlayFabId;
  6.             newlyPlayer = result.NewlyPlayer;
  7.             profile = ParseProfile(result.ProfileData);
  8.             doneCallback();
  9.         }, HandleError).Run();
  10.     }
  11.    
  12.     private class LoginTask
  13.     {
  14.         public Action<LoginTask> successCallback;
  15.         public Action<string, string> errorCallback;
  16.  
  17.         public string PlayFabId { get; private set; }
  18.         public bool NewlyPlayer { get; private set; }
  19.         public Dictionary<string, UserDataRecord> ProfileData { get; private set; }
  20.  
  21.         public LoginTask(Action<LoginTask> successCallback, Action<string, string> errorCallback)
  22.         {
  23.             this.successCallback = successCallback;
  24.             this.errorCallback = errorCallback;
  25.         }
  26.  
  27.         public void Run()
  28.         {
  29.             GoogleAuthenticate();
  30.         }
  31.         private void GoogleAuthenticate()
  32.         {
  33.             Social.localUser.Authenticate((success, error) =>
  34.             {
  35.                 if (success)
  36.                 {
  37.                     var serverAuthCode = PlayGamesPlatform.Instance.GetServerAuthCode();
  38.                     PlayfabLogin(serverAuthCode);
  39.                 }
  40.                 else
  41.                     errorCallback("google_auth", error);
  42.             });
  43.         }
  44.         private void PlayfabLogin(string serverAuthCode)
  45.         {
  46.             PlayFabClientAPI.LoginWithGoogleAccount(new LoginWithGoogleAccountRequest()
  47.             {
  48.                 ServerAuthCode = serverAuthCode,
  49.                 CreateAccount = true
  50.             }, (result) =>
  51.             {
  52.                 PlayFabId = result.PlayFabId;
  53.                 NewlyPlayer = result.NewlyCreated;
  54.                 if (result.NewlyCreated)
  55.                     CreateProfile();
  56.                 else
  57.                     GetProfile();
  58.             }, (playFabError) => errorCallback("playfab_login", playFabError.GenerateErrorReport()));
  59.         }
  60.         private void CreateProfile()
  61.         {
  62.             PlayFabClientAPI.ExecuteCloudScript(new ExecuteCloudScriptRequest()
  63.             {
  64.                 FunctionName = "createProfile"
  65.             }, (result) =>
  66.             {
  67.                 GetProfile();
  68.             }, (error) => errorCallback("create_profile", error.GenerateErrorReport()));
  69.         }
  70.         private void GetProfile()
  71.         {
  72.             PlayFabClientAPI.GetUserReadOnlyData(new GetUserDataRequest(), (result) =>
  73.             {
  74.                 ProfileData = result.Data;
  75.                 successCallback(this);
  76.             }, (error) => errorCallback("get_profile", error.GenerateErrorReport()));
  77.         }
  78.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement