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> <div><h2>Account</h2></div>
<span>UserName: @Session.UserName</span><br /> <span>UserName: @Session.UserName</span><br />
<div> <div>
@foreach(PurchasedStock cur in Session.TrackedStocks){ <span>Money: $@Session.Money</span>
<div>@cur.Symbol</div>
<div>@cur.Quantity</div>
<div>@cur.PurchasePrice</div>
<div>@cur.PurchaseDate.ToString("M-dd-yyyy")</div>
}
</div> </div>
</div> </div>
<!-- Tool Frame --> <!-- Tool Frame -->
@@ -74,7 +69,10 @@
@foreach (PurchasedStock cur in Session.TrackedStocks){ @foreach (PurchasedStock cur in Session.TrackedStocks){
<div class="signalBlock"> <div class="signalBlock">
<div> <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>
<div> <div>
<span>-></span> <span>-></span>
@@ -102,20 +100,10 @@
loginSession? Session = null; loginSession? Session = null;
// On Page Load // On Page Load
string PaymentKey = ""; string PaymentKey = "";
protected override async Task OnInitializedAsync(){ 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 string passwordhash = dbDriver.Get( dbPrefix + "password" ); // Pull the password hash
if (BCrypt.Verify( passWord, passwordhash )){ // If the password is valid if (BCrypt.Verify( passWord, passwordhash )){ // If the password is valid
List<PurchasedStock>? stocks = JsonConvert.DeserializeObject<List<PurchasedStock>>( dbDriver.Get( dbPrefix + "Stocks" ) ); 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(){ Session = new loginSession(){
UserName = userName.ToLower(), 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{ }else{
loginError = "wrong password"; loginError = "wrong password";
} }
@@ -147,9 +142,11 @@
string passwordhash = dbDriver.Get( dbPrefix + "password" ); string passwordhash = dbDriver.Get( dbPrefix + "password" );
if (string.IsNullOrEmpty(passwordhash)){ if (string.IsNullOrEmpty(passwordhash)){
dbDriver.Set( dbPrefix + "password", BCrypt.HashPassword( passWord, BCrypt.GenerateSalt() ) ); dbDriver.Set( dbPrefix + "password", BCrypt.HashPassword( passWord, BCrypt.GenerateSalt() ) );
dbDriver.Set( dbPrefix + "money", "1000" );
Session = new loginSession(){ Session = new loginSession(){
UserName = userName.ToLower(), UserName = userName.ToLower(),
TrackedStocks = new List<PurchasedStock>() TrackedStocks = new List<PurchasedStock>(),
Money = 1000
}; };
}else{ }else{
loginError = "account is taken"; loginError = "account is taken";
@@ -273,10 +270,14 @@
} ); } );
dbDriver.Set( dbPrefix + "Stocks", Newtonsoft.Json.JsonConvert.SerializeObject(Session.TrackedStocks) ); 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 // Reset the Impodent Key
result = PaymentProcessor.CreatePayment(); result = PaymentProcessor.CreatePayment(Session.UserName);
if (!result.Item1){ if (!result.Item1){
resultError = "[Payment suceeded but new payment session failed] : " + result.Item2; resultError = "[New payment session failed] : " + result.Item2;
return; return;
} }
PaymentKey = result.Item2; PaymentKey = result.Item2;
@@ -67,7 +67,6 @@
.signalBlock > :nth-child(1) { .signalBlock > :nth-child(1) {
border: solid #040 1px; border: solid #040 1px;
border-radius: 15px; border-radius: 15px;
width: 40px;
padding: 0px 20px; padding: 0px 20px;
background-color: greenyellow; background-color: greenyellow;
} }
+1 -1
View File
@@ -3,7 +3,7 @@ namespace Controllers.Payment {
public interface IPayment { public interface IPayment {
// [ Success, ErrorMessage | ImpodentKey ] // [ Success, ErrorMessage | ImpodentKey ]
public (bool, string) CreatePayment(); public (bool, string) CreatePayment(string User);
// [ Success, ErrorMessage ] // [ Success, ErrorMessage ]
public (bool, string) TryPayment(string ImpodentKey, float Price); public (bool, string) TryPayment(string ImpodentKey, float Price);
@@ -1,27 +1,72 @@
using Controllers.DataBase;
namespace Controllers.Payment { namespace Controllers.Payment {
public class PaymentTestor : IPayment { public class PaymentTestor : IPayment {
static List<ImpodentKey> ImpodentKeys = new List<ImpodentKey>();
DbDriver? _DBdriver = null;
public static List<string> ImpodentKeys = new List<string>(); // Inject the DB driver
public PaymentTestor() { _DBdriver = new DbDriver(); }
public (bool, string) CreatePayment() { public (bool, string) CreatePayment(string user) {
string guid = Guid.NewGuid().ToString(); string guid = Guid.NewGuid().ToString();
ImpodentKeys.Add(guid); ImpodentKeys.Add(new ImpodentKey{ Key = guid, User = user });
return (true, guid); return (true, guid);
} }
public (bool, string) TryPayment(string ImpodentKey, float Price) { public (bool, string) TryPayment(string ImpodentKey, float Price) {
try { try {
if (ImpodentKeys.Contains(ImpodentKey)) { // Find the impotency key
ImpodentKeys.Remove(ImpodentKey); ImpodentKey? found = null;
return (true, ""); foreach( ImpodentKey key in ImpodentKeys) {
} else { if (key.Key == ImpodentKey) {
found = key;
break;
}
}
if (found == null) {
return (false, "Payment session closed or never existed"); 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) { }catch(Exception e) {
return (false, e.ToString()); 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 { class loginSession {
public string UserName { get; set; } = ""; public string UserName { get; set; } = "";
public float Money { get; set; } = 0;
public List<PurchasedStock> TrackedStocks { get; set; } = new List<PurchasedStock>(); public List<PurchasedStock> TrackedStocks { get; set; } = new List<PurchasedStock>();
} }