From 054e9159e11190ae26c0eead4f9a19e4caf59baf Mon Sep 17 00:00:00 2001 From: hydrant Date: Thu, 22 Nov 2018 22:19:34 +0100 Subject: [PATCH] Almost finished base business system backend --- Server/Business/BusinessBase.cs | 16 +++++--- Server/Commands/Admin.cs | 60 ++++++++++++++++++++++++++++++ Server/Entities/User.cs | 7 +++- Server/Managers/BankManager.cs | 17 +++++---- Server/Managers/BusinessManager.cs | 2 +- 5 files changed, 87 insertions(+), 15 deletions(-) diff --git a/Server/Business/BusinessBase.cs b/Server/Business/BusinessBase.cs index 78d6a1d6..7233343a 100644 --- a/Server/Business/BusinessBase.cs +++ b/Server/Business/BusinessBase.cs @@ -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); } } diff --git a/Server/Commands/Admin.cs b/Server/Commands/Admin.cs index b70c7148..c9107429 100644 --- a/Server/Commands/Admin.cs +++ b/Server/Commands/Admin.cs @@ -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 diff --git a/Server/Entities/User.cs b/Server/Entities/User.cs index bb0f3399..db630016 100644 --- a/Server/Entities/User.cs +++ b/Server/Entities/User.cs @@ -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); + } } } diff --git a/Server/Managers/BankManager.cs b/Server/Managers/BankManager.cs index cf913207..c4b726bf 100644 --- a/Server/Managers/BankManager.cs +++ b/Server/Managers/BankManager.cs @@ -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; } } diff --git a/Server/Managers/BusinessManager.cs b/Server/Managers/BusinessManager.cs index 6f78bf18..02d95455 100644 --- a/Server/Managers/BusinessManager.cs +++ b/Server/Managers/BusinessManager.cs @@ -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); }