diff --git a/Client/Business/main.js b/Client/Business/main.js new file mode 100644 index 00000000..fe4206c3 --- /dev/null +++ b/Client/Business/main.js @@ -0,0 +1,143 @@ +var keyBound = false; + +var closeMenu = false; + +var businessName; +var businessMoney; +var mainMenu; +var bankMenu; + +const NativeUI = require("nativeui"); +const Menu = NativeUI.Menu; +const UIMenuItem = NativeUI.UIMenuItem; +const UIMenuListItem = NativeUI.UIMenuListItem; +const UIMenuCheckboxItem = NativeUI.UIMenuCheckboxItem; +const UIMenuSliderItem = NativeUI.UIMenuSliderItem; +const BadgeStyle = NativeUI.BadgeStyle; +const Point = NativeUI.Point; +const ItemsCollection = NativeUI.ItemsCollection; +const Color = NativeUI.Color; +const ListItem = NativeUI.ListItem; + +const InputHelper = require("inputhelper"); + +mp.events.add('business_showHelp', (bizName, bizMoney) => { + mp.game.ui.setTextComponentFormat('STRING'); + mp.game.ui.addTextComponentSubstringPlayerName('Drücke ~INPUT_CONTEXT~, um dein Business zu verwalten'); + mp.game.ui.displayHelpTextFromStringLabel(0, true, true, -1); + + businessName = bizName; + businessMoney = bizMoney; + + mp.keys.bind(0x45, false, keyPressHandler); + keyBound = true; +}); + +mp.events.add('business_removeHelp', (unbind) => { + mp.game.ui.clearHelp(true); + mp.gui.chat.show(true); + + if (keyBound && unbind) { + if (typeof mainMenu !== "undefined") mainMenu.Close(); + if (typeof bankMenu !== "undefined") { + closeMenu = true; + bankMenu.Close(); + } + + mp.keys.unbind(0x45, false, keyPressHandler); + keyBound = false; + } +}); + +mp.events.add('business_updateMoney', (newMoney) => { + businessMoney = newMoney; +}); + +function keyPressHandler() { + mp.events.call('business_removeHelp', false); + mp.gui.chat.show(false); + + if (typeof mainMenu !== "undefined" && mainMenu.Visible) { + return; + } + + if (typeof bankMenu !== "undefined" && bankMenu.Visible) { + return; + } + + mainMenu = new Menu("Businessverwaltung", businessName, new Point(50, 50)); + + var bankAccountItem = new UIMenuItem("Businesskasse", "Verwalte die Businesskasse"); + bankAccountItem.SetRightLabel("~g~~h~" + businessMoney); + mainMenu.AddItem(bankAccountItem); + + //var partnerItem = new UIMenuItem("Inteilhaber", "Verwalte den Inteilhaber"); + //partnerItem.SetRightLabel("Niemand"); + //mainMenu.AddItem(partnerItem); + + mainMenu.Open(); + + mainMenu.ItemSelect.on((item, index) => { + if (item === bankAccountItem) { + // manage bank account + + bankMenu = new Menu("Bankkonto", businessName, new Point(50, 50)); + + var infoItem = new UIMenuItem("Aktueller Kontostand"); + infoItem.SetRightLabel("~g~~h~" + businessMoney); + bankMenu.AddItem(infoItem); + + var depositItem = new UIMenuItem("Einzahlen", "Zahle Geld auf die Businesskasse ein"); + bankMenu.AddItem(depositItem); + + var withdrawItem = new UIMenuItem("Auszahlen", "Zahle Geld von der Businesskasse aus"); + bankMenu.AddItem(withdrawItem); + + bankMenu.ItemSelect.on((item, index) => { + if (item === depositItem) { + var depositInput = new InputHelper("Wie viel Geld möchtest du auf deine Businesskasse einzahlen?"); + depositInput.show(); + depositInput.getValue((data) => { + var amount = parseInt(data); + if (isNaN(amount)) { + mp.game.graphics.notify('~r~Du musst eine Nummer eingeben!'); + return; + } + + mp.events.callRemote('Business_DepositMoney', amount); + }); + } else if (item === withdrawItem) { + var withdrawInput = new InputHelper("Wie viel Geld möchtest du von deiner Businesskasse abheben?"); + withdrawInput.show(); + withdrawInput.getValue((data) => { + var amount = parseInt(data); + if (isNaN(amount)) { + mp.game.graphics.notify('~r~Du musst eine Nummer eingeben!'); + return; + } + + mp.events.callRemote('Business_WithdrawMoney', amount); + }); + } + }); + + bankMenu.MenuClose.on(() => { + if (closeMenu) { + closeMenu = false; + return; + } + mainMenu.Visible = true; + }); + + bankMenu.Visible = true; + mainMenu.Visible = false; + + } else if (item === partnerItem) { + // manage partner + } + }); + + mainMenu.MenuClose.on(() => { + mp.events.call('business_removeHelp', false); + }); +} \ No newline at end of file diff --git a/Client/index.js b/Client/index.js index 0beb4a31..39cecf52 100644 --- a/Client/index.js +++ b/Client/index.js @@ -30,3 +30,5 @@ require('./Save/main.js'); require('./Speedometer/index.js'); require('./Tuning/main.js'); + +require('./Business/main.js'); diff --git a/Client/inputhelper/index.js b/Client/inputhelper/index.js new file mode 100644 index 00000000..93d636a1 --- /dev/null +++ b/Client/inputhelper/index.js @@ -0,0 +1,66 @@ +class InputHelper { + constructor(title) { + this.title = title; + + this.cefTitleCall = this.cefTitleCall.bind(this); + mp.events.add('cef_request_title', this.cefTitleCall); + + this.cefCallback = this.cefCallback.bind(this); + mp.events.add('cef_inputhelper_sendvalue', this.cefCallback); + + this.finish = this.finish.bind(this); + this.show = this.show.bind(this); + this.valueGetter = this.valueGetter.bind(this); + this.getValue = this.getValue.bind(this); + + this.value = undefined; + + mp.events.add('render', this.disableControls); + } + + disableControls() { + for (var i = 0; i <= 33; i++) { + mp.game.controls.disableAllControlActions(i); + } + } + + show() { + if (this.created) return; + this.created = true; + this.browser = mp.browsers.new('package://inputhelper/web/inputhelper.html'); + } + + finish() { + if (this.browser) { + mp.events.remove('cef_inputhelper_sendvalue'); + mp.events.remove('cef_request_title'); + mp.events.remove('render', this.disableControls); + this.browser.destroy(); + this.created = false; + } + } + + cefTitleCall() { + this.browser.execute(`setTitle('${this.title}')`); + } + + cefCallback(val) { + this.value = val; + this.finish(); + } + + valueGetter() { + return new Promise(resolve => { + setInterval(() => { + if (this.value !== undefined) resolve(this.value); + }, 50); + }); + } + + async getValue(callback) { + var getVal = await this.valueGetter(); + callback(getVal); + } +} + +exports = InputHelper; \ No newline at end of file diff --git a/Client/inputhelper/web/inputhelper.css b/Client/inputhelper/web/inputhelper.css new file mode 100644 index 00000000..dc873c5a --- /dev/null +++ b/Client/inputhelper/web/inputhelper.css @@ -0,0 +1,50 @@ +* { + box-sizing: border-box; + margin: 0; + padding: 0; +} + +#black { + position: fixed; + top: 0; + left: 0; + right: 0; + bottom: 0; + width: 100%; + height: 100%; + z-index: -1; + background-color: rgba(0, 0, 0, .3); +} + +.input-main { + display: block; + width: 70%; + position: absolute; + top: 50%; + left: 50%; + transform: translate(-50%, -50%); + + background-color: rgba(0, 0, 0, .8); + padding: 10px; +} + +.input-main h1 { + color: white; + font-size: 24px; + font-family: "Arial"; + font-weight: lighter; + margin-bottom: 5px; +} + +.input-main input { + width: 100%; + background-color: black; + outline: 0; + border: grey 1px solid; + color: white; + padding: 5px; + font-size: 20px; + font-family: "Arial"; + font-weight: lighter; +} + \ No newline at end of file diff --git a/Client/inputhelper/web/inputhelper.html b/Client/inputhelper/web/inputhelper.html new file mode 100644 index 00000000..ea0b6df1 --- /dev/null +++ b/Client/inputhelper/web/inputhelper.html @@ -0,0 +1,32 @@ + + + + + + +
+
+

+ +
+ + + + \ No newline at end of file diff --git a/Main.cs b/Main.cs index 875f1932..08a2c96f 100644 --- a/Main.cs +++ b/Main.cs @@ -1,4 +1,5 @@ using System; +using System.Globalization; using System.Linq; using GTANetworkAPI; using Microsoft.EntityFrameworkCore; @@ -21,6 +22,8 @@ namespace reallife_gamemode public static readonly Vector3 DEFAULT_SPAWN_POSITION = new Vector3(-427.5189, 1116.453, 326.7829); public static readonly float DEFAULT_SPAWN_HEADING = 340.8f; + public static readonly CultureInfo SERVER_CULTURE = new CultureInfo("de-DE"); + [ServerEvent(Event.ResourceStart)] public void OnResourceStart() { diff --git a/Model/DatabaseContext.cs b/Model/DatabaseContext.cs index 4ab459b7..7cf90449 100644 --- a/Model/DatabaseContext.cs +++ b/Model/DatabaseContext.cs @@ -17,6 +17,7 @@ namespace reallife_gamemode.Model public DatabaseContext() { + } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) @@ -28,6 +29,14 @@ namespace reallife_gamemode.Model protected override void OnModelCreating(ModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); + + modelBuilder.Entity() + .HasIndex(b => b.BusinessId) + .IsUnique(true); + + modelBuilder.Entity() + .HasIndex(u => u.BusinessId) + .IsUnique(true); } //User @@ -66,6 +75,9 @@ namespace reallife_gamemode.Model public DbSet Vehicles { get; set; } public DbSet ShopVehicles { get; set; } + // Business + public DbSet BusinessBankAccounts { get; set; } + // Control Panel public DbSet News { get; set; } } diff --git a/Server/Business/BusinessBase.cs b/Server/Business/BusinessBase.cs new file mode 100644 index 00000000..921156ee --- /dev/null +++ b/Server/Business/BusinessBase.cs @@ -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(); + } +} diff --git a/Server/Business/ShopBusiness.cs b/Server/Business/ShopBusiness.cs new file mode 100644 index 00000000..283f33bb --- /dev/null +++ b/Server/Business/ShopBusiness.cs @@ -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() + { + + } + } +} diff --git a/Server/Business/TelefonBusiness.cs b/Server/Business/TelefonBusiness.cs new file mode 100644 index 00000000..02d11e2b --- /dev/null +++ b/Server/Business/TelefonBusiness.cs @@ -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() + { + + } + } +} diff --git a/Server/Commands/Admin.cs b/Server/Commands/Admin.cs index f9e2f6f2..5dd7a714 100644 --- a/Server/Commands/Admin.cs +++ b/Server/Commands/Admin.cs @@ -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 diff --git a/Server/Entities/BusinessBankAccount.cs b/Server/Entities/BusinessBankAccount.cs new file mode 100644 index 00000000..fd056e8d --- /dev/null +++ b/Server/Entities/BusinessBankAccount.cs @@ -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; } + } +} diff --git a/Server/Entities/User.cs b/Server/Entities/User.cs index 7257bac7..246e0d27 100644 --- a/Server/Entities/User.cs +++ b/Server/Entities/User.cs @@ -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); + } } } diff --git a/Server/Extensions/IntegerExtension.cs b/Server/Extensions/IntegerExtension.cs new file mode 100644 index 00000000..96bd1aa8 --- /dev/null +++ b/Server/Extensions/IntegerExtension.cs @@ -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('€', '$'); + } + } +} diff --git a/Server/Managers/BankManager.cs b/Server/Managers/BankManager.cs index 4d4b1d77..6f2fba39 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; @@ -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()) diff --git a/Server/Managers/BusinessManager.cs b/Server/Managers/BusinessManager.cs new file mode 100644 index 00000000..7dd56a1f --- /dev/null +++ b/Server/Managers/BusinessManager.cs @@ -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 Businesses { get; private set; } + + public static void LoadBusinesses() + { + Businesses = new List(); + + IEnumerable 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() 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; + } + } + } +} diff --git a/reallife-gamemode.csproj b/reallife-gamemode.csproj index 95166b1a..8019dc4d 100644 --- a/reallife-gamemode.csproj +++ b/reallife-gamemode.csproj @@ -22,6 +22,9 @@ ..\Bootstrapper.dll + + +