141 lines
4.7 KiB
C#
141 lines
4.7 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Threading.Tasks;
|
|
using Newtonsoft.Json;
|
|
using UnityEngine;
|
|
using UnityEngine.Events;
|
|
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 List<BuildingState> buildingStates = new List<BuildingState>();
|
|
public static UnityEvent OnStateChanged = new UnityEvent();
|
|
|
|
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 Task 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();
|
|
}
|
|
|
|
public static void GetBuildingStates(string rawData){
|
|
try{
|
|
|
|
buildingStates = JsonConvert.DeserializeObject<List<BuildingState>>(rawData);
|
|
Debug.Log("Updating buildings data, isNull: " + (buildingStates==null).ToString());
|
|
if(buildingStates==null){
|
|
buildingStates = new List<BuildingState>();
|
|
}
|
|
}catch(Exception e){
|
|
Debug.LogError(e.Message);
|
|
Debug.LogError("Error updating buildings from server, Response:" + rawData);
|
|
}
|
|
|
|
OnStateChanged.Invoke();
|
|
}
|
|
|
|
public async static Task AddBuilding(BuildingData buildingData){
|
|
|
|
foreach (BuildingState buildingState in buildingStates){
|
|
if(buildingState.id == buildingData.buildingName){
|
|
Debug.LogError("Building already exists. Cannot add " + buildingState.id);
|
|
return;
|
|
}
|
|
}
|
|
|
|
buildingStates.Add(new BuildingState(buildingData.buildingName, 0));
|
|
Debug.Log("Added new building "+ buildingData.buildingName);
|
|
await UpdateBuildingsToServer();
|
|
|
|
OnStateChanged.Invoke();
|
|
}
|
|
|
|
public async static Task UpgradeBuilding(string id, int newLevel){
|
|
for(int i=0; i < buildingStates.Count; i++){
|
|
if(buildingStates[i].id == id){
|
|
buildingStates[i].level = newLevel;
|
|
Debug.Log("Upgrading " + id + " to " + newLevel);
|
|
break;
|
|
}
|
|
}
|
|
|
|
await UpdateBuildingsToServer();
|
|
|
|
OnStateChanged.Invoke();
|
|
}
|
|
|
|
public async static Task UpdateBuildingsToServer(){
|
|
string buildingsJson = JsonConvert.SerializeObject(buildingStates);
|
|
Debug.Log(buildingsJson);
|
|
|
|
WWWForm form = new WWWForm();
|
|
form.AddField("name", username);
|
|
form.AddField("buildings", buildingsJson);
|
|
|
|
using(UnityWebRequest www = UnityWebRequest.Post(phpRoot + "set_buildings.php",form)){
|
|
var operation = www.SendWebRequest();
|
|
while(!operation.isDone){
|
|
await Task.Yield();
|
|
}
|
|
|
|
if(www.downloadHandler.text=="0"){
|
|
|
|
}else{
|
|
Debug.Log("Response : " + www.downloadHandler.text);
|
|
Debug.LogWarning("Failed to set buildings to " + buildingsJson);
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|