Skip to content

Commit

Permalink
access handling
Browse files Browse the repository at this point in the history
  • Loading branch information
MilonPL committed Nov 4, 2024
1 parent 315fd0e commit fa7491c
Show file tree
Hide file tree
Showing 11 changed files with 276 additions and 82 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Linq;
using Robust.Client.Graphics;
using Robust.Client.UserInterface.Controls;

namespace Content.Client.DeltaV.CartridgeLoader.Cartridges;
Expand Down Expand Up @@ -32,22 +33,52 @@ public PriceHistoryTable()

AddChild(header);

// Create a panel container with styled background
var panel = new PanelContainer
{
HorizontalExpand = true,
Margin = new Thickness(0, 2, 0, 0)
};

// Create and apply the style
var styleBox = new StyleBoxFlat
{
BackgroundColor = Color.FromHex("#1a1a1a"),
ContentMarginLeftOverride = 6,
ContentMarginRightOverride = 6,
ContentMarginTopOverride = 4,
ContentMarginBottomOverride = 4,
BorderColor = Color.FromHex("#404040"),
BorderThickness = new Thickness(1),
};

panel.PanelOverride = styleBox;

// Create a centering container
var centerContainer = new BoxContainer
{
Orientation = LayoutOrientation.Horizontal,
HorizontalExpand = true,
HorizontalAlignment = HAlignment.Center,
};

// Create grid for price history
_grid = new GridContainer
{
Columns = 5, // Display 5 entries per row
HorizontalExpand = true,
};

AddChild(_grid);
centerContainer.AddChild(_grid);
panel.AddChild(centerContainer);
AddChild(panel);
}

