Advertisement
thieumao

PlayFabManager - FullCode

Jun 27th, 2016
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 26.02 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6. using PlayFab;
  7. using PlayFab.ClientModels;
  8. using Assets.Scripts.Boss;
  9.  
  10. public class PlayFabManager : MonoBehaviour
  11. {
  12.     // PlayFab ID Default --> change it
  13.     // Petvenger Dev >> androidvn.com@gmail.com /1q2w3e4r5t@123
  14.     public string PlayfabID = "FD55";
  15.     public string CustomAccountID = "";
  16.     // DataUser
  17.     public List<HeroUser> HeroesUserData;
  18.     public List<LevelUser> LevelsUserData;
  19.     // Time
  20.     public int TimeStartCheckToken = 30;
  21.     public int TimePeriodCheckToken = 30;
  22.  
  23.     public static PlayFabManager Instance;
  24.  
  25.     private bool isDoneDownTitleData = false;
  26.     private bool isDoneDownDataUser = false;
  27.  
  28.     void Awake()
  29.     {
  30.         DontDestroyOnLoad(this);
  31.         Instance = this;
  32.         EncryptedPlayerPrefs.CreateKey();
  33.         PlayFabSettings.TitleId = PlayfabID;
  34.  
  35.         //Login("thieumao");
  36.     }
  37.  
  38.     #region PlayFab Core
  39.     [ContextMenu("Login PlayFab")]
  40.     public void Login(string CustomAccountLogin)
  41.     {
  42.         print("Login with " + CustomAccountLogin);
  43.         Token.CreateToken();
  44.  
  45.         // get TitleID from link json
  46.         if (EncryptedPlayerPrefs.GetString("TitleID").Length == 4)
  47.         {
  48.             PlayfabID = EncryptedPlayerPrefs.GetString("TitleID");
  49.         }
  50.  
  51.         LoginWithCustomIDRequest request = new LoginWithCustomIDRequest()
  52.         {
  53.             TitleId = PlayfabID,
  54.             CreateAccount = true,
  55.             CustomId = CustomAccountLogin
  56.         };
  57.  
  58.         PlayFabClientAPI.LoginWithCustomID(
  59.             request,
  60.             (result) =>
  61.             {
  62.                 isDoneDownTitleData = false;
  63.                 isDoneDownDataUser = false;
  64.  
  65.                 CustomAccountID = result.PlayFabId;
  66.                 Debug.Log("Got PlayFabID: " + CustomAccountLogin);
  67.  
  68.                 // Download DataGame, Level.xml, LevelTower.xml, string json
  69.                 DownTitleData();
  70.  
  71.                 if (result.NewlyCreated)
  72.                 {
  73.                     Debug.Log("(new account)");
  74.                     setDisplayName();
  75.                     isDoneDownDataUser = true;
  76.                     checkNextScene();
  77.                 }
  78.                 else
  79.                 {
  80.                     Debug.Log("(existing account)");
  81.                     DownDataAll();
  82.                 }
  83.                 sendTokenClient();
  84.                
  85.                 checkNextScene();
  86.             },
  87.             (error) =>
  88.             {
  89.                 Debug.Log("Error logging in player with custom ID:");
  90.                 Debug.Log(error.ErrorMessage);
  91.             }
  92.         );
  93.     }
  94.  
  95.     [ContextMenu("Get Url Cloud")]
  96.     public void GetUrlCloud()
  97.     {
  98.         GetCloudScriptUrlRequest request2 = new GetCloudScriptUrlRequest()
  99.         {
  100.             Testing = false
  101.         };
  102.  
  103.         PlayFabClientAPI.GetCloudScriptUrl(request2, (result) =>
  104.         {
  105.             Debug.Log("URL is set");
  106.         },
  107.         (error) =>
  108.         {
  109.             Debug.Log("Failed to retrieve Cloud Script URL");
  110.         });
  111.     }
  112.  
  113.     [ContextMenu("Test Cloud Script")]
  114.     public void TestCloudScript()
  115.     {
  116.         RunCloudScriptRequest request = new RunCloudScriptRequest()
  117.         {
  118.             ActionId = "SetDataGame",
  119.             Params = new { key = "name", value = "Thieu Van Nguyen" }
  120.         };
  121.         PlayFabClientAPI.RunCloudScript(
  122.             request,
  123.             (result) =>
  124.             {
  125.                 //Debug.Log("Got log entries:");
  126.                 Debug.Log("Dong 1: " + result.ActionLog);
  127.                 Debug.Log("Dong 2: " + result.Results.ToString());
  128.             },
  129.             (error) =>
  130.             {
  131.                 Debug.Log("Loi roi");
  132.                 Debug.Log(error.ErrorMessage);
  133.             }
  134.         );
  135.     }
  136.  
  137.     [ContextMenu("DownTitleData")]
  138.     public void DownTitleData()
  139.     {
  140.         GetTitleDataRequest request = new GetTitleDataRequest()
  141.         {
  142.             Keys = null
  143.         };
  144.  
  145.         PlayFabClientAPI.GetTitleData(
  146.             request,
  147.             (result) =>
  148.             {
  149.                 if ((result.Data == null) || (result.Data.Count == 0))
  150.                 {
  151.                     Debug.Log("TitleData dont have data");
  152.                 }
  153.                 else
  154.                 {
  155.                     isDoneDownTitleData = false;
  156.                     //Debug.Log(">> DownTitleData:");
  157.                     foreach (var item in result.Data)
  158.                     {
  159.                         //Debug.Log("    " + item.Key + " == " + item.Value);
  160.                         EncryptedPlayerPrefs.SetString(item.Key, item.Value);
  161.                     }
  162.                     isDoneDownTitleData = true;
  163.                     checkNextScene();
  164.                 }
  165.             },
  166.             (error) =>
  167.             {
  168.                 Debug.Log(">> Error in TitleData: " + error.ErrorMessage);
  169.             }
  170.         );
  171.     }
  172.  
  173.     private void sendTokenClient()
  174.     {
  175.         string token = PlayerPrefs.GetString(JSFUtils.Token);
  176.         Dictionary<string, string> listKeyValue = new Dictionary<string, string>();
  177.         listKeyValue.Add("Token", token);
  178.         //UpData(listKeyValue);
  179.         UpdateUserDataRequest request = new UpdateUserDataRequest()
  180.         {
  181.             Data = listKeyValue
  182.         };
  183.  
  184.         PlayFabClientAPI.UpdateUserData(
  185.             request,
  186.             (result) =>
  187.             {
  188.                 //Debug.Log("Successfully updated user data");
  189.                 InvokeRepeating("CheckToken", TimeStartCheckToken, TimePeriodCheckToken);
  190.             },
  191.             (error) =>
  192.             {
  193.                 //Debug.Log("Got error setting user data Ancestor to Arthur");
  194.                 //Debug.Log(error.ErrorDetails);
  195.                 print("Error: sendTokenClient");
  196.             }
  197.         );
  198.     }
  199.  
  200.     public void CheckToken()
  201.     {
  202.         List<string> listKey = new List<string>();
  203.         listKey.Add("Token");
  204.         GetUserDataRequest request = new GetUserDataRequest()
  205.         {
  206.             PlayFabId = CustomAccountID,
  207.             Keys = listKey
  208.         };
  209.  
  210.         PlayFabClientAPI.GetUserData(
  211.             request,
  212.             (result) =>
  213.             {
  214.                 if ((result.Data == null) || (result.Data.Count == 0))
  215.                 {
  216.                     Debug.Log("No user data available");
  217.                 }
  218.                 else
  219.                 {
  220.                     foreach (var item in result.Data)
  221.                     {
  222.                         string tokenServer = item.Value.Value;
  223.                         string tokenClient = PlayerPrefs.GetString(JSFUtils.Token);
  224.                         if (tokenServer == tokenClient)
  225.                         {
  226.                             break;
  227.                         }
  228.                         else
  229.                         {
  230.                             showDialogTokenError();
  231.                         }
  232.                     }
  233.                 }
  234.             },
  235.             (error) =>
  236.             {
  237.                 Debug.Log("Got error retrieving user data:");
  238.                 Debug.Log(error.ErrorMessage);
  239.             }
  240.         );
  241.     }
  242.  
  243.     private void showDialogTokenError()
  244.     {
  245.         DialogCheckView.Instance.ShowDialogTokenError();
  246.     }
  247.  
  248.     private void setDisplayName()
  249.     {
  250.         UpdateUserTitleDisplayNameRequest request = new UpdateUserTitleDisplayNameRequest()
  251.         {
  252.             DisplayName = FacebookManager.Instance.Name
  253.         };
  254.  
  255.         PlayFabClientAPI.UpdateUserTitleDisplayName(
  256.             request,
  257.             (result) =>
  258.             {
  259.                 print("Current name in playfab: " + result.DisplayName);
  260.             },
  261.             (error) =>
  262.             {
  263.                 Debug.Log(error.ErrorMessage);
  264.             }
  265.         );
  266.     }
  267.  
  268.     private void UpDataAll()
  269.     {
  270.         Dictionary<string, string> listKeyValue = getUserData();
  271.         UpData(listKeyValue);
  272.     }
  273.  
  274.     private void UpData(Dictionary<string, string> listKeyValue)
  275.     {
  276.         UpdateUserDataRequest request = new UpdateUserDataRequest()
  277.         {
  278.             Data = listKeyValue
  279.         };
  280.  
  281.         PlayFabClientAPI.UpdateUserData(
  282.             request,
  283.             (result) =>
  284.             {
  285.                 Debug.Log("Successfully updated user data");
  286.             },
  287.             (error) =>
  288.             {
  289.                 Debug.Log("Got error setting user data Ancestor to Arthur");
  290.                 Debug.Log(error.ErrorDetails);
  291.             }
  292.         );
  293.     }
  294.  
  295.     private void DownDataAll()
  296.     {
  297.         //DownData(null);
  298.         GetUserDataRequest request = new GetUserDataRequest()
  299.         {
  300.             PlayFabId = CustomAccountID,
  301.             Keys = null
  302.         };
  303.  
  304.         PlayFabClientAPI.GetUserData(
  305.             request,
  306.             (result) =>
  307.             {
  308.                 if ((result.Data == null) || (result.Data.Count == 0))
  309.                 {
  310.                     Debug.Log("No user data available");
  311.                 }
  312.                 else
  313.                 {
  314.                     isDoneDownDataUser = false;
  315.                     foreach (var item in result.Data)
  316.                     {
  317.                         //Debug.Log("    " + item.Key + " == " + item.Value.Value);
  318.                         KeyValuePair<string, string> keyValue = new KeyValuePair<string, string>(item.Key, item.Value.Value);
  319.                         setUserData(keyValue);
  320.                     }
  321.                     isDoneDownDataUser = true;
  322.                     checkNextScene();
  323.                 }
  324.             },
  325.             (error) =>
  326.             {
  327.                 Debug.Log("Got error retrieving user data:");
  328.                 Debug.Log(error.ErrorMessage);
  329.             }
  330.         );
  331.     }
  332.  
  333.     private void DownData(List<string> listKey)
  334.     {
  335.         GetUserDataRequest request = new GetUserDataRequest()
  336.         {
  337.             PlayFabId = CustomAccountID,
  338.             Keys = listKey
  339.             //Keys = new List<string>(){"hightscore",...}
  340.         };
  341.  
  342.         PlayFabClientAPI.GetUserData(
  343.             request,
  344.             (result) =>
  345.             {
  346.                 if ((result.Data == null) || (result.Data.Count == 0))
  347.                 {
  348.                     Debug.Log("No user data available");
  349.                 }
  350.                 else
  351.                 {
  352.                     foreach (var item in result.Data)
  353.                     {
  354.                         //Debug.Log("    " + item.Key + " == " + item.Value.Value);
  355.                         KeyValuePair<string, string> keyValue = new KeyValuePair<string, string>(item.Key, item.Value.Value);
  356.                         setUserData(keyValue);
  357.                     }
  358.                 }
  359.             },
  360.             (error) =>
  361.             {
  362.                 Debug.Log("Got error retrieving user data:");
  363.                 Debug.Log(error.ErrorMessage);
  364.             }
  365.         );
  366.     }
  367.  
  368.     private Dictionary<string, string> getUserData()
  369.     {
  370.         Dictionary<string, string> listUserData = new Dictionary<string, string>();
  371.         listUserData.Add("IdFacebook", FacebookManager.Instance.Id);
  372.         listUserData.Add("NameFacebook", FacebookManager.Instance.Name);
  373.         //listUserData.Add("TableHeroUser", ConnectData.Instance.GetTableHeroUser());
  374.         //listUserData.Add("TableLevelUser", ConnectData.Instance.GetTableLevelUser());
  375.         listUserData.Add("Level", "" + EncryptedPlayerPrefs.GetInt(JSFUtils.LEVEL_PLAYER));
  376.         listUserData.Add("Soul", "" + EncryptedPlayerPrefs.GetInt(JSFUtils.Soul));
  377.         listUserData.Add("Food", "" + EncryptedPlayerPrefs.GetInt(JSFUtils.Food));
  378.         listUserData.Add("Cube", "" + EncryptedPlayerPrefs.GetInt(JSFUtils.Cube));
  379.         listUserData.Add("Ticket", "" + EncryptedPlayerPrefs.GetInt(JSFUtils.Ticket));
  380.         listUserData.Add("Numbuy", "" + EncryptedPlayerPrefs.GetInt(JSFUtils.NumBuy));
  381.         listUserData.Add("EnergyBuy", "" + EncryptedPlayerPrefs.GetInt(JSFUtils.EnergyBuy));
  382.         listUserData.Add("Gold", "" + EncryptedPlayerPrefs.GetInt(JSFUtils.Gold));
  383.         listUserData.Add("TutorialUpdateHero", "" + EncryptedPlayerPrefs.GetInt(JSFUtils.TutorialUpdateHero));
  384.         listUserData.Add("Energy", "" + EncryptedPlayerPrefs.GetInt(JSFUtils.Energy));
  385.         listUserData.Add("H1", "" + PlayerPrefs.GetInt(JSFUtils.H1));
  386.         listUserData.Add("H2", "" + PlayerPrefs.GetInt(JSFUtils.H2));
  387.         listUserData.Add("H3", "" + PlayerPrefs.GetInt(JSFUtils.H3));
  388.         listUserData.Add("H4", "" + PlayerPrefs.GetInt(JSFUtils.H4));
  389.         listUserData.Add("H5", "" + PlayerPrefs.GetInt(JSFUtils.H5));
  390.         return listUserData;
  391.     }
  392.  
  393.     private void setUserData(KeyValuePair<string, string> item)
  394.     {
  395.         switch (item.Key)
  396.         {
  397.             case "IdFacebook":
  398.                 FacebookManager.Instance.Id = item.Value;
  399.                 break;
  400.             case "NameFacebook":
  401.                 FacebookManager.Instance.Name = item.Value;
  402.                 break;
  403.             case "TableHeroUser":
  404.                 HeroesUserData = convertStringToListHeroUser(item.Value);
  405.                 break;
  406.             case "TableLevelUser":
  407.                 LevelsUserData = convertStringToListLevelUser(item.Value);
  408.                 break;
  409.             case "Level":
  410.                 EncryptedPlayerPrefs.SetInt(JSFUtils.LEVEL_PLAYER, Int32.Parse(item.Value));
  411.                 break;
  412.             case "Soul":
  413.                 EncryptedPlayerPrefs.SetInt(JSFUtils.Soul, Int32.Parse(item.Value));
  414.                 break;
  415.             case "Food":
  416.                 EncryptedPlayerPrefs.SetInt(JSFUtils.Food, Int32.Parse(item.Value));
  417.                 break;
  418.             case "Cube":
  419.                 EncryptedPlayerPrefs.SetInt(JSFUtils.Cube, Int32.Parse(item.Value));
  420.                 break;
  421.             case "Ticket":
  422.                 EncryptedPlayerPrefs.SetInt(JSFUtils.Ticket, Int32.Parse(item.Value));
  423.                 break;
  424.             case "Numbuy":
  425.                 EncryptedPlayerPrefs.SetInt(JSFUtils.NumBuy, Int32.Parse(item.Value));
  426.                 break;
  427.             case "EnergyBuy":
  428.                 EncryptedPlayerPrefs.SetInt(JSFUtils.EnergyBuy, Int32.Parse(item.Value));
  429.                 break;
  430.             case "Gold":
  431.                 EncryptedPlayerPrefs.SetInt(JSFUtils.Gold, Int32.Parse(item.Value));
  432.                 break;
  433.             case "TutorialUpdateHero":
  434.                 EncryptedPlayerPrefs.SetInt(JSFUtils.TutorialUpdateHero, Int32.Parse(item.Value));
  435.                 break;
  436.             case "Energy":
  437.                 EncryptedPlayerPrefs.SetInt(JSFUtils.Energy, Int32.Parse(item.Value));
  438.                 break;
  439.             case "H1":
  440.                 PlayerPrefs.SetInt(JSFUtils.H1, Int32.Parse(item.Value));
  441.                 break;
  442.             case "H2":
  443.                 PlayerPrefs.SetInt(JSFUtils.H2, Int32.Parse(item.Value));
  444.                 break;
  445.             case "H3":
  446.                 PlayerPrefs.SetInt(JSFUtils.H3, Int32.Parse(item.Value));
  447.                 break;
  448.             case "H4":
  449.                 PlayerPrefs.SetInt(JSFUtils.H4, Int32.Parse(item.Value));
  450.                 break;
  451.             case "H5":
  452.                 PlayerPrefs.SetInt(JSFUtils.H5, Int32.Parse(item.Value));
  453.                 break;
  454.             default:
  455.                 break;
  456.         }
  457.     }
  458.     #endregion
  459.  
  460.     #region Send Data
  461.     // Khi vào bàn chơi hoặc reset game
  462.     public void WhenNewGameOrResetGame(CallBackFunc callback, int energy)
  463.     {
  464.         Dictionary<string, string> listKeyValue = new Dictionary<string, string>();
  465.         listKeyValue.Add("Energy", "" + energy);
  466.         UpdateUserDataRequest request = new UpdateUserDataRequest()
  467.         {
  468.             Data = listKeyValue
  469.         };
  470.         PlayFabClientAPI.UpdateUserData(
  471.             request,
  472.             (result) =>
  473.             {
  474.                 callback(true);
  475.             },
  476.             (error) =>
  477.             {
  478.                 callback(false);
  479.             }
  480.         );
  481.     }
  482.  
  483.     // Khi nâng cấp một hero
  484.     public void WhenUpdateHero(CallBackFunc callback, int food, int soul, int cube, List<HeroUser> heroesUserData)
  485.     {
  486.         Dictionary<string, string> listKeyValue = new Dictionary<string, string>();
  487.         listKeyValue.Add("Food", "" + food);
  488.         listKeyValue.Add("Soul", "" + soul);
  489.         listKeyValue.Add("Cube", "" + cube);
  490.         listKeyValue.Add("TableHeroUser", convertListHeroUserToString(heroesUserData));
  491.         UpdateUserDataRequest request = new UpdateUserDataRequest()
  492.         {
  493.             Data = listKeyValue
  494.         };
  495.         PlayFabClientAPI.UpdateUserData(
  496.             request,
  497.             (result) =>
  498.             {
  499.                 callback(true);
  500.             },
  501.             (error) =>
  502.             {
  503.                 callback(false);
  504.             }
  505.         );
  506.     }
  507.  
  508.     // Khi unlock một hero mới
  509.     public void WhenUnlockHere(CallBackFunc callback, List<HeroUser> heroesUserData)
  510.     {
  511.         Dictionary<string, string> listKeyValue = new Dictionary<string, string>();
  512.         listKeyValue.Add("TableHeroUser", convertListHeroUserToString(heroesUserData));
  513.         UpdateUserDataRequest request = new UpdateUserDataRequest()
  514.         {
  515.             Data = listKeyValue
  516.         };
  517.         PlayFabClientAPI.UpdateUserData(
  518.             request,
  519.             (result) =>
  520.             {
  521.                 callback(true);
  522.             },
  523.             (error) =>
  524.             {
  525.                 callback(false);
  526.             }
  527.         );
  528.     }
  529.  
  530.     // Khi chơi win hoặc lose
  531.     public void WhenWinOrLose(CallBackFunc callback, int food, int soul, int cube, int good, int ticket, List<LevelUser> levelsUserData)
  532.     {
  533.         Dictionary<string, string> listKeyValue = new Dictionary<string, string>();
  534.         listKeyValue.Add("Food", "" + food);
  535.         listKeyValue.Add("Soul", "" + soul);
  536.         listKeyValue.Add("Cube", "" + cube);
  537.         listKeyValue.Add("Good", "" + good);
  538.         listKeyValue.Add("Ticket", "" + ticket);
  539.         listKeyValue.Add("TableLevelUser", convertListLevelUserToString(levelsUserData));
  540.         UpdateUserDataRequest request = new UpdateUserDataRequest()
  541.         {
  542.             Data = listKeyValue
  543.         };
  544.         PlayFabClientAPI.UpdateUserData(
  545.             request,
  546.             (result) =>
  547.             {
  548.                 callback(true);
  549.             },
  550.             (error) =>
  551.             {
  552.                 callback(false);
  553.             }
  554.         );
  555.     }
  556.  
  557.     // Quay thùng đồ
  558.     public void WhenLucky(CallBackFunc callback, int food, int soul, int cube, int ticket)
  559.     {
  560.         Dictionary<string, string> listKeyValue = new Dictionary<string, string>();
  561.         listKeyValue.Add("Food", "" + food);
  562.         listKeyValue.Add("Soul", "" + soul);
  563.         listKeyValue.Add("Cube", "" + cube);
  564.         listKeyValue.Add("Ticket", "" + ticket);
  565.         UpdateUserDataRequest request = new UpdateUserDataRequest()
  566.         {
  567.             Data = listKeyValue
  568.         };
  569.         PlayFabClientAPI.UpdateUserData(
  570.             request,
  571.             (result) =>
  572.             {
  573.                 callback(true);
  574.             },
  575.             (error) =>
  576.             {
  577.                 callback(false);
  578.             }
  579.         );
  580.     }
  581.  
  582.     // Mua cube
  583.     public void WhenBuyCube(CallBackFunc callback, int cube)
  584.     {
  585.         Dictionary<string, string> listKeyValue = new Dictionary<string, string>();
  586.         listKeyValue.Add("Cube", "" + cube);
  587.         UpdateUserDataRequest request = new UpdateUserDataRequest()
  588.         {
  589.             Data = listKeyValue
  590.         };
  591.         PlayFabClientAPI.UpdateUserData(
  592.             request,
  593.             (result) =>
  594.             {
  595.                 callback(true);
  596.             },
  597.             (error) =>
  598.             {
  599.                 callback(false);
  600.             }
  601.         );
  602.     }
  603.  
  604.     // Mua gold
  605.     public void WhenBuyGold(CallBackFunc callback, int gold)
  606.     {
  607.         Dictionary<string, string> listKeyValue = new Dictionary<string, string>();
  608.         listKeyValue.Add("Gold", "" + gold);
  609.         UpdateUserDataRequest request = new UpdateUserDataRequest()
  610.         {
  611.             Data = listKeyValue
  612.         };
  613.         PlayFabClientAPI.UpdateUserData(
  614.             request,
  615.             (result) =>
  616.             {
  617.                 callback(true);
  618.             },
  619.             (error) =>
  620.             {
  621.                 callback(false);
  622.             }
  623.         );
  624.     }
  625.  
  626.     // Lưu vị trí các hero
  627.     public void WhenUpdateHeroPosition(CallBackFunc callback, int h1, int h2, int h3, int h4, int h5)
  628.     {
  629.         Dictionary<string, string> listKeyValue = new Dictionary<string, string>();
  630.         listKeyValue.Add("H1", "" + h1);
  631.         listKeyValue.Add("H2", "" + h2);
  632.         listKeyValue.Add("H3", "" + h3);
  633.         listKeyValue.Add("H4", "" + h4);
  634.         listKeyValue.Add("H5", "" + h5);
  635.         UpdateUserDataRequest request = new UpdateUserDataRequest()
  636.         {
  637.             Data = listKeyValue
  638.         };
  639.         PlayFabClientAPI.UpdateUserData(
  640.             request,
  641.             (result) =>
  642.             {
  643.                 callback(true);
  644.             },
  645.             (error) =>
  646.             {
  647.                 callback(false);
  648.             }
  649.         );
  650.     }
  651.  
  652.     // Lưu EnergyBuy
  653.     public void WhenSaveEnergyBuy(CallBackFunc callback, int energyBuy)
  654.     {
  655.         Dictionary<string, string> listKeyValue = new Dictionary<string, string>();
  656.         listKeyValue.Add("EnergyBuy", "" + energyBuy);
  657.         UpdateUserDataRequest request = new UpdateUserDataRequest()
  658.         {
  659.             Data = listKeyValue
  660.         };
  661.         PlayFabClientAPI.UpdateUserData(
  662.             request,
  663.             (result) =>
  664.             {
  665.                 callback(true);
  666.             },
  667.             (error) =>
  668.             {
  669.                 callback(false);
  670.             }
  671.         );
  672.     }
  673.  
  674.     // Lưu TutorialUpdateHero
  675.     public void WhenSaveTutorialUpdateHero(CallBackFunc callback, int tutorialUpdateHero)
  676.     {
  677.         Dictionary<string, string> listKeyValue = new Dictionary<string, string>();
  678.         listKeyValue.Add("TutorialUpdateHero", "" + tutorialUpdateHero);
  679.         UpdateUserDataRequest request = new UpdateUserDataRequest()
  680.         {
  681.             Data = listKeyValue
  682.         };
  683.         PlayFabClientAPI.UpdateUserData(
  684.             request,
  685.             (result) =>
  686.             {
  687.                 callback(true);
  688.             },
  689.             (error) =>
  690.             {
  691.                 callback(false);
  692.             }
  693.         );
  694.     }
  695.     #endregion
  696.  
  697.     #region Convert
  698.     private string convertListHeroUserToString(List<HeroUser> heroesUserData)
  699.     {
  700.         string strReturn = "";
  701.         for (int i = 0; i < heroesUserData.Count; i++)
  702.         {
  703.             //HeroUser heroUser = heroesUserData[i];
  704.             int id = heroesUserData[i].ID;
  705.             int level = heroesUserData[i].Level;
  706.             int unlockHeroID = heroesUserData[i].UnlockHeroID;
  707.             strReturn = strReturn + id + JSFUtils.WordSeparate + level + JSFUtils.WordSeparate + unlockHeroID + JSFUtils.LineSeparate;
  708.         }
  709.         return strReturn;
  710.     }
  711.  
  712.     private string convertListLevelUserToString(List<LevelUser> levelsUserData)
  713.     {
  714.         string strReturn = "";
  715.         for (int i = 0; i < levelsUserData.Count; i++)
  716.         {
  717.             int id = levelsUserData[i].ID;
  718.             int timePlay = levelsUserData[i].TimePlay;
  719.             int state = levelsUserData[i].State;
  720.             strReturn = strReturn + id + JSFUtils.WordSeparate + timePlay + JSFUtils.WordSeparate + state + JSFUtils.LineSeparate;
  721.         }
  722.         return strReturn;
  723.     }
  724.  
  725.     private List<HeroUser> convertStringToListHeroUser(string stringValue)
  726.     {
  727.         List<HeroUser> listHeroUser = new List<HeroUser>();
  728.         try
  729.         {
  730.             string stringTrim = stringValue.Trim();
  731.             string stringValidate = stringTrim.Substring(0, stringTrim.Length - 1);
  732.             string[] lines = stringValidate.Split(Convert.ToChar(JSFUtils.LineSeparate));
  733.             if (lines.Length > 0)
  734.             {
  735.                 foreach (string line in lines)
  736.                 {
  737.                     string[] values = line.Split(Convert.ToChar(JSFUtils.WordSeparate));
  738.                     int id = Int32.Parse(values[0]);
  739.                     int level = Int32.Parse(values[1]);
  740.                     int unlockHeroID = Int32.Parse(values[2]);
  741.                     HeroUser heroUser = new HeroUser(id, level, unlockHeroID);
  742.                     listHeroUser.Add(heroUser);
  743.                 }
  744.             }
  745.         }
  746.         catch (Exception e)
  747.         {
  748.             return null;
  749.         }
  750.         return listHeroUser;
  751.     }
  752.  
  753.     private List<LevelUser> convertStringToListLevelUser(string stringValue)
  754.     {
  755.         List<LevelUser> listLevelUser = new List<LevelUser>();
  756.  
  757.         string stringTrim = stringValue.Trim();
  758.         string stringValidate = stringTrim.Substring(0, stringTrim.Length - 1);
  759.         string[] lines = stringValidate.Split(Convert.ToChar(JSFUtils.LineSeparate));
  760.         try
  761.         {
  762.             if (lines.Length > 0)
  763.             {
  764.                 foreach (string line in lines)
  765.                 {
  766.                     string[] values = line.Split(Convert.ToChar(JSFUtils.WordSeparate));
  767.                     int id = Int32.Parse(values[0]);
  768.                     int timePlay = Int32.Parse(values[1]);
  769.                     int state = Int32.Parse(values[2]);
  770.                     LevelUser levelUser = new LevelUser(id, timePlay, state);
  771.                     listLevelUser.Add(levelUser);
  772.                 }
  773.             }
  774.         }
  775.         catch (Exception e)
  776.         {
  777.             return null;
  778.         }
  779.         return listLevelUser;
  780.     }
  781.     #endregion
  782.  
  783.     #region other
  784.     private void checkNextScene()
  785.     {
  786.         print(">> checkNextScene isDoneDownTitleData= " + isDoneDownTitleData + " - isDoneDownDataUser " + isDoneDownDataUser);
  787.         if (isDoneDownTitleData == true && isDoneDownDataUser == true)
  788.         {
  789.             print(">>>>>>>> Next Scene MainMenu");
  790.             Application.LoadLevelAsync("MainMenu");
  791.         }
  792.     }
  793.     #endregion
  794. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement