Give $1000 starting out and track money [ Paper trades ]

This commit is contained in:
2026-03-08 20:41:39 -07:00
parent ce4f9a197d
commit 2eab9d99ba
5 changed files with 77 additions and 31 deletions
+23 -22
View File
@@ -33,12 +33,7 @@
<div><h2>Account</h2></div>
<span>UserName: @Session.UserName</span><br />
<div>
@foreach(PurchasedStock cur in Session.TrackedStocks){
<div>@cur.Symbol</div>
<div>@cur.Quantity</div>
<div>@cur.PurchasePrice</div>
<div>@cur.PurchaseDate.ToString("M-dd-yyyy")</div>
}
<span>Money: $@Session.Money</span>
</div>
</div>
<!-- Tool Frame -->
@@ -74,7 +69,10 @@
@foreach (PurchasedStock cur in Session.TrackedStocks){
<div class="signalBlock">
<div>
<p>@cur.Symbol</p>
<p style="text-align: center; font-weight: bold;">@cur.Symbol</p>
<p>Purchased Price: @cur.PurchasePrice</p>
<p>Purchased Quantity: @cur.Quantity</p>
<p>Purchased Date: @cur.PurchaseDate.ToString("M-dd-yyyy")</p>
</div>
<div>
<span>-></span>
@@ -102,20 +100,10 @@
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;
}
@@ -133,10 +121,17 @@
string passwordhash = dbDriver.Get( dbPrefix + "password" ); // Pull the password hash
if (BCrypt.Verify( passWord, passwordhash )){ // If the password is valid
List<PurchasedStock>? stocks = JsonConvert.DeserializeObject<List<PurchasedStock>>( dbDriver.Get( dbPrefix + "Stocks" ) );
bool moneyLoaded = float.TryParse(dbDriver.Get( dbPrefix + "money" ), out float moneyResult);
Session = new loginSession(){
UserName = userName.ToLower(),
TrackedStocks = stocks != null ? stocks : new List<PurchasedStock>()
TrackedStocks = stocks != null ? stocks : new List<PurchasedStock>(),
Money = moneyLoaded ? moneyResult : 1000
};
(bool, string) result = PaymentProcessor.CreatePayment(Session.UserName);
if (!result.Item1){
resultError = result.Item2;
}
PaymentKey = result.Item2;
}else{
loginError = "wrong password";
}
@@ -147,9 +142,11 @@
string passwordhash = dbDriver.Get( dbPrefix + "password" );
if (string.IsNullOrEmpty(passwordhash)){
dbDriver.Set( dbPrefix + "password", BCrypt.HashPassword( passWord, BCrypt.GenerateSalt() ) );
dbDriver.Set( dbPrefix + "money", "1000" );
Session = new loginSession(){
UserName = userName.ToLower(),
TrackedStocks = new List<PurchasedStock>()
TrackedStocks = new List<PurchasedStock>(),
Money = 1000
};
}else{
loginError = "account is taken";
@@ -273,10 +270,14 @@
} );
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();
result = PaymentProcessor.CreatePayment(Session.UserName);
if (!result.Item1){
resultError = "[Payment suceeded but new payment session failed] : " + result.Item2;
resultError = "[New payment session failed] : " + result.Item2;
return;
}
PaymentKey = result.Item2;
@@ -67,7 +67,6 @@
.signalBlock > :nth-child(1) {
border: solid #040 1px;
border-radius: 15px;
width: 40px;
padding: 0px 20px;
background-color: greenyellow;
}
+1 -1
View File
@@ -3,7 +3,7 @@ namespace Controllers.Payment {
public interface IPayment {
// [ Success, ErrorMessage | ImpodentKey ]
public (bool, string) CreatePayment();
public (bool, string) CreatePayment(string User);
// [ Success, ErrorMessage ]
public (bool, string) TryPayment(string ImpodentKey, float Price);
@@ -1,27 +1,72 @@
using Controllers.DataBase;
namespace Controllers.Payment {
public class PaymentTestor : IPayment {
public static List<string> ImpodentKeys = new List<string>();
static List<ImpodentKey> ImpodentKeys = new List<ImpodentKey>();
DbDriver? _DBdriver = null;
public (bool, string) CreatePayment() {
// Inject the DB driver
public PaymentTestor() { _DBdriver = new DbDriver(); }
public (bool, string) CreatePayment(string user) {
string guid = Guid.NewGuid().ToString();
ImpodentKeys.Add(guid);
ImpodentKeys.Add(new ImpodentKey{ Key = guid, User = user });
return (true, guid);
}
public (bool, string) TryPayment(string ImpodentKey, float Price) {
try {
if (ImpodentKeys.Contains(ImpodentKey)) {
ImpodentKeys.Remove(ImpodentKey);
return (true, "");
} else {
// 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");
}
// Make sure the user has enough money
if (result < Price) {
found.Processed = true;
return (false, "You dont have enough money to purchase this item");
}
// Remove the money and save
_DBdriver.Set( dbPrefix + "money", (result - Price).ToString());
found.PaymentSuccess = true;
found.Processed = true;
return (true, "");
}catch(Exception e) {
return (false, e.ToString());
}
}
}
class ImpodentKey {
public string Key { get; set; } = "";
public string User { get; set; } = "";
public bool PaymentSuccess { get; set; } = false;
public bool Processed { get; set; } = false;
}
}
+1
View File
@@ -2,6 +2,7 @@ namespace Entities {
class loginSession {
public string UserName { get; set; } = "";
public float Money { get; set; } = 0;
public List<PurchasedStock> TrackedStocks { get; set; } = new List<PurchasedStock>();
}