Clean up StoreSystem (#14027)

This commit is contained in:
Nemanja
2023-02-12 07:39:14 -05:00
committed by GitHub
parent e7b18b33aa
commit 351c53e774
9 changed files with 108 additions and 101 deletions

View File

@@ -76,12 +76,12 @@ namespace Content.Server.PDA
UpdatePDAUserInterface(pda); UpdatePDAUserInterface(pda);
} }
private void OnUplinkInit(EntityUid uid, PDAComponent pda, StoreAddedEvent args) private void OnUplinkInit(EntityUid uid, PDAComponent pda, ref StoreAddedEvent args)
{ {
UpdatePDAUserInterface(pda); UpdatePDAUserInterface(pda);
} }
private void OnUplinkRemoved(EntityUid uid, PDAComponent pda, StoreRemovedEvent args) private void OnUplinkRemoved(EntityUid uid, PDAComponent pda, ref StoreRemovedEvent args)
{ {
UpdatePDAUserInterface(pda); UpdatePDAUserInterface(pda);
} }
@@ -132,6 +132,7 @@ namespace Content.Server.PDA
private void OnUIMessage(PDAComponent pda, ServerBoundUserInterfaceMessage msg) private void OnUIMessage(PDAComponent pda, ServerBoundUserInterfaceMessage msg)
{ {
var pdaEnt = pda.Owner;
// todo: move this to entity events // todo: move this to entity events
switch (msg.Message) switch (msg.Message)
{ {
@@ -140,28 +141,28 @@ namespace Content.Server.PDA
break; break;
case PDAToggleFlashlightMessage _: case PDAToggleFlashlightMessage _:
{ {
if (EntityManager.TryGetComponent(pda.Owner, out UnpoweredFlashlightComponent? flashlight)) if (EntityManager.TryGetComponent(pdaEnt, out UnpoweredFlashlightComponent? flashlight))
_unpoweredFlashlight.ToggleLight(flashlight.Owner, flashlight); _unpoweredFlashlight.ToggleLight(pdaEnt, flashlight);
break; break;
} }
case PDAShowUplinkMessage _: case PDAShowUplinkMessage _:
{ {
if (msg.Session.AttachedEntity != null && if (msg.Session.AttachedEntity != null &&
TryComp<StoreComponent>(pda.Owner, out var store)) TryComp<StoreComponent>(pdaEnt, out var store))
_storeSystem.ToggleUi(msg.Session.AttachedEntity.Value, store); _storeSystem.ToggleUi(msg.Session.AttachedEntity.Value, pdaEnt, store);
break; break;
} }
case PDAShowRingtoneMessage _: case PDAShowRingtoneMessage _:
{ {
if (EntityManager.TryGetComponent(pda.Owner, out RingerComponent? ringer)) if (EntityManager.TryGetComponent(pdaEnt, out RingerComponent? ringer))
_ringerSystem.ToggleRingerUI(ringer, msg.Session); _ringerSystem.ToggleRingerUI(ringer, msg.Session);
break; break;
} }
case PDAShowMusicMessage _: case PDAShowMusicMessage _:
{ {
if (TryComp(pda.Owner, out InstrumentComponent? instrument)) if (TryComp(pdaEnt, out InstrumentComponent? instrument))
_instrumentSystem.ToggleInstrumentUi(pda.Owner, msg.Session, instrument); _instrumentSystem.ToggleInstrumentUi(pdaEnt, msg.Session, instrument);
break; break;
} }
} }

View File

@@ -19,7 +19,6 @@ using Content.Shared.Bed.Sleep;
using System.Linq; using System.Linq;
using Content.Server.Maps; using Content.Server.Maps;
using Content.Server.Revenant.Components; using Content.Server.Revenant.Components;
using Content.Server.Store.Components;
using Content.Shared.Emag.Systems; using Content.Shared.Emag.Systems;
using Content.Shared.FixedPoint; using Content.Shared.FixedPoint;
using Content.Shared.Humanoid; using Content.Shared.Humanoid;
@@ -29,7 +28,6 @@ using Content.Shared.Mobs.Systems;
using Content.Shared.Revenant.Components; using Content.Shared.Revenant.Components;
using Robust.Shared.Physics.Components; using Robust.Shared.Physics.Components;
using Robust.Shared.Utility; using Robust.Shared.Utility;
using Content.Shared.Humanoid;
namespace Content.Server.Revenant.EntitySystems; namespace Content.Server.Revenant.EntitySystems;
@@ -180,13 +178,10 @@ public sealed partial class RevenantSystem
essence.Harvested = true; essence.Harvested = true;
ChangeEssenceAmount(uid, essence.EssenceAmount, component); ChangeEssenceAmount(uid, essence.EssenceAmount, component);
if (TryComp<StoreComponent>(uid, out var store)) _store.TryAddCurrency(new Dictionary<string, FixedPoint2>
{ { {component.StolenEssenceCurrencyPrototype, essence.EssenceAmount} }, uid);
_store.TryAddCurrency(new Dictionary<string, FixedPoint2>()
{ {component.StolenEssenceCurrencyPrototype, essence.EssenceAmount} }, store);
}
if (!TryComp<MobStateComponent>(args.Target, out var mobstate)) if (!HasComp<MobStateComponent>(args.Target))
return; return;
if (_mobState.IsAlive(args.Target) || _mobState.IsCritical(args.Target)) if (_mobState.IsAlive(args.Target) || _mobState.IsCritical(args.Target))

View File

@@ -18,7 +18,6 @@ using Content.Shared.Tag;
using Content.Server.Store.Components; using Content.Server.Store.Components;
using Content.Server.Store.Systems; using Content.Server.Store.Systems;
using Content.Shared.FixedPoint; using Content.Shared.FixedPoint;
using Robust.Shared.Player;
using Content.Shared.Maps; using Content.Shared.Maps;
using Content.Shared.Mobs.Systems; using Content.Shared.Mobs.Systems;
using Content.Shared.Physics; using Content.Shared.Physics;
@@ -80,7 +79,7 @@ public sealed partial class RevenantSystem : EntitySystem
} }
//ghost vision //ghost vision
if (TryComp(component.Owner, out EyeComponent? eye)) if (TryComp(uid, out EyeComponent? eye))
eye.VisibilityMask |= (uint) (VisibilityFlags.Ghost); eye.VisibilityMask |= (uint) (VisibilityFlags.Ghost);
var shopaction = new InstantAction(_proto.Index<InstantActionPrototype>("RevenantShop")); var shopaction = new InstantAction(_proto.Index<InstantActionPrototype>("RevenantShop"));
@@ -131,7 +130,7 @@ public sealed partial class RevenantSystem : EntitySystem
FixedPoint2.Min(component.Essence, component.EssenceRegenCap); FixedPoint2.Min(component.Essence, component.EssenceRegenCap);
if (TryComp<StoreComponent>(uid, out var store)) if (TryComp<StoreComponent>(uid, out var store))
_store.UpdateUserInterface(uid, store); _store.UpdateUserInterface(uid, uid, store);
_alerts.ShowAlert(uid, AlertType.Essence, (short) Math.Clamp(Math.Round(component.Essence.Float() / 10f), 0, 16)); _alerts.ShowAlert(uid, AlertType.Essence, (short) Math.Clamp(Math.Round(component.Essence.Float() / 10f), 0, 16));
@@ -172,7 +171,7 @@ public sealed partial class RevenantSystem : EntitySystem
{ {
if (!TryComp<StoreComponent>(uid, out var store)) if (!TryComp<StoreComponent>(uid, out var store))
return; return;
_store.ToggleUi(uid, store); _store.ToggleUi(uid, uid, store);
} }
public void MakeVisible(bool visible) public void MakeVisible(bool visible)

