mirror of
https://github.com/DeltaV-Station/Delta-v.git
synced 2026-10-02 17:17:25 +00:00
* Departmental Economy (#36445) * Cargo Accounts, Request Consoles, and lock boxes * Funding Allocation Computer * final changes * test fix * remove dumb code * ScarKy0 review * first cour * second cour * Update machines.yml * review --------- Co-authored-by: ScarKy0 <106310278+ScarKy0@users.noreply.github.com> Co-authored-by: Milon <milonpl.git@proton.me> (cherry picked from commit12b75beeab) * Fix station income instantly accumulating roundstart (#36572) Fix station money accumulating roundstart (cherry picked from commited3e4c7d4e) * DeltaV departmental economy --------- Co-authored-by: Nemanja <98561806+EmoGarbage404@users.noreply.github.com>
94 lines
3.2 KiB
C#
94 lines
3.2 KiB
C#
using System.Linq;
|
|
using Content.Server.Cargo.Components;
|
|
using Content.Server._DV.Cargo.Components;
|
|
using Content.Server._DV.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._DV.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<BankBalanceUpdatedEvent>(OnBalanceUpdated);
|
|
}
|
|
|
|
private void OnBalanceUpdated(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.Accounts[bankAccount.PrimaryAccount]
|
|
);
|
|
|
|
_cartridgeLoader.UpdateCartridgeUiState(loader, state);
|
|
}
|
|
}
|