@page "/" @using Controllers.Payment @rendermode InteractiveServer Home
@if (Session == null){

LOGIN

username
password
@loginError
}else{

Account

UserName: @Session.UserName
@foreach(PurchasedStock cur in Session.TrackedStocks){
@cur.Symbol
@cur.Quantity
@cur.PurchasePrice
@cur.PurchaseDate.ToString("M-dd-yyyy")
}

Actions

@if (Debounce){ }else{ }
@resultError
} @if (Session != null){

Current Signal

@foreach (PurchasedStock cur in Session.TrackedStocks){

@cur.Symbol

->
@if (cur.PredictedMovement == -1){

Sell

} else if (cur.PredictedMovement == 1){

Buy

} else{

Hold

}
}
}
@code { ///////////////////////////////////////////////////////////////////////////////////////////// /// Code Region ///////////////////////////////////////////////////////////////////////////////////////////// loginSession? Session = null; // On Page Load string PaymentKey = ""; protected override async Task OnInitializedAsync(){ (bool, string) result = PaymentProcessor.CreatePayment(); if (!result.Item1){ resultError = result.Item2; } PaymentKey = result.Item2; } // Login Stuff string userName = ""; string passWord = ""; string loginError = ""; 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 (BCrypt.Verify( passWord, passwordhash )){ // If the password is valid List? stocks = JsonConvert.DeserializeObject>( dbDriver.Get( dbPrefix + "Stocks" ) ); Session = new loginSession(){ UserName = userName.ToLower(), TrackedStocks = stocks != null ? stocks : new List() }; }else{ loginError = "wrong password"; } } async Task registerSession(){ string dbPrefix = $"[{userName.ToLower()}]:"; string passwordhash = dbDriver.Get( dbPrefix + "password" ); if (string.IsNullOrEmpty(passwordhash)){ dbDriver.Set( dbPrefix + "password", BCrypt.HashPassword( passWord, BCrypt.GenerateSalt() ) ); Session = new loginSession(){ UserName = userName.ToLower(), TrackedStocks = new List() }; }else{ loginError = "account is taken"; } } // AI Stuff string pullButtonText = "Pull Data"; string trainButtonText = "Train 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 = ""; if (Debounce){ Debounce = false; trainButtonText = "Do not refresh the page. The AI is training."; await Task.Delay(1); Task thread = new Task(async () => { aiModule.TrainAI(); trainButtonText = "AI Trained"; StateHasChanged(); await Task.Delay(2000); trainButtonText = "Train AI"; StateHasChanged(); Debounce = true; }); thread.Start(); } } async Task predict(){ resultError = ""; if (Debounce && Session != null){ Debounce = false; predictButtonText = "Do not refresh the page. The AI is predicting"; await Task.Delay(1); List threadpool = new List(); foreach(PurchasedStock cur in Session.TrackedStocks){ Task thread = new Task(() => { (string, int)Result = aiModule.PredictAI(cur.Symbol); if (string.IsNullOrEmpty(Result.Item1)){ cur.PredictedMovement = Result.Item2; }else{ resultError = Result.Item1; } Console.WriteLine("Received Signal [" + cur.Symbol + "] : " + cur.PredictedMovement); }); thread.Start(); threadpool.Add(thread); } await Task.WhenAll(threadpool); predictButtonText = "Predictions loaded"; await Task.Delay(2000); predictButtonText = "Predict AI"; Debounce = true; } } // Stock Manipulation string buyStockSymbol = ""; string buyStockQuantity = ""; float StockPrice = 0; void buyStock(){ if (Session != null){ string dbPrefix = $"[{userName.ToLower()}]:"; // Try Parse the quantitiy input bool success = float.TryParse(buyStockQuantity, out float QuantityResult); if (!success){ resultError = "Quantity field is not a number"; return; } // Try Pay for the stock (bool, string) result = PaymentProcessor.TryPayment(PaymentKey, QuantityResult * StockPrice); if (!result.Item1){ resultError = result.Item2; return; } // Add the stock Session.TrackedStocks.Add( new PurchasedStock(){ Symbol = buyStockSymbol.ToUpper(), PurchasePrice = StockPrice, Quantity = QuantityResult, PurchaseDate = DateTime.Now } ); dbDriver.Set( dbPrefix + "Stocks", Newtonsoft.Json.JsonConvert.SerializeObject(Session.TrackedStocks) ); // Reset the Impodent Key result = PaymentProcessor.CreatePayment(); if (!result.Item1){ resultError = "[Payment suceeded but new payment session failed] : " + result.Item2; return; } PaymentKey = result.Item2; } } }