Golf2D/Assets/Scripts/DataManager.cs
2023-02-19 00:07:15 +05:30

98 lines
3.1 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;
using Newtonsoft.Json;
using UnityEngine;
using UnityEngine.Networking;
public static class DataManager{
public const string API_ENDPOINT = "http://vps.playpoolstudios.com/faucet/golf/api/";
public static UserData userData{get; private set;}
public static async void Login(string username,string password){
WWWForm form = new WWWForm();
form.AddField("username", username);
form.AddField("password", password);
form.AddField("key", "#2CuV1Bit^S!sW1ZcgRv8BhrO");
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<UserData>(request.downloadHandler.text);
Debug.Log("Success parsing userdata");
}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 GoogleLogin(string username){
WWWForm form = new WWWForm();
form.AddField("username", username);
form.AddField("key", "#2CuV1Bit^S!sW1ZcgRv8BhrO");
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);
if(request.downloadHandler.text.Contains("{")){
try{
userData = JsonConvert.DeserializeObject<UserData>(request.downloadHandler.text);
Debug.Log("Success parsing userdata");
}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");
}
}
[System.Serializable]
public class UserData{
public string username;
public int score;
}