Merged feature/business-system into develop

This commit is contained in:
hydrant
2018-11-27 16:08:22 +01:00
17 changed files with 781 additions and 6 deletions

View File

@@ -0,0 +1,102 @@
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;
using System.Linq;
using System.Text;
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; }
public abstract Vector3 Position { get; }
public IBankAccount GetBankAccount(DatabaseContext databaseContext = null)
{
if (databaseContext == null)
{
using (databaseContext = new DatabaseContext())
{
return databaseContext.BusinessBankAccounts.FirstOrDefault(u => u.BusinessId == Id);
}
}
else
{
return databaseContext.BusinessBankAccounts.FirstOrDefault(u => u.BusinessId == Id);
}
}
public void Setup()
{
_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)
{
NAPI.Util.ConsoleOutput("Creating Bank Account for Business: " + Name);
using (var dbContext = new DatabaseContext())
{
dbContext.BusinessBankAccounts.Add(new BusinessBankAccount()
{
BusinessId = Id,
Balance = 0
});
dbContext.SaveChanges();
}
}
}
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~" + money.ToMoneyString();
_informationLabel.Text = infoText;
}
public User GetOwner(DatabaseContext dbContext = null)
{
if(dbContext == null)
{
using (dbContext = new DatabaseContext())
{
return dbContext.Users.FirstOrDefault(u => u.BusinessId == Id);
}
}
else
{
return dbContext.Users.FirstOrDefault(u => u.BusinessId == Id);
}
}
public abstract void Load();
}
}

View File

@@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.Text;
using GTANetworkAPI;
namespace reallife_gamemode.Server.Business
{
class ShopBusiness : BusinessBase
{
public override int Id => 2;
public override string Name => "24/7 Business";
public override Vector3 Position => new Vector3(-443, 1134, 326);
public override void Load()
{
}
}
}

View File

@@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.Text;
using GTANetworkAPI;
namespace reallife_gamemode.Server.Business
{
public class TelefonBusiness : BusinessBase
{
public override int Id => 1;
public override string Name => "Telefon Business";
public override Vector3 Position => new Vector3(-423, 1130, 326);
public override void Load()
{
}
}
}

View File

