Allow selling
This commit is contained in:
@@ -41,11 +41,9 @@
|
|||||||
<div>
|
<div>
|
||||||
<div><h2>Actions</h2></div>
|
<div><h2>Actions</h2></div>
|
||||||
@if (Debounce){
|
@if (Debounce){
|
||||||
<button @onclick="pull">@pullButtonText</button>
|
|
||||||
<button @onclick="train">@trainButtonText</button>
|
<button @onclick="train">@trainButtonText</button>
|
||||||
<button @onclick="predict">@predictButtonText</button>
|
<button @onclick="predict">@predictButtonText</button>
|
||||||
}else{
|
}else{
|
||||||
<button disabled>@pullButtonText</button>
|
|
||||||
<button disabled>@trainButtonText</button>
|
<button disabled>@trainButtonText</button>
|
||||||
<button disabled>@predictButtonText</button>
|
<button disabled>@predictButtonText</button>
|
||||||
}
|
}
|
||||||
@@ -73,6 +71,7 @@
|
|||||||
<p>Purchased Price: @cur.PurchasePrice</p>
|
<p>Purchased Price: @cur.PurchasePrice</p>
|
||||||
<p>Purchased Quantity: @cur.Quantity</p>
|
<p>Purchased Quantity: @cur.Quantity</p>
|
||||||
<p>Purchased Date: @cur.PurchaseDate.ToString("M-dd-yyyy")</p>
|
<p>Purchased Date: @cur.PurchaseDate.ToString("M-dd-yyyy")</p>
|
||||||
|
<button @onclick="() => {sellStock(cur);}">Sell All</button>
|
||||||
</div>
|
</div>
|
||||||
<div>
|
<div>
|
||||||
<span>-></span>
|
<span>-></span>
|
||||||
@@ -170,30 +169,10 @@
|
|||||||
|
|
||||||
// AI Stuff
|
// AI Stuff
|
||||||
|
|
||||||
string pullButtonText = "Pull Data";
|
string trainButtonText = "Force Retrain AI";
|
||||||
string trainButtonText = "Train AI";
|
|
||||||
string predictButtonText = "Predict AI";
|
string predictButtonText = "Predict AI";
|
||||||
string resultError = "";
|
string resultError = "";
|
||||||
|
|
||||||
bool Debounce = true;
|
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(){
|
async Task train(){
|
||||||
resultError = "";
|
resultError = "";
|
||||||
@@ -294,4 +273,36 @@
|
|||||||
PaymentKey = result.Item2;
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -8,6 +8,9 @@ namespace Controllers.Payment {
|
|||||||
// [ Success, ErrorMessage ]
|
// [ Success, ErrorMessage ]
|
||||||
public (bool, string) TryPayment(string ImpodentKey, float Price);
|
public (bool, string) TryPayment(string ImpodentKey, float Price);
|
||||||
|
|
||||||
|
//
|
||||||
|
public (bool, string) TrySell(string ImpodentKey, float Price);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -60,6 +60,40 @@ namespace Controllers.Payment {
|
|||||||
return (false, e.ToString());
|
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 {
|
class ImpodentKey {
|
||||||
|
|||||||
Reference in New Issue
Block a user