Give $1000 starting out and track money [ Paper trades ]
This commit is contained in:
@@ -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 {
|
||||
|
||||
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();
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user