diff --git a/Client/Business/main.js b/Client/Business/main.js new file mode 100644 index 00000000..d33b42e6 --- /dev/null +++ b/Client/Business/main.js @@ -0,0 +1,109 @@ +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; + +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; + } +}); + +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.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 825cc7c1..98771b62 100644 --- a/Client/index.js +++ b/Client/index.js @@ -29,3 +29,5 @@ require('./Save/main.js'); require('./Speedometer/index.js'); require('./Tuning/main.js'); + +require('./Business/main.js'); diff --git a/Main.cs b/Main.cs index c7fbc1c2..81453b22 100644 --- a/Main.cs +++ b/Main.cs @@ -1,4 +1,5 @@ using System; +using System.Globalization; using System.Linq; using GTANetworkAPI; using Microsoft.EntityFrameworkCore; @@ -20,6 +21,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/Server/Business/BusinessBase.cs b/Server/Business/BusinessBase.cs index 7233343a..921156ee 100644 --- a/Server/Business/BusinessBase.cs +++ b/Server/Business/BusinessBase.cs @@ -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; } diff --git a/Server/Entities/BusinessBankAccount.cs b/Server/Entities/BusinessBankAccount.cs index 745faa52..fd056e8d 100644 --- a/Server/Entities/BusinessBankAccount.cs +++ b/Server/Entities/BusinessBankAccount.cs @@ -22,7 +22,7 @@ namespace reallife_gamemode.Server.Entities set { _balance = value; - BusinessManager.GetBusiness(BusinessId).Update(); + BusinessManager.GetBusiness(BusinessId).Update(value); } } diff --git a/Server/Extensions/IntegerExtension.cs b/Server/Extensions/IntegerExtension.cs new file mode 100644 index 00000000..61f2553a --- /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 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); + } + } +} diff --git a/Server/Managers/BankManager.cs b/Server/Managers/BankManager.cs index c4b726bf..6f2fba39 100644 --- a/Server/Managers/BankManager.cs +++ b/Server/Managers/BankManager.cs @@ -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; } } diff --git a/Server/Managers/BusinessManager.cs b/Server/Managers/BusinessManager.cs index 02d95455..1ddc3b5d 100644 --- a/Server/Managers/BusinessManager.cs +++ b/Server/Managers/BusinessManager.cs @@ -10,12 +10,11 @@ namespace reallife_gamemode.Server.Managers { class BusinessManager : Script { - private static List businesses; - public static List Businesses { get => businesses; } + public static List Businesses { get; private set; } public static void LoadBusinesses() { - businesses = new List(); + Businesses = new List(); IEnumerable 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() 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); } } }