Skip to content

Commit

Permalink
address reviews
Browse files Browse the repository at this point in the history
  • Loading branch information
MilonPL committed Nov 4, 2024
1 parent 766fc8c commit 0ffef4b
Show file tree
Hide file tree
Showing 15 changed files with 333 additions and 396 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<cartridges:PriceHistoryTable
xmlns="https://spacestation14.io"
xmlns:cartridges="clr-namespace:Content.Client.DeltaV.CartridgeLoader.Cartridges"
Orientation="Vertical"
HorizontalExpand="True"
Margin="0,5,0,0">

<!-- Header -->
<BoxContainer Orientation="Horizontal" HorizontalExpand="True">
<Label Text="{Loc stock-trading-price-history}"
HorizontalExpand="True"
StyleClasses="LabelSubText" />
</BoxContainer>

<!-- Price history panel -->
<PanelContainer Name="HistoryPanel"
HorizontalExpand="True"
Margin="0,2,0,0">
<BoxContainer Orientation="Horizontal"
HorizontalExpand="True"
HorizontalAlignment="Center">
<GridContainer Name="PriceGrid" Columns="5" />
</BoxContainer>
</PanelContainer>
</cartridges:PriceHistoryTable>
Original file line number Diff line number Diff line change
@@ -1,46 +1,22 @@
using System.Linq;
using Robust.Client.AutoGenerated;
using Robust.Client.Graphics;
using Robust.Client.UserInterface.Controls;
using Robust.Client.UserInterface.XAML;

namespace Content.Client.DeltaV.CartridgeLoader.Cartridges;

public sealed class PriceHistoryTable : BoxContainer
[GenerateTypedNameReferences]
public sealed partial class PriceHistoryTable : BoxContainer
{
private readonly GridContainer _grid;

public PriceHistoryTable()
{
Orientation = LayoutOrientation.Vertical;
HorizontalExpand = true;
Margin = new Thickness(0, 5, 0, 0);
RobustXamlLoader.Load(this);

// Create header
var header = new BoxContainer
{
Orientation = LayoutOrientation.Horizontal,
HorizontalExpand = true,
};

header.AddChild(new Label
{
Text = "Price History",
HorizontalExpand = true,
StyleClasses = { "LabelSubText" }
});

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
// Create the stylebox here so we can use the colors from StockTradingUi
var styleBox = new StyleBoxFlat
{
BackgroundColor = StockTradingUiFragment.BackgroundColor,
BackgroundColor = StockTradingUiFragment.PriceBackgroundColor,
ContentMarginLeftOverride = 6,
ContentMarginRightOverride = 6,
ContentMarginTopOverride = 4,
Expand All @@ -49,30 +25,12 @@ public PriceHistoryTable()
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
};

centerContainer.AddChild(_grid);
panel.AddChild(centerContainer);
AddChild(panel);
HistoryPanel.PanelOverride = styleBox;
}

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

// Take last 5 prices
var lastFivePrices = priceHistory.TakeLast(5).ToList();
Expand Down Expand Up @@ -111,7 +69,7 @@ public void Update(List<float> priceHistory)

entryContainer.AddChild(priceLabel);
entryContainer.AddChild(changeLabel);
_grid.AddChild(entryContainer);
PriceGrid.AddChild(entryContainer);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public override void UpdateState(BoundUserInterfaceState state)
}
}

