Add House Menu
This commit is contained in:
@@ -1,8 +1,10 @@
|
||||
using GTANetworkAPI;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Newtonsoft.Json;
|
||||
using ReallifeGamemode.Server.Entities;
|
||||
using ReallifeGamemode.Server.Extensions;
|
||||
using ReallifeGamemode.Server.Models;
|
||||
using ReallifeGamemode.Server.Services;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
@@ -10,24 +12,25 @@ using System.Text;
|
||||
|
||||
namespace ReallifeGamemode.Server.Managers
|
||||
{
|
||||
class HouseManager
|
||||
class HouseManager : Script
|
||||
{
|
||||
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()
|
||||
public static async void LoadHouses()
|
||||
{
|
||||
using (var dbContext = new DatabaseContext())
|
||||
{
|
||||
foreach (House house in dbContext.Houses.Include(h => h.Owner))
|
||||
List<House> houses = await dbContext.Houses.Include(h => h.Owner).ToListAsync();
|
||||
foreach (House house in houses)
|
||||
{
|
||||
LoadHouse(house, false);
|
||||
LoadHouse(house, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public async static void ReloadAllHouses()
|
||||
public static async void ReloadAllHouses()
|
||||
{
|
||||
using (var dbContext = new DatabaseContext())
|
||||
{
|
||||
@@ -45,12 +48,12 @@ namespace ReallifeGamemode.Server.Managers
|
||||
{
|
||||
using (dbContext = new DatabaseContext())
|
||||
{
|
||||
return dbContext.Houses.Where(h => h.Position.DistanceTo(position) <= 5f).FirstOrDefault();
|
||||
return dbContext.Houses.Where(h => h.Position.DistanceTo(position) <= 5f).OrderBy(h => h.Position.DistanceTo(position)).FirstOrDefault();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
return dbContext.Houses.Where(h => h.Position.DistanceTo(position) <= 5f).FirstOrDefault();
|
||||
return dbContext.Houses.Where(h => h.Position.DistanceTo(position) <= 5f).OrderBy(h => h.Position.DistanceTo(position)).FirstOrDefault();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -74,17 +77,80 @@ namespace ReallifeGamemode.Server.Managers
|
||||
}
|
||||
}
|
||||
|
||||
public static House GetHouseById(int id, DatabaseContext dbContext = null)
|
||||
{
|
||||
if (dbContext == null)
|
||||
{
|
||||
using (dbContext = new DatabaseContext())
|
||||
{
|
||||
return dbContext.Houses.Where(h => h.Id == id).FirstOrDefault();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
return dbContext.Houses.Where(h => h.Id == id).FirstOrDefault();
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
string text = $"~g~Zum Verkauf\n~s~{house.Type}\nPreis: ~y~{(house.Price == 0 ? "~r~Nicht verkäuflich" : house.Price.ToMoneyString())}";
|
||||
if (house.OwnerId != null)
|
||||
{
|
||||
text = $"{house.Type}\n~s~Besitzer: ~y~{house.User.Name}";
|
||||
text = $"{house.Type}\n~s~Besitzer: ~y~{house.Owner.Name}\n~s~Mietpreis: ~g~{house.RentalFee.ToMoneyString()}";
|
||||
}
|
||||
houseLabels[house.Id] = NAPI.TextLabel.CreateTextLabel(text, house.Position, 10f, 1f, 0, new Color(255, 255, 255));
|
||||
|
||||
if (house.Price != 0)
|
||||
{
|
||||
houseColShapes[house.Id] = NAPI.ColShape.CreateCylinderColShape(house.Position.Subtract(new Vector3(0, 0, 2)), 2.0f, 5f);
|
||||
|
||||
houseColShapes[house.Id].Entity<ColShape>().OnEntityEnterColShape += HouseManager_OnEntityEnterColShape;
|
||||
houseColShapes[house.Id].Entity<ColShape>().OnEntityExitColShape += HouseManager_OnEntityExitColShape;
|
||||
}
|
||||
}
|
||||
|
||||
private static void HouseManager_OnEntityExitColShape(ColShape colShape, Client client)
|
||||
{
|
||||
client.TriggerEvent("SERVER:CloseHouseMenu");
|
||||
}
|
||||
|
||||
private static void HouseManager_OnEntityEnterColShape(ColShape colShape, Client client)
|
||||
{
|
||||
if (!client.IsLoggedIn()) return;
|
||||
if (!houseColShapes.ContainsValue(colShape.Handle))
|
||||
{
|
||||
return;
|
||||
}
|
||||
int houseId = houseColShapes.Where(p => p.Value.Value == colShape.Handle.Value).FirstOrDefault().Key;
|
||||
House house = GetHouseById(houseId);
|
||||
User user = client.GetUser();
|
||||
|
||||
var userHouseStatus = -1;
|
||||
|
||||
|
||||
using (var dbContext = new DatabaseContext())
|
||||
{
|
||||
if (house.OwnerId == null) userHouseStatus = 0;
|
||||
else if (house.OwnerId == user?.Id) userHouseStatus = 1;
|
||||
else if (dbContext.HouseRentals.Where(h => h.HouseId == houseId && h.UserId == user.Id).Count() == 1) userHouseStatus = 2;
|
||||
|
||||
var newHouse = new
|
||||
{
|
||||
house.RentalFee,
|
||||
house.Price,
|
||||
house.Type,
|
||||
Rentals = dbContext.HouseRentals.Where(h => h.Id == houseId).Include(h => h.UserId).Select(h => new
|
||||
{
|
||||
h.User.Name
|
||||
}).ToList()
|
||||
};
|
||||
|
||||
client.TriggerEvent("SERVER:ShowHouseMenu", JsonConvert.SerializeObject(newHouse), userHouseStatus);
|
||||
}
|
||||
}
|
||||
|
||||
public static void RemoveHouse(House house)
|
||||
@@ -100,6 +166,70 @@ namespace ReallifeGamemode.Server.Managers
|
||||
houseLabels[house.Id].Entity<TextLabel>().Delete();
|
||||
houseLabels.Remove(house.Id);
|
||||
}
|
||||
|
||||
if (houseColShapes.ContainsKey(house.Id))
|
||||
{
|
||||
houseColShapes[house.Id].Entity<ColShape>().Delete();
|
||||
houseColShapes.Remove(house.Id);
|
||||
}
|
||||
}
|
||||
|
||||
[RemoteEvent("CLIENT:House_BuyHouse")]
|
||||
public void HouseManagerBuyHouseEvent(Client player)
|
||||
{
|
||||
using (var dbContext = new DatabaseContext())
|
||||
{
|
||||
User user = player.GetUser(dbContext);
|
||||
|
||||
if(user.HouseId != null)
|
||||
{
|
||||
ChatService.ErrorMessage(player, "Du kann nicht mehrere Häuser besitzen");
|
||||
return;
|
||||
}
|
||||
|
||||
House house = GetNearHouse(player.Position, dbContext);
|
||||
|
||||
var userBank = user.GetBankAccount(dbContext);
|
||||
|
||||
if (userBank.Balance < house.Price)
|
||||
{
|
||||
ChatService.ErrorMessage(player, $"Du hast nicht genug Geld für das Haus ({house.Price.ToMoneyString()})");
|
||||
return;
|
||||
}
|
||||
|
||||
house.Owner = user;
|
||||
user.House = house;
|
||||
|
||||
userBank.Balance -= house.Price;
|
||||
|
||||
dbContext.SaveChanges();
|
||||
|
||||
RemoveHouse(house);
|
||||
LoadHouse(house);
|
||||
}
|
||||
}
|
||||
|
||||
[RemoteEvent("CLIENT:House_SetRentalFee")]
|
||||
public void HouseManagerSetRentalFeeEvent(Client player, int rentalFee)
|
||||
{
|
||||
using (var dbContext = new DatabaseContext())
|
||||
{
|
||||
User user = player.GetUser(dbContext);
|
||||
|
||||
if (user.HouseId == null)
|
||||
{
|
||||
ChatService.ErrorMessage(player, "Du besitzt kein Haus");
|
||||
return;
|
||||
}
|
||||
|
||||
House house = GetHouseById(user.HouseId.Value, dbContext);
|
||||
|
||||
house.RentalFee = rentalFee;
|
||||
|
||||
player.SendNotification($"Der Mietpreis wurde auf ~g~{rentalFee.ToMoneyString()}~s~ gesetzt");
|
||||
|
||||
dbContext.SaveChanges();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user