using UnityEngine; using TMPro; using System.Collections; using System.Collections.Generic; using UnityEngine.UI; using UnityEngine.Networking; using System.Collections; using System.IO; using System; public class Currencies { public int GetCurr(int CurNum) { List CurS = Load("CurS"); return CurS[CurNum]; } public static void AddValue(string n, int v) { List l = Load(n); l.Add(v); Debug.Log("l=" + string.Join(",",l)); Save(n,l); Debug.Log("ll=" + PlayerPrefs.GetString(n)); } public static void AddToIndex(string n, int i, int v) { List l = Load(n); Debug.Log(string.Join(",",l)); if (l.Count <= i) while (l.Count <= i) l.Add(0); l[i] += v; Save(n,l); } public static int CheckIndex(string Name, int index) { List l = Load(Name); if(l.Count <= index) return 0; else return l[index]; } public static void SaveV(string Name, int index, int count) { List CurS = Load(Name); if(CurS.Count <= index) while (CurS.Count <=index) CurS.Add(0); CurS[index] = count; Save(Name, CurS); } public void SaveValue(int CurNum, int count) { List CurS = Load("CurS"); CurS[CurNum] = count; Save("CurS", CurS); } public static void Save(string Cur, List lst) { string json = JsonUtility.ToJson(new Serialization(lst)); PlayerPrefs.SetString(Cur, json); PlayerPrefs.Save(); } public static List Load(string Cur) {if (PlayerPrefs.HasKey(Cur)) { string json = PlayerPrefs.GetString(Cur); return JsonUtility.FromJson>(json).List; } else{return new List {0};} } [System.Serializable] private class Serialization { public List List; public Serialization(List list) { this.List = list; } public List ToList() { return List; } } } [System.Serializable] public class accountData {//30 public string id ; public List completeLevels ; public List collectAchievements; public List haveSword; public List haveHead ; public List haveSkins ; public List haveAccessory; public List entityKilled ; public List otherStat ; public List haveOther ; public List passlevel ; public List collectrewards ; } [Serializable] public class ADataWrapper { public accountData levels; } public class AcountSystem : MonoBehaviour { public TMP_Text PlayerName; string AcName; public TMP_InputField EnterName; string AccountName = ""; string Password; public TMP_Text info; public GameObject IfLogIn; public GameObject IfNotLogIn; accountData ADATA; // Start is called once before the first execution of Update after the MonoBehaviour is created void Start() { PlayerName.text = PlayerPrefs.GetString("AccountName"); EnterName.text = PlayerPrefs.GetString("AccountName"); if(PlayerPrefs.HasKey("AccountID") && PlayerPrefs.GetString("AccountID") != "0") {LogIn();} print((PlayerPrefs.GetString("AccountID"), PlayerPrefs.GetString("AccountPassword"), PlayerPrefs.GetString("AccountName"))); } public void SetName(string Name) { PlayerPrefs.SetString("PlayerName", Name); } public void SetAccName(string Name) { AccountName = Name; PlayerPrefs.SetString("AccountName", Name); } public void SetPassword(string Pass) { Password = Pass; PlayerPrefs.SetString("AccountPassword", Password); } public void Registr() { StartCoroutine(Reg(AccountName, Password)); } private IEnumerator Reg(string AccountName, string Password) { string baseUrl = "http://mc1.live-on.pro:64096/api/Accounts/"; if (Password == null) Password = "00000000"; WWWForm form = new WWWForm(); form.AddField("name", AccountName); form.AddField("password", Password); using (UnityWebRequest www = UnityWebRequest.Post(baseUrl + "register", form)) { yield return www.SendWebRequest(); if (www.result == UnityWebRequest.Result.Success){ Debug.Log("Аккаунт Зарегестрирован, id:" + www.downloadHandler.text); PlayerPrefs.SetString("AccountID", www.downloadHandler.text); PlayerPrefs.SetString("AccountName", AccountName); PlayerPrefs.SetString("AccountPassword", Password); PlayerPrefs.SetString("PlayerName", AccountName); } else { info.text = www.downloadHandler.text; Debug.LogError("Ошибка при скачивании: " + www.error + www.downloadHandler.text); } } } public void OpenAS() { if(!PlayerPrefs.HasKey("AccountID") || PlayerPrefs.GetString("AccountID") == "0") { IfLogIn.SetActive(false); IfNotLogIn.SetActive(true); } else { IfLogIn.SetActive(true); IfNotLogIn.SetActive(false); } } public void ExitAccount() { PlayerPrefs.SetString("AccountID", "0"); PlayerPrefs.SetString("AccountName", "Player"); PlayerPrefs.SetString("AccountPassword", "0"); //CompleteLevels List EL = new List {}; PlayerPrefs.SetInt("LevelComplete", 0); PlayerPrefs.SetInt("CompleteLeftL", 0); PlayerPrefs.SetInt("CompleteCenterL", 0); PlayerPrefs.SetInt("RightCompleteL", 0); Currencies.Save("HaveA", EL); Currencies.Save("HaveSw", EL); Currencies.Save("HaveHe", EL); Currencies.Save("HaveS", EL); //HaveAccessory = new List {0}; //EntityKilled = new List {0}; PlayerPrefs.SetInt("CountDie", 0); PlayerPrefs.SetInt("CountWin", 0); PlayerPrefs.SetInt("MaxMoney", 0); PlayerPrefs.SetInt("EnemiesKilled", 0); PlayerPrefs.SetInt("Moneys", 0); PlayerPrefs.SetInt("Metall", 0); } public void LogIn() { StartCoroutine(Login(AccountName, Password)); } private IEnumerator Login(string AccountName, string Password) { string baseUrl = "http://mc1.live-on.pro:64096/api/Accounts/"; WWWForm form = new WWWForm(); form.AddField("name", AccountName); form.AddField("password", Password); using (UnityWebRequest www = UnityWebRequest.Post(baseUrl + "login", form)) { yield return www.SendWebRequest(); if (www.result == UnityWebRequest.Result.Success) { Debug.Log("Аккаунт Войден" + www.downloadHandler.text); PlayerPrefs.SetString("AccountName", AccountName); PlayerPrefs.SetString("AccountPassword", Password); PlayerPrefs.SetString("AccountID", www.downloadHandler.text); PlayerPrefs.SetString("PlayerName", AccountName); } else { Debug.Log(www.downloadHandler.text); info.text = www.downloadHandler.text; if(PlayerPrefs.GetString("AccountID") != "0" || PlayerPrefs.HasKey("AccountID")) ExitAccount(); } } } public void Save() { List CompleteLevels; List CollectAchievements; List HaveSword; List HaveHead; List HaveSkins; List HaveAccessory; List EntityKilled; List OtherStat; List HaveOther; CompleteLevels = new List{PlayerPrefs.GetInt("LevelComplete"), PlayerPrefs.GetInt("CompleteLeftL"),PlayerPrefs.GetInt("CompleteCenterL"),PlayerPrefs.GetInt("RightCompleteL")}; CollectAchievements = Currencies.Load("HaveA"); HaveSword = Currencies.Load("HaveSw"); HaveHead = Currencies.Load("HaveHe"); HaveSkins = Currencies.Load("HaveS"); HaveAccessory = new List {0}; EntityKilled = new List {0}; OtherStat = new List {PlayerPrefs.GetInt("CountDie"), PlayerPrefs.GetInt("CountWin"), PlayerPrefs.GetInt("MaxMoney"), PlayerPrefs.GetInt("EnemiesKilled")}; HaveOther = new List {PlayerPrefs.GetInt("Moneys"), PlayerPrefs.GetInt("Metall")}; List> Data = new List> {CompleteLevels, CollectAchievements, HaveSword, HaveHead, HaveSkins, HaveAccessory, EntityKilled, OtherStat, HaveOther}; string Id = "s"; StartCoroutine(save(Data)); } private IEnumerator save(List> Data) { string baseUrl = "http://mc1.live-on.pro:64096/api/AData/"; WWWForm form = new WWWForm(); form.AddField("name", PlayerPrefs.GetString("AccountName")); form.AddField("password", PlayerPrefs.GetString("AccountPassword")); form.AddField("completelevels", string.Join(", ",Data[0])); form.AddField("achievements",string.Join(", ", Data[1])); form.AddField("havesword",string.Join(", ", Data[2])); form.AddField("havehead",string.Join(", ", Data[3])); form.AddField("haveskins",string.Join(", ", Data[4])); form.AddField("haveaccessory",string.Join(", ", Data[5])); form.AddField("entitykilled",string.Join(", ", Data[6])); form.AddField("otherstat",string.Join(", ", Data[7])); form.AddField("haveother",string.Join(", ", Data[8])); form.AddField("SSkin", PlayerPrefs.GetInt("SelectedSkin")); print(PlayerPrefs.GetInt("SelectedSkin")); using (UnityWebRequest www = UnityWebRequest.Post(baseUrl + "save", form)) { yield return www.SendWebRequest(); if (www.result == UnityWebRequest.Result.Success) { Debug.Log("сохранено" + www.downloadHandler.text); } else { Debug.Log(www.downloadHandler.text); info.text = www.downloadHandler.text; } } } public void Load() { StartCoroutine(load(PlayerPrefs.GetString("AccountID"))); } private IEnumerator load(string id) { string baseUrl = "http://mc1.live-on.pro:64096/api/AData/"; WWWForm form = new WWWForm(); form.AddField("id", id); string nid = id.Substring(1, id.Length - 2); string tnid = id.Trim('"'); string url = baseUrl + "load/" + tnid; print(id); print(url); using (UnityWebRequest www = UnityWebRequest.Get(url)) { yield return www.SendWebRequest(); if (www.result == UnityWebRequest.Result.Success) { string json = www.downloadHandler.text; print(json); ADATA = JsonUtility.FromJson(json); print(JsonUtility.FromJson(json).id); LoadToPlayer(ADATA); } else { Debug.Log("err" + www.downloadHandler.text); } } } void LoadToPlayer(accountData ad) { //CompleteLevels print(string.Join(", ", ad.completeLevels)); PlayerPrefs.SetInt("LevelComplete", ad.completeLevels[0]); PlayerPrefs.SetInt("CompleteLeftL", ad.completeLevels[1]); PlayerPrefs.SetInt("CompleteCenterL", ad.completeLevels[2]); PlayerPrefs.SetInt("RightCompleteL", ad.completeLevels[3]); Currencies.Save("HaveA", ad.collectAchievements); Currencies.Save("HaveSw", ad.haveSword); Currencies.Save("HaveHe", ad.haveHead); Currencies.Save("HaveS", ad.haveSkins); //HaveAccessory = new List {0}; //EntityKilled = new List {0}; PlayerPrefs.SetInt("CountDie", ad.otherStat[0]); PlayerPrefs.SetInt("CountWin", ad.otherStat[1]); PlayerPrefs.SetInt("MaxMoney", ad.otherStat[2]); PlayerPrefs.SetInt("EnemiesKilled", ad.otherStat[3]); PlayerPrefs.SetInt("Moneys", ad.haveOther[0]); PlayerPrefs.SetInt("Metall", ad.haveOther[1]); } // Update is called once per frame }