From 52d8a744980dfade83b1f2868d2561b351eeea19 Mon Sep 17 00:00:00 2001 From: hydrant Date: Sun, 18 Nov 2018 16:56:37 +0100 Subject: [PATCH 01/12] Added business database entity (+ bank account) --- Model/DatabaseContext.cs | 4 +++ Server/Entities/Business.cs | 35 ++++++++++++++++++++++++++ Server/Entities/BusinessBankAccount.cs | 21 ++++++++++++++++ 3 files changed, 60 insertions(+) create mode 100644 Server/Entities/Business.cs create mode 100644 Server/Entities/BusinessBankAccount.cs diff --git a/Model/DatabaseContext.cs b/Model/DatabaseContext.cs index 828b051e..b9758761 100644 --- a/Model/DatabaseContext.cs +++ b/Model/DatabaseContext.cs @@ -68,6 +68,10 @@ namespace reallife_gamemode.Model public DbSet Vehicles { get; set; } public DbSet ShopVehicles { get; set; } + // Business + public DbSet Businesses { get; set; } + public DbSet BusinessBankAccounts { get; set; } + // Control Panel public DbSet News { get; set; } } diff --git a/Server/Entities/Business.cs b/Server/Entities/Business.cs new file mode 100644 index 00000000..3f4ab18f --- /dev/null +++ b/Server/Entities/Business.cs @@ -0,0 +1,35 @@ +using reallife_gamemode.Model; +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; + +namespace reallife_gamemode.Server.Entities +{ + public class Business : IBankAccountOwner + { + [Key] + [DatabaseGenerated(DatabaseGeneratedOption.Identity)] + public int Id { get; set; } + + public string Name { get; set; } + + public IBankAccount GetBankAccount (DatabaseContext databaseContext = null) + { + if (databaseContext == null) + { + using (databaseContext = new DatabaseContext()) + { + return databaseContext.BusinessBankAccounts.FirstOrDefault(u => u.BusinessId == this.Id); + } + } + else + { + return databaseContext.BusinessBankAccounts.FirstOrDefault(u => u.BusinessId == this.Id); + } + } + } +} diff --git a/Server/Entities/BusinessBankAccount.cs b/Server/Entities/BusinessBankAccount.cs new file mode 100644 index 00000000..0139e19f --- /dev/null +++ b/Server/Entities/BusinessBankAccount.cs @@ -0,0 +1,21 @@ +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 + { + [Key] + [DatabaseGenerated(DatabaseGeneratedOption.Identity)] + public int Id { get; set; } + public int Balance { get; set; } + + [ForeignKey("Business")] + public int BusinessId { get; set; } + public Business Business { get; set; } + } +} From 53b9acab8b7b0e02d2b4c49ace4434eb16af2f98 Mon Sep 17 00:00:00 2001 From: hydrant Date: Sun, 18 Nov 2018 22:21:02 +0100 Subject: [PATCH 02/12] Continued business system --- Main.cs | 2 + Model/DatabaseContext.cs | 6 ++- .../Business.cs => Business/IBusiness.cs} | 20 ++++----- Server/Business/TestBusiness.cs | 23 +++++++++++ Server/Entities/BusinessBankAccount.cs | 7 ++-- Server/Managers/BankManager.cs | 2 +- Server/Managers/BusinessManager.cs | 41 +++++++++++++++++++ reallife-gamemode.csproj | 3 ++ 8 files changed, 89 insertions(+), 15 deletions(-) rename Server/{Entities/Business.cs => Business/IBusiness.cs} (56%) create mode 100644 Server/Business/TestBusiness.cs create mode 100644 Server/Managers/BusinessManager.cs diff --git a/Main.cs b/Main.cs index 4f19f29f..c7fbc1c2 100644 --- a/Main.cs +++ b/Main.cs @@ -34,6 +34,8 @@ namespace reallife_gamemode TuningManager.AddTuningGarage(new Vector3(-341, -134, 38.5)); + BusinessManager.LoadBusinesses(); + using (var context = new DatabaseContext()) { context.Bans.FirstOrDefault(); diff --git a/Model/DatabaseContext.cs b/Model/DatabaseContext.cs index b9758761..4255923e 100644 --- a/Model/DatabaseContext.cs +++ b/Model/DatabaseContext.cs @@ -23,6 +23,7 @@ namespace reallife_gamemode.Model public DatabaseContext() { + } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) @@ -34,6 +35,10 @@ namespace reallife_gamemode.Model protected override void OnModelCreating(ModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); + + modelBuilder.Entity() + .HasIndex(b => b.BusinessId) + .IsUnique(true); } //User @@ -69,7 +74,6 @@ namespace reallife_gamemode.Model public DbSet ShopVehicles { get; set; } // Business - public DbSet Businesses { get; set; } public DbSet BusinessBankAccounts { get; set; } // Control Panel diff --git a/Server/Entities/Business.cs b/Server/Business/IBusiness.cs similarity index 56% rename from Server/Entities/Business.cs rename to Server/Business/IBusiness.cs index 3f4ab18f..f118b7ae 100644 --- a/Server/Entities/Business.cs +++ b/Server/Business/IBusiness.cs @@ -1,23 +1,21 @@ -using reallife_gamemode.Model; +using GTANetworkAPI; +using reallife_gamemode.Model; 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; -namespace reallife_gamemode.Server.Entities +namespace reallife_gamemode.Server.Business { - public class Business : IBankAccountOwner + public abstract class BusinessBase : IBankAccountOwner { - [Key] - [DatabaseGenerated(DatabaseGeneratedOption.Identity)] - public int Id { get; set; } + public abstract int Id { get; } + public abstract string Name { get; } - public string Name { get; set; } + public abstract Vector3 Position { get; } - public IBankAccount GetBankAccount (DatabaseContext databaseContext = null) + public IBankAccount GetBankAccount(DatabaseContext databaseContext = null) { if (databaseContext == null) { @@ -31,5 +29,7 @@ namespace reallife_gamemode.Server.Entities return databaseContext.BusinessBankAccounts.FirstOrDefault(u => u.BusinessId == this.Id); } } + + public abstract void Load(); } } diff --git a/Server/Business/TestBusiness.cs b/Server/Business/TestBusiness.cs new file mode 100644 index 00000000..d856214c --- /dev/null +++ b/Server/Business/TestBusiness.cs @@ -0,0 +1,23 @@ +using System; +using System.Collections.Generic; +using System.Text; +using GTANetworkAPI; +using reallife_gamemode.Model; +using reallife_gamemode.Server.Util; + +namespace reallife_gamemode.Server.Business +{ + class TestBusiness : BusinessBase + { + public override int Id => 1; + + public override string Name => "Test Business"; + + public override Vector3 Position => throw new NotImplementedException(); + + public override void Load() + { + throw new NotImplementedException(); + } + } +} diff --git a/Server/Entities/BusinessBankAccount.cs b/Server/Entities/BusinessBankAccount.cs index 0139e19f..fd283008 100644 --- a/Server/Entities/BusinessBankAccount.cs +++ b/Server/Entities/BusinessBankAccount.cs @@ -1,4 +1,5 @@ -using reallife_gamemode.Server.Util; +using reallife_gamemode.Server.Business; +using reallife_gamemode.Server.Util; using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; @@ -14,8 +15,8 @@ namespace reallife_gamemode.Server.Entities public int Id { get; set; } public int Balance { get; set; } - [ForeignKey("Business")] public int BusinessId { get; set; } - public Business Business { get; set; } + [NotMapped] + public BusinessBase Business { get; set; } } } diff --git a/Server/Managers/BankManager.cs b/Server/Managers/BankManager.cs index 4d4b1d77..6b5cf39c 100644 --- a/Server/Managers/BankManager.cs +++ b/Server/Managers/BankManager.cs @@ -16,7 +16,7 @@ using reallife_gamemode.Server.Util; namespace reallife_gamemode.Server.Managers { - public class BankManager : Script + public class BankManager { public static TransactionResult TransferMoney(IBankAccountOwner sender, IBankAccountOwner receiver, int amount, string origin) { diff --git a/Server/Managers/BusinessManager.cs b/Server/Managers/BusinessManager.cs new file mode 100644 index 00000000..be3e0ef7 --- /dev/null +++ b/Server/Managers/BusinessManager.cs @@ -0,0 +1,41 @@ +using GTANetworkAPI; +using reallife_gamemode.Server.Business; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using System.Text; + +namespace reallife_gamemode.Server.Managers +{ + class BusinessManager : Script + { + private static List businesses; + + 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 (businesses.Find(b => b.GetType() == item) != null) + { + throw new InvalidOperationException($"Double Business found: {o.Id} | {o.Name}"); + } + businesses.Add(o); + o.Load(); + } + } + } + + public static T GetBusiness() where T : BusinessBase + { + return (T)businesses.Find(b => b.GetType() == typeof(T)); + } + + } +} 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 + + + From 826114e6629f7297d9d6273991d4c40c0b97ffb7 Mon Sep 17 00:00:00 2001 From: hydrant Date: Mon, 19 Nov 2018 08:40:38 +0100 Subject: [PATCH 03/12] Renamed IBusiness.cs to BusinessBase.cs --- Server/Business/{IBusiness.cs => BusinessBase.cs} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename Server/Business/{IBusiness.cs => BusinessBase.cs} (97%) diff --git a/Server/Business/IBusiness.cs b/Server/Business/BusinessBase.cs similarity index 97% rename from Server/Business/IBusiness.cs rename to Server/Business/BusinessBase.cs index f118b7ae..bc9a2b1d 100644 --- a/Server/Business/IBusiness.cs +++ b/Server/Business/BusinessBase.cs @@ -1,4 +1,4 @@ -using GTANetworkAPI; +using GTANetworkAPI; using reallife_gamemode.Model; using reallife_gamemode.Server.Util; using System; From 225eb78ced0596d2d96c10685844fbf07674ad6b Mon Sep 17 00:00:00 2001 From: hydrant Date: Mon, 19 Nov 2018 20:11:59 +0100 Subject: [PATCH 04/12] Continued business system --- Server/Entities/BusinessBankAccount.cs | 2 -- 1 file changed, 2 deletions(-) diff --git a/Server/Entities/BusinessBankAccount.cs b/Server/Entities/BusinessBankAccount.cs index fd283008..525b8192 100644 --- a/Server/Entities/BusinessBankAccount.cs +++ b/Server/Entities/BusinessBankAccount.cs @@ -16,7 +16,5 @@ namespace reallife_gamemode.Server.Entities public int Balance { get; set; } public int BusinessId { get; set; } - [NotMapped] - public BusinessBase Business { get; set; } } } From cef42180c9a8298baffac68e5c38a015ecab48c9 Mon Sep 17 00:00:00 2001 From: hydrant Date: Mon, 19 Nov 2018 22:24:06 +0100 Subject: [PATCH 05/12] Added commands to assign business to user --- Model/DatabaseContext.cs | 4 ++ Server/Business/BusinessBase.cs | 31 ++++++++++++++- Server/Business/SmsBusiness.cs | 21 ++++++++++ Server/Business/TestBusiness.cs | 6 +-- Server/Commands/Admin.cs | 64 +++++++++++++++++++++++++++++- Server/Entities/User.cs | 2 + Server/Managers/BusinessManager.cs | 11 ++++- 7 files changed, 130 insertions(+), 9 deletions(-) create mode 100644 Server/Business/SmsBusiness.cs diff --git a/Model/DatabaseContext.cs b/Model/DatabaseContext.cs index 4255923e..fd90f1c1 100644 --- a/Model/DatabaseContext.cs +++ b/Model/DatabaseContext.cs @@ -39,6 +39,10 @@ namespace reallife_gamemode.Model modelBuilder.Entity() .HasIndex(b => b.BusinessId) .IsUnique(true); + + modelBuilder.Entity() + .HasIndex(u => u.BusinessId) + .IsUnique(true); } //User diff --git a/Server/Business/BusinessBase.cs b/Server/Business/BusinessBase.cs index bc9a2b1d..271df4aa 100644 --- a/Server/Business/BusinessBase.cs +++ b/Server/Business/BusinessBase.cs @@ -1,5 +1,6 @@ using GTANetworkAPI; using reallife_gamemode.Model; +using reallife_gamemode.Server.Entities; using reallife_gamemode.Server.Util; using System; using System.Collections.Generic; @@ -10,6 +11,8 @@ namespace reallife_gamemode.Server.Business { public abstract class BusinessBase : IBankAccountOwner { + private TextLabel _informationLabel; + public abstract int Id { get; } public abstract string Name { get; } @@ -21,12 +24,36 @@ namespace reallife_gamemode.Server.Business { using (databaseContext = new DatabaseContext()) { - return databaseContext.BusinessBankAccounts.FirstOrDefault(u => u.BusinessId == this.Id); + return databaseContext.BusinessBankAccounts.FirstOrDefault(u => u.BusinessId == Id); } } else { - return databaseContext.BusinessBankAccounts.FirstOrDefault(u => u.BusinessId == this.Id); + return databaseContext.BusinessBankAccounts.FirstOrDefault(u => u.BusinessId == Id); + } + } + + public void Setup() + { + _informationLabel = NAPI.TextLabel.CreateTextLabel(Name, Position, 20.0f, 1.3f, 0, new Color(255, 255, 255)); + } + + public void Update() + { + //NAPI.Util.ConsoleOutput("Updating Business: " + Name); + + User owner = GetOwner(); + string infoText = Name + "\n" + "Besitzer: " + (owner == null ? "Niemand" : owner.Name) + "\nKasse: " + (GetBankAccount()?.Balance ?? 0); + _informationLabel.Text = infoText; + } + + public User GetOwner() + { + using (var dbContext = new DatabaseContext()) + { + User user = dbContext.Users.FirstOrDefault(u => u.BusinessId == Id); + //NAPI.Util.ConsoleOutput("[" + Name + "] GetOwner: " + (user?.Name ?? "null")); + return user; } } diff --git a/Server/Business/SmsBusiness.cs b/Server/Business/SmsBusiness.cs new file mode 100644 index 00000000..d8cec834 --- /dev/null +++ b/Server/Business/SmsBusiness.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using System.Text; +using GTANetworkAPI; + +namespace reallife_gamemode.Server.Business +{ + public class SmsBusiness : BusinessBase + { + public override int Id => 1; + + public override string Name => "SMS Business"; + + public override Vector3 Position => new Vector3(-423, 1130, 326); + + public override void Load() + { + + } + } +} diff --git a/Server/Business/TestBusiness.cs b/Server/Business/TestBusiness.cs index d856214c..5dd58e44 100644 --- a/Server/Business/TestBusiness.cs +++ b/Server/Business/TestBusiness.cs @@ -9,15 +9,15 @@ namespace reallife_gamemode.Server.Business { class TestBusiness : BusinessBase { - public override int Id => 1; + public override int Id => 0; public override string Name => "Test Business"; - public override Vector3 Position => throw new NotImplementedException(); + public override Vector3 Position => new Vector3(-443, 1134, 326); public override void Load() { - throw new NotImplementedException(); + } } } diff --git a/Server/Commands/Admin.cs b/Server/Commands/Admin.cs index 0263a0ec..b70c7148 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 @@ -1644,6 +1661,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) { @@ -1661,7 +1679,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) @@ -1685,7 +1704,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) @@ -1693,6 +1712,7 @@ namespace reallife_gamemode.Server.Commands ChatService.NotAuthorized(player); return; } + Client target = ClientService.GetClientByNameOrId(receiver); if (target == null || !target.IsLoggedIn()) { @@ -1708,6 +1728,46 @@ 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; + } + + 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; + + business.Update(); + + dbContext.SaveChanges(); + } + } #endregion #region ALevel1338 diff --git a/Server/Entities/User.cs b/Server/Entities/User.cs index 6f488be3..bb0f3399 100644 --- a/Server/Entities/User.cs +++ b/Server/Entities/User.cs @@ -58,6 +58,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()) diff --git a/Server/Managers/BusinessManager.cs b/Server/Managers/BusinessManager.cs index be3e0ef7..6f78bf18 100644 --- a/Server/Managers/BusinessManager.cs +++ b/Server/Managers/BusinessManager.cs @@ -11,6 +11,7 @@ namespace reallife_gamemode.Server.Managers class BusinessManager : Script { private static List businesses; + public static List Businesses { get => businesses; } public static void LoadBusinesses() { @@ -22,12 +23,14 @@ namespace reallife_gamemode.Server.Managers NAPI.Util.ConsoleOutput($"Loading Business {item.Name}"); if (Activator.CreateInstance(item) is BusinessBase o) { - if (businesses.Find(b => b.GetType() == item) != null) + if (businesses.Any(x => x.Id == o.Id)) { - throw new InvalidOperationException($"Double Business found: {o.Id} | {o.Name}"); + throw new InvalidOperationException($"Double Business ID found: {o.Id} | {o.Name}"); } businesses.Add(o); + o.Setup(); o.Load(); + o.Update(); } } } @@ -37,5 +40,9 @@ namespace reallife_gamemode.Server.Managers return (T)businesses.Find(b => b.GetType() == typeof(T)); } + public static BusinessBase GetBusiness(int id) + { + return businesses.Find(b => b.Id == id); + } } } From 74dc1db8af9c6a8c17768aea71c6f0a1237406fa Mon Sep 17 00:00:00 2001 From: hydrant Date: Thu, 22 Nov 2018 18:06:27 +0100 Subject: [PATCH 06/12] Automatic add business bank account, renamed sms to telephone business --- Server/Business/BusinessBase.cs | 18 +++++++++++++--- Server/Business/ShopBusiness.cs | 21 +++++++++++++++++++ .../{SmsBusiness.cs => TelefonBusiness.cs} | 4 ++-- 3 files changed, 38 insertions(+), 5 deletions(-) create mode 100644 Server/Business/ShopBusiness.cs rename Server/Business/{SmsBusiness.cs => TelefonBusiness.cs} (75%) diff --git a/Server/Business/BusinessBase.cs b/Server/Business/BusinessBase.cs index 271df4aa..78d6a1d6 100644 --- a/Server/Business/BusinessBase.cs +++ b/Server/Business/BusinessBase.cs @@ -36,12 +36,25 @@ namespace reallife_gamemode.Server.Business public void Setup() { _informationLabel = NAPI.TextLabel.CreateTextLabel(Name, Position, 20.0f, 1.3f, 0, new Color(255, 255, 255)); + + 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(); + } + } } public void Update() { - //NAPI.Util.ConsoleOutput("Updating Business: " + Name); - User owner = GetOwner(); string infoText = Name + "\n" + "Besitzer: " + (owner == null ? "Niemand" : owner.Name) + "\nKasse: " + (GetBankAccount()?.Balance ?? 0); _informationLabel.Text = infoText; @@ -52,7 +65,6 @@ namespace reallife_gamemode.Server.Business using (var dbContext = new DatabaseContext()) { User user = dbContext.Users.FirstOrDefault(u => u.BusinessId == Id); - //NAPI.Util.ConsoleOutput("[" + Name + "] GetOwner: " + (user?.Name ?? "null")); return user; } } diff --git a/Server/Business/ShopBusiness.cs b/Server/Business/ShopBusiness.cs new file mode 100644 index 00000000..f498b60e --- /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(); + + public override void Load() + { + + } + } +} diff --git a/Server/Business/SmsBusiness.cs b/Server/Business/TelefonBusiness.cs similarity index 75% rename from Server/Business/SmsBusiness.cs rename to Server/Business/TelefonBusiness.cs index d8cec834..02d11e2b 100644 --- a/Server/Business/SmsBusiness.cs +++ b/Server/Business/TelefonBusiness.cs @@ -5,11 +5,11 @@ using GTANetworkAPI; namespace reallife_gamemode.Server.Business { - public class SmsBusiness : BusinessBase + public class TelefonBusiness : BusinessBase { public override int Id => 1; - public override string Name => "SMS Business"; + public override string Name => "Telefon Business"; public override Vector3 Position => new Vector3(-423, 1130, 326); From a72d65eb18b992c931c90d5d5bad5a37aa31e068 Mon Sep 17 00:00:00 2001 From: hydrant Date: Thu, 22 Nov 2018 20:17:04 +0100 Subject: [PATCH 07/12] Added automatic update of business when bank account changes --- Server/Entities/BusinessBankAccount.cs | 13 ++++++++++++- Server/Managers/BankManager.cs | 1 - 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/Server/Entities/BusinessBankAccount.cs b/Server/Entities/BusinessBankAccount.cs index 525b8192..745faa52 100644 --- a/Server/Entities/BusinessBankAccount.cs +++ b/Server/Entities/BusinessBankAccount.cs @@ -1,4 +1,5 @@ using reallife_gamemode.Server.Business; +using reallife_gamemode.Server.Managers; using reallife_gamemode.Server.Util; using System; using System.Collections.Generic; @@ -10,10 +11,20 @@ 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; set; } + public int Balance { + get => _balance; + set + { + _balance = value; + BusinessManager.GetBusiness(BusinessId).Update(); + } + } public int BusinessId { get; set; } } diff --git a/Server/Managers/BankManager.cs b/Server/Managers/BankManager.cs index 6b5cf39c..cf913207 100644 --- a/Server/Managers/BankManager.cs +++ b/Server/Managers/BankManager.cs @@ -13,7 +13,6 @@ using reallife_gamemode.Server.Util; * @copyright (c) 2008 - 2018 Life of German */ - namespace reallife_gamemode.Server.Managers { public class BankManager From 054e9159e11190ae26c0eead4f9a19e4caf59baf Mon Sep 17 00:00:00 2001 From: hydrant Date: Thu, 22 Nov 2018 22:19:34 +0100 Subject: [PATCH 08/12] Almost finished base business system backend --- Server/Business/BusinessBase.cs | 16 +++++--- Server/Commands/Admin.cs | 60 ++++++++++++++++++++++++++++++ Server/Entities/User.cs | 7 +++- Server/Managers/BankManager.cs | 17 +++++---- Server/Managers/BusinessManager.cs | 2 +- 5 files changed, 87 insertions(+), 15 deletions(-) diff --git a/Server/Business/BusinessBase.cs b/Server/Business/BusinessBase.cs index 78d6a1d6..7233343a 100644 --- a/Server/Business/BusinessBase.cs +++ b/Server/Business/BusinessBase.cs @@ -56,16 +56,22 @@ namespace reallife_gamemode.Server.Business public void Update() { User owner = GetOwner(); - string infoText = Name + "\n" + "Besitzer: " + (owner == null ? "Niemand" : owner.Name) + "\nKasse: " + (GetBankAccount()?.Balance ?? 0); + string infoText = Name + "\n" + "Besitzer: " + (owner == null ? "Niemand" : owner.Name) + "\nKasse: ~g~" + (GetBankAccount()?.Balance ?? 0) + "$"; _informationLabel.Text = infoText; } - public User GetOwner() + public User GetOwner(DatabaseContext dbContext = null) { - using (var dbContext = new DatabaseContext()) + if(dbContext == null) { - User user = dbContext.Users.FirstOrDefault(u => u.BusinessId == Id); - return user; + using (dbContext = new DatabaseContext()) + { + return dbContext.Users.FirstOrDefault(u => u.BusinessId == Id); + } + } + else + { + return dbContext.Users.FirstOrDefault(u => u.BusinessId == Id); } } diff --git a/Server/Commands/Admin.cs b/Server/Commands/Admin.cs index b70c7148..c9107429 100644 --- a/Server/Commands/Admin.cs +++ b/Server/Commands/Admin.cs @@ -1745,6 +1745,12 @@ namespace reallife_gamemode.Server.Commands 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) { @@ -1763,11 +1769,65 @@ namespace reallife_gamemode.Server.Commands 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/User.cs b/Server/Entities/User.cs index bb0f3399..db630016 100644 --- a/Server/Entities/User.cs +++ b/Server/Entities/User.cs @@ -2,11 +2,9 @@ using reallife_gamemode.Model; 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) @@ -146,5 +144,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/Managers/BankManager.cs b/Server/Managers/BankManager.cs index cf913207..c4b726bf 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; @@ -23,22 +24,22 @@ namespace reallife_gamemode.Server.Managers { if (amount < 0) return TransactionResult.NEGATIVE_MONEY_SENT; - IBankAccount senderAccount = sender.GetBankAccount(transferMoney); + IBankAccount senderAccount = sender?.GetBankAccount(transferMoney) ?? null; IBankAccount receiverAccount = receiver.GetBankAccount(transferMoney); - if (senderAccount == null) return TransactionResult.SENDER_NO_BANKACCOUNT; + if (senderAccount == null && sender != null) return TransactionResult.SENDER_NO_BANKACCOUNT; if (receiverAccount == null) return TransactionResult.RECEIVER_NO_BANKACCOUNT; - if (senderAccount.Balance < amount) return TransactionResult.SENDER_NOT_ENOUGH_MONEY; + if ((senderAccount?.Balance ?? amount) < amount) return TransactionResult.SENDER_NOT_ENOUGH_MONEY; var transactionLog = new Logs.BankAccountTransactionHistory { - Sender = sender.Name, - SenderBalance = senderAccount.Balance, + Sender = sender?.Name ?? "Admin", + SenderBalance = senderAccount?.Balance ?? 0, Receiver = receiver.Name, ReceiverBalance = receiverAccount.Balance, NewReceiverBalance = receiverAccount.Balance + amount, - NewSenderBalance = senderAccount.Balance - amount, + NewSenderBalance = (senderAccount?.Balance ?? amount) - amount, MoneySent = amount, Fee = 0, Origin = origin @@ -47,11 +48,13 @@ namespace reallife_gamemode.Server.Managers // add log transferMoney.BankAccountTransactionLogs.Add(transactionLog); - senderAccount.Balance -= amount; + if(senderAccount != null) 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 6f78bf18..02d95455 100644 --- a/Server/Managers/BusinessManager.cs +++ b/Server/Managers/BusinessManager.cs @@ -40,7 +40,7 @@ namespace reallife_gamemode.Server.Managers return (T)businesses.Find(b => b.GetType() == typeof(T)); } - public static BusinessBase GetBusiness(int id) + public static BusinessBase GetBusiness(int? id) { return businesses.Find(b => b.Id == id); } From d30930a94982e4a2a11b0a79d9440d43d27c74c7 Mon Sep 17 00:00:00 2001 From: hydrant Date: Sun, 25 Nov 2018 20:10:39 +0100 Subject: [PATCH 09/12] Start business management from menu --- Client/Business/main.js | 109 +++++++++++++++++++++++++ Client/index.js | 2 + Main.cs | 3 + Server/Business/BusinessBase.cs | 28 ++++++- Server/Entities/BusinessBankAccount.cs | 2 +- Server/Extensions/IntegerExtension.cs | 18 ++++ Server/Managers/BankManager.cs | 50 ++++++++++-- Server/Managers/BusinessManager.cs | 13 ++- 8 files changed, 205 insertions(+), 20 deletions(-) create mode 100644 Client/Business/main.js create mode 100644 Server/Extensions/IntegerExtension.cs 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); } } } From 58f8a0a262033da56220addc7974c70450a53b41 Mon Sep 17 00:00:00 2001 From: hydrant Date: Tue, 27 Nov 2018 16:04:57 +0100 Subject: [PATCH 10/12] Added function to form integer to money string --- Server/Extensions/IntegerExtension.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Server/Extensions/IntegerExtension.cs b/Server/Extensions/IntegerExtension.cs index 61f2553a..96bd1aa8 100644 --- a/Server/Extensions/IntegerExtension.cs +++ b/Server/Extensions/IntegerExtension.cs @@ -8,11 +8,11 @@ namespace reallife_gamemode.Server.Extensions { public static string ToMoneyString(this int? money) { - return string.Format(Main.SERVER_CULTURE, "{0:C0}", money ?? 0); + return ToMoneyString(money ?? 0); } public static string ToMoneyString(this int money) { - return string.Format(Main.SERVER_CULTURE, "{0:C0}", money); + return string.Format(Main.SERVER_CULTURE, "{0:C0}", money).Replace('€', '$'); } } } From 1de28e880dc885b121dd5f80698c2a15f39798b4 Mon Sep 17 00:00:00 2001 From: hydrant Date: Tue, 27 Nov 2018 16:05:31 +0100 Subject: [PATCH 11/12] Aded business menu --- Client/Business/main.js | 40 +++++++++++++-- Client/inputhelper/index.js | 66 +++++++++++++++++++++++++ Client/inputhelper/web/inputhelper.css | 50 +++++++++++++++++++ Client/inputhelper/web/inputhelper.html | 32 ++++++++++++ 4 files changed, 185 insertions(+), 3 deletions(-) create mode 100644 Client/inputhelper/index.js create mode 100644 Client/inputhelper/web/inputhelper.css create mode 100644 Client/inputhelper/web/inputhelper.html diff --git a/Client/Business/main.js b/Client/Business/main.js index d33b42e6..fe4206c3 100644 --- a/Client/Business/main.js +++ b/Client/Business/main.js @@ -19,6 +19,8 @@ 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'); @@ -47,6 +49,10 @@ mp.events.add('business_removeHelp', (unbind) => { } }); +mp.events.add('business_updateMoney', (newMoney) => { + businessMoney = newMoney; +}); + function keyPressHandler() { mp.events.call('business_removeHelp', false); mp.gui.chat.show(false); @@ -65,9 +71,9 @@ function keyPressHandler() { bankAccountItem.SetRightLabel("~g~~h~" + businessMoney); mainMenu.AddItem(bankAccountItem); - var partnerItem = new UIMenuItem("Inteilhaber", "Verwalte den Inteilhaber"); - partnerItem.SetRightLabel("Niemand"); - mainMenu.AddItem(partnerItem); + //var partnerItem = new UIMenuItem("Inteilhaber", "Verwalte den Inteilhaber"); + //partnerItem.SetRightLabel("Niemand"); + //mainMenu.AddItem(partnerItem); mainMenu.Open(); @@ -87,6 +93,34 @@ function keyPressHandler() { 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; 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 From 94161f4f637bd4fd558abd95d9a68b64846655eb Mon Sep 17 00:00:00 2001 From: hydrant Date: Tue, 27 Nov 2018 16:05:42 +0100 Subject: [PATCH 12/12] Finished business system --- Server/Business/ShopBusiness.cs | 2 +- Server/Business/TestBusiness.cs | 23 ---------- Server/Managers/BusinessManager.cs | 67 +++++++++++++++++++++++++++++- 3 files changed, 67 insertions(+), 25 deletions(-) delete mode 100644 Server/Business/TestBusiness.cs diff --git a/Server/Business/ShopBusiness.cs b/Server/Business/ShopBusiness.cs index f498b60e..283f33bb 100644 --- a/Server/Business/ShopBusiness.cs +++ b/Server/Business/ShopBusiness.cs @@ -11,7 +11,7 @@ namespace reallife_gamemode.Server.Business public override string Name => "24/7 Business"; - public override Vector3 Position => new Vector3(); + public override Vector3 Position => new Vector3(-443, 1134, 326); public override void Load() { diff --git a/Server/Business/TestBusiness.cs b/Server/Business/TestBusiness.cs deleted file mode 100644 index 5dd58e44..00000000 --- a/Server/Business/TestBusiness.cs +++ /dev/null @@ -1,23 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Text; -using GTANetworkAPI; -using reallife_gamemode.Model; -using reallife_gamemode.Server.Util; - -namespace reallife_gamemode.Server.Business -{ - class TestBusiness : BusinessBase - { - public override int Id => 0; - - public override string Name => "Test Business"; - - public override Vector3 Position => new Vector3(-443, 1134, 326); - - public override void Load() - { - - } - } -} diff --git a/Server/Managers/BusinessManager.cs b/Server/Managers/BusinessManager.cs index 1ddc3b5d..7dd56a1f 100644 --- a/Server/Managers/BusinessManager.cs +++ b/Server/Managers/BusinessManager.cs @@ -1,5 +1,8 @@ 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; @@ -22,7 +25,7 @@ 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 (GetBusiness(o.Id) != null) { throw new InvalidOperationException($"Double Business ID found: {o.Id} | {o.Name}"); } @@ -43,5 +46,67 @@ namespace reallife_gamemode.Server.Managers { 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; + } + } } }