private static void SendStockTradingUiMessage(StockTradingUiAction action, string company, float amount, BoundUserInterface userInterface)
private static void SendStockTradingUiMessage(StockTradingUiAction action, int company, float amount, BoundUserInterface userInterface)
{
var newsMessage = new StockTradingUiMessageEvent(action, company, amount);
var message = new CartridgeUiMessage(newsMessage);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@
<PanelContainer StyleClasses="AngleRect">
<BoxContainer Orientation="Horizontal" HorizontalExpand="True">
<BoxContainer Orientation="Horizontal" HorizontalExpand="True" Margin="0,0,5,0">
<Label Text="Intergalactic Stock Market"
<Label Text="{Loc stock-trading-title}"
HorizontalExpand="True"
HorizontalAlignment="Left" />
</BoxContainer>
<Label Name="Balance"
Text="Balance: 0"
Text="{Loc stock-trading-balance}"
HorizontalAlignment="Right" />
</BoxContainer>
</PanelContainer>
Expand All @@ -27,7 +27,7 @@
<!-- Main content -->
<BoxContainer Orientation="Vertical" HorizontalExpand="True" VerticalExpand="True">
<Label Name="NoEntries"
Text="No entries"
Text="{Loc stock-trading-no-entries}"
HorizontalExpand="True"
HorizontalAlignment="Center"
Visible="False" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,18 @@ namespace Content.Client.DeltaV.CartridgeLoader.Cartridges;
[GenerateTypedNameReferences]
public sealed partial class StockTradingUiFragment : BoxContainer
{
private readonly Dictionary<string, CompanyEntry> _companyEntries = new();
private readonly Dictionary<int, CompanyEntry> _companyEntries = new();

// Event handlers for the parent UI
public event Action<string, float>? OnBuyButtonPressed;
public event Action<string, float>? OnSellButtonPressed;
public event Action<int, float>? OnBuyButtonPressed;
public event Action<int, float>? OnSellButtonPressed;

// Define colors
public static readonly Color PositiveColor = Color.FromHex("#00ff00"); // Green
public static readonly Color NegativeColor = Color.FromHex("#ff0000"); // Red
public static readonly Color NeutralColor = Color.FromHex("#ffffff"); // White
public static readonly Color BackgroundColor = Color.FromHex("#25252a"); // Dark grey
public static readonly Color PriceBackgroundColor = Color.FromHex("#1a1a1a"); // Darker grey
public static readonly Color BorderColor = Color.FromHex("#404040"); // Light grey

public StockTradingUiFragment()
Expand All @@ -32,26 +33,25 @@ public StockTradingUiFragment()
public void UpdateState(StockTradingUiState state)
{
NoEntries.Visible = state.Entries.Count == 0;
Balance.Text = $"Balance: {state.Balance:F2} credits";
Balance.Text = Loc.GetString("stock-trading-balance", ("balance", state.Balance));

// Remove old entries
foreach (var key in _companyEntries.Keys.ToList().Where(key => state.Entries.All(e => e.Name != key)))
// Clear all existing entries
foreach (var entry in _companyEntries.Values)
{
_companyEntries[key].Container.RemoveAllChildren();
_companyEntries.Remove(key);
entry.Container.RemoveAllChildren();
}
_companyEntries.Clear();
Entries.RemoveAllChildren();

// Update or add new entries
foreach (var company in state.Entries)
// Add new entries
for (var i = 0; i < state.Entries.Count; i++)
{
if (!_companyEntries.TryGetValue(company.Name, out var entry))
{
entry = new CompanyEntry(company.Name, OnBuyButtonPressed, OnSellButtonPressed);
_companyEntries[company.Name] = entry;
Entries.AddChild(entry.Container);
}
var company = state.Entries[i];
var entry = new CompanyEntry(i, company.LocalizedDisplayName, OnBuyButtonPressed, OnSellButtonPressed);
_companyEntries[i] = entry;
Entries.AddChild(entry.Container);

var ownedStocks = state.OwnedStocks.GetValueOrDefault(company.Name, 0);
var ownedStocks = state.OwnedStocks.GetValueOrDefault(i, 0);
entry.Update(company, ownedStocks);
}
}
Expand All @@ -68,12 +68,11 @@ private sealed class CompanyEntry
private readonly LineEdit _amountEdit;
private readonly PriceHistoryTable _priceHistory;

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

Container = new BoxContainer
{
Orientation = LayoutOrientation.Vertical,
Expand Down Expand Up @@ -101,7 +100,7 @@ public CompanyEntry(string companyName,
_nameLabel = new Label
{
HorizontalExpand = true,
Text = companyName,
Text = displayName,
};

// Create a panel for price and change
Expand Down Expand Up @@ -163,13 +162,13 @@ public CompanyEntry(string companyName,

_sharesLabel = new Label
{
Text = "Owned: 0",
Text = Loc.GetString("stock-trading-owned-shares"),
MinWidth = 100,
};

_amountEdit = new LineEdit
{
PlaceHolder = "Amount",
PlaceHolder = Loc.GetString("stock-trading-amount-placeholder"),
HorizontalExpand = true,
MinWidth = 80,
};
Expand All @@ -183,14 +182,14 @@ public CompanyEntry(string companyName,

_buyButton = new Button
{
Text = "Buy",
Text = Loc.GetString("stock-trading-buy-button"),
MinWidth = 65,
Margin = new Thickness(3, 0, 3, 0),
};

_sellButton = new Button
{
Text = "Sell",
Text = Loc.GetString("stock-trading-sell-button"),
MinWidth = 65,
};

Expand Down Expand Up @@ -218,13 +217,13 @@ public CompanyEntry(string companyName,
_buyButton.OnPressed += _ =>
{
if (float.TryParse(_amountEdit.Text, out var amount) && amount > 0)
onBuyPressed?.Invoke(companyName1, amount);
onBuyPressed?.Invoke(companyIndex, amount);
};

_sellButton.OnPressed += _ =>
{
if (float.TryParse(_amountEdit.Text, out var amount) && amount > 0)
onSellPressed?.Invoke(companyName1, amount);
onSellPressed?.Invoke(companyIndex, amount);
};

// There has to be a better way of doing this
Expand All @@ -238,12 +237,12 @@ public CompanyEntry(string companyName,

public void Update(StockCompanyStruct company, int ownedStocks)
{
_nameLabel.Text = company.DisplayName;
_nameLabel.Text = company.LocalizedDisplayName;
_priceLabel.Text = $"${company.CurrentPrice:F2}";
_sharesLabel.Text = $"Owned: {ownedStocks}";
_sharesLabel.Text = Loc.GetString("stock-trading-owned-shares", ("shares", ownedStocks));

var priceChange = 0f;
if (company.PriceHistory.Count > 0)
if (company.PriceHistory is { Count: > 0 })
{
var previousPrice = company.PriceHistory[^1];
priceChange = (company.CurrentPrice - previousPrice) / previousPrice * 100;
Expand All @@ -259,8 +258,9 @@ public void Update(StockCompanyStruct company, int ownedStocks)
_ => NeutralColor,
};

// Update the price history table
_priceHistory.Update(company.PriceHistory);
// Update the price history table if not null
if (company.PriceHistory != null)
_priceHistory.Update(company.PriceHistory);

// Disable sell button if no shares owned
_sellButton.Disabled = ownedStocks <= 0;
Expand Down
Loading

0 comments on commit 0ffef4b

Please sign in to comment.