diff --git a/Content.Client/GameObjects/Components/PDA/PDABoundUserInterface.cs b/Content.Client/GameObjects/Components/PDA/PDABoundUserInterface.cs index e581f8a021..8f5b4cf999 100644 --- a/Content.Client/GameObjects/Components/PDA/PDABoundUserInterface.cs +++ b/Content.Client/GameObjects/Components/PDA/PDABoundUserInterface.cs @@ -69,7 +69,7 @@ namespace Content.Client.GameObjects.Components.PDA }; } - SendMessage(new PDAUplinkBuyListingMessage(listing)); + SendMessage(new PDAUplinkBuyListingMessage(listing.ItemId)); }; _menu.OnCategoryButtonPressed += (args, category) => diff --git a/Content.Server/GameObjects/Components/PDA/PDAComponent.cs b/Content.Server/GameObjects/Components/PDA/PDAComponent.cs index fc87d0ddf5..bf85b1274c 100644 --- a/Content.Server/GameObjects/Components/PDA/PDAComponent.cs +++ b/Content.Server/GameObjects/Components/PDA/PDAComponent.cs @@ -102,7 +102,7 @@ namespace Content.Server.GameObjects.Components.PDA case PDAUplinkBuyListingMessage buyMsg: { - if (!_uplinkManager.TryPurchaseItem(_syndicateUplinkAccount, buyMsg.ListingToBuy)) + if (!_uplinkManager.TryPurchaseItem(_syndicateUplinkAccount, buyMsg.ItemId)) { SendNetworkMessage(new PDAUplinkInsufficientFundsMessage(), message.Session.ConnectedClient); break; @@ -128,7 +128,7 @@ namespace Content.Server.GameObjects.Components.PDA { var accData = new UplinkAccountData(_syndicateUplinkAccount.AccountHolder, _syndicateUplinkAccount.Balance); - var listings = _uplinkManager.FetchListings.ToArray(); + var listings = _uplinkManager.FetchListings.Values.ToArray(); UserInterface?.SetState(new PDAUpdateState(_lightOn, ownerInfo, accData, listings)); } else diff --git a/Content.Server/Interfaces/PDA/IPDAUplinkManager.cs b/Content.Server/Interfaces/PDA/IPDAUplinkManager.cs index 3ba9bdf5c8..f58fc5d755 100644 --- a/Content.Server/Interfaces/PDA/IPDAUplinkManager.cs +++ b/Content.Server/Interfaces/PDA/IPDAUplinkManager.cs @@ -5,13 +5,15 @@ namespace Content.Server.Interfaces.PDA { public interface IPDAUplinkManager { - public IReadOnlyList FetchListings => null; + public IReadOnlyDictionary FetchListings => null; + void Initialize(); + public bool AddNewAccount(UplinkAccount acc); public bool ChangeBalance(UplinkAccount acc, int amt); - public bool TryPurchaseItem(UplinkAccount acc, UplinkListingData listing); + public bool TryPurchaseItem(UplinkAccount acc, string itemId); } } diff --git a/Content.Server/PDA/PDAUplinkManager.cs b/Content.Server/PDA/PDAUplinkManager.cs index ab2b63213a..c1baaf9533 100644 --- a/Content.Server/PDA/PDAUplinkManager.cs +++ b/Content.Server/PDA/PDAUplinkManager.cs @@ -18,13 +18,13 @@ namespace Content.Server.PDA [Dependency] private readonly IEntityManager _entityManager = default!; private List _accounts; - private List _listings; + private Dictionary _listings; - public IReadOnlyList FetchListings => _listings; + public IReadOnlyDictionary FetchListings => _listings; public void Initialize() { - _listings = new List(); + _listings = new Dictionary(); foreach (var item in _prototypeManager.EnumeratePrototypes()) { var newListing = new UplinkListingData(item.ListingName, item.ItemId, item.Price, item.Category, @@ -40,26 +40,32 @@ namespace Content.Server.PDA { if (!ContainsListing(listing)) { - _listings.Add(listing); + _listings.Add(listing.ItemId, listing); } - } private bool ContainsListing(UplinkListingData listing) { - return _listings.Any(otherListing => listing.Equals(otherListing)); + return _listings.ContainsKey(listing.ItemId); } public bool AddNewAccount(UplinkAccount acc) { var entity = _entityManager.GetEntity(acc.AccountHolder); + if (entity.TryGetComponent(out MindComponent mindComponent)) { - if (mindComponent.Mind.AllRoles.Any(role => !role.Antagonist)) + if (!mindComponent.HasMind) + { + return false; + } + + if (mindComponent.Mind!.AllRoles.Any(role => !role.Antagonist)) { return false; } } + if (_accounts.Contains(acc)) { return false; @@ -72,21 +78,35 @@ namespace Content.Server.PDA public bool ChangeBalance(UplinkAccount acc, int amt) { var account = _accounts.Find(uplinkAccount => uplinkAccount.AccountHolder == acc.AccountHolder); - if (account != null && account.Balance + amt < 0) + + if (account == null) { return false; } + + if (account.Balance + amt < 0) + { + return false; + } + account.ModifyAccountBalance(account.Balance + amt); + return true; } - public bool TryPurchaseItem(UplinkAccount acc, UplinkListingData listing) + public bool TryPurchaseItem(UplinkAccount acc, string itemId) { - if (acc == null || listing == null) + if (acc == null) { return false; } - if (!ContainsListing(listing) || acc.Balance < listing.Price) + + if (!_listings.TryGetValue(itemId, out var listing)) + { + return false; + } + + if (acc.Balance < listing.Price) { return false; } @@ -95,14 +115,12 @@ namespace Content.Server.PDA { return false; } + var player = _entityManager.GetEntity(acc.AccountHolder); var hands = player.GetComponent(); hands.PutInHandOrDrop(_entityManager.SpawnEntity(listing.ItemId, player.Transform.GridPosition).GetComponent()); return true; - } - - } } diff --git a/Content.Shared/GameObjects/Components/PDA/SharedPDAComponent.cs b/Content.Shared/GameObjects/Components/PDA/SharedPDAComponent.cs index cb2fdcd60e..5051cc553c 100644 --- a/Content.Shared/GameObjects/Components/PDA/SharedPDAComponent.cs +++ b/Content.Shared/GameObjects/Components/PDA/SharedPDAComponent.cs @@ -30,7 +30,6 @@ namespace Content.Shared.GameObjects.Components.PDA } } - [Serializable, NetSerializable] public class PDAUBoundUserInterfaceState : BoundUserInterfaceState { @@ -70,10 +69,11 @@ namespace Content.Shared.GameObjects.Components.PDA [Serializable, NetSerializable] public sealed class PDAUplinkBuyListingMessage : BoundUserInterfaceMessage { - public UplinkListingData ListingToBuy; - public PDAUplinkBuyListingMessage(UplinkListingData itemToBuy) + public string ItemId; + + public PDAUplinkBuyListingMessage(string itemId) { - ListingToBuy = itemToBuy; + ItemId = itemId; } } @@ -96,8 +96,7 @@ namespace Content.Shared.GameObjects.Components.PDA } } - - [NetSerializable, Serializable] + [Serializable, NetSerializable] public struct PDAIdInfoText { public string ActualOwnerName; @@ -105,13 +104,13 @@ namespace Content.Shared.GameObjects.Components.PDA public string JobTitle; } - [NetSerializable, Serializable] + [Serializable, NetSerializable] public enum PDAVisuals { FlashlightLit, } - [NetSerializable, Serializable] + [Serializable, NetSerializable] public enum PDAUiKey { Key @@ -142,7 +141,7 @@ namespace Content.Shared.GameObjects.Components.PDA } } - [NetSerializable, Serializable] + [Serializable, NetSerializable] public class UplinkAccountData { public EntityUid DataAccountHolder; @@ -155,7 +154,7 @@ namespace Content.Shared.GameObjects.Components.PDA } } - [NetSerializable, Serializable] + [Serializable, NetSerializable] public class UplinkListingData : ComponentState, IEquatable { public string ItemId; @@ -185,5 +184,4 @@ namespace Content.Shared.GameObjects.Components.PDA return ItemId == other.ItemId; } } - }