View File

@@ -59,11 +59,6 @@ public sealed class StoreComponent : Component
[ViewVariables] [ViewVariables]
public HashSet<ListingData> LastAvailableListings = new(); public HashSet<ListingData> LastAvailableListings = new();
/// <summary>
/// checks whether or not the store has been opened yet.
/// </summary>
public bool Opened = false;
#region audio #region audio
/// <summary> /// <summary>
/// The sound played to the buyer when a purchase is succesfully made. /// The sound played to the buyer when a purchase is succesfully made.
@@ -76,8 +71,10 @@ public sealed class StoreComponent : Component
/// <summary> /// <summary>
/// Event that is broadcast when a store is added to an entity /// Event that is broadcast when a store is added to an entity
/// </summary> /// </summary>
public sealed class StoreAddedEvent : EntityEventArgs { }; [ByRefEvent]
public readonly record struct StoreAddedEvent;
/// <summary> /// <summary>
/// Event that is broadcast when a store is removed from an entity /// Event that is broadcast when a store is removed from an entity
/// </summary> /// </summary>
public sealed class StoreRemovedEvent : EntityEventArgs { }; [ByRefEvent]
public readonly record struct StoreRemovedEvent;

View File

@@ -3,7 +3,7 @@ using Content.Shared.Store;
namespace Content.Server.Store.Systems; namespace Content.Server.Store.Systems;
public sealed partial class StoreSystem : EntitySystem public sealed partial class StoreSystem
{ {
/// <summary> /// <summary>
/// Refreshes all listings on a store. /// Refreshes all listings on a store.
@@ -64,11 +64,12 @@ public sealed partial class StoreSystem : EntitySystem
/// Gets the available listings for a store /// Gets the available listings for a store
/// </summary> /// </summary>
/// <param name="buyer">Either the account owner, user, or an inanimate object (e.g., surplus bundle)</param> /// <param name="buyer">Either the account owner, user, or an inanimate object (e.g., surplus bundle)</param>
/// <param name="store"></param>
/// <param name="component">The store the listings are coming from.</param> /// <param name="component">The store the listings are coming from.</param>
/// <returns>The available listings.</returns> /// <returns>The available listings.</returns>
public IEnumerable<ListingData> GetAvailableListings(EntityUid buyer, StoreComponent component) public IEnumerable<ListingData> GetAvailableListings(EntityUid buyer, EntityUid store, StoreComponent component)
{ {
return GetAvailableListings(buyer, component.Listings, component.Categories, component.Owner); return GetAvailableListings(buyer, component.Listings, component.Categories, store);
} }
/// <summary> /// <summary>
@@ -81,8 +82,7 @@ public sealed partial class StoreSystem : EntitySystem
/// <returns>The available listings.</returns> /// <returns>The available listings.</returns>
public IEnumerable<ListingData> GetAvailableListings(EntityUid buyer, HashSet<ListingData>? listings, HashSet<string> categories, EntityUid? storeEntity = null) public IEnumerable<ListingData> GetAvailableListings(EntityUid buyer, HashSet<ListingData>? listings, HashSet<string> categories, EntityUid? storeEntity = null)
{ {
if (listings == null) listings ??= GetAllListings();
listings = GetAllListings();
foreach (var listing in listings) foreach (var listing in listings)
{ {

View File

@@ -1,6 +1,5 @@
using Content.Server.Actions; using Content.Server.Actions;
using Content.Server.Administration.Logs; using Content.Server.Administration.Logs;
using Content.Server.Mind.Components;
using Content.Server.Store.Components; using Content.Server.Store.Components;
using Content.Shared.Actions.ActionTypes; using Content.Shared.Actions.ActionTypes;
using Content.Shared.FixedPoint; using Content.Shared.FixedPoint;
@@ -10,11 +9,11 @@ using Content.Shared.Database;
using Robust.Server.GameObjects; using Robust.Server.GameObjects;
using System.Linq; using System.Linq;
using Content.Server.Stack; using Content.Server.Stack;
using Robust.Shared.Player; using Content.Server.UserInterface;
namespace Content.Server.Store.Systems; namespace Content.Server.Store.Systems;
public sealed partial class StoreSystem : EntitySystem public sealed partial class StoreSystem
{ {
[Dependency] private readonly IAdminLogManager _admin = default!; [Dependency] private readonly IAdminLogManager _admin = default!;
[Dependency] private readonly SharedHandsSystem _hands = default!; [Dependency] private readonly SharedHandsSystem _hands = default!;
@@ -25,7 +24,7 @@ public sealed partial class StoreSystem : EntitySystem
private void InitializeUi() private void InitializeUi()
{ {
SubscribeLocalEvent<StoreComponent, StoreRequestUpdateInterfaceMessage>((_,c,r) => UpdateUserInterface(r.Session.AttachedEntity, c)); SubscribeLocalEvent<StoreComponent, StoreRequestUpdateInterfaceMessage>(OnRequestUpdate);
SubscribeLocalEvent<StoreComponent, StoreBuyListingMessage>(OnBuyRequest); SubscribeLocalEvent<StoreComponent, StoreBuyListingMessage>(OnBuyRequest);
SubscribeLocalEvent<StoreComponent, StoreRequestWithdrawMessage>(OnRequestWithdraw); SubscribeLocalEvent<StoreComponent, StoreRequestWithdrawMessage>(OnRequestWithdraw);
} }
@@ -34,45 +33,41 @@ public sealed partial class StoreSystem : EntitySystem
/// Toggles the store Ui open and closed /// Toggles the store Ui open and closed
/// </summary> /// </summary>
/// <param name="user">the person doing the toggling</param> /// <param name="user">the person doing the toggling</param>
/// <param name="component">the store being toggled</param> /// <param name="storeEnt">the store being toggled</param>
public void ToggleUi(EntityUid user, StoreComponent component) /// <param name="component"></param>
public void ToggleUi(EntityUid user, EntityUid storeEnt, StoreComponent? component = null)
{ {
if (!Resolve(storeEnt, ref component))
return;
if (!TryComp<ActorComponent>(user, out var actor)) if (!TryComp<ActorComponent>(user, out var actor))
return; return;
if (!_ui.TryToggleUi(component.Owner, StoreUiKey.Key, actor.PlayerSession)) if (!_ui.TryToggleUi(storeEnt, StoreUiKey.Key, actor.PlayerSession))
return; return;
UpdateUserInterface(user, component); UpdateUserInterface(user, storeEnt, component);
} }
/// <summary> /// <summary>
/// Updates the user interface for a store and refreshes the listings /// Updates the user interface for a store and refreshes the listings
/// </summary> /// </summary>
/// <param name="user">The person who if opening the store ui. Listings are filtered based on this.</param> /// <param name="user">The person who if opening the store ui. Listings are filtered based on this.</param>
/// <param name="store">The store entity itself</param>
/// <param name="component">The store component being refreshed.</param> /// <param name="component">The store component being refreshed.</param>
/// <param name="ui"></param> /// <param name="ui"></param>
public void UpdateUserInterface(EntityUid? user, StoreComponent component, BoundUserInterface? ui = null) public void UpdateUserInterface(EntityUid? user, EntityUid store, StoreComponent? component = null, BoundUserInterface? ui = null)
{ {
if (ui == null) if (!Resolve(store, ref component))
{ return;
ui = _ui.GetUiOrNull(component.Owner, StoreUiKey.Key);
if (ui == null)
return;
}
//if we haven't opened it before, initialize the shit if (ui == null && !_ui.TryGetUi(store, StoreUiKey.Key, out ui))
if (!component.Opened) return;
{
RefreshAllListings(component);
InitializeFromPreset(component.Preset, component);
component.Opened = true;
}
//this is the person who will be passed into logic for all listing filtering. //this is the person who will be passed into logic for all listing filtering.
if (user != null) //if we have no "buyer" for this update, then don't update the listings if (user != null) //if we have no "buyer" for this update, then don't update the listings
{ {
component.LastAvailableListings = GetAvailableListings(component.AccountOwner ?? user.Value, component).ToHashSet(); component.LastAvailableListings = GetAvailableListings(component.AccountOwner ?? user.Value, store, component).ToHashSet();
} }
//dictionary for all currencies, including 0 values for currencies on the whitelist //dictionary for all currencies, including 0 values for currencies on the whitelist
@@ -92,12 +87,22 @@ public sealed partial class StoreSystem : EntitySystem
_ui.SetUiState(ui, state); _ui.SetUiState(ui, state);
} }
private void OnRequestUpdate(EntityUid uid, StoreComponent component, StoreRequestUpdateInterfaceMessage args)
{
UpdateUserInterface(args.Session.AttachedEntity, args.Entity, component);
}
private void BeforeActivatableUiOpen(EntityUid uid, StoreComponent component, BeforeActivatableUIOpenEvent args)
{
UpdateUserInterface(args.User, uid, component);
}
/// <summary> /// <summary>
/// Handles whenever a purchase was made. /// Handles whenever a purchase was made.
/// </summary> /// </summary>
private void OnBuyRequest(EntityUid uid, StoreComponent component, StoreBuyListingMessage msg) private void OnBuyRequest(EntityUid uid, StoreComponent component, StoreBuyListingMessage msg)
{ {
ListingData? listing = component.Listings.FirstOrDefault(x => x.Equals(msg.Listing)); var listing = component.Listings.FirstOrDefault(x => x.Equals(msg.Listing));
if (listing == null) //make sure this listing actually exists if (listing == null) //make sure this listing actually exists
{ {
Logger.Debug("listing does not exist"); Logger.Debug("listing does not exist");
@@ -114,7 +119,7 @@ public sealed partial class StoreSystem : EntitySystem
//condition checking because why not //condition checking because why not
if (listing.Conditions != null) if (listing.Conditions != null)
{ {
var args = new ListingConditionArgs(component.AccountOwner ?? buyer, component.Owner, listing, EntityManager); var args = new ListingConditionArgs(component.AccountOwner ?? buyer, uid, listing, EntityManager);
var conditionsMet = listing.Conditions.All(condition => condition.Condition(args)); var conditionsMet = listing.Conditions.All(condition => condition.Condition(args));
if (!conditionsMet) if (!conditionsMet)
@@ -156,16 +161,13 @@ public sealed partial class StoreSystem : EntitySystem
} }
//log dat shit. //log dat shit.
if (TryComp<MindComponent>(buyer, out var mind)) _admin.Add(LogType.StorePurchase, LogImpact.Low,
{ $"{ToPrettyString(buyer):player} purchased listing \"{Loc.GetString(listing.Name)}\" from {ToPrettyString(uid)}");
_admin.Add(LogType.StorePurchase, LogImpact.Low,
$"{ToPrettyString(mind.Owner):player} purchased listing \"{Loc.GetString(listing.Name)}\" from {ToPrettyString(uid)}");
}
listing.PurchaseAmount++; //track how many times something has been purchased listing.PurchaseAmount++; //track how many times something has been purchased
_audio.PlayEntity(component.BuySuccessSound, msg.Session, uid); //cha-ching! _audio.PlayEntity(component.BuySuccessSound, msg.Session, uid); //cha-ching!
UpdateUserInterface(buyer, component); UpdateUserInterface(buyer, uid, component);
} }
/// <summary> /// <summary>
@@ -206,6 +208,6 @@ public sealed partial class StoreSystem : EntitySystem
} }
component.Balance[msg.Currency] -= msg.Amount; component.Balance[msg.Currency] -= msg.Amount;
UpdateUserInterface(buyer, component); UpdateUserInterface(buyer, uid, component);
} }
} }

View File

@@ -1,14 +1,13 @@
using Content.Server.Stack;
using Content.Server.Store.Components; using Content.Server.Store.Components;
using Content.Shared.FixedPoint; using Content.Shared.FixedPoint;
using Content.Shared.Interaction; using Content.Shared.Interaction;
using Content.Shared.Popups; using Content.Shared.Popups;
using Content.Shared.Store; using Content.Shared.Store;
using Robust.Shared.Player;
using Robust.Shared.Prototypes; using Robust.Shared.Prototypes;
using System.Linq; using System.Linq;
using Content.Server.UserInterface; using Content.Server.UserInterface;
using Content.Shared.Stacks; using Content.Shared.Stacks;
using JetBrains.Annotations;
namespace Content.Server.Store.Systems; namespace Content.Server.Store.Systems;
@@ -26,22 +25,38 @@ public sealed partial class StoreSystem : EntitySystem
base.Initialize(); base.Initialize();
SubscribeLocalEvent<CurrencyComponent, AfterInteractEvent>(OnAfterInteract); SubscribeLocalEvent<CurrencyComponent, AfterInteractEvent>(OnAfterInteract);
SubscribeLocalEvent<StoreComponent, BeforeActivatableUIOpenEvent>((_,c,a) => UpdateUserInterface(a.User, c)); SubscribeLocalEvent<StoreComponent, BeforeActivatableUIOpenEvent>(BeforeActivatableUiOpen);
SubscribeLocalEvent<StoreComponent, MapInitEvent>(OnMapInit);
SubscribeLocalEvent<StoreComponent, ComponentStartup>(OnStartup); SubscribeLocalEvent<StoreComponent, ComponentStartup>(OnStartup);
SubscribeLocalEvent<StoreComponent, ComponentShutdown>(OnShutdown); SubscribeLocalEvent<StoreComponent, ComponentShutdown>(OnShutdown);
InitializeUi(); InitializeUi();
} }
private void OnMapInit(EntityUid uid, StoreComponent component, MapInitEvent args)
{
RefreshAllListings(component);
InitializeFromPreset(component.Preset, uid, component);
}
private void OnStartup(EntityUid uid, StoreComponent component, ComponentStartup args) private void OnStartup(EntityUid uid, StoreComponent component, ComponentStartup args)
{ {
RaiseLocalEvent(uid, new StoreAddedEvent(), true); // for traitors, because the StoreComponent for the PDA can be added at any time.
if (MetaData(uid).EntityLifeStage == EntityLifeStage.MapInitialized)
{
RefreshAllListings(component);
InitializeFromPreset(component.Preset, uid, component);
}
var ev = new StoreAddedEvent();
RaiseLocalEvent(uid, ref ev, true);
} }
private void OnShutdown(EntityUid uid, StoreComponent component, ComponentShutdown args) private void OnShutdown(EntityUid uid, StoreComponent component, ComponentShutdown args)
{ {
RaiseLocalEvent(uid, new StoreRemovedEvent(), true); var ev = new StoreRemovedEvent();
RaiseLocalEvent(uid, ref ev, true);
} }
private void OnAfterInteract(EntityUid uid, CurrencyComponent component, AfterInteractEvent args) private void OnAfterInteract(EntityUid uid, CurrencyComponent component, AfterInteractEvent args)
@@ -52,15 +67,7 @@ public sealed partial class StoreSystem : EntitySystem
if (args.Target == null || !TryComp<StoreComponent>(args.Target, out var store)) if (args.Target == null || !TryComp<StoreComponent>(args.Target, out var store))
return; return;
//if you somehow are inserting cash before the store initializes. args.Handled = TryAddCurrency(GetCurrencyValue(uid, component), args.Target.Value, store);
if (!store.Opened)
{
RefreshAllListings(store);
InitializeFromPreset(store.Preset, store);
store.Opened = true;
}
args.Handled = TryAddCurrency(GetCurrencyValue(component), store);
if (args.Handled) if (args.Handled)
{ {
@@ -74,35 +81,43 @@ public sealed partial class StoreSystem : EntitySystem
/// Gets the value from an entity's currency component. /// Gets the value from an entity's currency component.
/// Scales with stacks. /// Scales with stacks.
/// </summary> /// </summary>
/// <param name="uid"></param>
/// <param name="component"></param> /// <param name="component"></param>
/// <returns>The value of the currency</returns> /// <returns>The value of the currency</returns>
public Dictionary<string, FixedPoint2> GetCurrencyValue(CurrencyComponent component) public Dictionary<string, FixedPoint2> GetCurrencyValue(EntityUid uid, CurrencyComponent component)
{ {
TryComp<StackComponent>(component.Owner, out var stack); var amount = EntityManager.GetComponentOrNull<StackComponent>(uid)?.Count ?? 1;
var amount = stack?.Count ?? 1;
return component.Price.ToDictionary(v => v.Key, p => p.Value * amount); return component.Price.ToDictionary(v => v.Key, p => p.Value * amount);
} }
/// <summary> /// <summary>
/// Tries to add a currency to a store's balance. /// Tries to add a currency to a store's balance.
/// </summary> /// </summary>
/// <param name="component">The currency to add</param> /// <param name="currencyEnt"></param>
/// <param name="storeEnt"></param>
/// <param name="currency">The currency to add</param>
/// <param name="store">The store to add it to</param> /// <param name="store">The store to add it to</param>
/// <returns>Whether or not the currency was succesfully added</returns> /// <returns>Whether or not the currency was succesfully added</returns>
public bool TryAddCurrency(CurrencyComponent component, StoreComponent store) [PublicAPI]
public bool TryAddCurrency(EntityUid currencyEnt, EntityUid storeEnt, StoreComponent? store = null, CurrencyComponent? currency = null)
{ {
return TryAddCurrency(GetCurrencyValue(component), store); if (!Resolve(currencyEnt, ref currency) || !Resolve(storeEnt, ref store))
return false;
return TryAddCurrency(GetCurrencyValue(currencyEnt, currency), storeEnt, store);
} }
/// <summary> /// <summary>
/// Tries to add a currency to a store's balance /// Tries to add a currency to a store's balance
/// </summary> /// </summary>
/// <param name="currency">The value to add to the store</param> /// <param name="currency">The value to add to the store</param>
/// <param name="uid"></param>
/// <param name="store">The store to add it to</param> /// <param name="store">The store to add it to</param>
/// <returns>Whether or not the currency was succesfully added</returns> /// <returns>Whether or not the currency was succesfully added</returns>
public bool TryAddCurrency(Dictionary<string, FixedPoint2> currency, StoreComponent store) public bool TryAddCurrency(Dictionary<string, FixedPoint2> currency, EntityUid uid, StoreComponent? store = null)
{ {
if (!Resolve(uid, ref store))
return false;
//verify these before values are modified //verify these before values are modified
foreach (var type in currency) foreach (var type in currency)
{ {
@@ -116,7 +131,7 @@ public sealed partial class StoreSystem : EntitySystem
store.Balance[type.Key] += type.Value; store.Balance[type.Key] += type.Value;
} }
UpdateUserInterface(null, store); UpdateUserInterface(null, uid, store);
return true; return true;
} }
@@ -124,8 +139,9 @@ public sealed partial class StoreSystem : EntitySystem
/// Initializes a store based on a preset ID /// Initializes a store based on a preset ID
/// </summary> /// </summary>
/// <param name="preset">The ID of a store preset prototype</param> /// <param name="preset">The ID of a store preset prototype</param>
/// <param name="uid"></param>
/// <param name="component">The store being initialized</param> /// <param name="component">The store being initialized</param>
public void InitializeFromPreset(string? preset, StoreComponent component) public void InitializeFromPreset(string? preset, EntityUid uid, StoreComponent component)
{ {
if (preset == null) if (preset == null)
return; return;
@@ -133,23 +149,24 @@ public sealed partial class StoreSystem : EntitySystem
if (!_proto.TryIndex<StorePresetPrototype>(preset, out var proto)) if (!_proto.TryIndex<StorePresetPrototype>(preset, out var proto))
return; return;
InitializeFromPreset(proto, component); InitializeFromPreset(proto, uid, component);
} }
/// <summary> /// <summary>
/// Initializes a store based on a given preset /// Initializes a store based on a given preset
/// </summary> /// </summary>
/// <param name="preset">The StorePresetPrototype</param> /// <param name="preset">The StorePresetPrototype</param>
/// <param name="uid"></param>
/// <param name="component">The store being initialized</param> /// <param name="component">The store being initialized</param>
public void InitializeFromPreset(StorePresetPrototype preset, StoreComponent component) public void InitializeFromPreset(StorePresetPrototype preset, EntityUid uid, StoreComponent component)
{ {
component.Preset = preset.ID; component.Preset = preset.ID;
component.CurrencyWhitelist.UnionWith(preset.CurrencyWhitelist); component.CurrencyWhitelist.UnionWith(preset.CurrencyWhitelist);
component.Categories.UnionWith(preset.Categories); component.Categories.UnionWith(preset.Categories);
if (component.Balance == new Dictionary<string, FixedPoint2>() && preset.InitialBalance != null) //if we don't have a value stored, use the preset if (component.Balance == new Dictionary<string, FixedPoint2>() && preset.InitialBalance != null) //if we don't have a value stored, use the preset
TryAddCurrency(preset.InitialBalance, component); TryAddCurrency(preset.InitialBalance, uid, component);
var ui = _ui.GetUiOrNull(component.Owner, StoreUiKey.Key); var ui = _ui.GetUiOrNull(uid, StoreUiKey.Key);
if (ui != null) if (ui != null)
_ui.SetUiState(ui, new StoreInitializeState(preset.StoreName)); _ui.SetUiState(ui, new StoreInitializeState(preset.StoreName));
} }

View File

@@ -24,7 +24,7 @@ namespace Content.Server.Traitor.Uplink
public int GetTCBalance(StoreComponent component) public int GetTCBalance(StoreComponent component)
{ {
FixedPoint2? tcBalance = component.Balance.GetValueOrDefault(TelecrystalCurrencyPrototype); FixedPoint2? tcBalance = component.Balance.GetValueOrDefault(TelecrystalCurrencyPrototype);
return tcBalance != null ? tcBalance.Value.Int() : 0; return tcBalance?.Int() ?? 0;
} }
/// <summary> /// <summary>
@@ -46,15 +46,14 @@ namespace Content.Server.Traitor.Uplink
} }
var store = EnsureComp<StoreComponent>(uplinkEntity.Value); var store = EnsureComp<StoreComponent>(uplinkEntity.Value);
_store.InitializeFromPreset(uplinkPresetId, store); _store.InitializeFromPreset(uplinkPresetId, uplinkEntity.Value, store);
store.AccountOwner = user; store.AccountOwner = user;
store.Balance.Clear(); store.Balance.Clear();
if (balance != null) if (balance != null)
{ {
store.Balance.Clear(); store.Balance.Clear();
_store.TryAddCurrency( _store.TryAddCurrency(new Dictionary<string, FixedPoint2> { { TelecrystalCurrencyPrototype, balance.Value } }, uplinkEntity.Value, store);
new Dictionary<string, FixedPoint2>() { { TelecrystalCurrencyPrototype, balance.Value } }, store);
} }
// TODO add BUI. Currently can't be done outside of yaml -_- // TODO add BUI. Currently can't be done outside of yaml -_-

View File

@@ -7,7 +7,6 @@ using Content.Shared.FixedPoint;
using Content.Shared.Interaction; using Content.Shared.Interaction;
using Content.Shared.Inventory; using Content.Shared.Inventory;
using Content.Shared.Popups; using Content.Shared.Popups;
using Robust.Shared.Player;
namespace Content.Server.TraitorDeathMatch; namespace Content.Server.TraitorDeathMatch;
@@ -18,8 +17,6 @@ public sealed class TraitorDeathMatchRedemptionSystem : EntitySystem
[Dependency] private readonly UplinkSystem _uplink = default!; [Dependency] private readonly UplinkSystem _uplink = default!;
[Dependency] private readonly StoreSystem _store = default!; [Dependency] private readonly StoreSystem _store = default!;
private const string TcCurrencyPrototype = "Telecrystal";
public override void Initialize() public override void Initialize()
{ {
base.Initialize(); base.Initialize();
@@ -99,7 +96,7 @@ public sealed class TraitorDeathMatchRedemptionSystem : EntitySystem
// 4 is the per-PDA bonus amount // 4 is the per-PDA bonus amount
var transferAmount = _uplink.GetTCBalance(victimUplink) + 4; var transferAmount = _uplink.GetTCBalance(victimUplink) + 4;
victimUplink.Balance.Clear(); victimUplink.Balance.Clear();
_store.TryAddCurrency(new Dictionary<string, FixedPoint2>() { {"Telecrystal", FixedPoint2.New(transferAmount)}}, userUplink); _store.TryAddCurrency(new Dictionary<string, FixedPoint2>() { {"Telecrystal", FixedPoint2.New(transferAmount)}}, userUplink.Owner, userUplink);
EntityManager.DeleteEntity(victimUplink.Owner); EntityManager.DeleteEntity(victimUplink.Owner);