Files
wwdpublic/Content.Server/DeltaV/CartridgeLoader/Cartridges/StockTradingCartridgeSystem.cs
sleepyyapril 1e423cabf9 v245.0.0 (and Storage UI V2) (#1799)
Contains:

- Storage UI v2, required for removing DeferredClose.
- Stock market refactor (mostly some basic changes to stock market,
didn't want to make a whole other PR for it)
- Make guidebook remember where you left off
- Any other PRs are purely for fixing issues related to the above PRs or
the engine update.

🆑
- add: Ported Storage UI v2.
- tweak: The guidebook will now remember where you left off.

---------

Co-authored-by: Leon Friedrich <60421075+ElectroJr@users.noreply.github.com>
Co-authored-by: DrSmugleaf <10968691+DrSmugleaf@users.noreply.github.com>
Co-authored-by: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com>
Co-authored-by: deltanedas <39013340+deltanedas@users.noreply.github.com>
Co-authored-by: 12rabbits <53499656+12rabbits@users.noreply.github.com>
Co-authored-by: Pieter-Jan Briers <pieterjan.briers@gmail.com>
Co-authored-by: gluesniffler <159397573+gluesniffler@users.noreply.github.com>
Co-authored-by: AJCM-git <60196617+AJCM-git@users.noreply.github.com>
(cherry picked from commit 3c37ff1c48637d1cdf8bc3c6b1412dad338ea205)
2025-02-28 16:22:37 +03:00

94 lines
3.3 KiB
C#

using System.Linq;
using Content.Server.Cargo.Components;
using Content.Server.DeltaV.Cargo.Components;
using Content.Server.DeltaV.Cargo.Systems;
using Content.Server.Station.Systems;
using Content.Server.CartridgeLoader;
using Content.Shared.Cargo.Components;
using Content.Shared.CartridgeLoader;
using Content.Shared.CartridgeLoader.Cartridges;
namespace Content.Server.DeltaV.CartridgeLoader.Cartridges;
public sealed class StockTradingCartridgeSystem : EntitySystem
{
[Dependency] private readonly CartridgeLoaderSystem _cartridgeLoader = default!;
[Dependency] private readonly StationSystem _station = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<StockTradingCartridgeComponent, CartridgeUiReadyEvent>(OnUiReady);
SubscribeLocalEvent<StockMarketUpdatedEvent>(OnStockMarketUpdated);
SubscribeLocalEvent<StationStockMarketComponent, MapInitEvent>(OnMapInit);
SubscribeLocalEvent<StockTradingCartridgeComponent, BankBalanceUpdatedEvent>(OnBalanceUpdated);
}
private void OnBalanceUpdated(Entity<StockTradingCartridgeComponent> ent, ref BankBalanceUpdatedEvent args)
{
UpdateAllCartridges(args.Station);
}
private void OnUiReady(Entity<StockTradingCartridgeComponent> ent, ref CartridgeUiReadyEvent args)
{
UpdateUI(ent, args.Loader);
}
private void OnStockMarketUpdated(ref StockMarketUpdatedEvent args)
{
UpdateAllCartridges(args.Station);
}
private void OnMapInit(Entity<StationStockMarketComponent> ent, ref MapInitEvent args)
{
// Initialize price history for each company
for (var i = 0; i < ent.Comp.Companies.Count; i++)
{
var company = ent.Comp.Companies[i];
// Create initial price history using base price
company.PriceHistory = new List<float>();
for (var j = 0; j < 5; j++)
{
company.PriceHistory.Add(company.BasePrice);
}
ent.Comp.Companies[i] = company;
}
if (_station.GetOwningStation(ent.Owner) is { } station)
UpdateAllCartridges(station);
}
private void UpdateAllCartridges(EntityUid station)
{
var query = EntityQueryEnumerator<StockTradingCartridgeComponent, CartridgeComponent>();
while (query.MoveNext(out var uid, out var comp, out var cartridge))
{
if (cartridge.LoaderUid is not { } loader || comp.Station != station)
continue;
UpdateUI((uid, comp), loader);
}
}
private void UpdateUI(Entity<StockTradingCartridgeComponent> ent, EntityUid loader)
{
if (_station.GetOwningStation(loader) is { } station)
ent.Comp.Station = station;
if (!TryComp<StationStockMarketComponent>(ent.Comp.Station, out var stockMarket) ||
!TryComp<StationBankAccountComponent>(ent.Comp.Station, out var bankAccount))
return;
// Send the UI state with balance and owned stocks
var state = new StockTradingUiState(
entries: stockMarket.Companies,
ownedStocks: stockMarket.StockOwnership,
balance: bankAccount.Balance
);
_cartridgeLoader.UpdateCartridgeUiState(loader, state);
}
}