Start business management from menu
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
using GTANetworkAPI;
|
||||
using reallife_gamemode.Model;
|
||||
using reallife_gamemode.Server.Entities;
|
||||
using reallife_gamemode.Server.Extensions;
|
||||
using reallife_gamemode.Server.Util;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
@@ -12,6 +13,8 @@ namespace reallife_gamemode.Server.Business
|
||||
public abstract class BusinessBase : IBankAccountOwner
|
||||
{
|
||||
private TextLabel _informationLabel;
|
||||
private Marker _marker;
|
||||
private ColShape _colShape;
|
||||
|
||||
public abstract int Id { get; }
|
||||
public abstract string Name { get; }
|
||||
@@ -35,7 +38,12 @@ namespace reallife_gamemode.Server.Business
|
||||
|
||||
public void Setup()
|
||||
{
|
||||
_informationLabel = NAPI.TextLabel.CreateTextLabel(Name, Position, 20.0f, 1.3f, 0, new Color(255, 255, 255));
|
||||
_informationLabel = NAPI.TextLabel.CreateTextLabel(Name, Position.Add(new Vector3(0, 0, 0.5)), 20.0f, 1.3f, 0, new Color(255, 255, 255));
|
||||
_marker = NAPI.Marker.CreateMarker(MarkerType.VerticalCylinder, Position.Subtract(new Vector3(0, 0, 1.5)), new Vector3(), new Vector3(), 1f, new Color(255, 255, 255), true);
|
||||
|
||||
_colShape = NAPI.ColShape.CreateSphereColShape(Position.Subtract(new Vector3(0, 0, 1.5)), 3f);
|
||||
_colShape.OnEntityEnterColShape += EntityEnterBusinessColShape;
|
||||
_colShape.OnEntityExitColShape += EntityExitBusinessColShape;
|
||||
|
||||
if (GetBankAccount() == null)
|
||||
{
|
||||
@@ -53,10 +61,24 @@ namespace reallife_gamemode.Server.Business
|
||||
}
|
||||
}
|
||||
|
||||
public void Update()
|
||||
private void EntityExitBusinessColShape(ColShape colShape, Client client)
|
||||
{
|
||||
client.TriggerEvent("business_removeHelp", true);
|
||||
}
|
||||
|
||||
private void EntityEnterBusinessColShape(ColShape colShape, Client client)
|
||||
{
|
||||
if (GetOwner() != null && GetOwner().Id == client.GetUser()?.Id && !client.IsInVehicle)
|
||||
{
|
||||
client.TriggerEvent("business_showHelp", Name, (GetBankAccount()?.Balance ?? 0).ToMoneyString());
|
||||
}
|
||||
}
|
||||
|
||||
public void Update(int? money = null)
|
||||
{
|
||||
if (money == null) money = GetBankAccount()?.Balance ?? 0;
|
||||
User owner = GetOwner();
|
||||
string infoText = Name + "\n" + "Besitzer: " + (owner == null ? "Niemand" : owner.Name) + "\nKasse: ~g~" + (GetBankAccount()?.Balance ?? 0) + "$";
|
||||
string infoText = Name + "\n" + "Besitzer: " + (owner == null ? "Niemand" : owner.Name) + "\nKasse: ~g~" + money.ToMoneyString();
|
||||
_informationLabel.Text = infoText;
|
||||
}
|
||||
|
||||
|
||||
@@ -22,7 +22,7 @@ namespace reallife_gamemode.Server.Entities
|
||||
set
|
||||
{
|
||||
_balance = value;
|
||||
BusinessManager.GetBusiness(BusinessId).Update();
|
||||
BusinessManager.GetBusiness(BusinessId).Update(value);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
18
Server/Extensions/IntegerExtension.cs
Normal file
18
Server/Extensions/IntegerExtension.cs
Normal file
@@ -0,0 +1,18 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace reallife_gamemode.Server.Extensions
|
||||
{
|
||||
public static class IntegerExtension
|
||||
{
|
||||
public static string ToMoneyString(this int? money)
|
||||
{
|
||||
return string.Format(Main.SERVER_CULTURE, "{0:C0}", money ?? 0);
|
||||
}
|
||||
public static string ToMoneyString(this int money)
|
||||
{
|
||||
return string.Format(Main.SERVER_CULTURE, "{0:C0}", money);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -18,28 +18,62 @@ namespace reallife_gamemode.Server.Managers
|
||||
{
|
||||
public class BankManager
|
||||
{
|
||||
public static TransactionResult SetMoney(Client admin, IBankAccountOwner owner, int amount, string reason = "Von Admin gesetzt")
|
||||
{
|
||||
using (var transferMoney = new Model.DatabaseContext())
|
||||
{
|
||||
if (amount < 0) return TransactionResult.NEGATIVE_MONEY_SENT;
|
||||
|
||||
IBankAccount account = owner.GetBankAccount(transferMoney);
|
||||
|
||||
if (account == null) return TransactionResult.SENDER_NO_BANKACCOUNT;
|
||||
|
||||
var transactionLog = new Logs.BankAccountTransactionHistory
|
||||
{
|
||||
Sender = "ADMIN: " + admin.Name,
|
||||
SenderBalance = 0,
|
||||
Receiver = owner.Name,
|
||||
ReceiverBalance = amount,
|
||||
NewReceiverBalance = amount,
|
||||
NewSenderBalance = 0,
|
||||
MoneySent = amount,
|
||||
Fee = 0,
|
||||
Origin = reason
|
||||
};
|
||||
|
||||
// add log
|
||||
transferMoney.BankAccountTransactionLogs.Add(transactionLog);
|
||||
|
||||
account.Balance += amount;
|
||||
|
||||
transferMoney.SaveChanges();
|
||||
|
||||
return TransactionResult.SUCCESS;
|
||||
}
|
||||
}
|
||||
|
||||
public static TransactionResult TransferMoney(IBankAccountOwner sender, IBankAccountOwner receiver, int amount, string origin)
|
||||
{
|
||||
using (var transferMoney = new Model.DatabaseContext())
|
||||
{
|
||||
if (amount < 0) return TransactionResult.NEGATIVE_MONEY_SENT;
|
||||
|
||||
IBankAccount senderAccount = sender?.GetBankAccount(transferMoney) ?? null;
|
||||
IBankAccount senderAccount = sender.GetBankAccount(transferMoney);
|
||||
IBankAccount receiverAccount = receiver.GetBankAccount(transferMoney);
|
||||
|
||||
if (senderAccount == null && sender != null) return TransactionResult.SENDER_NO_BANKACCOUNT;
|
||||
if (senderAccount == null) return TransactionResult.SENDER_NO_BANKACCOUNT;
|
||||
if (receiverAccount == null) return TransactionResult.RECEIVER_NO_BANKACCOUNT;
|
||||
|
||||
if ((senderAccount?.Balance ?? amount) < amount) return TransactionResult.SENDER_NOT_ENOUGH_MONEY;
|
||||
if (senderAccount.Balance < amount) return TransactionResult.SENDER_NOT_ENOUGH_MONEY;
|
||||
|
||||
var transactionLog = new Logs.BankAccountTransactionHistory
|
||||
{
|
||||
Sender = sender?.Name ?? "Admin",
|
||||
SenderBalance = senderAccount?.Balance ?? 0,
|
||||
Sender = sender.Name,
|
||||
SenderBalance = senderAccount.Balance,
|
||||
Receiver = receiver.Name,
|
||||
ReceiverBalance = receiverAccount.Balance,
|
||||
NewReceiverBalance = receiverAccount.Balance + amount,
|
||||
NewSenderBalance = (senderAccount?.Balance ?? amount) - amount,
|
||||
NewSenderBalance = senderAccount.Balance - amount,
|
||||
MoneySent = amount,
|
||||
Fee = 0,
|
||||
Origin = origin
|
||||
@@ -48,13 +82,11 @@ namespace reallife_gamemode.Server.Managers
|
||||
// add log
|
||||
transferMoney.BankAccountTransactionLogs.Add(transactionLog);
|
||||
|
||||
if(senderAccount != null) senderAccount.Balance -= amount;
|
||||
senderAccount.Balance -= amount;
|
||||
receiverAccount.Balance += amount;
|
||||
|
||||
transferMoney.SaveChanges();
|
||||
|
||||
if (receiver is BusinessBase business) business.Update();
|
||||
|
||||
return TransactionResult.SUCCESS;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,12 +10,11 @@ namespace reallife_gamemode.Server.Managers
|
||||
{
|
||||
class BusinessManager : Script
|
||||
{
|
||||
private static List<BusinessBase> businesses;
|
||||
public static List<BusinessBase> Businesses { get => businesses; }
|
||||
public static List<BusinessBase> Businesses { get; private set; }
|
||||
|
||||
public static void LoadBusinesses()
|
||||
{
|
||||
businesses = new List<BusinessBase>();
|
||||
Businesses = new List<BusinessBase>();
|
||||
|
||||
IEnumerable<Type> allTypes = Assembly.GetExecutingAssembly().GetTypes().Where(type => type.IsClass && !type.IsAbstract && type.IsSubclassOf(typeof(BusinessBase)));
|
||||
foreach (Type item in allTypes)
|
||||
@@ -23,11 +22,11 @@ namespace reallife_gamemode.Server.Managers
|
||||
NAPI.Util.ConsoleOutput($"Loading Business {item.Name}");
|
||||
if (Activator.CreateInstance(item) is BusinessBase o)
|
||||
{
|
||||
if (businesses.Any(x => x.Id == o.Id))
|
||||
if (Businesses.Any(x => x.Id == o.Id))
|
||||
{
|
||||
throw new InvalidOperationException($"Double Business ID found: {o.Id} | {o.Name}");
|
||||
}
|
||||
businesses.Add(o);
|
||||
Businesses.Add(o);
|
||||
o.Setup();
|
||||
o.Load();
|
||||
o.Update();
|
||||
@@ -37,12 +36,12 @@ namespace reallife_gamemode.Server.Managers
|
||||
|
||||
public static T GetBusiness<T>() where T : BusinessBase
|
||||
{
|
||||
return (T)businesses.Find(b => b.GetType() == typeof(T));
|
||||
return (T)Businesses.Find(b => b.GetType() == typeof(T));
|
||||
}
|
||||
|
||||
public static BusinessBase GetBusiness(int? id)
|
||||
{
|
||||
return businesses.Find(b => b.Id == id);
|
||||
return Businesses.Find(b => b.Id == id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user