113 lines
4.0 KiB
C#
113 lines
4.0 KiB
C#
using GTANetworkAPI;
|
|
using reallife_gamemode.Server.Business;
|
|
using reallife_gamemode.Server.Entities;
|
|
using reallife_gamemode.Server.Extensions;
|
|
using reallife_gamemode.Server.Util;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
using System.Text;
|
|
|
|
namespace reallife_gamemode.Server.Managers
|
|
{
|
|
class BusinessManager : Script
|
|
{
|
|
public static List<BusinessBase> Businesses { get; private set; }
|
|
|
|
public static void LoadBusinesses()
|
|
{
|
|
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)
|
|
{
|
|
NAPI.Util.ConsoleOutput($"Loading Business {item.Name}");
|
|
if (Activator.CreateInstance(item) is BusinessBase o)
|
|
{
|
|
if (GetBusiness(o.Id) != null)
|
|
{
|
|
throw new InvalidOperationException($"Double Business ID found: {o.Id} | {o.Name}");
|
|
}
|
|
Businesses.Add(o);
|
|
o.Setup();
|
|
o.Load();
|
|
o.Update();
|
|
}
|
|
}
|
|
}
|
|
|
|
public static T GetBusiness<T>() where T : BusinessBase
|
|
{
|
|
return (T)Businesses.Find(b => b.GetType() == typeof(T));
|
|
}
|
|
|
|
public static BusinessBase GetBusiness(int? id)
|
|
{
|
|
return Businesses.Find(b => b.Id == id);
|
|
}
|
|
|
|
[RemoteEvent("Business_DepositMoney")]
|
|
public void BusinessDepositMoney(Client player, int amount)
|
|
{
|
|
User user = player.GetUser();
|
|
if(user == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
BusinessBase playerBusiness = GetBusiness(user.BusinessId);
|
|
|
|
TransactionResult result = BankManager.TransferMoney(user, playerBusiness, amount, "Überweisung");
|
|
|
|
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)
|
|
{
|
|
player.TriggerEvent("business_updateMoney", playerBusiness.GetBankAccount().Balance.ToMoneyString());
|
|
player.SendNotification("~g~Du hast erfolgreich ~s~" + amount.ToMoneyString() + " ~g~ überwiesen");
|
|
return;
|
|
}
|
|
}
|
|
|
|
[RemoteEvent("Business_WithdrawMoney")]
|
|
public void BusinessWithdrawMoney(Client player, int amount)
|
|
{
|
|
User user = player.GetUser();
|
|
if (user == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
BusinessBase playerBusiness = GetBusiness(user.BusinessId);
|
|
|
|
TransactionResult result = BankManager.TransferMoney(playerBusiness, user, amount, "Überweisung");
|
|
|
|
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)
|
|
{
|
|
player.TriggerEvent("business_updateMoney", playerBusiness.GetBankAccount().Balance.ToMoneyString());
|
|
player.SendNotification("~g~Du hast erfolgreich ~s~" + amount.ToMoneyString() + " ~g~ überwiesen");
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
}
|