public void Update(List<float> priceHistory)
{
_grid.RemoveAllChildren();

// Take last 10 prices as per StockMarketSystem
var lastTenPrices = priceHistory.TakeLast(10).ToList();
// Take last 5 prices
var lastTenPrices = priceHistory.TakeLast(5).ToList();

for (var i = 0; i < lastTenPrices.Count; i++)
{
Expand All @@ -59,19 +90,19 @@ public void Update(List<float> priceHistory)
{
Orientation = LayoutOrientation.Vertical,
MinWidth = 80,
Margin = new Thickness(2),
HorizontalAlignment = HAlignment.Center,
};

var priceLabel = new Label
{
Text = $"${price:F2}",
HorizontalAlignment = HAlignment.Right,
HorizontalAlignment = HAlignment.Center,
};

var changeLabel = new Label
{
Text = $"{(priceChange >= 0 ? "+" : "")}{priceChange:F2}%",
HorizontalAlignment = HAlignment.Right,
HorizontalAlignment = HAlignment.Center,
StyleClasses = { "LabelSubText" },
Modulate = priceChange switch
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Content.Client.Administration.UI.CustomControls;
using Content.Shared.CartridgeLoader.Cartridges;
using Robust.Client.AutoGenerated;
using Robust.Client.Graphics;
using Robust.Client.UserInterface.Controls;
using Robust.Client.UserInterface.XAML;

Expand Down Expand Up @@ -64,8 +65,14 @@ private sealed class CompanyEntry
private static readonly Color PositiveColor = Color.FromHex("#00ff00"); // Green
private static readonly Color NegativeColor = Color.FromHex("#ff0000"); // Red
private static readonly Color NeutralColor = Color.FromHex("#ffffff"); // White
private static readonly Color BackgroundColor = Color.FromHex("#25252a"); // Dark grey
private static readonly Color BorderColor = Color.FromHex("#404040"); // Light grey

public CompanyEntry(string companyName, Action<string, float>? onBuyPressed, Action<string, float>? onSellPressed)


public CompanyEntry(string companyName,
Action<string, float>? onBuyPressed,
Action<string, float>? onSellPressed)
{
var companyName1 = companyName;

Expand All @@ -86,7 +93,7 @@ public CompanyEntry(string companyName, Action<string, float>? onBuyPressed, Act
Margin = new Thickness(8),
};

// Top row with company name, price, and change
// Top row with company name and price info
var topRow = new BoxContainer
{
Orientation = LayoutOrientation.Horizontal,
Expand All @@ -99,22 +106,47 @@ public CompanyEntry(string companyName, Action<string, float>? onBuyPressed, Act
Text = companyName,
};

_priceLabel = new Label
// Create a panel for price and change
var pricePanel = new PanelContainer
{
MinWidth = 100,
HorizontalAlignment = HAlignment.Right,
};

// Style the price panel
var priceStyleBox = new StyleBoxFlat
{
BackgroundColor = BackgroundColor,
ContentMarginLeftOverride = 8,
ContentMarginRightOverride = 8,
ContentMarginTopOverride = 4,
ContentMarginBottomOverride = 4,
BorderColor = BorderColor,
BorderThickness = new Thickness(1),
};

pricePanel.PanelOverride = priceStyleBox;

// Container for price and change labels
var priceContainer = new BoxContainer
{
Orientation = LayoutOrientation.Horizontal,
};

_priceLabel = new Label();

_changeLabel = new Label
{
MinWidth = 100,
HorizontalAlignment = HAlignment.Right,
Modulate = NeutralColor,
Margin = new Thickness(15, 0, 0, 0),
};

priceContainer.AddChild(_priceLabel);
priceContainer.AddChild(_changeLabel);
pricePanel.AddChild(priceContainer);

topRow.AddChild(_nameLabel);
topRow.AddChild(_priceLabel);
topRow.AddChild(_changeLabel);
topRow.AddChild(pricePanel);

// Add the top row
mainContent.AddChild(topRow);
Expand Down Expand Up @@ -202,23 +234,21 @@ public CompanyEntry(string companyName, Action<string, float>? onBuyPressed, Act
{
var newText = string.Concat(args.Text.Where(char.IsDigit));
if (newText != args.Text)
{
_amountEdit.Text = newText;
}
};
}

public void Update(StockCompanyStruct company, int ownedStocks)
{
_nameLabel.Text = company.Name;
_nameLabel.Text = company.DisplayName;
_priceLabel.Text = $"${company.CurrentPrice:F2}";
_sharesLabel.Text = $"Owned: {ownedStocks}";

var priceChange = 0f;
if (company.PriceHistory.Count > 0)
{
var previousPrice = company.PriceHistory[^1];
priceChange = ((company.CurrentPrice - previousPrice) / previousPrice) * 100;
priceChange = (company.CurrentPrice - previousPrice) / previousPrice * 100;
}

_changeLabel.Text = $"{(priceChange >= 0 ? "+" : "")}{priceChange:F2}%";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,26 @@ public sealed partial class StationStockMarketComponent : Component
["Nanotrasen"] = new StockCompanyStruct
{
Name = "Nanotrasen",
DisplayName = "Nanotrasen [NT]",
CurrentPrice = 100f,
BasePrice = 100f,
PriceHistory = [],
PriceHistory = [100f, 100f, 100f, 100f, 100f], // look somewhere else
},
["Gorlex"] = new StockCompanyStruct
{
Name = "Gorlex",
DisplayName = "Gorlex [GRX]",
CurrentPrice = 75f,
BasePrice = 75f,
PriceHistory = [],
PriceHistory = [75f, 75f, 75f, 75f, 75f],
},
["FishInc"] = new StockCompanyStruct
{
Name = "Fish Inc.",
Name = "FishInc",
DisplayName = "Fish Inc. [FIN]",
CurrentPrice = 25f,
BasePrice = 25f,
PriceHistory = [],
PriceHistory = [25f, 25f, 25f, 25f, 25f],
},
};

Expand All @@ -60,7 +63,13 @@ public sealed partial class StationStockMarketComponent : Component
/// The sound to play after selling or buying stocks
/// </summary>
[DataField]
public SoundSpecifier Sound = new SoundPathSpecifier("/Audio/Effects/Cargo/ping.ogg");
public SoundSpecifier ConfirmSound = new SoundPathSpecifier("/Audio/Effects/Cargo/ping.ogg");

/// <summary>
/// The sound to play if the don't have access to buy or sell stocks
/// </summary>
[DataField]
public SoundSpecifier DenySound = new SoundPathSpecifier("/Audio/Effects/Cargo/buzz_sigh.ogg");

/// <summary>
/// The chance for minor market changes
Expand All @@ -84,7 +93,7 @@ public sealed partial class StationStockMarketComponent : Component
/// The chance for catastrophic market changes
/// </summary>
[DataField]
public float CatastrophicChangeChance = 0.1f; // 1%
public float CatastrophicChangeChance = 0.01f; // 1%

/// <summary>
/// The price range for minor changes
Expand Down
17 changes: 14 additions & 3 deletions Content.Server/DeltaV/Cargo/StocksCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Content.Server.DeltaV.Cargo.Components;
using Content.Server.DeltaV.Cargo.Systems;
using Content.Shared.Administration;
using Content.Shared.CartridgeLoader.Cartridges;
using Robust.Shared.Console;

namespace Content.Server.DeltaV.Cargo;
Expand Down Expand Up @@ -59,26 +60,36 @@ public sealed class AddStocksCompanyCommand : IConsoleCommand

public async void Execute(IConsoleShell shell, string argStr, string[] args)
{
if (args.Length != 2)
if (args.Length != 3)
{
shell.WriteLine(Loc.GetString("shell-wrong-arguments-number"));
return;
}

if (!float.TryParse(args[1], out var basePrice))
if (!float.TryParse(args[2], out var basePrice))
{
shell.WriteError(Loc.GetString("shell-argument-must-be-number"));
return;
}

var name = args[0];
var displayName = args[1];

var company = new StockCompanyStruct
{
Name = name,
DisplayName = displayName,
CurrentPrice = basePrice,
BasePrice = basePrice,
PriceHistory = [basePrice, basePrice, basePrice, basePrice, basePrice],
};

var stockMarket = _entitySystemManager.GetEntitySystem<StockMarketSystem>();

var query = _entityManager.EntityQueryEnumerator<StationStockMarketComponent>();
while (query.MoveNext(out var uid, out var comp))
{
if (stockMarket.TryAddCompany(uid, comp, basePrice, name))
if (stockMarket.TryAddCompany(uid, comp, company))
continue;
shell.WriteLine(Loc.GetString("cmd-addstockscompany-failure"));
return;
Expand Down
Loading

0 comments on commit fa7491c

Please sign in to comment.