Inventory implemented

This commit is contained in:
2022-05-18 14:51:42 +05:30
31 changed files with 2265 additions and 1045 deletions

View File

@@ -20,6 +20,7 @@ public class DBmanager : MonoBehaviour
private static int gems = 0;
private static int metal = 0;
private static int oxygen = 0;
private static List<InventoryEntry> inventory;
private static List<int> expPassCollected = new List<int>();
public static List<BuildingState> buildingStates = new List<BuildingState>();
public static UnityEvent OnStateChanged = new UnityEvent();
@@ -32,6 +33,7 @@ public class DBmanager : MonoBehaviour
public static float Level => level;
public static int LevelInt => Mathf.CeilToInt(level);
public static List<int> ExpPassCollected => expPassCollected;
public static List<InventoryEntry> Inventory => inventory;
public static bool LoggedIn { get { return username != null; } }
@@ -43,21 +45,25 @@ public class DBmanager : MonoBehaviour
public static async Task<DateTime> GetNetworkTime()
{
int unixTime = 0;
using (UnityWebRequest www = UnityWebRequest.Get(phpRoot + "get_time.php"))
using (UnityWebRequest www = UnityWebRequest.Get(phpRoot + "get_time.php"))
{
var operation = www.SendWebRequest();
while (!operation.isDone)
{
var operation = www.SendWebRequest();
while (!operation.isDone)
{
await Task.Yield();
}
try{
unixTime = int.Parse(www.downloadHandler.text);
}catch{
Debug.Log("Invalid response from server : " + www.downloadHandler.text);
}
await Task.Yield();
}
try
{
unixTime = int.Parse(www.downloadHandler.text);
}
catch
{
Debug.Log("Invalid response from server : " + www.downloadHandler.text);
}
}
if(unixTime> 0){
if (unixTime > 0)
{
return DateTimeOffset.FromUnixTimeSeconds(unixTime).UtcDateTime;
}
@@ -151,10 +157,11 @@ public class DBmanager : MonoBehaviour
// for(int i =0; i < xp / 100; i++){
// i
// }
level = (Mathf.Sqrt((float)Mathf.Clamp(xp,100,float.PositiveInfinity) / 100f));
if(level == LevelInt){
level-=0.1f;
level = (Mathf.Sqrt((float)Mathf.Clamp(xp, 100, float.PositiveInfinity) / 100f));
if (level == LevelInt)
{
level -= 0.1f;
}
Debug.Log("Level : " + (float)xp / 100f + " : " + level + " : " + LevelInt);
GameManagerInstance.gameManager.RefreshData();
@@ -167,7 +174,7 @@ public class DBmanager : MonoBehaviour
form.AddField("name", username);
form.AddField("coins", newValue);
coins = newValue;
if (justOffline) { return; }
if (justOffline) { return; }
using (UnityWebRequest www = UnityWebRequest.Post(phpRoot + "set_coins.php", form))
{
var operation = www.SendWebRequest();
@@ -280,22 +287,29 @@ public class DBmanager : MonoBehaviour
public static void SetExpPassCollected(string rawData, bool justOffline = false)
{
try{
string[] data = rawData.Split(',');
expPassCollected = new List<int>();
foreach(string item in data){
try{
int itemVal = int.Parse(item);
expPassCollected.Add(itemVal);
}catch {
try
{
string[] data = rawData.Split(',');
expPassCollected = new List<int>();
foreach (string item in data)
{
try
{
int itemVal = int.Parse(item);
expPassCollected.Add(itemVal);
}
catch
{
}
}
}
GameManagerInstance.gameManager.RefreshData();
OnStateChanged.Invoke();
}catch{
GameManagerInstance.gameManager.RefreshData();
OnStateChanged.Invoke();
}
catch
{
}
}
@@ -303,9 +317,11 @@ public class DBmanager : MonoBehaviour
{
expPassCollected.Add(newPassLevel);
string output = "";
for(int i =0;i < expPassCollected.Count;i++){
for (int i = 0; i < expPassCollected.Count; i++)
{
output += expPassCollected[i].ToString();
if(i < expPassCollected.Count -1){
if (i < expPassCollected.Count - 1)
{
output += ",";
}
}
@@ -359,6 +375,8 @@ public class DBmanager : MonoBehaviour
return success;
}
public async static Task AddBuilding(BuildingData buildingData)
{
@@ -371,7 +389,7 @@ public class DBmanager : MonoBehaviour
}
}
Debug.Log("adding new building " + buildingData.buildingName);
buildingStates.Add(new BuildingState(buildingData.buildingName, 0, Vector3.zero,await GetNetworkTime()));
buildingStates.Add(new BuildingState(buildingData.buildingName, 0, Vector3.zero, await GetNetworkTime()));
Debug.Log("Added new building " + buildingData.buildingName);
await UpdateBuildingsToServer();
OnStateChanged.Invoke();
@@ -459,4 +477,70 @@ public class DBmanager : MonoBehaviour
}
}
public static bool GetInventoryFromServer(string rawJson)
{
// try{
inventory = JsonConvert.DeserializeObject<List<InventoryEntry>>(rawJson);
// }catch{
// inventory = null;
// }
if(inventory==null){
Debug.Log("Failed to set inventory, server said : " + rawJson);
inventory= new List<InventoryEntry>();
}
return true;
}
public async static void AddInventoryItem(InventoryItem item)
{
bool exists = false;
foreach(InventoryEntry entry in inventory){
if(entry.Item == item.itemName){
entry.Count++;
exists=true;
break;
}
}
if(!exists){
inventory.Add(new InventoryEntry(item.itemName,1));
}
await UpdateInventoryToServer();
OnStateChanged.Invoke();
}
public async static Task UpdateInventoryToServer()
{
string inventoryJson = JsonConvert.SerializeObject(inventory);
Debug.Log(inventoryJson);
WWWForm form = new WWWForm();
form.AddField("name", username);
form.AddField("inventory", inventoryJson);
using (UnityWebRequest www = UnityWebRequest.Post(phpRoot + "set_inventory.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 inventory to " + inventoryJson);
}
}
}
}

View File

@@ -0,0 +1,25 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[CreateAssetMenu(fileName = "InventoryItem", menuName = "GameData/InventoryItem", order = 1)]
[System.Serializable]
public class InventoryItem : ScriptableObject
{
public string itemName;
public int price;
public int gems;
public int stat;
public Sprite image;
}
[System.Serializable]
public class InventoryEntry{
public string Item;
public int Count;
public InventoryEntry(string item, int count){
Item = item;
Count = count;
}
}

View File

@@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: b98fd43ffe0dd7235a6e6b3ab35e9d33
guid: 1e0f25debf988f94daf1dedf149c9494
MonoImporter:
externalObjects: {}
serializedVersion: 2

View File

@@ -5,7 +5,7 @@ using TMPro;
using UnityEngine.UI;
public class ItemShop : MonoBehaviour
{
public ShopItemData selectedItem;
public InventoryItem selectedItem;
public Image itemImage;
public TMP_Text itemTxt;
@@ -21,7 +21,7 @@ public class ItemShop : MonoBehaviour
SelectShopItem(selectedItem);
}
public void SelectShopItem(ShopItemData selected){
public void SelectShopItem(InventoryItem selected){
selectedItem = selected;
if(selectedItem==null){Debug.LogError("Null shop item selected, Please check this"); return;}
@@ -44,6 +44,7 @@ public class ItemShop : MonoBehaviour
buyBtn.interactable=false;
await DBmanager.SetCoins(DBmanager.Coins - selectedItem.price);
DBmanager.SetGems(DBmanager.Gems- selectedItem.gems);
DBmanager.AddInventoryItem(selectedItem);
SelectShopItem(selectedItem);
}
}

View File

@@ -86,6 +86,8 @@ public class LoginManager : MonoBehaviour
PlayerPrefs.SetString("password", login_password.text);
PlayerPrefs.Save();}
DBmanager.username = login_username.text;
DBmanager.GetInventoryFromServer(www.text.Split('\t')[8]);
DBmanager.SetExpPassCollected(www.text.Split('\t')[7]);
DBmanager.SetXp(int.Parse(www.text.Split('\t')[6]),true);
DBmanager.GetBuildingStates(www.text.Split('\t')[5]);

View File

@@ -1,13 +0,0 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[CreateAssetMenu(fileName = "ShopItem", menuName = "GameData/ShopItemData", order = 1)]
public class ShopItemData : ScriptableObject
{
public string itemName;
public int price;
public int gems;
public int stat;
public Sprite image;
}