@@ -15,6 +15,7 @@ using reallife_gamemode.Server.Services;
using reallife_gamemode.Server.Util;
using reallife_gamemode.Server.Managers;
using reallife_gamemode.Server.Saves;
using reallife_gamemode.Server.Business;
/**
* @overview Life of German Reallife - Admin Commands (Admin.cs)
@@ -172,6 +173,22 @@ namespace reallife_gamemode.Server.Commands
}
}
}
[Command("businesslist", "~m~Benutzung: ~s~/businesslist")]
public void CmdAdminBusinessList(Client player)
{
if (!player.GetUser()?.IsAdmin(AdminLevel.SUPPORTER) ?? true)
{
ChatService.NotAuthorized(player);
return;
}
player.SendChatMessage("~m~__________ ~s~Businesses ~m~__________");
foreach (Business.BusinessBase b in BusinessManager.Businesses)
{
player.SendChatMessage(b.Id.ToString().PadRight(3) + " | " + b.Name);
}
}
#endregion
@@ -1622,6 +1639,7 @@ namespace reallife_gamemode.Server.Commands
NAPI.Chat.SendChatMessageToPlayer(player, "~w~Das Wetter konnte nicht geändert werden");
}
}
[Command("aspeed", "~m~Benutzung: ~s~/aspeed [Modifier]")] //TODO: Überarbeiten ?? SetPlayerVelocity ??
public void CmdAdminAspeed(Client player, float modifier)
{
@@ -1639,7 +1657,8 @@ namespace reallife_gamemode.Server.Commands
player.Vehicle.EnginePowerMultiplier = modifier;
}
[Command("setmoney")]
[Command("setmoney", "~m~Benutzung: ~s~/setmoney [Name] [Menge]")]
public void SetPlayerMoney(Client player, string receiver, int amount)
{
if (!player.GetUser()?.IsAdmin(AdminLevel.HEADADMIN) ?? true)
@@ -1663,7 +1682,7 @@ namespace reallife_gamemode.Server.Commands
target.SendChatMessage("~b~[ADMIN]~s~ Dein Geld wurde von Admin " + player.Name + " auf ~g~$" + amount + "~s~ gesetzt.");
}
[Command("givemoney")]
[Command("givemoney", "~m~Benutzung: ~s~/givemoney [Name] [Menge]")]
public void GivePlayerMoney(Client player, string receiver, int amount)
{
if (!player.GetUser()?.IsAdmin(AdminLevel.HEADADMIN) ?? true)
@@ -1671,6 +1690,7 @@ namespace reallife_gamemode.Server.Commands
ChatService.NotAuthorized(player);
return;
}
Client target = ClientService.GetClientByNameOrId(receiver);
if (target == null || !target.IsLoggedIn())
{
@@ -1686,6 +1706,106 @@ namespace reallife_gamemode.Server.Commands
player.SendChatMessage("~b~[ADMIN]~s~ Du hast " + target.Name + " ~g~$" + amount + "~s~ gegeben.");
target.SendChatMessage("~b~[ADMIN]~s~ Admin " + player.Name + " hat dir ~g~$" + amount + "~s~ gegeben.");
}
[Command("setbusinessowner", "~m~Benutzung: ~s~/setbusinessowner [Name] [Business ID]")]
public void CmdAdminSetbusinessowner(Client player, string name, int businessid)
{
if (!player.GetUser()?.IsAdmin(AdminLevel.HEADADMIN) ?? true)
{
ChatService.NotAuthorized(player);
return;
}
Client target = ClientService.GetClientByNameOrId(name);
if (target == null || !target.IsLoggedIn())
{
ChatService.PlayerNotFound(player);
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)
{
player.SendChatMessage("~r~[FEHLER]~s~ Dieses Business existiert nicht. ~m~/businesslist");
return;
}
if(business.GetOwner() != null)
{
player.SendChatMessage("~r~[FEHLER]~s~ Das Business hat momentan noch einen Besitzer: ~o~" + business.GetOwner().Name + "~s~. Entferne diesen Besitzer erst mit ~m~/clearbusiness");
return;
}
using(var dbContext = new DatabaseContext())
{
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

View File

@@ -0,0 +1,31 @@
using reallife_gamemode.Server.Business;
using reallife_gamemode.Server.Managers;
using reallife_gamemode.Server.Util;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Text;
namespace reallife_gamemode.Server.Entities
{
public class BusinessBankAccount : IBankAccount
{
[NotMapped]
private int _balance;
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public int Balance {
get => _balance;
set
{
_balance = value;
BusinessManager.GetBusiness(BusinessId).Update(value);
}
}
public int BusinessId { get; set; }
}
}

View File

@@ -3,11 +3,9 @@ using reallife_gamemode.Model;
using reallife_gamemode.Server.Extensions;
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)
@@ -59,6 +57,8 @@ namespace reallife_gamemode.Server.Entities
public int? FactionRankId { get; set; }
public FactionRank FactionRank { get;set; }
public int? BusinessId { get; set; }
public Faction GetFaction()
{
using(var context = new DatabaseContext())
@@ -146,5 +146,10 @@ namespace reallife_gamemode.Server.Entities
return databaseContext.UserBankAccounts.FirstOrDefault(u => u.UserId == this.Id);
}
}
public Client GetClient()
{
return NAPI.Player.GetPlayerFromName(Name);
}
}
}

View 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 ToMoneyString(money ?? 0);
}
public static string ToMoneyString(this int money)
{
return string.Format(Main.SERVER_CULTURE, "{0:C0}", money).Replace('€', '$');
}
}
}

View File

@@ -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;
@@ -13,11 +14,44 @@ using reallife_gamemode.Server.Util;
* @copyright (c) 2008 - 2018 Life of German
*/
namespace reallife_gamemode.Server.Managers
{
public class BankManager : Script
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())

View File

@@ -0,0 +1,112 @@
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;
}
}
}
}