Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stock market refactor #2141

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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, int company, float amount, BoundUserInterface userInterface)
private static void SendStockTradingUiMessage(StockTradingUiAction action, int company, int 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 @@ -14,8 +14,8 @@ public sealed partial class StockTradingUiFragment : BoxContainer
private readonly Dictionary<int, CompanyEntry> _companyEntries = new();

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

// Define colors
public static readonly Color PositiveColor = Color.FromHex("#00ff00"); // Green
Expand Down Expand Up @@ -70,8 +70,8 @@ private sealed class CompanyEntry

public CompanyEntry(int companyIndex,
string displayName,
Action<int, float>? onBuyPressed,
Action<int, float>? onSellPressed)
Action<int, int>? onBuyPressed,
Action<int, int>? onSellPressed)
{
Container = new BoxContainer
{
Expand Down Expand Up @@ -216,13 +216,13 @@ public CompanyEntry(int companyIndex,
// Button click events
_buyButton.OnPressed += _ =>
{
if (float.TryParse(_amountEdit.Text, out var amount) && amount > 0)
if (int.TryParse(_amountEdit.Text, out var amount) && amount > 0)
onBuyPressed?.Invoke(companyIndex, amount);
};

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

Expand All @@ -235,7 +235,7 @@ public CompanyEntry(int companyIndex,
};
}

public void Update(StockCompanyStruct company, int ownedStocks)
public void Update(StockCompany company, int ownedStocks)
{
_nameLabel.Text = company.LocalizedDisplayName;
_priceLabel.Text = $"${company.CurrentPrice:F2}";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public sealed partial class StationStockMarketComponent : Component
/// The list of companies you can invest in
/// </summary>
[DataField]
public List<StockCompanyStruct> Companies = [];
public List<StockCompany> Companies = [];

/// <summary>
/// The list of shares owned by the station
Expand Down Expand Up @@ -53,19 +53,12 @@ public sealed partial class StationStockMarketComponent : Component
[DataField]
public List<MarketChange> MarketChanges =
[
new() { Chance = 0.86f, Range = new Vector2(-0.05f, 0.05f) }, // Minor
new() { Chance = 0.10f, Range = new Vector2(-0.3f, 0.2f) }, // Moderate
new() { Chance = 0.03f, Range = new Vector2(-0.5f, 1.5f) }, // Major
new() { Chance = 0.01f, Range = new Vector2(-0.9f, 4.0f) }, // Catastrophic
new(0.86f, new Vector2(-0.05f, 0.05f)), // Minor
new(0.10f, new Vector2(-0.3f, 0.2f)), // Moderate
new(0.03f, new Vector2(-0.5f, 1.5f)), // Major
new(0.01f, new Vector2(-0.9f, 4.0f)), // Catastrophic
];
}

[DataDefinition]
public sealed partial class MarketChange
{
[DataField(required: true)]
public float Chance;

[DataField(required: true)]
public Vector2 Range;
}
[DataRecord]
public record struct MarketChange(float Chance, Vector2 Range);
Loading