diff --git a/WebServer/AIPython/currentprice.py b/WebServer/AIPython/currentprice.py new file mode 100644 index 00000000..8a50b48d --- /dev/null +++ b/WebServer/AIPython/currentprice.py @@ -0,0 +1,7 @@ +import yfinance as yf + +def getCurrentPrice(symbol): + ticker = yf.Ticker(symbol) + data = ticker.history(period="1d", interval="1m") + current_price = data['Close'].iloc[-1] + return current_price \ No newline at end of file diff --git a/WebServer/Components/Pages/Home.razor b/WebServer/Components/Pages/Home.razor index 61abbcaf..9afe3951 100644 --- a/WebServer/Components/Pages/Home.razor +++ b/WebServer/Components/Pages/Home.razor @@ -119,6 +119,10 @@ async Task LoginSession(){ string dbPrefix = $"[{userName.ToLower()}]:"; // Set the DB prefix for the get and set string passwordhash = dbDriver.Get( dbPrefix + "password" ); // Pull the password hash + if (string.IsNullOrEmpty(passwordhash)){ + loginError = "no account found with that username"; + return; + } if (BCrypt.Verify( passWord, passwordhash )){ // If the password is valid List? stocks = JsonConvert.DeserializeObject>( dbDriver.Get( dbPrefix + "Stocks" ) ); bool moneyLoaded = float.TryParse(dbDriver.Get( dbPrefix + "money" ), out float moneyResult); @@ -148,6 +152,11 @@ TrackedStocks = new List(), Money = 1000 }; + (bool, string) result = PaymentProcessor.CreatePayment(Session.UserName); + if (!result.Item1){ + resultError = result.Item2; + } + PaymentKey = result.Item2; }else{ loginError = "account is taken"; } @@ -243,7 +252,6 @@ string buyStockSymbol = ""; string buyStockQuantity = ""; - float StockPrice = 0; void buyStock(){ if (Session != null){ string dbPrefix = $"[{userName.ToLower()}]:"; @@ -254,8 +262,11 @@ return; } + // Get Stock Price + float stockPrice = aiModule.GetCurrentPrice( buyStockSymbol ); + // Try Pay for the stock - (bool, string) result = PaymentProcessor.TryPayment(PaymentKey, QuantityResult * StockPrice); + (bool, string) result = PaymentProcessor.TryPayment(PaymentKey, QuantityResult * stockPrice); if (!result.Item1){ resultError = result.Item2; return; @@ -264,7 +275,7 @@ // Add the stock Session.TrackedStocks.Add( new PurchasedStock(){ Symbol = buyStockSymbol.ToUpper(), - PurchasePrice = StockPrice, + PurchasePrice = stockPrice, Quantity = QuantityResult, PurchaseDate = DateTime.Now } ); diff --git a/WebServer/Controllers/PythonInterop.cs b/WebServer/Controllers/PythonInterop.cs index 9958c158..5dbc4e23 100644 --- a/WebServer/Controllers/PythonInterop.cs +++ b/WebServer/Controllers/PythonInterop.cs @@ -50,6 +50,16 @@ namespace Controllers.PythonInterop { } } + public float GetCurrentPrice(string StockSymbol) { + using (Py.GIL()) { + dynamic price = Py.Import("currentprice"); + using (dynamic x = price.getCurrentPrice(StockSymbol)) { + float CurrentPrice = (float)x; + return x; + } + } + } + } } \ No newline at end of file