106 lines
3.5 KiB
C#
106 lines
3.5 KiB
C#
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>();
|
|
private static readonly Dictionary<int, NetHandle> houseColShapes = new Dictionary<int, NetHandle>();
|
|
|
|
public static void LoadHouses()
|
|
{
|
|
using(var dbContext = new DatabaseContext())
|
|
{
|
|
foreach (House house in dbContext.Houses.Include(h => h.Owner))
|
|
{
|
|
LoadHouse(house, false);
|
|
}
|
|
}
|
|
}
|
|
|
|
public async static void ReloadAllHouses()
|
|
{
|
|
using (var dbContext = new DatabaseContext())
|
|
{
|
|
foreach(House house in await dbContext.Houses.Include(h => h.Owner).ToListAsync())
|
|
{
|
|
RemoveHouse(house);
|
|
LoadHouse(house, false);
|
|
}
|
|
}
|
|
}
|
|
|
|
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, bool loadUser = true)
|
|
{
|
|
if (loadUser) house = house.Refresh();
|
|
|
|
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)
|
|
{
|
|
if (houseMarkers.ContainsKey(house.Id))
|
|
{
|
|
houseMarkers[house.Id].Entity<Marker>().Delete();
|
|
houseMarkers.Remove(house.Id);
|
|
}
|
|
|
|
if(houseLabels.ContainsKey(house.Id))
|
|
{
|
|
houseLabels[house.Id].Entity<TextLabel>().Delete();
|
|
houseLabels.Remove(house.Id);
|
|
}
|
|
}
|
|
}
|
|
}
|