PJ/Assets/scripts/UI/AcountSystem.cs

377 lines
No EOL
12 KiB
C#

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<int> CurS = Load("CurS");
return CurS[CurNum];
}
public static void AddValue(string n, int v)
{
List<int> 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<int> 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<int> l = Load(Name);
if(l.Count <= index)
return 0;
else
return l[index];
}
public static void SaveV(string Name, int index, int count)
{
List<int> 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<int> CurS = Load("CurS");
CurS[CurNum] = count;
Save("CurS", CurS);
}
public static void Save(string Cur, List<int> lst)
{
string json = JsonUtility.ToJson(new Serialization<int>(lst));
PlayerPrefs.SetString(Cur, json);
PlayerPrefs.Save();
}
public static List<int> Load(string Cur)
{if (PlayerPrefs.HasKey(Cur))
{
string json = PlayerPrefs.GetString(Cur);
return JsonUtility.FromJson<Serialization<int>>(json).List;
}
else{return new List<int> {0};}
}
[System.Serializable]
private class Serialization<T>
{
public List<T> List;
public Serialization(List<T> list)
{
this.List = list;
}
public List<T> ToList()
{
return List;
}
}
}
[System.Serializable]
public class accountData
{//30
public string id ;
public List<int> completeLevels ;
public List<int> collectAchievements;
public List<int> haveSword;
public List<int> haveHead ;
public List<int> haveSkins ;
public List<int> haveAccessory;
public List<int> entityKilled ;
public List<int> otherStat ;
public List<int> haveOther ;
public List<int> passlevel ;
public List<int> 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<int> EL = new List<int> {};
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<int> {0};
//EntityKilled = new List<int> {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<int> CompleteLevels;
List<int> CollectAchievements;
List<int> HaveSword;
List<int> HaveHead;
List<int> HaveSkins;
List<int> HaveAccessory;
List<int> EntityKilled;
List<int> OtherStat;
List<int> HaveOther;
CompleteLevels = new List<int>{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<int> {0};
EntityKilled = new List<int> {0};
OtherStat = new List<int> {PlayerPrefs.GetInt("CountDie"), PlayerPrefs.GetInt("CountWin"), PlayerPrefs.GetInt("MaxMoney"), PlayerPrefs.GetInt("EnemiesKilled")};
HaveOther = new List<int> {PlayerPrefs.GetInt("Moneys"), PlayerPrefs.GetInt("Metall")};
List<List<int>> Data = new List<List<int>> {CompleteLevels, CollectAchievements, HaveSword, HaveHead, HaveSkins, HaveAccessory, EntityKilled, OtherStat, HaveOther};
string Id = "s";
StartCoroutine(save(Data));
}
private IEnumerator save(List<List<int>> 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<accountData>(json);
print(JsonUtility.FromJson<accountData>(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<int> {0};
//EntityKilled = new List<int> {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
}