UPF/Assets/Game/Scripts/DBmanager.cs

68 lines
2.2 KiB
C#

using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.Networking;
public class DBmanager : MonoBehaviour
{
public static string phpRoot = "http://38.242.232.13/upf/";
public static string username = null;
private static int coins = 0;
private static int gems = 0;
public static int Coins => coins;
public static int Gems => gems;
public static bool LoggedIn { get { return username != null; } }
public static void LogOut()
{
username = null;
}
public async static void SetCoins(int newValue, bool justOffline = false){
WWWForm form = new WWWForm();
form.AddField("name", username);
form.AddField("coins", newValue);
if(justOffline){coins=newValue; return;}
using(UnityWebRequest www = UnityWebRequest.Post(phpRoot + "set_coins.php",form)){
var operation = www.SendWebRequest();
while(!operation.isDone){
await Task.Yield();
}
if(www.downloadHandler.text=="0"){
coins = newValue;
}else{
Debug.Log("Response : " + www.downloadHandler.text);
Debug.LogWarning("Failed to set coins to " + newValue);
}
}
GameManagerInstance.gameManager.RefreshData();
}
public async static void SetGems(int newValue,bool justOffline=false){
WWWForm form = new WWWForm();
form.AddField("name", username);
form.AddField("gems", newValue);
if(justOffline){gems=newValue; return;}
using(UnityWebRequest www = UnityWebRequest.Post(phpRoot + "set_gems.php",form)){
var operation = www.SendWebRequest();
while(!operation.isDone){
await Task.Yield();
}
if(www.downloadHandler.text=="0"){
gems = newValue;
}else{
Debug.Log("Response : " + www.downloadHandler.text);
Debug.LogWarning("Failed to set gems to " + newValue);
}
}
GameManagerInstance.gameManager.RefreshData();
}
}