store system currency rework (#10893)

This commit is contained in:
Nemanja
2022-09-11 02:54:16 -04:00
committed by GitHub
parent 5760151fb2
commit e43ee216f6
9 changed files with 80 additions and 97 deletions

View File

@@ -2,7 +2,6 @@ using Content.Server.Actions;
using Content.Server.Administration.Logs;
using Content.Server.Mind.Components;
using Content.Server.Store.Components;
using Content.Server.UserInterface;
using Content.Shared.Actions.ActionTypes;
using Content.Shared.FixedPoint;
using Content.Shared.Hands.EntitySystems;
@@ -24,6 +23,7 @@ public sealed partial class StoreSystem : EntitySystem
[Dependency] private readonly ActionsSystem _actions = default!;
[Dependency] private readonly SharedAudioSystem _audio = default!;
[Dependency] private readonly StackSystem _stack = default!;
[Dependency] private readonly UserInterfaceSystem _ui = default!;
private void InitializeUi()
{
@@ -42,10 +42,10 @@ public sealed partial class StoreSystem : EntitySystem
if (!TryComp<ActorComponent>(user, out var actor))
return;
var ui = component.Owner.GetUIOrNull(StoreUiKey.Key);
ui?.Toggle(actor.PlayerSession);
if (!_ui.TryToggleUi(component.Owner, StoreUiKey.Key, actor.PlayerSession))
return;
UpdateUserInterface(user, component, ui);
UpdateUserInterface(user, component);
}
/// <summary>
@@ -58,7 +58,7 @@ public sealed partial class StoreSystem : EntitySystem
{
if (ui == null)
{
ui = component.Owner.GetUIOrNull(StoreUiKey.Key);
ui = _ui.GetUiOrNull(component.Owner, StoreUiKey.Key);
if (ui == null)
return;
}
@@ -92,7 +92,7 @@ public sealed partial class StoreSystem : EntitySystem
}
var state = new StoreUpdateState(buyer, component.LastAvailableListings, allCurrency);
ui.SetState(state);
_ui.SetUiState(ui, state);
}
/// <summary>
@@ -114,10 +114,7 @@ public sealed partial class StoreSystem : EntitySystem
if (listing.Conditions != null)
{
var args = new ListingConditionArgs(msg.Buyer, component.Owner, listing, EntityManager);
var conditionsMet = true;
foreach (var condition in listing.Conditions.Where(condition => !condition.Condition(args)))
conditionsMet = false;
var conditionsMet = listing.Conditions.All(condition => condition.Condition(args));
if (!conditionsMet)
return;
@@ -128,7 +125,6 @@ public sealed partial class StoreSystem : EntitySystem
{
if (!component.Balance.TryGetValue(currency.Key, out var balance) || balance < currency.Value)
{
_audio.Play(component.InsufficientFundsSound, Filter.SinglePlayer(msg.Session), uid);
return;
}
}
@@ -187,35 +183,46 @@ public sealed partial class StoreSystem : EntitySystem
return;
//we need an actually valid entity to spawn. This check has been done earlier, but just in case.
if (proto.EntityId == null || !proto.CanWithdraw)
if (proto.Cash == null || !proto.CanWithdraw)
return;
var entproto = _proto.Index<EntityPrototype>(proto.EntityId);
var amountRemaining = msg.Amount;
FixedPoint2 amountRemaining = msg.Amount;
var coordinates = Transform(msg.Buyer).Coordinates;
if (entproto.HasComponent<StackComponent>())
{
while (amountRemaining > 0)
{
var ent = Spawn(proto.EntityId, coordinates);
var stackComponent = Comp<StackComponent>(ent); //we already know it exists
var amountPerStack = Math.Min(stackComponent.MaxCount, amountRemaining);
_stack.SetCount(ent, amountPerStack, stackComponent);
amountRemaining -= amountPerStack;
_hands.TryPickupAnyHand(msg.Buyer, ent);
}
}
else //please for the love of christ give your currency stack component
var sortedCashValues = proto.Cash.Keys.OrderByDescending(x => x).ToList();
foreach (var value in sortedCashValues)
{
while (amountRemaining > 0)
var cashId = proto.Cash[value];
if (!_proto.TryIndex<EntityPrototype>(cashId, out var cashProto))
continue;
//how many times this subdivision fits in the amount remaining
var amountToSpawn = (int) Math.Floor((double) (amountRemaining / value));
if (cashProto.HasComponent<StackComponent>())
{
var ent = Spawn(proto.EntityId, coordinates);
_hands.TryPickupAnyHand(msg.Buyer, ent);
amountRemaining--;
var amountToRemove = amountToSpawn; //we don't want to modify amountToSpawn, as we use it for calculations
while (amountToRemove > 0)
{
var ent = Spawn(cashId, coordinates);
if (!TryComp<StackComponent>(ent, out var stack))
return; //you really fucked up if you got here
var maxAmount = Math.Min(amountToRemove, stack.MaxCount); //limit it based on max stack amount
_stack.SetCount(ent, maxAmount, stack);
_hands.TryPickupAnyHand(msg.Buyer, ent);
amountToRemove -= maxAmount;
}
}
else //please for the love of christ give your currency stack component
{
for (var i = 0; i < amountToSpawn; i++)
{
var ent = Spawn(cashId, coordinates);
_hands.TryPickupAnyHand(msg.Buyer, ent);
}
}
amountRemaining -= value * amountToSpawn;
}
component.Balance[msg.Currency] -= msg.Amount;