diff --git a/ReallifeGamemode.Server/Commands/AdminCommands.cs b/ReallifeGamemode.Server/Commands/AdminCommands.cs index 449eb6c3..07d94fe1 100644 --- a/ReallifeGamemode.Server/Commands/AdminCommands.cs +++ b/ReallifeGamemode.Server/Commands/AdminCommands.cs @@ -858,12 +858,12 @@ namespace ReallifeGamemode.Server.Commands return; } - using(var dbContext = new DatabaseContext()) + using (var dbContext = new DatabaseContext()) { User user = target.GetUser(dbContext); bool duty = user.GetData("duty"); CharacterCloth cloth = dbContext.CharacterClothes.Where(c => c.UserId == user.Id && c.SlotId == component && c.SlotType == 0 && c.Duty == duty).FirstOrDefault(); - if(cloth == null) + if (cloth == null) { cloth = new CharacterCloth() { @@ -902,7 +902,7 @@ namespace ReallifeGamemode.Server.Commands return; } - if(component == -1) + if (component == -1) { target.ClearAccessory(slot); return; @@ -1083,7 +1083,7 @@ namespace ReallifeGamemode.Server.Commands uHash = NAPI.Util.GetHashKey($"weapon_{hash}"); } - if(!WeaponManager.IsValidHash(uHash)) + if (!WeaponManager.IsValidHash(uHash)) { ChatService.ErrorMessage(player, "Diese Waffe existiert nicht"); return; @@ -1704,7 +1704,7 @@ namespace ReallifeGamemode.Server.Commands } else { - if(!int.TryParse(option1, out int jobId)) + if (!int.TryParse(option1, out int jobId)) { ChatService.ErrorMessage(player, "Du musst eine gültige Zahl als JobID angeben"); return; @@ -2431,6 +2431,128 @@ namespace ReallifeGamemode.Server.Commands DoorManager.ReloadDoors(); ChatService.SendMessage(player, "~b~[ADMIN]~s~ Die Türen wurden erfolgreich neugeladen."); } + + [Command("house", "~m~Benutzung: ~s~/house [add / remove / price / type]", GreedyArg = true)] + public void CmdAdminHouse(Client player, string option1, string option2 = null) + { + if (!player.GetUser()?.IsAdmin(AdminLevel.HEADADMIN) ?? true) + { + ChatService.NotAuthorized(player); + return; + } + + option1 = option1.ToLower(); + + if (option1 == "add") + { + House nearHouse = HouseManager.GetNearHouse(player.Position); + if (nearHouse != null) + { + ChatService.ErrorMessage(player, "In der Nähe ist schon ein Haus"); + return; + } + + using (var dbContext = new DatabaseContext()) + { + House house = new House() + { + Price = 0, + Type = "Haus", + X = player.Position.X, + Y = player.Position.Y, + Z = player.Position.Z + }; + + dbContext.Houses.Add(house); + dbContext.SaveChanges(); + + HouseManager.LoadHouse(house); + + player.SendNotification("Das Haus wurde erstellt"); + } + + return; + } + else if (option1 == "remove") + { + using (var dbContext = new DatabaseContext()) + { + House nearHouse = HouseManager.GetNearHouse(player.Position, dbContext); + if (nearHouse == null) + { + ChatService.ErrorMessage(player, "In deiner Nähe befindet sich kein Haus"); + return; + } + + dbContext.Houses.Remove(nearHouse); + dbContext.SaveChanges(); + + HouseManager.RemoveHouse(nearHouse); + + player.SendNotification("Das Haus wurde gelöscht"); + } + + return; + } + else if (option1 == "price") + { + if (!int.TryParse(option2, out int price)) + { + ChatService.ErrorMessage(player, "~m~Benutzung: ~s~/house price [Price]"); + return; + } + + using (var dbContext = new DatabaseContext()) + { + House nearHouse = HouseManager.GetNearHouse(player.Position, dbContext); + if (nearHouse == null) + { + ChatService.ErrorMessage(player, "In deiner Nähe befindet sich kein Haus"); + return; + } + + nearHouse.Price = price; + dbContext.SaveChanges(); + + HouseManager.RemoveHouse(nearHouse); + HouseManager.LoadHouse(nearHouse); + + player.SendNotification("Der Hauspreis wurde gesetzt"); + } + + return; + } + else if (option1 == "type") + { + if(option2 == null) + { + ChatService.ErrorMessage(player, "~m~Benutzung: ~s~/house type [Type]"); + return; + } + + using (var dbContext = new DatabaseContext()) + { + House nearHouse = HouseManager.GetNearHouse(player.Position, dbContext); + if (nearHouse == null) + { + ChatService.ErrorMessage(player, "In deiner Nähe befindet sich kein Haus"); + return; + } + + nearHouse.Type = option2; + dbContext.SaveChanges(); + + HouseManager.RemoveHouse(nearHouse); + HouseManager.LoadHouse(nearHouse); + + player.SendNotification("Der Haustyp wurde gesetzt"); + } + + return; + } + + player.SendChatMessage("~m~Benutzung: ~s~/house [add / remove / price / type]"); + } #endregion #region ALevel1338 diff --git a/ReallifeGamemode.Server/Main.cs b/ReallifeGamemode.Server/Main.cs index ac0af4b8..98dbdb40 100644 --- a/ReallifeGamemode.Server/Main.cs +++ b/ReallifeGamemode.Server/Main.cs @@ -48,6 +48,7 @@ namespace ReallifeGamemode.Server ATMManager.InitATMs(); CityHallManager.LoadCityHall(); JobManager.LoadJobs(); + HouseManager.LoadHouses(); TempBlip tempBlip = new TempBlip() diff --git a/ReallifeGamemode.Server/Managers/HouseManager.cs b/ReallifeGamemode.Server/Managers/HouseManager.cs new file mode 100644 index 00000000..7d0d600d --- /dev/null +++ b/ReallifeGamemode.Server/Managers/HouseManager.cs @@ -0,0 +1,84 @@ +using GTANetworkAPI; +using Microsoft.EntityFrameworkCore; +using ReallifeGamemode.Server.Entities; +using ReallifeGamemode.Server.Extensions; +using ReallifeGamemode.Server.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace ReallifeGamemode.Server.Managers +{ + class HouseManager + { + private static readonly Dictionary houseMarkers = new Dictionary(); + private static readonly Dictionary houseLabels = new Dictionary(); + + public static void LoadHouses() + { + using(var dbContext = new DatabaseContext()) + { + foreach (House house in dbContext.Houses.Include(h => h.User)) + { + LoadHouse(house); + } + } + } + + public static House GetNearHouse(Vector3 position, DatabaseContext dbContext = null) + { + if(dbContext == null) + { + using (dbContext = new DatabaseContext()) + { + return dbContext.Houses.Where(h => h.Position.DistanceTo(position) <= 5f).FirstOrDefault(); + } + } + else + { + return dbContext.Houses.Where(h => h.Position.DistanceTo(position) <= 5f).FirstOrDefault(); + } + } + + public void AddHouse(string type, int price, Vector3 position) + { + using (var dbContext = new DatabaseContext()) + { + var house = new House() + { + Price = price, + Type = type, + X = position.X, + Y = position.Y, + Z = position.Z + }; + + dbContext.Houses.Add(house); + dbContext.SaveChanges(); + + LoadHouse(house); + } + } + + public static void LoadHouse(House house) + { + houseMarkers[house.Id] = NAPI.Marker.CreateMarker(MarkerType.VerticalCylinder, house.Position.Subtract(new Vector3(0, 0, 1.7)), new Vector3(), new Vector3(), 1.6f, new Color(255, 255, 255)); + string text = $"~g~Zum Verkauf\n~s~{house.Type}\nPreis: ~y~{house.Price.ToMoneyString()}"; + if(house.User != null) + { + text = $"{house.Type}\n~s~Besitzer: ~y~{house.User.Name}"; + } + houseLabels[house.Id] = NAPI.TextLabel.CreateTextLabel(text, house.Position, 10f, 1f, 0, new Color(255, 255, 255)); + } + + public static void RemoveHouse(House house) + { + houseMarkers[house.Id].Entity().Delete(); + houseLabels[house.Id].Entity().Delete(); + + houseMarkers.Remove(house.Id); + houseLabels.Remove(house.Id); + } + } +}