Almost finished base business system backend

This commit is contained in:
hydrant
2018-11-22 22:19:34 +01:00
parent a72d65eb18
commit 054e9159e1
5 changed files with 87 additions and 15 deletions

View File

@@ -56,16 +56,22 @@ namespace reallife_gamemode.Server.Business
public void Update() public void Update()
{ {
User owner = GetOwner(); 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; _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); using (dbContext = new DatabaseContext())
return user; {
return dbContext.Users.FirstOrDefault(u => u.BusinessId == Id);
}
}
else
{
return dbContext.Users.FirstOrDefault(u => u.BusinessId == Id);
} }
} }

View File

@@ -1745,6 +1745,12 @@ namespace reallife_gamemode.Server.Commands
return; 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); BusinessBase business = BusinessManager.GetBusiness(businessid);
if(business == null) if(business == null)
{ {
@@ -1763,11 +1769,65 @@ namespace reallife_gamemode.Server.Commands
Entities.User targetUser = target.GetUser(dbContext); Entities.User targetUser = target.GetUser(dbContext);
targetUser.BusinessId = businessid; targetUser.BusinessId = businessid;
dbContext.SaveChanges();
business.Update(); 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(); 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 #endregion
#region ALevel1338 #region ALevel1338

View File

@@ -2,11 +2,9 @@
using reallife_gamemode.Model; using reallife_gamemode.Model;
using reallife_gamemode.Server.Util; using reallife_gamemode.Server.Util;
using System; using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema; using System.ComponentModel.DataAnnotations.Schema;
using System.Linq; using System.Linq;
using System.Text;
/** /**
* @overview Life of German Reallife - Entities User (User.cs) * @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); return databaseContext.UserBankAccounts.FirstOrDefault(u => u.UserId == this.Id);
} }
} }
public Client GetClient()
{
return NAPI.Player.GetPlayerFromName(Name);
}
} }
} }

View File

@@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using GTANetworkAPI; using GTANetworkAPI;
using reallife_gamemode.Server.Business;
using reallife_gamemode.Server.Entities; using reallife_gamemode.Server.Entities;
using reallife_gamemode.Server.Extensions; using reallife_gamemode.Server.Extensions;
using reallife_gamemode.Server.Util; using reallife_gamemode.Server.Util;
@@ -23,22 +24,22 @@ namespace reallife_gamemode.Server.Managers
{ {
if (amount < 0) return TransactionResult.NEGATIVE_MONEY_SENT; if (amount < 0) return TransactionResult.NEGATIVE_MONEY_SENT;
IBankAccount senderAccount = sender.GetBankAccount(transferMoney); IBankAccount senderAccount = sender?.GetBankAccount(transferMoney) ?? null;
IBankAccount receiverAccount = receiver.GetBankAccount(transferMoney); 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 (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 var transactionLog = new Logs.BankAccountTransactionHistory
{ {
Sender = sender.Name, Sender = sender?.Name ?? "Admin",
SenderBalance = senderAccount.Balance, SenderBalance = senderAccount?.Balance ?? 0,
Receiver = receiver.Name, Receiver = receiver.Name,
ReceiverBalance = receiverAccount.Balance, ReceiverBalance = receiverAccount.Balance,
NewReceiverBalance = receiverAccount.Balance + amount, NewReceiverBalance = receiverAccount.Balance + amount,
NewSenderBalance = senderAccount.Balance - amount, NewSenderBalance = (senderAccount?.Balance ?? amount) - amount,
MoneySent = amount, MoneySent = amount,
Fee = 0, Fee = 0,
Origin = origin Origin = origin
@@ -47,11 +48,13 @@ namespace reallife_gamemode.Server.Managers
// add log // add log
transferMoney.BankAccountTransactionLogs.Add(transactionLog); transferMoney.BankAccountTransactionLogs.Add(transactionLog);
senderAccount.Balance -= amount; if(senderAccount != null) senderAccount.Balance -= amount;
receiverAccount.Balance += amount; receiverAccount.Balance += amount;
transferMoney.SaveChanges(); transferMoney.SaveChanges();
if (receiver is BusinessBase business) business.Update();
return TransactionResult.SUCCESS; return TransactionResult.SUCCESS;
} }
} }

View File

@@ -40,7 +40,7 @@ namespace reallife_gamemode.Server.Managers
return (T)businesses.Find(b => b.GetType() == typeof(T)); 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); return businesses.Find(b => b.Id == id);
} }