add house commands

This commit is contained in:
hydrant
2019-06-27 17:52:13 +02:00
parent dd647732c0
commit f2cf7464d0
3 changed files with 212 additions and 5 deletions

View File

@@ -2431,6 +2431,128 @@ namespace ReallifeGamemode.Server.Commands
DoorManager.ReloadDoors(); DoorManager.ReloadDoors();
ChatService.SendMessage(player, "~b~[ADMIN]~s~ Die Türen wurden erfolgreich neugeladen."); 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 #endregion
#region ALevel1338 #region ALevel1338

View File

@@ -48,6 +48,7 @@ namespace ReallifeGamemode.Server
ATMManager.InitATMs(); ATMManager.InitATMs();
CityHallManager.LoadCityHall(); CityHallManager.LoadCityHall();
JobManager.LoadJobs(); JobManager.LoadJobs();
HouseManager.LoadHouses();
TempBlip tempBlip = new TempBlip() TempBlip tempBlip = new TempBlip()

View File

@@ -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<int, NetHandle> houseMarkers = new Dictionary<int, NetHandle>();
private static readonly Dictionary<int, NetHandle> houseLabels = new Dictionary<int, NetHandle>();
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<Marker>().Delete();
houseLabels[house.Id].Entity<TextLabel>().Delete();
houseMarkers.Remove(house.Id);
houseLabels.Remove(house.Id);
}
}
}