using System; using System.Collections; using System.Collections.Generic; using System.Threading.Tasks; using Google; using Newtonsoft.Json; using UnityEngine; using UnityEngine.Networking; public static class DataManager{ public const string API_ENDPOINT = "http://vps.playpoolstudios.com/faucet/golf/api/"; private const string key = "#2CuV1Bit^S!sW1ZcgRv8BhrO"; public static UserData userData{get; private set;} public static bool isLogged{ get{return userData != null;}} public static void Signout(){ GoogleSignIn.DefaultInstance.SignOut(); PlayerPrefs.DeleteAll(); PlayerPrefs.Save(); userData = null; } public static async Task Login(string username,string password){ WWWForm form = new WWWForm(); form.AddField("username", username); form.AddField("password", password); form.AddField("key", key); using (UnityWebRequest request = UnityWebRequest.Post(API_ENDPOINT + "login.php", form)) { var operation = request.SendWebRequest(); while (!operation.isDone) { await Task.Yield(); } Debug.Log("login response: " +request.downloadHandler.text); if(request.downloadHandler.text.Contains("{")){ try{ userData = JsonConvert.DeserializeObject(request.downloadHandler.text); Debug.Log("Success parsing userdata"); PlayerPrefs.SetString("username", username); PlayerPrefs.SetString("password", password); PlayerPrefs.Save(); }catch(Exception e){ Debug.Log("Error parsing userdata"); } }else{ if(request.downloadHandler.text == "0"){ userData = new UserData(){username = username}; Debug.Log("Created local account"); }else{ MessageBox.ShowMessage("Error logging in, Server said\n" +request.downloadHandler.text); return 1; } } } LoadingScreen.LoadLevel("MainMenu"); return 0; } public static async void GoogleLogin(string username){ WWWForm form = new WWWForm(); form.AddField("username", username); form.AddField("key", key); using (UnityWebRequest request = UnityWebRequest.Post(API_ENDPOINT + "google_login.php", form)) { var operation = request.SendWebRequest(); while (!operation.isDone) { await Task.Yield(); } Debug.Log("glogin response: " +request.downloadHandler.text); MessageBox.ShowMessage(request.downloadHandler.text); if(request.downloadHandler.text.Contains("{")){ try{ userData = JsonConvert.DeserializeObject(request.downloadHandler.text); if(userData == null){ throw new NullReferenceException(); } if(userData.username.Length < 3){ throw new IndexOutOfRangeException(); } Debug.Log("Success parsing userdata"); PlayerPrefs.SetString("username", username); PlayerPrefs.SetString("password", username); PlayerPrefs.Save(); }catch(Exception e){ Debug.Log("Error parsing userdata"); } }else{ if(request.downloadHandler.text == "0"){ userData = new UserData(){username = username}; }else{ MessageBox.ShowMessage("Error logging in, Server said\n" +request.downloadHandler.text); return; } } } LoadingScreen.LoadLevel("MainMenu"); } public static async void AddScores(int amount){ WWWForm form = new WWWForm(); Debug.Log(userData.ToString()); form.AddField("username", userData.username); form.AddField("password", userData.password); form.AddField("amount", amount); form.AddField("key", key); using (UnityWebRequest request = UnityWebRequest.Post(API_ENDPOINT + "add_scores.php", form)) { var operation = request.SendWebRequest(); while (!operation.isDone) { await Task.Yield(); } Debug.Log("add scores response: " +request.downloadHandler.text); if(request.downloadHandler.text.Contains("{")){ try{ userData = JsonConvert.DeserializeObject(request.downloadHandler.text); if(userData == null){ throw new NullReferenceException(); } if(userData.username.Length < 3){ throw new IndexOutOfRangeException(); } Debug.Log("Success parsing userdata"); }catch(Exception e){ Debug.Log("Error parsing userdata"); } }else{ MessageBox.ShowMessage("Error Updating scores, Server said\n" +request.downloadHandler.text); } } LoadingScreen.LoadLevel("MainMenu"); } } [System.Serializable] public class UserData{ public int id; public string username; public string password; public int score; public int TopScore; public override string ToString() { return JsonConvert.SerializeObject(this); } }