namespace Content.Server.Store.Components;
+// TODO: Refund on a per-item/action level.
+// Requires a refund button next to each purchase (disabled/invis by default)
+// Interactions with ActionUpgrades would need to be modified to reset all upgrade progress and return the original action purchase to the store.
+
/// <summary>
/// Keeps track of entities bought from stores for refunds, especially useful if entities get deleted before they can be refunded.
/// </summary>
[RegisterComponent, Access(typeof(StoreSystem))]
public sealed partial class StoreRefundComponent : Component
{
- [ViewVariables, DataField]
+ /// <summary>
+ /// The store this entity was bought from
+ /// </summary>
+ [DataField]
public EntityUid? StoreEntity;
+
+ /// <summary>
+ /// The time this entity was bought
+ /// </summary>
+ [DataField]
+ public TimeSpan? BoughtTime;
+
+ /// <summary>
+ /// How long until this entity disables refund purchase?
+ /// </summary>
+ [DataField]
+ public TimeSpan DisableTime = TimeSpan.FromSeconds(300);
}
using Content.Server.Store.Components;
+using Content.Shared.Actions.Events;
+using Content.Shared.Interaction.Events;
using Content.Shared.Store.Components;
+using Content.Shared.Weapons.Ranged.Systems;
using Robust.Shared.Containers;
namespace Content.Server.Store.Systems;
SubscribeLocalEvent<StoreRefundComponent, EntityTerminatingEvent>(OnRefundTerminating);
SubscribeLocalEvent<StoreRefundComponent, EntRemovedFromContainerMessage>(OnEntityRemoved);
SubscribeLocalEvent<StoreRefundComponent, EntInsertedIntoContainerMessage>(OnEntityInserted);
+ SubscribeLocalEvent<StoreRefundComponent, ActionPerformedEvent>(OnActionPerformed);
+ SubscribeLocalEvent<StoreRefundComponent, UseInHandEvent>(OnUseInHand);
+ SubscribeLocalEvent<StoreRefundComponent, AttemptShootEvent>(OnShootAttempt);
+ // TODO: Handle guardian refund disabling when guardians support refunds.
}
- private void OnEntityRemoved(EntityUid uid, StoreRefundComponent component, EntRemovedFromContainerMessage args)
+ private void OnEntityRemoved(Entity<StoreRefundComponent> ent, ref EntRemovedFromContainerMessage args)
{
- if (component.StoreEntity == null || _actions.TryGetActionData(uid, out _, false) || !TryComp<StoreComponent>(component.StoreEntity.Value, out var storeComp))
- return;
+ CheckDisableRefund(ent);
+ }
- DisableRefund(component.StoreEntity.Value, storeComp);
+ private void OnEntityInserted(Entity<StoreRefundComponent> ent, ref EntInsertedIntoContainerMessage args)
+ {
+ CheckDisableRefund(ent);
}
- private void OnEntityInserted(EntityUid uid, StoreRefundComponent component, EntInsertedIntoContainerMessage args)
+ private void OnActionPerformed(Entity<StoreRefundComponent> ent, ref ActionPerformedEvent args)
{
- if (component.StoreEntity == null || _actions.TryGetActionData(uid, out _) || !TryComp<StoreComponent>(component.StoreEntity.Value, out var storeComp))
+ CheckDisableRefund(ent);
+ }
+
+ private void OnUseInHand(Entity<StoreRefundComponent> ent, ref UseInHandEvent args)
+ {
+ args.Handled = true;
+ CheckDisableRefund(ent);
+ }
+
+ private void OnShootAttempt(Entity<StoreRefundComponent> ent, ref AttemptShootEvent args)
+ {
+ if (args.Cancelled)
return;
- DisableRefund(component.StoreEntity.Value, storeComp);
+ CheckDisableRefund(ent);
}
private void OnStoreTerminating(Entity<StoreComponent> ent, ref EntityTerminatingEvent args)
var ev = new RefundEntityDeletedEvent(ent);
RaiseLocalEvent(ent.Comp.StoreEntity.Value, ref ev);
}
+
+ private void CheckDisableRefund(Entity<StoreRefundComponent> ent)
+ {
+ var component = ent.Comp;
+
+ if (component.StoreEntity == null || !TryComp<StoreComponent>(component.StoreEntity.Value, out var storeComp) || !storeComp.RefundAllowed)
+ return;
+
+ var endTime = component.BoughtTime + component.DisableTime;
+
+ if (IsOnStartingMap(component.StoreEntity.Value, storeComp) && _timing.CurTime < endTime)
+ return;
+
+ DisableRefund(component.StoreEntity.Value, storeComp);
+ }
}
}
if (!IsOnStartingMap(uid, component))
- component.RefundAllowed = false;
+ DisableRefund(uid, component);
//subtract the cash
foreach (var (currency, amount) in cost)
if (!IsOnStartingMap(uid, component))
{
- component.RefundAllowed = false;
+ DisableRefund(uid, component);
UpdateUserInterface(buyer, uid, component);
}
component.BoughtEntities.Add(purchase);
var refundComp = EnsureComp<StoreRefundComponent>(purchase);
refundComp.StoreEntity = uid;
+ refundComp.BoughtTime = _timing.CurTime;
}
private bool IsOnStartingMap(EntityUid store, StoreComponent component)