Skip to content

Commit

Permalink
Store Refund - Add more disable scenerios & time to disable refund. (s…
Browse files Browse the repository at this point in the history
…pace-wizards#34671)

* Puts disable refund logic into a helper method. Removes action check. Disables refund on action use.

* Adds check if refund is disabled for the store already

* Adds a way to disable refunds based on time

* Checks if the ent is on the starting map and the end time is below the current time before disabling refund

* Replaces instances of component.RefundAllowed = false with DisableRefund for more consistency and easier tracking

* Adds methods to handle inhand and shooting events

* Removes gamestates

---------

Co-authored-by: ScarKy0 <[email protected]>
  • Loading branch information
keronshb and ScarKy0 authored Feb 15, 2025
1 parent 71b5133 commit 0cb1124
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 10 deletions.
21 changes: 20 additions & 1 deletion Content.Server/Store/StoreRefundComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,31 @@

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);
}
49 changes: 42 additions & 7 deletions Content.Server/Store/Systems/StoreSystem.Refund.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
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;
Expand All @@ -12,22 +15,39 @@ private void InitializeRefund()
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)
Expand All @@ -52,4 +72,19 @@ private void OnRefundTerminating(Entity<StoreRefundComponent> ent, ref EntityTer
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);
}
}
5 changes: 3 additions & 2 deletions Content.Server/Store/Systems/StoreSystem.Ui.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ private void OnBuyRequest(EntityUid uid, StoreComponent component, StoreBuyListi
}

if (!IsOnStartingMap(uid, component))
component.RefundAllowed = false;
DisableRefund(uid, component);

//subtract the cash
foreach (var (currency, amount) in cost)
Expand Down Expand Up @@ -332,7 +332,7 @@ private void OnRequestRefund(EntityUid uid, StoreComponent component, StoreReque

if (!IsOnStartingMap(uid, component))
{
component.RefundAllowed = false;
DisableRefund(uid, component);
UpdateUserInterface(buyer, uid, component);
}

Expand Down Expand Up @@ -376,6 +376,7 @@ private void HandleRefundComp(EntityUid uid, StoreComponent component, EntityUid
component.BoughtEntities.Add(purchase);
var refundComp = EnsureComp<StoreRefundComponent>(purchase);
refundComp.StoreEntity = uid;
refundComp.BoughtTime = _timing.CurTime;
}

private bool IsOnStartingMap(EntityUid store, StoreComponent component)
Expand Down
2 changes: 2 additions & 0 deletions Content.Server/Store/Systems/StoreSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Robust.Shared.Prototypes;
using Robust.Shared.Utility;
using System.Linq;
using Robust.Shared.Timing;
using Content.Shared.Mind;

namespace Content.Server.Store.Systems;
Expand All @@ -22,6 +23,7 @@ public sealed partial class StoreSystem : EntitySystem
{
[Dependency] private readonly IPrototypeManager _proto = default!;
[Dependency] private readonly SharedPopupSystem _popup = default!;
[Dependency] private readonly IGameTiming _timing = default!;

public override void Initialize()
{
Expand Down

0 comments on commit 0cb1124

Please sign in to comment.