XP done
This commit is contained in:
@@ -61,6 +61,7 @@ public class Building : MonoBehaviour
|
||||
Mathf.Clamp(curLevel++,0, buildingData.levels.Count-1);
|
||||
|
||||
DBmanager.UpgradeBuilding(buildingData.buildingName, curLevel);
|
||||
DBmanager.SetXp(DBmanager.Xp + buildingData.levels[curLevel].xpGain);
|
||||
}
|
||||
//
|
||||
// public List<Collider> buildingsInsideMe = new List<Collider>();
|
||||
@@ -104,10 +105,10 @@ public class BuildingState{
|
||||
public Vector3 position;
|
||||
public DateTime lastCollectedTimestamp;
|
||||
|
||||
public BuildingState(string m_id, int m_level, Vector3 m_position){
|
||||
public BuildingState(string m_id, int m_level, Vector3 m_position, DateTime _lastCollectedTimestamp){
|
||||
id = m_id;
|
||||
level = m_level;
|
||||
position = m_position;
|
||||
lastCollectedTimestamp = DBmanager.GetNetworkTime();
|
||||
lastCollectedTimestamp= _lastCollectedTimestamp;
|
||||
}
|
||||
}
|
||||
@@ -41,6 +41,7 @@ public class BuildingLevel{
|
||||
public int level = 0;
|
||||
public List<BuildingStat> stats;
|
||||
public int price = 1000;
|
||||
public int xpGain = 100;
|
||||
|
||||
public BuildingLevel(int _level, List<BuildingStat> _stats, int _price){
|
||||
level = _level;
|
||||
|
||||
@@ -13,6 +13,9 @@ public class DBmanager : MonoBehaviour
|
||||
public static string phpRoot = "http://38.242.232.13/upf/";
|
||||
|
||||
public static string username = null;
|
||||
|
||||
private static float level = 0;
|
||||
private static int xp = 0;
|
||||
private static int coins = 0;
|
||||
private static int gems = 0;
|
||||
private static int metal = 0;
|
||||
@@ -20,10 +23,12 @@ public class DBmanager : MonoBehaviour
|
||||
public static List<BuildingState> buildingStates = new List<BuildingState>();
|
||||
public static UnityEvent OnStateChanged = new UnityEvent();
|
||||
|
||||
public static int Xp => xp;
|
||||
public static int Coins => coins;
|
||||
public static int Gems => gems;
|
||||
public static int Metal => metal;
|
||||
public static int Oxygen => oxygen;
|
||||
public static float Level => level;
|
||||
|
||||
|
||||
public static bool LoggedIn { get { return username != null; } }
|
||||
@@ -49,7 +54,7 @@ public class DBmanager : MonoBehaviour
|
||||
var ipEndPoint = new IPEndPoint(addresses[0], 123);
|
||||
//NTP uses UDP
|
||||
|
||||
using(var socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp))
|
||||
using (var socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp))
|
||||
{
|
||||
socket.Connect(ipEndPoint);
|
||||
|
||||
@@ -86,140 +91,215 @@ public class DBmanager : MonoBehaviour
|
||||
|
||||
static uint SwapEndianness(ulong x)
|
||||
{
|
||||
return (uint) (((x & 0x000000ff) << 24) +
|
||||
return (uint)(((x & 0x000000ff) << 24) +
|
||||
((x & 0x0000ff00) << 8) +
|
||||
((x & 0x00ff0000) >> 8) +
|
||||
((x & 0xff000000) >> 24));
|
||||
}
|
||||
|
||||
public async static Task SetXp(int newValue, bool justOffline = false)
|
||||
{
|
||||
WWWForm form = new WWWForm();
|
||||
form.AddField("name", username);
|
||||
form.AddField("xp", newValue);
|
||||
if (justOffline) { xp = newValue; }
|
||||
else
|
||||
{
|
||||
using (UnityWebRequest www = UnityWebRequest.Post(phpRoot + "set_xp.php", form))
|
||||
{
|
||||
var operation = www.SendWebRequest();
|
||||
while (!operation.isDone)
|
||||
{
|
||||
await Task.Yield();
|
||||
}
|
||||
|
||||
public async static Task SetCoins(int newValue, bool justOffline = false){
|
||||
if (www.downloadHandler.text == "0")
|
||||
{
|
||||
xp = newValue;
|
||||
// Debug
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.Log("Response : " + www.downloadHandler.text);
|
||||
Debug.LogWarning("Failed to set xp to " + newValue);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// for(int i =0; i < xp / 100; i++){
|
||||
// i
|
||||
// }
|
||||
|
||||
level = 1 + (Mathf.Sqrt((float)xp / 100f));
|
||||
Debug.Log("Level : " + (float)xp / 100f + " : " + level);
|
||||
GameManagerInstance.gameManager.RefreshData();
|
||||
OnStateChanged.Invoke();
|
||||
}
|
||||
|
||||
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)){
|
||||
if (justOffline) { coins = newValue; return; }
|
||||
using (UnityWebRequest www = UnityWebRequest.Post(phpRoot + "set_coins.php", form))
|
||||
{
|
||||
var operation = www.SendWebRequest();
|
||||
while(!operation.isDone){
|
||||
while (!operation.isDone)
|
||||
{
|
||||
await Task.Yield();
|
||||
}
|
||||
|
||||
if(www.downloadHandler.text=="0"){
|
||||
if (www.downloadHandler.text == "0")
|
||||
{
|
||||
coins = newValue;
|
||||
}else{
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.Log("Response : " + www.downloadHandler.text);
|
||||
Debug.LogWarning("Failed to set coins to " + newValue);
|
||||
}
|
||||
}
|
||||
|
||||
GameManagerInstance.gameManager.RefreshData();
|
||||
OnStateChanged.Invoke();
|
||||
}
|
||||
|
||||
public async static void SetGems(int newValue,bool justOffline=false){
|
||||
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)){
|
||||
if (justOffline) { gems = newValue; return; }
|
||||
using (UnityWebRequest www = UnityWebRequest.Post(phpRoot + "set_gems.php", form))
|
||||
{
|
||||
var operation = www.SendWebRequest();
|
||||
while(!operation.isDone){
|
||||
while (!operation.isDone)
|
||||
{
|
||||
await Task.Yield();
|
||||
}
|
||||
|
||||
if(www.downloadHandler.text=="0"){
|
||||
if (www.downloadHandler.text == "0")
|
||||
{
|
||||
gems = newValue;
|
||||
}else{
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.Log("Response : " + www.downloadHandler.text);
|
||||
Debug.LogWarning("Failed to set gems to " + newValue);
|
||||
}
|
||||
}
|
||||
|
||||
GameManagerInstance.gameManager.RefreshData();
|
||||
OnStateChanged.Invoke();
|
||||
}
|
||||
|
||||
public async static void SetMetal(int newValue,bool justOffline=false){
|
||||
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)){
|
||||
if (justOffline) { metal = newValue; return; }
|
||||
using (UnityWebRequest www = UnityWebRequest.Post(phpRoot + "set_metal.php", form))
|
||||
{
|
||||
var operation = www.SendWebRequest();
|
||||
while(!operation.isDone){
|
||||
while (!operation.isDone)
|
||||
{
|
||||
await Task.Yield();
|
||||
}
|
||||
|
||||
if(www.downloadHandler.text=="0"){
|
||||
if (www.downloadHandler.text == "0")
|
||||
{
|
||||
metal = newValue;
|
||||
}else{
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.Log("Response : " + www.downloadHandler.text);
|
||||
Debug.LogWarning("Failed to set metal to " + newValue);
|
||||
}
|
||||
}
|
||||
|
||||
GameManagerInstance.gameManager.RefreshData();
|
||||
OnStateChanged.Invoke();
|
||||
}
|
||||
|
||||
public async static void SetOxygen(int newValue,bool justOffline=false){
|
||||
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)){
|
||||
if (justOffline) { oxygen = newValue; return; }
|
||||
using (UnityWebRequest www = UnityWebRequest.Post(phpRoot + "set_oxygen.php", form))
|
||||
{
|
||||
var operation = www.SendWebRequest();
|
||||
while(!operation.isDone){
|
||||
while (!operation.isDone)
|
||||
{
|
||||
await Task.Yield();
|
||||
}
|
||||
|
||||
if(www.downloadHandler.text=="0"){
|
||||
if (www.downloadHandler.text == "0")
|
||||
{
|
||||
oxygen = newValue;
|
||||
}else{
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.Log("Response : " + www.downloadHandler.text);
|
||||
Debug.LogWarning("Failed to set oxygen to " + newValue);
|
||||
}
|
||||
}
|
||||
|
||||
GameManagerInstance.gameManager.RefreshData();
|
||||
OnStateChanged.Invoke();
|
||||
}
|
||||
|
||||
public static bool GetBuildingStates(string rawData){
|
||||
public static bool GetBuildingStates(string rawData)
|
||||
{
|
||||
bool success = false;
|
||||
try{
|
||||
//try{
|
||||
|
||||
buildingStates = JsonConvert.DeserializeObject<List<BuildingState>>(rawData);
|
||||
Debug.Log("Updating buildings data, isNull: " + (buildingStates==null).ToString());
|
||||
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;
|
||||
buildingStates = JsonConvert.DeserializeObject<List<BuildingState>>(rawData);
|
||||
Debug.Log("Updating buildings data, isNull: " + (buildingStates == null).ToString());
|
||||
if (buildingStates == null)
|
||||
{
|
||||
buildingStates = new List<BuildingState>();
|
||||
}
|
||||
success = true;
|
||||
// }catch(Exception e){
|
||||
// Debug.LogError(e.Message+'\n' + e.StackTrace);
|
||||
//
|
||||
// Debug.LogError("Error updating buildings from server, Response:" + rawData);
|
||||
// success=false;
|
||||
// }
|
||||
|
||||
OnStateChanged.Invoke();
|
||||
return success;
|
||||
}
|
||||
|
||||
public async static Task AddBuilding(BuildingData buildingData){
|
||||
public async static Task AddBuilding(BuildingData buildingData)
|
||||
{
|
||||
|
||||
foreach (BuildingState buildingState in buildingStates){
|
||||
if(buildingState.id == buildingData.buildingName){
|
||||
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,Vector3.zero));
|
||||
Debug.Log("Added new building "+ buildingData.buildingName);
|
||||
Debug.Log("adding new building " + buildingData.buildingName);
|
||||
buildingStates.Add(new BuildingState(buildingData.buildingName, 0, Vector3.zero, GetNetworkTime()));
|
||||
Debug.Log("Added new building " + buildingData.buildingName);
|
||||
await UpdateBuildingsToServer();
|
||||
|
||||
OnStateChanged.Invoke();
|
||||
SetXp(xp + buildingData.levels[0].xpGain);
|
||||
}
|
||||
|
||||
public async static Task UpgradeBuilding(string id, int newLevel){
|
||||
for(int i=0; i < buildingStates.Count; i++){
|
||||
if(buildingStates[i].id == id){
|
||||
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;
|
||||
@@ -232,9 +312,12 @@ public class DBmanager : MonoBehaviour
|
||||
}
|
||||
|
||||
|
||||
public async static Task CollectBuilding(string id){
|
||||
for(int i=0; i < buildingStates.Count; i++){
|
||||
if(buildingStates[i].id == id){
|
||||
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;
|
||||
@@ -246,9 +329,12 @@ public class DBmanager : MonoBehaviour
|
||||
OnStateChanged.Invoke();
|
||||
}
|
||||
|
||||
public async static Task RelocateBuilding(string id, Vector3 newPosition){
|
||||
for(int i=0; i < buildingStates.Count; i++){
|
||||
if(buildingStates[i].id == id){
|
||||
public async static Task RelocateBuilding(string id, Vector3 newPosition)
|
||||
{
|
||||
for (int i = 0; i < buildingStates.Count; i++)
|
||||
{
|
||||
if (buildingStates[i].id == id)
|
||||
{
|
||||
buildingStates[i].position = newPosition;
|
||||
Debug.Log("Relocating " + id + " to " + newPosition);
|
||||
// break;
|
||||
@@ -260,7 +346,8 @@ public class DBmanager : MonoBehaviour
|
||||
OnStateChanged.Invoke();
|
||||
}
|
||||
|
||||
public async static Task UpdateBuildingsToServer(){
|
||||
public async static Task UpdateBuildingsToServer()
|
||||
{
|
||||
string buildingsJson = JsonConvert.SerializeObject(buildingStates);
|
||||
Debug.Log(buildingsJson);
|
||||
|
||||
@@ -268,15 +355,20 @@ public class DBmanager : MonoBehaviour
|
||||
form.AddField("name", username);
|
||||
form.AddField("buildings", buildingsJson);
|
||||
|
||||
using(UnityWebRequest www = UnityWebRequest.Post(phpRoot + "set_buildings.php",form)){
|
||||
using (UnityWebRequest www = UnityWebRequest.Post(phpRoot + "set_buildings.php", form))
|
||||
{
|
||||
var operation = www.SendWebRequest();
|
||||
while(!operation.isDone){
|
||||
while (!operation.isDone)
|
||||
{
|
||||
await Task.Yield();
|
||||
}
|
||||
|
||||
if(www.downloadHandler.text=="0"){
|
||||
if (www.downloadHandler.text == "0")
|
||||
{
|
||||
|
||||
}else{
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.Log("Response : " + www.downloadHandler.text);
|
||||
Debug.LogWarning("Failed to set buildings to " + buildingsJson);
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ using UnityEngine;
|
||||
using TMPro;
|
||||
using UnityEngine.Events;
|
||||
using UnityEngine.SceneManagement;
|
||||
using UnityEngine.UI;
|
||||
|
||||
public class GameManager : MonoBehaviour
|
||||
{
|
||||
@@ -12,7 +13,8 @@ public class GameManager : MonoBehaviour
|
||||
public TMP_Text gemsTxt;
|
||||
public TMP_Text metalTxt;
|
||||
public TMP_Text oxygenTxt;
|
||||
|
||||
public TMP_Text levelTxt;
|
||||
public Slider levelSlider;
|
||||
void Start()
|
||||
{
|
||||
GameManagerInstance.gameManager = this;
|
||||
@@ -39,6 +41,10 @@ public class GameManager : MonoBehaviour
|
||||
gemsTxt.text = DBmanager.Gems.ToString();
|
||||
metalTxt.text = DBmanager.Metal.ToString();
|
||||
oxygenTxt.text = DBmanager.Oxygen.ToString();
|
||||
levelTxt.text = Mathf.CeilToInt(DBmanager.Level).ToString();
|
||||
|
||||
float levelExcess = DBmanager.Level - Mathf.FloorToInt(DBmanager.Level);
|
||||
levelSlider.value = levelExcess;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -86,12 +86,15 @@ public class LoginManager : MonoBehaviour
|
||||
PlayerPrefs.SetString("password", login_password.text);
|
||||
PlayerPrefs.Save();}
|
||||
DBmanager.username = login_username.text;
|
||||
DBmanager.SetXp(int.Parse(www.text.Split('\t')[6]),true);
|
||||
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);
|
||||
|
||||
Debug.Log("XP : " + DBmanager.Xp);
|
||||
|
||||
UnityEngine.SceneManagement.SceneManager.LoadScene(1);
|
||||
}
|
||||
else
|
||||
|
||||
Reference in New Issue
Block a user