Almost finished base business system backend
This commit is contained in:
@@ -56,16 +56,22 @@ namespace reallife_gamemode.Server.Business
|
||||
public void Update()
|
||||
{
|
||||
User owner = GetOwner();
|
||||
string infoText = Name + "\n" + "Besitzer: " + (owner == null ? "Niemand" : owner.Name) + "\nKasse: " + (GetBankAccount()?.Balance ?? 0);
|
||||
string infoText = Name + "\n" + "Besitzer: " + (owner == null ? "Niemand" : owner.Name) + "\nKasse: ~g~" + (GetBankAccount()?.Balance ?? 0) + "$";
|
||||
_informationLabel.Text = infoText;
|
||||
}
|
||||
|
||||
public User GetOwner()
|
||||
public User GetOwner(DatabaseContext dbContext = null)
|
||||
{
|
||||
using (var dbContext = new DatabaseContext())
|
||||
if(dbContext == null)
|
||||
{
|
||||
User user = dbContext.Users.FirstOrDefault(u => u.BusinessId == Id);
|
||||
return user;
|
||||
using (dbContext = new DatabaseContext())
|
||||
{
|
||||
return dbContext.Users.FirstOrDefault(u => u.BusinessId == Id);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
return dbContext.Users.FirstOrDefault(u => u.BusinessId == Id);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1745,6 +1745,12 @@ namespace reallife_gamemode.Server.Commands
|
||||
return;
|
||||
}
|
||||
|
||||
if(target.GetUser().BusinessId != null)
|
||||
{
|
||||
player.SendChatMessage("~r~[FEHLER]~s~ Der Spieler besitzt momentan schon ein Business: ~o~" + BusinessManager.GetBusiness(target.GetUser().BusinessId).Name);
|
||||
return;
|
||||
}
|
||||
|
||||
BusinessBase business = BusinessManager.GetBusiness(businessid);
|
||||
if(business == null)
|
||||
{
|
||||
@@ -1763,11 +1769,65 @@ namespace reallife_gamemode.Server.Commands
|
||||
Entities.User targetUser = target.GetUser(dbContext);
|
||||
targetUser.BusinessId = businessid;
|
||||
|
||||
dbContext.SaveChanges();
|
||||
business.Update();
|
||||
}
|
||||
}
|
||||
|
||||
[Command("clearbusiness", "~m~Benutzung:~s~ /clearbusiness [Business ID]")]
|
||||
public void CmdAdminClearbusiness(Client player, int businessid)
|
||||
{
|
||||
if (!player.GetUser()?.IsAdmin(AdminLevel.HEADADMIN) ?? true)
|
||||
{
|
||||
ChatService.NotAuthorized(player);
|
||||
return;
|
||||
}
|
||||
|
||||
BusinessBase business = BusinessManager.GetBusiness(businessid);
|
||||
if (business == null)
|
||||
{
|
||||
player.SendChatMessage("~r~[FEHLER]~s~ Dieses Business existiert nicht. ~m~/businesslist");
|
||||
return;
|
||||
}
|
||||
|
||||
using(var dbContext = new DatabaseContext())
|
||||
{
|
||||
Entities.User owner = business.GetOwner(dbContext);
|
||||
if(owner == null)
|
||||
{
|
||||
player.SendChatMessage("~r~[FEHLER]~s~ Dieses Business hat momentan keinen Besitzer.");
|
||||
return;
|
||||
}
|
||||
|
||||
owner.BusinessId = null;
|
||||
business.GetBankAccount(dbContext).Balance = 0;
|
||||
|
||||
owner.GetClient()?.SendChatMessage("~b~[ADMIN]~s~ Dir wurde von ~y~" + player.Name + "~s~ dein Business entzogen.");
|
||||
player.SendChatMessage("~b~[ADMIN]~s~ Du hast ~y~" + owner.Name + "~s~ sein Business ~o~" + business.Name + "~s~ entzogen.");
|
||||
|
||||
dbContext.SaveChanges();
|
||||
business.Update();
|
||||
}
|
||||
}
|
||||
|
||||
[Command("givebusinessbankbalance", "~m~Benutzung: ~s~/givebusinessbankbalance [Business ID] [Menge]")]
|
||||
public void CmdAdminGivebusinessbankbalance(Client player, int businessid, int amount)
|
||||
{
|
||||
if (!player.GetUser()?.IsAdmin(AdminLevel.HEADADMIN) ?? true)
|
||||
{
|
||||
ChatService.NotAuthorized(player);
|
||||
return;
|
||||
}
|
||||
|
||||
BusinessBase business = BusinessManager.GetBusiness(businessid);
|
||||
if (business == null)
|
||||
{
|
||||
player.SendChatMessage("~r~[FEHLER]~s~ Dieses Business existiert nicht. ~m~/businesslist");
|
||||
return;
|
||||
}
|
||||
|
||||
BankManager.TransferMoney(null, business, amount, "Admin");
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region ALevel1338
|
||||
|
||||
@@ -2,11 +2,9 @@
|
||||
using reallife_gamemode.Model;
|
||||
using reallife_gamemode.Server.Util;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
/**
|
||||
* @overview Life of German Reallife - Entities User (User.cs)
|
||||
@@ -146,5 +144,10 @@ namespace reallife_gamemode.Server.Entities
|
||||
return databaseContext.UserBankAccounts.FirstOrDefault(u => u.UserId == this.Id);
|
||||
}
|
||||
}
|
||||
|
||||
public Client GetClient()
|
||||
{
|
||||
return NAPI.Player.GetPlayerFromName(Name);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using GTANetworkAPI;
|
||||
using reallife_gamemode.Server.Business;
|
||||
using reallife_gamemode.Server.Entities;
|
||||
using reallife_gamemode.Server.Extensions;
|
||||
using reallife_gamemode.Server.Util;
|
||||
@@ -23,22 +24,22 @@ namespace reallife_gamemode.Server.Managers
|
||||
{
|
||||
if (amount < 0) return TransactionResult.NEGATIVE_MONEY_SENT;
|
||||
|
||||
IBankAccount senderAccount = sender.GetBankAccount(transferMoney);
|
||||
IBankAccount senderAccount = sender?.GetBankAccount(transferMoney) ?? null;
|
||||
IBankAccount receiverAccount = receiver.GetBankAccount(transferMoney);
|
||||
|
||||
if (senderAccount == null) return TransactionResult.SENDER_NO_BANKACCOUNT;
|
||||
if (senderAccount == null && sender != null) return TransactionResult.SENDER_NO_BANKACCOUNT;
|
||||
if (receiverAccount == null) return TransactionResult.RECEIVER_NO_BANKACCOUNT;
|
||||
|
||||
if (senderAccount.Balance < amount) return TransactionResult.SENDER_NOT_ENOUGH_MONEY;
|
||||
if ((senderAccount?.Balance ?? amount) < amount) return TransactionResult.SENDER_NOT_ENOUGH_MONEY;
|
||||
|
||||
var transactionLog = new Logs.BankAccountTransactionHistory
|
||||
{
|
||||
Sender = sender.Name,
|
||||
SenderBalance = senderAccount.Balance,
|
||||
Sender = sender?.Name ?? "Admin",
|
||||
SenderBalance = senderAccount?.Balance ?? 0,
|
||||
Receiver = receiver.Name,
|
||||
ReceiverBalance = receiverAccount.Balance,
|
||||
NewReceiverBalance = receiverAccount.Balance + amount,
|
||||
NewSenderBalance = senderAccount.Balance - amount,
|
||||
NewSenderBalance = (senderAccount?.Balance ?? amount) - amount,
|
||||
MoneySent = amount,
|
||||
Fee = 0,
|
||||
Origin = origin
|
||||
@@ -47,11 +48,13 @@ namespace reallife_gamemode.Server.Managers
|
||||
// add log
|
||||
transferMoney.BankAccountTransactionLogs.Add(transactionLog);
|
||||
|
||||
senderAccount.Balance -= amount;
|
||||
if(senderAccount != null) senderAccount.Balance -= amount;
|
||||
receiverAccount.Balance += amount;
|
||||
|
||||
transferMoney.SaveChanges();
|
||||
|
||||
if (receiver is BusinessBase business) business.Update();
|
||||
|
||||
return TransactionResult.SUCCESS;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,7 +40,7 @@ namespace reallife_gamemode.Server.Managers
|
||||
return (T)businesses.Find(b => b.GetType() == typeof(T));
|
||||
}
|
||||
|
||||
public static BusinessBase GetBusiness(int id)
|
||||
public static BusinessBase GetBusiness(int? id)
|
||||
{
|
||||
return businesses.Find(b => b.Id == id);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user