Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Linq;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using PlayFab;
- using PlayFab.ClientModels;
- using Assets.Scripts.Boss;
- public class PlayFabManager : MonoBehaviour
- {
- // PlayFab ID Default --> change it
- // Petvenger Dev >> androidvn.com@gmail.com /1q2w3e4r5t@123
- public string PlayfabID = "FD55";
- public string CustomAccountID = "";
- // DataUser
- public List<HeroUser> HeroesUserData;
- public List<LevelUser> LevelsUserData;
- // Time
- public int TimeStartCheckToken = 30;
- public int TimePeriodCheckToken = 30;
- public static PlayFabManager Instance;
- private bool isDoneDownTitleData = false;
- private bool isDoneDownDataUser = false;
- void Awake()
- {
- DontDestroyOnLoad(this);
- Instance = this;
- EncryptedPlayerPrefs.CreateKey();
- PlayFabSettings.TitleId = PlayfabID;
- //Login("thieumao");
- }
- #region PlayFab Core
- [ContextMenu("Login PlayFab")]
- public void Login(string CustomAccountLogin)
- {
- print("Login with " + CustomAccountLogin);
- Token.CreateToken();
- // get TitleID from link json
- if (EncryptedPlayerPrefs.GetString("TitleID").Length == 4)
- {
- PlayfabID = EncryptedPlayerPrefs.GetString("TitleID");
- }
- LoginWithCustomIDRequest request = new LoginWithCustomIDRequest()
- {
- TitleId = PlayfabID,
- CreateAccount = true,
- CustomId = CustomAccountLogin
- };
- PlayFabClientAPI.LoginWithCustomID(
- request,
- (result) =>
- {
- isDoneDownTitleData = false;
- isDoneDownDataUser = false;
- CustomAccountID = result.PlayFabId;
- Debug.Log("Got PlayFabID: " + CustomAccountLogin);
- // Download DataGame, Level.xml, LevelTower.xml, string json
- DownTitleData();
- if (result.NewlyCreated)
- {
- Debug.Log("(new account)");
- setDisplayName();
- isDoneDownDataUser = true;
- checkNextScene();
- }
- else
- {
- Debug.Log("(existing account)");
- DownDataAll();
- }
- sendTokenClient();
- checkNextScene();
- },
- (error) =>
- {
- Debug.Log("Error logging in player with custom ID:");
- Debug.Log(error.ErrorMessage);
- }
- );
- }
- [ContextMenu("Get Url Cloud")]
- public void GetUrlCloud()
- {
- GetCloudScriptUrlRequest request2 = new GetCloudScriptUrlRequest()
- {
- Testing = false
- };
- PlayFabClientAPI.GetCloudScriptUrl(request2, (result) =>
- {
- Debug.Log("URL is set");
- },
- (error) =>
- {
- Debug.Log("Failed to retrieve Cloud Script URL");
- });
- }
- [ContextMenu("Test Cloud Script")]
- public void TestCloudScript()
- {
- RunCloudScriptRequest request = new RunCloudScriptRequest()
- {
- ActionId = "SetDataGame",
- Params = new { key = "name", value = "Thieu Van Nguyen" }
- };
- PlayFabClientAPI.RunCloudScript(
- request,
- (result) =>
- {
- //Debug.Log("Got log entries:");
- Debug.Log("Dong 1: " + result.ActionLog);
- Debug.Log("Dong 2: " + result.Results.ToString());
- },
- (error) =>
- {
- Debug.Log("Loi roi");
- Debug.Log(error.ErrorMessage);
- }
- );
- }
- [ContextMenu("DownTitleData")]
- public void DownTitleData()
- {
- GetTitleDataRequest request = new GetTitleDataRequest()
- {
- Keys = null
- };
- PlayFabClientAPI.GetTitleData(
- request,
- (result) =>
- {
- if ((result.Data == null) || (result.Data.Count == 0))
- {
- Debug.Log("TitleData dont have data");
- }
- else
- {
- isDoneDownTitleData = false;
- //Debug.Log(">> DownTitleData:");
- foreach (var item in result.Data)
- {
- //Debug.Log(" " + item.Key + " == " + item.Value);
- EncryptedPlayerPrefs.SetString(item.Key, item.Value);
- }
- isDoneDownTitleData = true;
- checkNextScene();
- }
- },
- (error) =>
- {
- Debug.Log(">> Error in TitleData: " + error.ErrorMessage);
- }
- );
- }
- private void sendTokenClient()
- {
- string token = PlayerPrefs.GetString(JSFUtils.Token);
- Dictionary<string, string> listKeyValue = new Dictionary<string, string>();
- listKeyValue.Add("Token", token);
- //UpData(listKeyValue);
- UpdateUserDataRequest request = new UpdateUserDataRequest()
- {
- Data = listKeyValue
- };
- PlayFabClientAPI.UpdateUserData(
- request,
- (result) =>
- {
- //Debug.Log("Successfully updated user data");
- InvokeRepeating("CheckToken", TimeStartCheckToken, TimePeriodCheckToken);
- },
- (error) =>
- {
- //Debug.Log("Got error setting user data Ancestor to Arthur");
- //Debug.Log(error.ErrorDetails);
- print("Error: sendTokenClient");
- }
- );
- }
- public void CheckToken()
- {
- List<string> listKey = new List<string>();
- listKey.Add("Token");
- GetUserDataRequest request = new GetUserDataRequest()
- {
- PlayFabId = CustomAccountID,
- Keys = listKey
- };
- PlayFabClientAPI.GetUserData(
- request,
- (result) =>
- {
- if ((result.Data == null) || (result.Data.Count == 0))
- {
- Debug.Log("No user data available");
- }
- else
- {
- foreach (var item in result.Data)
- {
- string tokenServer = item.Value.Value;
- string tokenClient = PlayerPrefs.GetString(JSFUtils.Token);
- if (tokenServer == tokenClient)
- {
- break;
- }
- else
- {
- showDialogTokenError();
- }
- }
- }
- },
- (error) =>
- {
- Debug.Log("Got error retrieving user data:");
- Debug.Log(error.ErrorMessage);
- }
- );
- }
- private void showDialogTokenError()
- {
- DialogCheckView.Instance.ShowDialogTokenError();
- }
- private void setDisplayName()
- {
- UpdateUserTitleDisplayNameRequest request = new UpdateUserTitleDisplayNameRequest()
- {
- DisplayName = FacebookManager.Instance.Name
- };
- PlayFabClientAPI.UpdateUserTitleDisplayName(
- request,
- (result) =>
- {
- print("Current name in playfab: " + result.DisplayName);
- },
- (error) =>
- {
- Debug.Log(error.ErrorMessage);
- }
- );
- }
- private void UpDataAll()
- {
- Dictionary<string, string> listKeyValue = getUserData();
- UpData(listKeyValue);
- }
- private void UpData(Dictionary<string, string> listKeyValue)
- {
- UpdateUserDataRequest request = new UpdateUserDataRequest()
- {
- Data = listKeyValue
- };
- PlayFabClientAPI.UpdateUserData(
- request,
- (result) =>
- {
- Debug.Log("Successfully updated user data");
- },
- (error) =>
- {
- Debug.Log("Got error setting user data Ancestor to Arthur");
- Debug.Log(error.ErrorDetails);
- }
- );
- }
- private void DownDataAll()
- {
- //DownData(null);
- GetUserDataRequest request = new GetUserDataRequest()
- {
- PlayFabId = CustomAccountID,
- Keys = null
- };
- PlayFabClientAPI.GetUserData(
- request,
- (result) =>
- {
- if ((result.Data == null) || (result.Data.Count == 0))
- {
- Debug.Log("No user data available");
- }
- else
- {
- isDoneDownDataUser = false;
- foreach (var item in result.Data)
- {
- //Debug.Log(" " + item.Key + " == " + item.Value.Value);
- KeyValuePair<string, string> keyValue = new KeyValuePair<string, string>(item.Key, item.Value.Value);
- setUserData(keyValue);
- }
- isDoneDownDataUser = true;
- checkNextScene();
- }
- },
- (error) =>
- {
- Debug.Log("Got error retrieving user data:");
- Debug.Log(error.ErrorMessage);
- }
- );
- }
- private void DownData(List<string> listKey)
- {
- GetUserDataRequest request = new GetUserDataRequest()
- {
- PlayFabId = CustomAccountID,
- Keys = listKey
- //Keys = new List<string>(){"hightscore",...}
- };
- PlayFabClientAPI.GetUserData(
- request,
- (result) =>
- {
- if ((result.Data == null) || (result.Data.Count == 0))
- {
- Debug.Log("No user data available");
- }
- else
- {
- foreach (var item in result.Data)
- {
- //Debug.Log(" " + item.Key + " == " + item.Value.Value);
- KeyValuePair<string, string> keyValue = new KeyValuePair<string, string>(item.Key, item.Value.Value);
- setUserData(keyValue);
- }
- }
- },
- (error) =>
- {
- Debug.Log("Got error retrieving user data:");
- Debug.Log(error.ErrorMessage);
- }
- );
- }
- private Dictionary<string, string> getUserData()
- {
- Dictionary<string, string> listUserData = new Dictionary<string, string>();
- listUserData.Add("IdFacebook", FacebookManager.Instance.Id);
- listUserData.Add("NameFacebook", FacebookManager.Instance.Name);
- //listUserData.Add("TableHeroUser", ConnectData.Instance.GetTableHeroUser());
- //listUserData.Add("TableLevelUser", ConnectData.Instance.GetTableLevelUser());
- listUserData.Add("Level", "" + EncryptedPlayerPrefs.GetInt(JSFUtils.LEVEL_PLAYER));
- listUserData.Add("Soul", "" + EncryptedPlayerPrefs.GetInt(JSFUtils.Soul));
- listUserData.Add("Food", "" + EncryptedPlayerPrefs.GetInt(JSFUtils.Food));
- listUserData.Add("Cube", "" + EncryptedPlayerPrefs.GetInt(JSFUtils.Cube));
- listUserData.Add("Ticket", "" + EncryptedPlayerPrefs.GetInt(JSFUtils.Ticket));
- listUserData.Add("Numbuy", "" + EncryptedPlayerPrefs.GetInt(JSFUtils.NumBuy));
- listUserData.Add("EnergyBuy", "" + EncryptedPlayerPrefs.GetInt(JSFUtils.EnergyBuy));
- listUserData.Add("Gold", "" + EncryptedPlayerPrefs.GetInt(JSFUtils.Gold));
- listUserData.Add("TutorialUpdateHero", "" + EncryptedPlayerPrefs.GetInt(JSFUtils.TutorialUpdateHero));
- listUserData.Add("Energy", "" + EncryptedPlayerPrefs.GetInt(JSFUtils.Energy));
- listUserData.Add("H1", "" + PlayerPrefs.GetInt(JSFUtils.H1));
- listUserData.Add("H2", "" + PlayerPrefs.GetInt(JSFUtils.H2));
- listUserData.Add("H3", "" + PlayerPrefs.GetInt(JSFUtils.H3));
- listUserData.Add("H4", "" + PlayerPrefs.GetInt(JSFUtils.H4));
- listUserData.Add("H5", "" + PlayerPrefs.GetInt(JSFUtils.H5));
- return listUserData;
- }
- private void setUserData(KeyValuePair<string, string> item)
- {
- switch (item.Key)
- {
- case "IdFacebook":
- FacebookManager.Instance.Id = item.Value;
- break;
- case "NameFacebook":
- FacebookManager.Instance.Name = item.Value;
- break;
- case "TableHeroUser":
- HeroesUserData = convertStringToListHeroUser(item.Value);
- break;
- case "TableLevelUser":
- LevelsUserData = convertStringToListLevelUser(item.Value);
- break;
- case "Level":
- EncryptedPlayerPrefs.SetInt(JSFUtils.LEVEL_PLAYER, Int32.Parse(item.Value));
- break;
- case "Soul":
- EncryptedPlayerPrefs.SetInt(JSFUtils.Soul, Int32.Parse(item.Value));
- break;
- case "Food":
- EncryptedPlayerPrefs.SetInt(JSFUtils.Food, Int32.Parse(item.Value));
- break;
- case "Cube":
- EncryptedPlayerPrefs.SetInt(JSFUtils.Cube, Int32.Parse(item.Value));
- break;
- case "Ticket":
- EncryptedPlayerPrefs.SetInt(JSFUtils.Ticket, Int32.Parse(item.Value));
- break;
- case "Numbuy":
- EncryptedPlayerPrefs.SetInt(JSFUtils.NumBuy, Int32.Parse(item.Value));
- break;
- case "EnergyBuy":
- EncryptedPlayerPrefs.SetInt(JSFUtils.EnergyBuy, Int32.Parse(item.Value));
- break;
- case "Gold":
- EncryptedPlayerPrefs.SetInt(JSFUtils.Gold, Int32.Parse(item.Value));
- break;
- case "TutorialUpdateHero":
- EncryptedPlayerPrefs.SetInt(JSFUtils.TutorialUpdateHero, Int32.Parse(item.Value));
- break;
- case "Energy":
- EncryptedPlayerPrefs.SetInt(JSFUtils.Energy, Int32.Parse(item.Value));
- break;
- case "H1":
- PlayerPrefs.SetInt(JSFUtils.H1, Int32.Parse(item.Value));
- break;
- case "H2":
- PlayerPrefs.SetInt(JSFUtils.H2, Int32.Parse(item.Value));
- break;
- case "H3":
- PlayerPrefs.SetInt(JSFUtils.H3, Int32.Parse(item.Value));
- break;
- case "H4":
- PlayerPrefs.SetInt(JSFUtils.H4, Int32.Parse(item.Value));
- break;
- case "H5":
- PlayerPrefs.SetInt(JSFUtils.H5, Int32.Parse(item.Value));
- break;
- default:
- break;
- }
- }
- #endregion
- #region Send Data
- // Khi vào bàn chơi hoặc reset game
- public void WhenNewGameOrResetGame(CallBackFunc callback, int energy)
- {
- Dictionary<string, string> listKeyValue = new Dictionary<string, string>();
- listKeyValue.Add("Energy", "" + energy);
- UpdateUserDataRequest request = new UpdateUserDataRequest()
- {
- Data = listKeyValue
- };
- PlayFabClientAPI.UpdateUserData(
- request,
- (result) =>
- {
- callback(true);
- },
- (error) =>
- {
- callback(false);
- }
- );
- }
- // Khi nâng cấp một hero
- public void WhenUpdateHero(CallBackFunc callback, int food, int soul, int cube, List<HeroUser> heroesUserData)
- {
- Dictionary<string, string> listKeyValue = new Dictionary<string, string>();
- listKeyValue.Add("Food", "" + food);
- listKeyValue.Add("Soul", "" + soul);
- listKeyValue.Add("Cube", "" + cube);
- listKeyValue.Add("TableHeroUser", convertListHeroUserToString(heroesUserData));
- UpdateUserDataRequest request = new UpdateUserDataRequest()
- {
- Data = listKeyValue
- };
- PlayFabClientAPI.UpdateUserData(
- request,
- (result) =>
- {
- callback(true);
- },
- (error) =>
- {
- callback(false);
- }
- );
- }
- // Khi unlock một hero mới
- public void WhenUnlockHere(CallBackFunc callback, List<HeroUser> heroesUserData)
- {
- Dictionary<string, string> listKeyValue = new Dictionary<string, string>();
- listKeyValue.Add("TableHeroUser", convertListHeroUserToString(heroesUserData));
- UpdateUserDataRequest request = new UpdateUserDataRequest()
- {
- Data = listKeyValue
- };
- PlayFabClientAPI.UpdateUserData(
- request,
- (result) =>
- {
- callback(true);
- },
- (error) =>
- {
- callback(false);
- }
- );
- }
- // Khi chơi win hoặc lose
- public void WhenWinOrLose(CallBackFunc callback, int food, int soul, int cube, int good, int ticket, List<LevelUser> levelsUserData)
- {
- Dictionary<string, string> listKeyValue = new Dictionary<string, string>();
- listKeyValue.Add("Food", "" + food);
- listKeyValue.Add("Soul", "" + soul);
- listKeyValue.Add("Cube", "" + cube);
- listKeyValue.Add("Good", "" + good);
- listKeyValue.Add("Ticket", "" + ticket);
- listKeyValue.Add("TableLevelUser", convertListLevelUserToString(levelsUserData));
- UpdateUserDataRequest request = new UpdateUserDataRequest()
- {
- Data = listKeyValue
- };
- PlayFabClientAPI.UpdateUserData(
- request,
- (result) =>
- {
- callback(true);
- },
- (error) =>
- {
- callback(false);
- }
- );
- }
- // Quay thùng đồ
- public void WhenLucky(CallBackFunc callback, int food, int soul, int cube, int ticket)
- {
- Dictionary<string, string> listKeyValue = new Dictionary<string, string>();
- listKeyValue.Add("Food", "" + food);
- listKeyValue.Add("Soul", "" + soul);
- listKeyValue.Add("Cube", "" + cube);
- listKeyValue.Add("Ticket", "" + ticket);
- UpdateUserDataRequest request = new UpdateUserDataRequest()
- {
- Data = listKeyValue
- };
- PlayFabClientAPI.UpdateUserData(
- request,
- (result) =>
- {
- callback(true);
- },
- (error) =>
- {
- callback(false);
- }
- );
- }
- // Mua cube
- public void WhenBuyCube(CallBackFunc callback, int cube)
- {
- Dictionary<string, string> listKeyValue = new Dictionary<string, string>();
- listKeyValue.Add("Cube", "" + cube);
- UpdateUserDataRequest request = new UpdateUserDataRequest()
- {
- Data = listKeyValue
- };
- PlayFabClientAPI.UpdateUserData(
- request,
- (result) =>
- {
- callback(true);
- },
- (error) =>
- {
- callback(false);
- }
- );
- }
- // Mua gold
- public void WhenBuyGold(CallBackFunc callback, int gold)
- {
- Dictionary<string, string> listKeyValue = new Dictionary<string, string>();
- listKeyValue.Add("Gold", "" + gold);
- UpdateUserDataRequest request = new UpdateUserDataRequest()
- {
- Data = listKeyValue
- };
- PlayFabClientAPI.UpdateUserData(
- request,
- (result) =>
- {
- callback(true);
- },
- (error) =>
- {
- callback(false);
- }
- );
- }
- // Lưu vị trí các hero
- public void WhenUpdateHeroPosition(CallBackFunc callback, int h1, int h2, int h3, int h4, int h5)
- {
- Dictionary<string, string> listKeyValue = new Dictionary<string, string>();
- listKeyValue.Add("H1", "" + h1);
- listKeyValue.Add("H2", "" + h2);
- listKeyValue.Add("H3", "" + h3);
- listKeyValue.Add("H4", "" + h4);
- listKeyValue.Add("H5", "" + h5);
- UpdateUserDataRequest request = new UpdateUserDataRequest()
- {
- Data = listKeyValue
- };
- PlayFabClientAPI.UpdateUserData(
- request,
- (result) =>
- {
- callback(true);
- },
- (error) =>
- {
- callback(false);
- }
- );
- }
- // Lưu EnergyBuy
- public void WhenSaveEnergyBuy(CallBackFunc callback, int energyBuy)
- {
- Dictionary<string, string> listKeyValue = new Dictionary<string, string>();
- listKeyValue.Add("EnergyBuy", "" + energyBuy);
- UpdateUserDataRequest request = new UpdateUserDataRequest()
- {
- Data = listKeyValue
- };
- PlayFabClientAPI.UpdateUserData(
- request,
- (result) =>
- {
- callback(true);
- },
- (error) =>
- {
- callback(false);
- }
- );
- }
- // Lưu TutorialUpdateHero
- public void WhenSaveTutorialUpdateHero(CallBackFunc callback, int tutorialUpdateHero)
- {
- Dictionary<string, string> listKeyValue = new Dictionary<string, string>();
- listKeyValue.Add("TutorialUpdateHero", "" + tutorialUpdateHero);
- UpdateUserDataRequest request = new UpdateUserDataRequest()
- {
- Data = listKeyValue
- };
- PlayFabClientAPI.UpdateUserData(
- request,
- (result) =>
- {
- callback(true);
- },
- (error) =>
- {
- callback(false);
- }
- );
- }
- #endregion
- #region Convert
- private string convertListHeroUserToString(List<HeroUser> heroesUserData)
- {
- string strReturn = "";
- for (int i = 0; i < heroesUserData.Count; i++)
- {
- //HeroUser heroUser = heroesUserData[i];
- int id = heroesUserData[i].ID;
- int level = heroesUserData[i].Level;
- int unlockHeroID = heroesUserData[i].UnlockHeroID;
- strReturn = strReturn + id + JSFUtils.WordSeparate + level + JSFUtils.WordSeparate + unlockHeroID + JSFUtils.LineSeparate;
- }
- return strReturn;
- }
- private string convertListLevelUserToString(List<LevelUser> levelsUserData)
- {
- string strReturn = "";
- for (int i = 0; i < levelsUserData.Count; i++)
- {
- int id = levelsUserData[i].ID;
- int timePlay = levelsUserData[i].TimePlay;
- int state = levelsUserData[i].State;
- strReturn = strReturn + id + JSFUtils.WordSeparate + timePlay + JSFUtils.WordSeparate + state + JSFUtils.LineSeparate;
- }
- return strReturn;
- }
- private List<HeroUser> convertStringToListHeroUser(string stringValue)
- {
- List<HeroUser> listHeroUser = new List<HeroUser>();
- try
- {
- string stringTrim = stringValue.Trim();
- string stringValidate = stringTrim.Substring(0, stringTrim.Length - 1);
- string[] lines = stringValidate.Split(Convert.ToChar(JSFUtils.LineSeparate));
- if (lines.Length > 0)
- {
- foreach (string line in lines)
- {
- string[] values = line.Split(Convert.ToChar(JSFUtils.WordSeparate));
- int id = Int32.Parse(values[0]);
- int level = Int32.Parse(values[1]);
- int unlockHeroID = Int32.Parse(values[2]);
- HeroUser heroUser = new HeroUser(id, level, unlockHeroID);
- listHeroUser.Add(heroUser);
- }
- }
- }
- catch (Exception e)
- {
- return null;
- }
- return listHeroUser;
- }
- private List<LevelUser> convertStringToListLevelUser(string stringValue)
- {
- List<LevelUser> listLevelUser = new List<LevelUser>();
- string stringTrim = stringValue.Trim();
- string stringValidate = stringTrim.Substring(0, stringTrim.Length - 1);
- string[] lines = stringValidate.Split(Convert.ToChar(JSFUtils.LineSeparate));
- try
- {
- if (lines.Length > 0)
- {
- foreach (string line in lines)
- {
- string[] values = line.Split(Convert.ToChar(JSFUtils.WordSeparate));
- int id = Int32.Parse(values[0]);
- int timePlay = Int32.Parse(values[1]);
- int state = Int32.Parse(values[2]);
- LevelUser levelUser = new LevelUser(id, timePlay, state);
- listLevelUser.Add(levelUser);
- }
- }
- }
- catch (Exception e)
- {
- return null;
- }
- return listLevelUser;
- }
- #endregion
- #region other
- private void checkNextScene()
- {
- print(">> checkNextScene isDoneDownTitleData= " + isDoneDownTitleData + " - isDoneDownDataUser " + isDoneDownDataUser);
- if (isDoneDownTitleData == true && isDoneDownDataUser == true)
- {
- print(">>>>>>>> Next Scene MainMenu");
- Application.LoadLevelAsync("MainMenu");
- }
- }
- #endregion
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement