Skip to content

Commit

Permalink
stocks trading (#2103)
Browse files Browse the repository at this point in the history
* sort of works

* fix

* oh right

* admin abuse

* buy/sell events

* price history, better error handling

* joke

* buying and selling

* access handling

* IT'S FINALLY REAL

* fixes

* AAAAAAAAAAAAAAAAA

* i keep fucking forgetting things

* FUCKING

* move company structs out of the comp

* ok im going to sleep

* Revert "oh right"

This reverts commit 8681e5a.

* Revert "fix"

This reverts commit 3992ec9.

* address reviews
  • Loading branch information
MilonPL authored Nov 6, 2024
1 parent 1e0c4f3 commit e7065f0
Show file tree
Hide file tree
Showing 24 changed files with 1,378 additions and 8 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
@@ -0,0 +1,75 @@
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;

[GenerateTypedNameReferences]
public sealed partial class PriceHistoryTable : BoxContainer
{
public PriceHistoryTable()
{
RobustXamlLoader.Load(this);

// Create the stylebox here so we can use the colors from StockTradingUi
var styleBox = new StyleBoxFlat
{
BackgroundColor = StockTradingUiFragment.PriceBackgroundColor,
ContentMarginLeftOverride = 6,
ContentMarginRightOverride = 6,
ContentMarginTopOverride = 4,
ContentMarginBottomOverride = 4,
BorderColor = StockTradingUiFragment.BorderColor,
BorderThickness = new Thickness(1),
};

HistoryPanel.PanelOverride = styleBox;
}

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

// Take last 5 prices
var lastFivePrices = priceHistory.TakeLast(5).ToList();

for (var i = 0; i < lastFivePrices.Count; i++)
{
var price = lastFivePrices[i];
var previousPrice = i > 0 ? lastFivePrices[i - 1] : price;
var priceChange = ((price - previousPrice) / previousPrice) * 100;

var entryContainer = new BoxContainer
{
Orientation = LayoutOrientation.Vertical,
MinWidth = 80,
HorizontalAlignment = HAlignment.Center,
};

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

var changeLabel = new Label
{
Text = $"{(priceChange >= 0 ? "+" : "")}{priceChange:F2}%",
HorizontalAlignment = HAlignment.Center,
StyleClasses = { "LabelSubText" },
Modulate = priceChange switch
{
> 0 => StockTradingUiFragment.PositiveColor,
< 0 => StockTradingUiFragment.NegativeColor,
_ => StockTradingUiFragment.NeutralColor,
}
};

entryContainer.AddChild(priceLabel);
entryContainer.AddChild(changeLabel);
PriceGrid.AddChild(entryContainer);
}
}
}
45 changes: 45 additions & 0 deletions Content.Client/DeltaV/CartridgeLoader/Cartridges/StockTradingUi.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using Robust.Client.UserInterface;
using Content.Client.UserInterface.Fragments;
using Content.Shared.CartridgeLoader;
using Content.Shared.CartridgeLoader.Cartridges;

namespace Content.Client.DeltaV.CartridgeLoader.Cartridges;

public sealed partial class StockTradingUi : UIFragment
{
private StockTradingUiFragment? _fragment;

public override Control GetUIFragmentRoot()
{
return _fragment!;
}

public override void Setup(BoundUserInterface userInterface, EntityUid? fragmentOwner)
{
_fragment = new StockTradingUiFragment();

_fragment.OnBuyButtonPressed += (company, amount) =>
{
SendStockTradingUiMessage(StockTradingUiAction.Buy, company, amount, userInterface);
};
_fragment.OnSellButtonPressed += (company, amount) =>
{
SendStockTradingUiMessage(StockTradingUiAction.Sell, company, amount, userInterface);
};
}

public override void UpdateState(BoundUserInterfaceState state)
{
if (state is StockTradingUiState cast)
{
_fragment?.UpdateState(cast);
}
}

private static void SendStockTradingUiMessage(StockTradingUiAction action, int company, float amount, BoundUserInterface userInterface)
{
var newsMessage = new StockTradingUiMessageEvent(action, company, amount);
var message = new CartridgeUiMessage(newsMessage);
userInterface.SendMessage(message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<cartridges:StockTradingUiFragment
xmlns="https://spacestation14.io"
xmlns:cartridges="clr-namespace:Content.Client.DeltaV.CartridgeLoader.Cartridges"
xmlns:customControls="clr-namespace:Content.Client.Administration.UI.CustomControls"
Margin="5"
VerticalExpand="True">

<!-- A parent container to hold the balance label and main content -->
<BoxContainer Orientation="Vertical" HorizontalExpand="True" VerticalExpand="True">
<!-- Header section with balance -->
<PanelContainer StyleClasses="AngleRect">
<BoxContainer Orientation="Horizontal" HorizontalExpand="True">
<BoxContainer Orientation="Horizontal" HorizontalExpand="True" Margin="0,0,5,0">
<Label Text="{Loc stock-trading-title}"
HorizontalExpand="True"
HorizontalAlignment="Left" />
</BoxContainer>
<Label Name="Balance"
Text="{Loc stock-trading-balance}"
HorizontalAlignment="Right" />
</BoxContainer>
</PanelContainer>

<!-- Horizontal line under header -->
<customControls:HSeparator Margin="5 3 5 5"/>

<!-- Main content -->
<BoxContainer Orientation="Vertical" HorizontalExpand="True" VerticalExpand="True">
<Label Name="NoEntries"
Text="{Loc stock-trading-no-entries}"
HorizontalExpand="True"
HorizontalAlignment="Center"
Visible="False" />
<ScrollContainer HorizontalExpand="True"
VerticalExpand="True"
Margin="0,5,0,0">
<BoxContainer Name="Entries"
Orientation="Vertical"
VerticalAlignment="Top"
HorizontalExpand="True" />
</ScrollContainer>
</BoxContainer>
</BoxContainer>
</cartridges:StockTradingUiFragment>
Loading

0 comments on commit e7065f0

Please sign in to comment.