Pull money manipulation out of the thread for thread safety
This commit is contained in:
@@ -57,11 +57,39 @@ namespace Controllers.Automation {
|
|||||||
// Wait for all the threads to finish
|
// Wait for all the threads to finish
|
||||||
await Task.WhenAll(threadpool);
|
await Task.WhenAll(threadpool);
|
||||||
|
|
||||||
// Process Stocks -> Buy Sell Hold / Based on the global data from earlier
|
// Get the highest ranked
|
||||||
sellStock(username);
|
Stock HighestRanking = new Stock(){ Symbol="NVDA", Score = -400 }; // Just a placeholder incase an empty list comes through there is a fallback
|
||||||
|
foreach(Stock cur in VerifiedTrackedStocks) {
|
||||||
|
if (HighestRanking.Score < cur.Score) {
|
||||||
|
HighestRanking = cur;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Save the new scores to the database for reference from the UI
|
// Get users money
|
||||||
|
string MoneyStr = _dbDriver.Get( dbPrefix + "money");
|
||||||
|
bool Money = float.TryParse( MoneyStr, out float VerifiedMoney );
|
||||||
|
if (!Money) {
|
||||||
|
Console.WriteLine( "Unable to load users money" );
|
||||||
|
VerifiedMoney = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Sell all stocks
|
||||||
|
VerifiedMoney = sellStock(username, VerifiedMoney);
|
||||||
|
if (VerifiedMoney == -1f) {
|
||||||
|
Console.WriteLine("Failed to sell stocks");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Buy predicted stock
|
||||||
|
VerifiedMoney = buyStock(username, VerifiedMoney, HighestRanking.Symbol);
|
||||||
|
if (VerifiedMoney == -1f) {
|
||||||
|
Console.WriteLine("Failed to buy stocks");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Save to the database
|
||||||
_dbDriver.Set( dbPrefix + "watched", JsonConvert.SerializeObject( VerifiedTrackedStocks ) );
|
_dbDriver.Set( dbPrefix + "watched", JsonConvert.SerializeObject( VerifiedTrackedStocks ) );
|
||||||
|
_dbDriver.Set( dbPrefix + "money", VerifiedMoney.ToString());
|
||||||
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -76,7 +104,7 @@ namespace Controllers.Automation {
|
|||||||
thread.Start();
|
thread.Start();
|
||||||
}
|
}
|
||||||
|
|
||||||
void sellStock(string username){
|
float sellStock(string username, float Money){
|
||||||
string dbPrefix = $"[{username.ToLower()}]:";
|
string dbPrefix = $"[{username.ToLower()}]:";
|
||||||
|
|
||||||
// Get all stock history
|
// Get all stock history
|
||||||
@@ -84,6 +112,7 @@ namespace Controllers.Automation {
|
|||||||
List<PurchasedStock> VerifiedPurchaseHistory = PurchaseHistory != null ? PurchaseHistory : new List<PurchasedStock>();
|
List<PurchasedStock> VerifiedPurchaseHistory = PurchaseHistory != null ? PurchaseHistory : new List<PurchasedStock>();
|
||||||
|
|
||||||
// Find the stocks that need to be sold
|
// Find the stocks that need to be sold
|
||||||
|
float totalSale = 0;
|
||||||
foreach(PurchasedStock cur in VerifiedPurchaseHistory) {
|
foreach(PurchasedStock cur in VerifiedPurchaseHistory) {
|
||||||
if (cur.Sold == false) {
|
if (cur.Sold == false) {
|
||||||
|
|
||||||
@@ -94,55 +123,53 @@ namespace Controllers.Automation {
|
|||||||
(bool, string) createResult = _paymentProcessor.CreatePayment(username);
|
(bool, string) createResult = _paymentProcessor.CreatePayment(username);
|
||||||
if (!createResult.Item1) {
|
if (!createResult.Item1) {
|
||||||
Console.WriteLine("Create Payment Failure: " + createResult.Item2);
|
Console.WriteLine("Create Payment Failure: " + createResult.Item2);
|
||||||
return;
|
return -1f;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Try to sell the stock
|
// Try to sell the stock
|
||||||
(bool, string) paymentResult = _paymentProcessor.TrySell(createResult.Item2, sellPrice);
|
(bool, string) paymentResult = _paymentProcessor.TrySell(createResult.Item2, sellPrice);
|
||||||
if (!paymentResult.Item1){
|
if (!paymentResult.Item1){
|
||||||
Console.WriteLine("Process Payment Failure: " + paymentResult.Item2);
|
Console.WriteLine("Process Payment Failure: " + paymentResult.Item2);
|
||||||
return;
|
return -1f;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Add up the total sale
|
||||||
|
totalSale += sellPrice;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Save the stock history
|
// Save the stock history
|
||||||
_dbDriver.Set( dbPrefix + "Stocks", JsonConvert.SerializeObject(VerifiedPurchaseHistory) );
|
_dbDriver.Set( dbPrefix + "Stocks", JsonConvert.SerializeObject(VerifiedPurchaseHistory) );
|
||||||
|
|
||||||
|
// return the new calculated total
|
||||||
|
return Money + totalSale;
|
||||||
}
|
}
|
||||||
|
|
||||||
void buyStock(string username, string stockSymbol){
|
float buyStock(string username, float Money, string stockSymbol){
|
||||||
string dbPrefix = $"[{username.ToLower()}]:";
|
string dbPrefix = $"[{username.ToLower()}]:";
|
||||||
|
|
||||||
// Get all stock history
|
// Get all stock history
|
||||||
List<PurchasedStock>? PurchaseHistory = JsonConvert.DeserializeObject<List<PurchasedStock>>( _dbDriver.Get( dbPrefix + "history" ) );
|
List<PurchasedStock>? PurchaseHistory = JsonConvert.DeserializeObject<List<PurchasedStock>>( _dbDriver.Get( dbPrefix + "history" ) );
|
||||||
List<PurchasedStock> VerifiedPurchaseHistory = PurchaseHistory != null ? PurchaseHistory : new List<PurchasedStock>();
|
List<PurchasedStock> VerifiedPurchaseHistory = PurchaseHistory != null ? PurchaseHistory : new List<PurchasedStock>();
|
||||||
|
|
||||||
// Get users money
|
|
||||||
string MoneyStr = _dbDriver.Get( dbPrefix + "money");
|
|
||||||
bool Money = float.TryParse( MoneyStr, out float VerifiedMoney );
|
|
||||||
if (!Money) {
|
|
||||||
Console.WriteLine( "Unable to load users money" );
|
|
||||||
VerifiedMoney = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get Stock Price
|
// Get Stock Price
|
||||||
float stockPrice = _aiModule.GetCurrentPrice( stockSymbol );
|
float stockPrice = _aiModule.GetCurrentPrice( stockSymbol );
|
||||||
|
|
||||||
// Get max stocks user can purchase [ int cast truncates the decimal ]
|
// Get max stocks user can purchase [ int cast truncates the decimal ]
|
||||||
int MaxQty = (int)( VerifiedMoney / stockPrice );
|
int MaxQty = (int)( Money / stockPrice );
|
||||||
|
|
||||||
// Try create payment session
|
// Try create payment session
|
||||||
(bool, string) createResult = _paymentProcessor.CreatePayment(username);
|
(bool, string) createResult = _paymentProcessor.CreatePayment(username);
|
||||||
if (!createResult.Item1) {
|
if (!createResult.Item1) {
|
||||||
Console.WriteLine("Create Payment Failure: " + createResult.Item2);
|
Console.WriteLine("Create Payment Failure: " + createResult.Item2);
|
||||||
return;
|
return -1f;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Try Pay for the stock
|
// Try Pay for the stock
|
||||||
(bool, string) result = _paymentProcessor.TryPayment(createResult.Item2, stockPrice * MaxQty);
|
(bool, string) result = _paymentProcessor.TryPayment(createResult.Item2, stockPrice * MaxQty);
|
||||||
if (!result.Item1){
|
if (!result.Item1){
|
||||||
Console.WriteLine("Process Payment Failure: " + result.Item2);
|
Console.WriteLine("Process Payment Failure: " + result.Item2);
|
||||||
return;
|
return -1f;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add the stock
|
// Add the stock
|
||||||
@@ -152,6 +179,9 @@ namespace Controllers.Automation {
|
|||||||
Quantity = MaxQty,
|
Quantity = MaxQty,
|
||||||
} );
|
} );
|
||||||
_dbDriver.Set( dbPrefix + "Stocks", JsonConvert.SerializeObject(VerifiedPurchaseHistory) );
|
_dbDriver.Set( dbPrefix + "Stocks", JsonConvert.SerializeObject(VerifiedPurchaseHistory) );
|
||||||
|
|
||||||
|
// Return the new money
|
||||||
|
return Money - ( stockPrice * MaxQty );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -51,7 +51,7 @@ namespace Controllers.Payment {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Remove the money and save
|
// Remove the money and save
|
||||||
_DBdriver.Set( dbPrefix + "money", (result - Price).ToString());
|
// _DBdriver.Set( dbPrefix + "money", (result - Price).ToString()); -> Dont save in here as its done externally for thread safty
|
||||||
found.PaymentSuccess = true;
|
found.PaymentSuccess = true;
|
||||||
found.Processed = true;
|
found.Processed = true;
|
||||||
return (true, "");
|
return (true, "");
|
||||||
@@ -89,7 +89,7 @@ namespace Controllers.Payment {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Add the money and save
|
// Add the money and save
|
||||||
_DBdriver.Set( dbPrefix + "money", (result + Price).ToString());
|
// _DBdriver.Set( dbPrefix + "money", (result + Price).ToString()); -> Dont save in here as its done externally for thread safty
|
||||||
found.PaymentSuccess = true;
|
found.PaymentSuccess = true;
|
||||||
found.Processed = true;
|
found.Processed = true;
|
||||||
return (true, "");
|
return (true, "");
|
||||||
|
|||||||
Reference in New Issue
Block a user