From b757672bdfcbd1017fb7aef0d0332a3894120553 Mon Sep 17 00:00:00 2001 From: Derek Holloway Date: Mon, 9 Mar 2026 17:12:18 -0700 Subject: [PATCH] Allow selling --- WebServer/Components/Pages/Home.razor | 57 +++++++++++-------- WebServer/Controllers/Payments/IPayments.cs | 3 + .../Controllers/Payments/PaymentTester.cs | 34 +++++++++++ 3 files changed, 71 insertions(+), 23 deletions(-) diff --git a/WebServer/Components/Pages/Home.razor b/WebServer/Components/Pages/Home.razor index 9afe3951..cc669aee 100644 --- a/WebServer/Components/Pages/Home.razor +++ b/WebServer/Components/Pages/Home.razor @@ -41,11 +41,9 @@

Actions

@if (Debounce){ - }else{ - } @@ -73,6 +71,7 @@

Purchased Price: @cur.PurchasePrice

Purchased Quantity: @cur.Quantity

Purchased Date: @cur.PurchaseDate.ToString("M-dd-yyyy")

+
-> @@ -170,30 +169,10 @@ // AI Stuff - string pullButtonText = "Pull Data"; - string trainButtonText = "Train AI"; + string trainButtonText = "Force Retrain AI"; string predictButtonText = "Predict AI"; string resultError = ""; - bool Debounce = true; - async Task pull(){ - resultError = ""; - if (Debounce){ - Debounce = false; - pullButtonText = "Do not refresh the page. The data is pulling."; - await Task.Delay(1); - Task thread = new Task(async () => { - aiModule.PullAI(); - pullButtonText = "Data pulled"; - await InvokeAsync(StateHasChanged); - await Task.Delay(2000); - pullButtonText = "Pull Data"; - await InvokeAsync(StateHasChanged); - Debounce = true; - }); - thread.Start(); - } - } async Task train(){ resultError = ""; @@ -294,4 +273,36 @@ PaymentKey = result.Item2; } } + + void sellStock(PurchasedStock stock){ + string dbPrefix = $"[{userName.ToLower()}]:"; + if (Session != null){ + + // Get sell price + float sellPrice = stock.Quantity * aiModule.GetCurrentPrice( stock.Symbol ); + + // Try to sell the stock + (bool, string) result = PaymentProcessor.TrySell(PaymentKey, sellPrice); + if (!result.Item1){ + resultError = result.Item2; + return; + } + + // Remove Stock + Session.TrackedStocks.Remove(stock); + dbDriver.Set( dbPrefix + "Stocks", Newtonsoft.Json.JsonConvert.SerializeObject(Session.TrackedStocks) ); + + // Reload the users money + bool moneyLoaded = float.TryParse(dbDriver.Get( dbPrefix + "money" ), out float moneyResult); + Session.Money = moneyLoaded ? moneyResult : 1000; + + // Reset the Impodent Key + result = PaymentProcessor.CreatePayment(Session.UserName); + if (!result.Item1){ + resultError = "[New payment session failed] : " + result.Item2; + return; + } + PaymentKey = result.Item2; + } + } } \ No newline at end of file diff --git a/WebServer/Controllers/Payments/IPayments.cs b/WebServer/Controllers/Payments/IPayments.cs index 53f1d9b0..974728eb 100644 --- a/WebServer/Controllers/Payments/IPayments.cs +++ b/WebServer/Controllers/Payments/IPayments.cs @@ -8,6 +8,9 @@ namespace Controllers.Payment { // [ Success, ErrorMessage ] public (bool, string) TryPayment(string ImpodentKey, float Price); + // + public (bool, string) TrySell(string ImpodentKey, float Price); + } } \ No newline at end of file diff --git a/WebServer/Controllers/Payments/PaymentTester.cs b/WebServer/Controllers/Payments/PaymentTester.cs index b3a0c586..c676822f 100644 --- a/WebServer/Controllers/Payments/PaymentTester.cs +++ b/WebServer/Controllers/Payments/PaymentTester.cs @@ -60,6 +60,40 @@ namespace Controllers.Payment { return (false, e.ToString()); } } + + public (bool, string) TrySell(string ImpodentKey, float Price) { + // Find the impotency key + ImpodentKey? found = null; + foreach( ImpodentKey key in ImpodentKeys) { + if (key.Key == ImpodentKey) { + found = key; + break; + } + } + if (found == null) { + return (false, "Payment session closed or never existed"); + } + + // Make sure the database is injected correctly + if (_DBdriver == null) { + found.Processed = true; + return (false, "The Database cannot be loaded"); + } + + // Get the users money + string dbPrefix = $"[{found.User.ToLower()}]:"; + bool passed = float.TryParse(_DBdriver.Get( dbPrefix + "money" ), out float result); + if (!passed) { + found.Processed = true; + return (false, "Unable to parse money from the database"); + } + + // Add the money and save + _DBdriver.Set( dbPrefix + "money", (result + Price).ToString()); + found.PaymentSuccess = true; + found.Processed = true; + return (true, ""); + } } class ImpodentKey {