bank account refactor
This commit is contained in:
@@ -89,18 +89,17 @@ namespace ReallifeGamemode.Server.Managers
|
||||
[RemoteEvent("CLIENT:ATM_MANAGER:ATM_ACTION")]
|
||||
public void AtmAction(Player client, int site, int inputField1, int inputField2)
|
||||
{
|
||||
var user = client.GetUser();
|
||||
using (var dbContext = new DatabaseContext())
|
||||
{
|
||||
var user = client.GetUser(dbContext);
|
||||
int nearATM = client.GetData<int>("nearATM");
|
||||
//SITE //0 Geld einzahlen //1 Geld auszahlen //2 Geld überweisen
|
||||
switch (site)
|
||||
{
|
||||
//GELD EINZAHLEN in1
|
||||
case 0:
|
||||
var checkPlayer = dbContext.Users.FirstOrDefault(u => u.Id == user.Id);
|
||||
|
||||
if (checkPlayer.Handmoney < inputField1)
|
||||
if (user.Handmoney < inputField1)
|
||||
{
|
||||
//TODO im CEFBrowser anzeigen
|
||||
//client.TriggerEvent("SERVER:WORLD_INTERACTION:ATM_ERROR", 0, checkATM.Balance);
|
||||
@@ -108,22 +107,20 @@ namespace ReallifeGamemode.Server.Managers
|
||||
}
|
||||
else
|
||||
{
|
||||
var updateHandMoneyIn = dbContext.Users.FirstOrDefault(u => u.Id == user.Id);
|
||||
var updateBankMoneyIn = dbContext.UserBankAccounts.FirstOrDefault(b => b.UserId == user.Id);
|
||||
var updateBankMoneyIn = user.BankAccount;
|
||||
var updateATMBalanceIn = dbContext.ATMs.FirstOrDefault(a => a.Id == nearATM);
|
||||
updateHandMoneyIn.Handmoney -= inputField1;
|
||||
user.Handmoney -= inputField1;
|
||||
updateBankMoneyIn.Balance += inputField1;
|
||||
updateATMBalanceIn.Balance += inputField1;
|
||||
client.TriggerEvent("SERVER:SET_HANDMONEY", updateHandMoneyIn.Handmoney);
|
||||
client.TriggerEvent("SERVER:SET_HANDMONEY", user.Handmoney);
|
||||
}
|
||||
break;
|
||||
|
||||
//GELD AUSZAHLEN in1
|
||||
case 1:
|
||||
var checkATM = dbContext.ATMs.FirstOrDefault(a => a.Id == nearATM);
|
||||
var checkBankAccount = dbContext.UserBankAccounts.FirstOrDefault(b => b.UserId == user.Id);
|
||||
|
||||
if (checkBankAccount.Balance < inputField1)
|
||||
if (user.BankAccount.Balance < inputField1)
|
||||
{
|
||||
client.SendNotification("~r~Nicht genügend Geld auf dem Bankkonto!"); //TODO Im Automaten anzeigen lassen
|
||||
}
|
||||
@@ -134,10 +131,9 @@ namespace ReallifeGamemode.Server.Managers
|
||||
else
|
||||
{
|
||||
var updateHandMoneyOut = dbContext.Users.FirstOrDefault(u => u.Id == user.Id);
|
||||
var updateBankMoneyOut = dbContext.UserBankAccounts.FirstOrDefault(b => b.UserId == user.Id);
|
||||
var updateATMBalanceOut = dbContext.ATMs.FirstOrDefault(a => a.Id == nearATM);
|
||||
updateHandMoneyOut.Handmoney += inputField1;
|
||||
updateBankMoneyOut.Balance -= inputField1;
|
||||
user.BankAccount.Balance -= inputField1;
|
||||
updateATMBalanceOut.Balance -= inputField1;
|
||||
client.TriggerEvent("SERVER:SET_HANDMONEY", updateHandMoneyOut.Handmoney);
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ using ReallifeGamemode.Server.Util;
|
||||
using ReallifeGamemode.Database;
|
||||
using ReallifeGamemode.Database.Models;
|
||||
using ReallifeGamemode.Database.Entities.Logs;
|
||||
using ReallifeGamemode.Database.Entities;
|
||||
|
||||
/**
|
||||
* @overview Life of German Reallife - Managers BankManager (BankManager.cs)
|
||||
@@ -54,43 +55,63 @@ namespace ReallifeGamemode.Server.Managers
|
||||
}
|
||||
}
|
||||
|
||||
public static TransactionResult TransferMoney(IBankAccountOwner sender, IBankAccountOwner receiver, int amount, string origin)
|
||||
public static TransactionResult TransferMoney<TSender, TReceiver>(
|
||||
BankAccountHolder<TSender> sender,
|
||||
BankAccountHolder<TReceiver> receiver,
|
||||
int amount,
|
||||
string origin,
|
||||
DatabaseContext dbContext) where TSender : class, IBankAccount, new() where TReceiver : class, IBankAccount, new()
|
||||
{
|
||||
using (var transferMoney = new DatabaseContext())
|
||||
if (amount < 0) return TransactionResult.NEGATIVE_MONEY_SENT;
|
||||
|
||||
IBankAccount senderAccount;
|
||||
IBankAccount receiverAccount;
|
||||
|
||||
if (sender is BankAccountHolder<BusinessBankAccount> businessSender)
|
||||
{
|
||||
if (amount < 0) return TransactionResult.NEGATIVE_MONEY_SENT;
|
||||
|
||||
IBankAccount senderAccount = sender.GetBankAccount(transferMoney);
|
||||
IBankAccount receiverAccount = receiver.GetBankAccount(transferMoney);
|
||||
|
||||
if (senderAccount == null) return TransactionResult.SENDER_NO_BANKACCOUNT;
|
||||
if (receiverAccount == null) return TransactionResult.RECEIVER_NO_BANKACCOUNT;
|
||||
|
||||
if (senderAccount.Balance < amount) return TransactionResult.SENDER_NOT_ENOUGH_MONEY;
|
||||
|
||||
var transactionLog = new BankAccountTransactionHistory
|
||||
{
|
||||
Sender = sender.Name,
|
||||
SenderBalance = senderAccount.Balance,
|
||||
Receiver = receiver.Name,
|
||||
ReceiverBalance = receiverAccount.Balance,
|
||||
NewReceiverBalance = receiverAccount.Balance + amount,
|
||||
NewSenderBalance = senderAccount.Balance - amount,
|
||||
MoneySent = amount,
|
||||
Fee = 0,
|
||||
Origin = origin
|
||||
};
|
||||
|
||||
// add log
|
||||
transferMoney.BankAccountTransactionLogs.Add(transactionLog);
|
||||
|
||||
senderAccount.Balance -= amount;
|
||||
receiverAccount.Balance += amount;
|
||||
|
||||
transferMoney.SaveChanges();
|
||||
|
||||
return TransactionResult.SUCCESS;
|
||||
senderAccount = dbContext.BusinessBankAccounts.Where(b => b.Id == businessSender.BankAccountId).First();
|
||||
}
|
||||
else
|
||||
{
|
||||
senderAccount = sender.BankAccount;
|
||||
}
|
||||
|
||||
if (receiver is BankAccountHolder<BusinessBankAccount> businessReceiver)
|
||||
{
|
||||
receiverAccount = dbContext.BusinessBankAccounts.Where(b => b.Id == businessReceiver.BankAccountId).First();
|
||||
}
|
||||
else
|
||||
{
|
||||
receiverAccount = receiver.BankAccount;
|
||||
}
|
||||
|
||||
if (senderAccount == null) return TransactionResult.SENDER_NO_BANKACCOUNT;
|
||||
if (receiverAccount == null) return TransactionResult.RECEIVER_NO_BANKACCOUNT;
|
||||
|
||||
if (senderAccount.Balance < amount) return TransactionResult.SENDER_NOT_ENOUGH_MONEY;
|
||||
|
||||
var transactionLog = new BankAccountTransactionHistory
|
||||
{
|
||||
Sender = sender.BankAccountName,
|
||||
SenderBalance = senderAccount.Balance,
|
||||
Receiver = receiver.BankAccountName,
|
||||
ReceiverBalance = receiverAccount.Balance,
|
||||
NewReceiverBalance = receiverAccount.Balance + amount,
|
||||
NewSenderBalance = senderAccount.Balance - amount,
|
||||
MoneySent = amount,
|
||||
Fee = 0,
|
||||
Origin = origin
|
||||
};
|
||||
|
||||
// add log
|
||||
dbContext.BankAccountTransactionLogs.Add(transactionLog);
|
||||
|
||||
senderAccount.Balance -= amount;
|
||||
receiverAccount.Balance += amount;
|
||||
|
||||
dbContext.SaveChanges();
|
||||
|
||||
return TransactionResult.SUCCESS;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -59,62 +59,70 @@ namespace ReallifeGamemode.Server.Managers
|
||||
[RemoteEvent("Business_DepositMoney")]
|
||||
public void BusinessDepositMoney(Player player, int amount)
|
||||
{
|
||||
User user = player.GetUser();
|
||||
if (user == null)
|
||||
using (var dbContext = new DatabaseContext())
|
||||
{
|
||||
return;
|
||||
}
|
||||
User user = player.GetUser(dbContext);
|
||||
if (user == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
BusinessBase playerBusiness = GetBusiness(user.BusinessId);
|
||||
BusinessBase playerBusiness = GetBusiness(user.BusinessId);
|
||||
|
||||
TransactionResult result = BankManager.TransferMoney(user, playerBusiness, amount, "Überweisung");
|
||||
TransactionResult result = BankManager.TransferMoney(user, playerBusiness, amount, "Überweisung", dbContext);
|
||||
|
||||
if (result == TransactionResult.NEGATIVE_MONEY_SENT)
|
||||
{
|
||||
player.SendNotification("~r~Es können nur positive Beträge überwiesen werden");
|
||||
return;
|
||||
}
|
||||
else if (result == TransactionResult.SENDER_NOT_ENOUGH_MONEY)
|
||||
{
|
||||
player.SendNotification("~r~Du hast nicht genug Geld");
|
||||
return;
|
||||
}
|
||||
else if (result == TransactionResult.SUCCESS)
|
||||
{
|
||||
playerBusiness.SendBusinessDataToPlayer(player);
|
||||
player.SendNotification("~g~Du hast erfolgreich ~s~" + amount.ToMoneyString() + " ~g~ überwiesen");
|
||||
return;
|
||||
if (result == TransactionResult.NEGATIVE_MONEY_SENT)
|
||||
{
|
||||
player.SendNotification("~r~Es können nur positive Beträge überwiesen werden");
|
||||
return;
|
||||
}
|
||||
else if (result == TransactionResult.SENDER_NOT_ENOUGH_MONEY)
|
||||
{
|
||||
player.SendNotification("~r~Du hast nicht genug Geld");
|
||||
return;
|
||||
}
|
||||
else if (result == TransactionResult.SUCCESS)
|
||||
{
|
||||
playerBusiness.SendBusinessDataToPlayer(player);
|
||||
playerBusiness.Update();
|
||||
player.SendNotification("~g~Du hast erfolgreich ~s~" + amount.ToMoneyString() + " ~g~ überwiesen");
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[RemoteEvent("Business_WithdrawMoney")]
|
||||
public void BusinessWithdrawMoney(Player player, int amount)
|
||||
{
|
||||
User user = player.GetUser();
|
||||
if (user == null)
|
||||
using (var dbContext = new DatabaseContext())
|
||||
{
|
||||
return;
|
||||
}
|
||||
User user = player.GetUser(dbContext);
|
||||
if (user == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
BusinessBase playerBusiness = GetBusiness(user.BusinessId);
|
||||
BusinessBase playerBusiness = GetBusiness(user.BusinessId);
|
||||
|
||||
TransactionResult result = BankManager.TransferMoney(playerBusiness, user, amount, "Überweisung");
|
||||
TransactionResult result = BankManager.TransferMoney(playerBusiness, user, amount, "Überweisung", dbContext);
|
||||
|
||||
if (result == TransactionResult.NEGATIVE_MONEY_SENT)
|
||||
{
|
||||
player.SendNotification("~r~Es können nur positive Beträge überwiesen werden");
|
||||
return;
|
||||
}
|
||||
else if (result == TransactionResult.SENDER_NOT_ENOUGH_MONEY)
|
||||
{
|
||||
player.SendNotification("~r~Es ist nicht genug Geld auf der Businesskasse vorhanden");
|
||||
return;
|
||||
}
|
||||
else if (result == TransactionResult.SUCCESS)
|
||||
{
|
||||
playerBusiness.SendBusinessDataToPlayer(player);
|
||||
player.SendNotification("~g~Du hast erfolgreich ~s~" + amount.ToMoneyString() + " ~g~ überwiesen");
|
||||
return;
|
||||
if (result == TransactionResult.NEGATIVE_MONEY_SENT)
|
||||
{
|
||||
player.SendNotification("~r~Es können nur positive Beträge überwiesen werden");
|
||||
return;
|
||||
}
|
||||
else if (result == TransactionResult.SENDER_NOT_ENOUGH_MONEY)
|
||||
{
|
||||
player.SendNotification("~r~Es ist nicht genug Geld auf der Businesskasse vorhanden");
|
||||
return;
|
||||
}
|
||||
else if (result == TransactionResult.SUCCESS)
|
||||
{
|
||||
playerBusiness.SendBusinessDataToPlayer(player);
|
||||
playerBusiness.Update();
|
||||
player.SendNotification("~g~Du hast erfolgreich ~s~" + amount.ToMoneyString() + " ~g~ überwiesen");
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -140,81 +148,80 @@ namespace ReallifeGamemode.Server.Managers
|
||||
[RemoteEvent("VehShop_BuyVehicle")]
|
||||
public void CarDealerBusiness_BuyVehicle(Player player, string target)
|
||||
{
|
||||
ServerVehicle sVeh = player.Vehicle?.GetServerVehicle();
|
||||
if (sVeh == null) return;
|
||||
if (!(sVeh is ShopVehicle)) return;
|
||||
ShopVehicle shopVehicle = (ShopVehicle)sVeh;
|
||||
int price = shopVehicle.Price;
|
||||
CarDealerBusinessBase business = GetBusiness(shopVehicle.BusinessId) as CarDealerBusinessBase;
|
||||
TransactionResult result = BankManager.TransferMoney(player.GetUser(), business, price, "Auto gekauft");
|
||||
if (result == TransactionResult.SENDER_NOT_ENOUGH_MONEY)
|
||||
{
|
||||
player.SendNotification("~r~Du hast nicht genug Geld: " + price.ToMoneyString());
|
||||
return;
|
||||
}
|
||||
|
||||
Vector3 spawnPos = business.CarSpawnPositon.Around(3);
|
||||
|
||||
player.TriggerEvent("SERVER:Util_setWaypoint", spawnPos.X, spawnPos.Y);
|
||||
|
||||
ServerVehicle newVeh = null;
|
||||
|
||||
if (target == "Spieler")
|
||||
{
|
||||
newVeh = new UserVehicle
|
||||
{
|
||||
Heading = business.CarSpawnHeading,
|
||||
PositionX = spawnPos.X,
|
||||
PositionY = spawnPos.Y,
|
||||
PositionZ = spawnPos.Z,
|
||||
Locked = false,
|
||||
UserId = player.GetUser().Id,
|
||||
Model = shopVehicle.Model,
|
||||
PrimaryColor = 111,
|
||||
SecondaryColor = 111,
|
||||
Active = true,
|
||||
};
|
||||
}
|
||||
else if (target == "Fraktion")
|
||||
{
|
||||
newVeh = new FactionVehicle
|
||||
{
|
||||
Heading = business.CarSpawnHeading,
|
||||
PositionX = spawnPos.X,
|
||||
PositionY = spawnPos.Y,
|
||||
PositionZ = spawnPos.Z,
|
||||
Locked = false,
|
||||
FactionId = player.GetUser().FactionId,
|
||||
Model = shopVehicle.Model,
|
||||
PrimaryColor = 111,
|
||||
SecondaryColor = 111,
|
||||
Active = true,
|
||||
};
|
||||
}
|
||||
else if (target == "Gruppe")
|
||||
{
|
||||
newVeh = new GroupVehicle
|
||||
{
|
||||
Heading = business.CarSpawnHeading,
|
||||
PositionX = spawnPos.X,
|
||||
PositionY = spawnPos.Y,
|
||||
PositionZ = spawnPos.Z,
|
||||
Locked = false,
|
||||
GroupId = player.GetUser().Group.Id,
|
||||
Model = shopVehicle.Model,
|
||||
PrimaryColor = 111,
|
||||
SecondaryColor = 111,
|
||||
Active = true,
|
||||
};
|
||||
}
|
||||
|
||||
using (var dbContext = new DatabaseContext())
|
||||
{
|
||||
ServerVehicle sVeh = player.Vehicle?.GetServerVehicle();
|
||||
if (sVeh == null) return;
|
||||
if (!(sVeh is ShopVehicle)) return;
|
||||
ShopVehicle shopVehicle = (ShopVehicle)sVeh;
|
||||
int price = shopVehicle.Price;
|
||||
CarDealerBusinessBase business = GetBusiness(shopVehicle.BusinessId) as CarDealerBusinessBase;
|
||||
TransactionResult result = BankManager.TransferMoney(player.GetUser(dbContext), business, price, "Auto gekauft", dbContext);
|
||||
if (result == TransactionResult.SENDER_NOT_ENOUGH_MONEY)
|
||||
{
|
||||
player.SendNotification("~r~Du hast nicht genug Geld: " + price.ToMoneyString());
|
||||
return;
|
||||
}
|
||||
|
||||
Vector3 spawnPos = business.CarSpawnPositon.Around(3);
|
||||
|
||||
player.TriggerEvent("SERVER:Util_setWaypoint", spawnPos.X, spawnPos.Y);
|
||||
|
||||
ServerVehicle newVeh = null;
|
||||
|
||||
if (target == "Spieler")
|
||||
{
|
||||
newVeh = new UserVehicle
|
||||
{
|
||||
Heading = business.CarSpawnHeading,
|
||||
PositionX = spawnPos.X,
|
||||
PositionY = spawnPos.Y,
|
||||
PositionZ = spawnPos.Z,
|
||||
Locked = false,
|
||||
UserId = player.GetUser().Id,
|
||||
Model = shopVehicle.Model,
|
||||
PrimaryColor = 111,
|
||||
SecondaryColor = 111,
|
||||
Active = true,
|
||||
};
|
||||
}
|
||||
else if (target == "Fraktion")
|
||||
{
|
||||
newVeh = new FactionVehicle
|
||||
{
|
||||
Heading = business.CarSpawnHeading,
|
||||
PositionX = spawnPos.X,
|
||||
PositionY = spawnPos.Y,
|
||||
PositionZ = spawnPos.Z,
|
||||
Locked = false,
|
||||
FactionId = player.GetUser().FactionId,
|
||||
Model = shopVehicle.Model,
|
||||
PrimaryColor = 111,
|
||||
SecondaryColor = 111,
|
||||
Active = true,
|
||||
};
|
||||
}
|
||||
else if (target == "Gruppe")
|
||||
{
|
||||
newVeh = new GroupVehicle
|
||||
{
|
||||
Heading = business.CarSpawnHeading,
|
||||
PositionX = spawnPos.X,
|
||||
PositionY = spawnPos.Y,
|
||||
PositionZ = spawnPos.Z,
|
||||
Locked = false,
|
||||
GroupId = player.GetUser().Group.Id,
|
||||
Model = shopVehicle.Model,
|
||||
PrimaryColor = 111,
|
||||
SecondaryColor = 111,
|
||||
Active = true,
|
||||
};
|
||||
}
|
||||
dbContext.ServerVehicles.Add(newVeh);
|
||||
dbContext.SaveChanges();
|
||||
}
|
||||
|
||||
newVeh.Spawn();
|
||||
newVeh.Spawn();
|
||||
}
|
||||
}
|
||||
|
||||
[RemoteEvent("CLIENT:Business_BuyBusiness")]
|
||||
@@ -239,7 +246,7 @@ namespace ReallifeGamemode.Server.Managers
|
||||
ChatService.ErrorMessage(player, "Du kannst nicht mehrere Businesse besitzen");
|
||||
}
|
||||
|
||||
IBankAccount bankAccount = user.GetBankAccount(dbContext);
|
||||
IBankAccount bankAccount = user.BankAccount;
|
||||
|
||||
if (price > bankAccount.Balance)
|
||||
{
|
||||
|
||||
@@ -229,7 +229,7 @@ namespace ReallifeGamemode.Server.Managers
|
||||
|
||||
House house = GetNearHouse(player.Position, dbContext);
|
||||
|
||||
var userBank = user.GetBankAccount(dbContext);
|
||||
var userBank = user.BankAccount;
|
||||
|
||||
if (userBank.Balance < house.Price)
|
||||
{
|
||||
@@ -400,7 +400,7 @@ namespace ReallifeGamemode.Server.Managers
|
||||
|
||||
ChatService.SendMessage(player, "Du bekommst vom Hausverkauf ~g~" + backMoney.ToMoneyString() + "~s~ zurück.");
|
||||
|
||||
user.GetBankAccount(dbContext).Balance += backMoney;
|
||||
user.BankAccount.Balance += backMoney;
|
||||
|
||||
dbContext.SaveChanges();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user