Collectables stored + Metal and Oxygen added to game

This commit is contained in:
Sewmina
2022-04-21 07:08:38 +05:30
parent 4e63909cf5
commit fa429899d6
9 changed files with 867 additions and 88 deletions

View File

@@ -12,9 +12,17 @@ public class BuildingData : ScriptableObject
public string description;
public bool collectable;
public CollectablesData.ResourceType resourceType;
public float[] productinoRates;
}
public static class CollectablesData{
public enum ResourceType{
Metal,
Oxygen
}
}
[System.Serializable]
public class BuildingStat{
public string name;

View File

@@ -6,11 +6,18 @@ using UnityEngine.UI;
public class CollectBtn : MonoBehaviour
{
public DateTime lastCollected;
public string buildingId;
public float productionRate;
public double collectableAmount;
public Button btn;
public TMP_Text txt;
public CollectablesData.ResourceType resourceType;
void Start(){
btn.onClick.AddListener(OnClick);
}
void Update()
{
@@ -20,8 +27,25 @@ public class CollectBtn : MonoBehaviour
btn.interactable = collectableAmount > 1;
}
public void Set(DateTime _lastCollected, float _productionRate){
public void Set(string id,DateTime _lastCollected, float _productionRate, CollectablesData.ResourceType _resourceType){
buildingId = id;
lastCollected = _lastCollected;
productionRate=_productionRate;
resourceType = _resourceType;
}
void OnClick(){
collectableAmount=((DateTime.Now - lastCollected).TotalSeconds * productionRate);
lastCollected = DBmanager.GetNetworkTime();
switch (resourceType){
case CollectablesData.ResourceType.Metal:
DBmanager.SetMetal(DBmanager.Metal + (int)collectableAmount);
break;
case CollectablesData.ResourceType.Oxygen:
DBmanager.SetOxygen(DBmanager.Oxygen + (int)collectableAmount);
break;
}
DBmanager.CollectBuilding(buildingId);
}
}

View File

@@ -15,11 +15,16 @@ public class DBmanager : MonoBehaviour
public static string username = null;
private static int coins = 0;
private static int gems = 0;
private static int metal = 0;
private static int oxygen = 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 int Metal => metal;
public static int Oxygen => oxygen;
public static bool LoggedIn { get { return username != null; } }
public static void LogOut()
@@ -78,7 +83,7 @@ public class DBmanager : MonoBehaviour
return networkDateTime.ToLocalTime();
}
// stackoverflow.com/a/3294698/162671
static uint SwapEndianness(ulong x)
{
return (uint) (((x & 0x000000ff) << 24) +
@@ -132,7 +137,52 @@ public class DBmanager : MonoBehaviour
GameManagerInstance.gameManager.RefreshData();
}
public static void GetBuildingStates(string rawData){
public async static void SetMetal(int newValue,bool justOffline=false){
WWWForm form = new WWWForm();
form.AddField("name", username);
form.AddField("metal", newValue);
if(justOffline){metal=newValue; return;}
using(UnityWebRequest www = UnityWebRequest.Post(phpRoot + "set_metal.php",form)){
var operation = www.SendWebRequest();
while(!operation.isDone){
await Task.Yield();
}
if(www.downloadHandler.text=="0"){
metal = newValue;
}else{
Debug.Log("Response : " + www.downloadHandler.text);
Debug.LogWarning("Failed to set metal to " + newValue);
}
}
GameManagerInstance.gameManager.RefreshData();
}
public async static void SetOxygen(int newValue,bool justOffline=false){
WWWForm form = new WWWForm();
form.AddField("name", username);
form.AddField("oxygen", newValue);
if(justOffline){oxygen=newValue; return;}
using(UnityWebRequest www = UnityWebRequest.Post(phpRoot + "set_oxygen.php",form)){
var operation = www.SendWebRequest();
while(!operation.isDone){
await Task.Yield();
}
if(www.downloadHandler.text=="0"){
oxygen = newValue;
}else{
Debug.Log("Response : " + www.downloadHandler.text);
Debug.LogWarning("Failed to set oxygen to " + newValue);
}
}
GameManagerInstance.gameManager.RefreshData();
}
public static bool GetBuildingStates(string rawData){
bool success = false;
try{
buildingStates = JsonConvert.DeserializeObject<List<BuildingState>>(rawData);
@@ -140,12 +190,15 @@ public class DBmanager : MonoBehaviour
if(buildingStates==null){
buildingStates = new List<BuildingState>();
}
success=true;
}catch(Exception e){
Debug.LogError(e.Message);
Debug.LogError("Error updating buildings from server, Response:" + rawData);
success=false;
}
OnStateChanged.Invoke();
return success;
}
public async static Task AddBuilding(BuildingData buildingData){
@@ -178,6 +231,21 @@ public class DBmanager : MonoBehaviour
OnStateChanged.Invoke();
}
public async static Task CollectBuilding(string id){
for(int i=0; i < buildingStates.Count; i++){
if(buildingStates[i].id == id){
buildingStates[i].lastCollectedTimestamp = GetNetworkTime();
Debug.Log("Setting " + id + " last collected to " + buildingStates[i].lastCollectedTimestamp);
break;
}
}
await UpdateBuildingsToServer();
OnStateChanged.Invoke();
}
public async static Task RelocateBuilding(string id, Vector3 newPosition){
for(int i=0; i < buildingStates.Count; i++){
if(buildingStates[i].id == id){

View File

@@ -10,6 +10,8 @@ public class GameManager : MonoBehaviour
public TMP_Text usernameTxt;
public TMP_Text coinsTxt;
public TMP_Text gemsTxt;
public TMP_Text metalTxt;
public TMP_Text oxygenTxt;
void Start()
{
@@ -35,6 +37,8 @@ public class GameManager : MonoBehaviour
public void RefreshData(){
coinsTxt.text = DBmanager.Coins.ToString();
gemsTxt.text = DBmanager.Gems.ToString();
metalTxt.text = DBmanager.Metal.ToString();
oxygenTxt.text = DBmanager.Oxygen.ToString();
}
}

View File

@@ -86,7 +86,9 @@ public class LoginManager : MonoBehaviour
PlayerPrefs.SetString("password", login_password.text);
PlayerPrefs.Save();}
DBmanager.username = login_username.text;
DBmanager.GetBuildingStates(www.text.Split('\t')[3]);
DBmanager.GetBuildingStates(www.text.Split('\t')[5]);
DBmanager.SetOxygen(int.Parse(www.text.Split('\t')[4]),true);
DBmanager.SetMetal(int.Parse(www.text.Split('\t')[3]),true);
DBmanager.SetGems(int.Parse(www.text.Split('\t')[2]),true);
DBmanager.SetCoins(int.Parse(www.text.Split('\t')[1]),true);

View File

@@ -52,7 +52,7 @@ public class SelectedItemMenu : MonoBehaviour
if(Selector.selectedData.collectable){
collectBtn.gameObject.SetActive(true);
Debug.Log("Last collected : " +Selector.selectedBuilding.lastCollected );
collectBtn.Set(Selector.selectedBuilding.lastCollected,Selector.selectedData.productinoRates[Selector.selectedBuilding.curLevel]);
collectBtn.Set(Selector.selectedData.buildingName,Selector.selectedBuilding.lastCollected,Selector.selectedData.productinoRates[Selector.selectedBuilding.curLevel],Selector.selectedData.resourceType);
}else{
collectBtn.gameObject.SetActive(false);
}