add house commands
This commit is contained in:
84
ReallifeGamemode.Server/Managers/HouseManager.cs
Normal file
84
ReallifeGamemode.Server/Managers/HouseManager.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user