Skip to content

Commit

Permalink
WIP: Get upgrades from backend
Browse files Browse the repository at this point in the history
  • Loading branch information
ncontinanza committed Jun 12, 2024
1 parent e9244c6 commit c20861b
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 31 deletions.
8 changes: 8 additions & 0 deletions client/Assets/AddressableAssetsData/OSX.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 29 additions & 15 deletions client/Assets/Scripts/BackendConnection/SocketConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using UnityEditor.Build.Utilities;
using UnityEngine;

public class SocketConnection : MonoBehaviour
Expand Down Expand Up @@ -1004,7 +1005,7 @@ private void AwaitLevelUpDungeonSettlementResponse(byte[] data, Action<User> onL
}
}

public void GetDungeonUpgrades(string userId, Action<User> onLeveledUpUserReceived, Action<string> onError = null)
public void GetDungeonUpgrades(string userId, Action<List<Upgrade>> onUpgradesDataReceived, Action<string> onError = null)
{
GetDungeonUpgrades getDungeonUpgrades = new GetDungeonUpgrades
{
Expand All @@ -1014,13 +1015,13 @@ public void GetDungeonUpgrades(string userId, Action<User> onLeveledUpUserReceiv
{
GetDungeonUpgrades = getDungeonUpgrades
};
currentMessageHandler = (data) => AwaitGetDungeonUpgradesResponse(data, onLeveledUpUserReceived, onError);
currentMessageHandler = (data) => AwaitGetDungeonUpgradesResponse(data, onUpgradesDataReceived, onError);
ws.OnMessage += currentMessageHandler;
ws.OnMessage -= OnWebSocketMessage;
SendWebSocketMessage(request);
}

private void AwaitGetDungeonUpgradesResponse(byte[] data, Action<User> onLeveledUpUserReceived, Action<string> onError = null)
private void AwaitGetDungeonUpgradesResponse(byte[] data, Action<List<Upgrade>> onUpgradesDataReceived, Action<string> onError = null)
{
try
{
Expand All @@ -1029,16 +1030,29 @@ private void AwaitGetDungeonUpgradesResponse(byte[] data, Action<User> onLeveled
WebSocketResponse webSocketResponse = WebSocketResponse.Parser.ParseFrom(data);
if (webSocketResponse.ResponseTypeCase == WebSocketResponse.ResponseTypeOneofCase.Upgrades)
{
GlobalUserData.Instance.AvailableUpgrades = webSocketResponse.Upgrades.Upgrades_.Select(upgrade => new Upgrade
{
id = upgrade.Id,
name = upgrade.Name,
description = upgrade.Description,
cost = upgrade.Cost.ToDictionary(cost => cost.Currency.Name, cost => (int)cost.Amount),
level = (int)upgrade.Level,
maxLevel = (int)upgrade.MaxLevel,
type = upgrade.Type,
value = upgrade.Value
}).ToList();

List<Upgrade> upgrades = ParseUpgradesFromResponse(webSocketResponse.Upgrades);
onUpgradesDataReceived?.Invoke(upgrades);
}
}
catch (Exception e)
{
Debug.LogError(e.Message);
}
}

private List<Upgrade> ParseUpgradesFromResponse(Upgrades upgradesMessage)
{
return upgradesMessage.Upgrades_.Select(upgrade =>
{
return new Upgrade
{
name = upgrade.Name,
description = upgrade.Description,
costs = upgrade.Cost.Select(cost => new CurrencyCost { currency = new Currency { name = cost.Currency.Name }, amount = (int)cost.Amount }).ToList(),
buffs = upgrade.Buffs.Select(buff => new Buff { attribute = buff.Attribute, value = buff.Value, operation = buff.Operation }).ToList()
};
}).ToList();
}
}


6 changes: 3 additions & 3 deletions client/Assets/Scripts/Buff.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

public class Buff
{
string attribute;
float value;
string operation;
public string attribute;
public float value;
public string operation;
}
11 changes: 11 additions & 0 deletions client/Assets/Scripts/Buff.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 1 addition & 7 deletions client/Assets/Scripts/Dungeon/DungeonUpgradesManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,7 @@ void Start()

SocketConnection.Instance.GetDungeonUpgrades(user.id, (upgrades) =>
{
foreach (var group in upgrades.GroupBy(upgrade => upgrade.template.name))
{
GameObject upgradeUIObject = Instantiate(upgradePrefab, upgradesContainer.transform);
upgradeUIObject.GetComponent<DungeonUpgradeUI>().SetUpUpgrade(group.First(), group.Count());
Button unitUpgradeButton = upgradeUIObject.GetComponent<Button>();
unitUpgradeButton.onClick.AddListener(() => ShowUpgradeDetailPopup(group.First()));
}
Debug.Log("Got dungeon upgrades: " + upgrades.Count);
},
(error) =>
{
Expand Down
11 changes: 5 additions & 6 deletions client/Assets/Scripts/Upgrade.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

public class Upgrade
{
string name;
string description;
int group;
List<CurrencyCost> cost;
List<Buff> buffs;

public string name;
public string description;
public int group;
public List<CurrencyCost> costs;
public List<Buff> buffs;
}
1 change: 1 addition & 0 deletions client/Assets/Scripts/User/User.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,5 @@ public class User
public int afkMaxExperienceReward = 0;

public List<(string superCampaignName, string campaignId, string levelId)> supercampaignsProgresses = new List<(string, string, string)>();
public List<Upgrade> upgrades = new List<Upgrade>();
}

0 comments on commit c20861b

Please sign in to comment.