Improve House system

This commit is contained in:
hydrant
2019-07-30 12:42:02 +02:00
parent f30036ca15
commit abba223f57
7 changed files with 645 additions and 619 deletions

View File

@@ -6,7 +6,7 @@ using System.Text;
namespace ReallifeGamemode.Server.Entities
{
public class HouseRentals
public class HouseRental
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]

View File

@@ -48,12 +48,12 @@ namespace ReallifeGamemode.Server.Managers
{
using (dbContext = new DatabaseContext())
{
return dbContext.Houses.Where(h => h.Position.DistanceTo(position) <= 5f).OrderBy(h => h.Position.DistanceTo(position)).FirstOrDefault();
return dbContext.Houses.Where(h => h.Position.DistanceTo(position) <= 5f).Include(h => h.Owner).OrderBy(h => h.Position.DistanceTo(position)).FirstOrDefault();
}
}
else
{
return dbContext.Houses.Where(h => h.Position.DistanceTo(position) <= 5f).OrderBy(h => h.Position.DistanceTo(position)).FirstOrDefault();
return dbContext.Houses.Where(h => h.Position.DistanceTo(position) <= 5f).Include(h => h.Owner).OrderBy(h => h.Position.DistanceTo(position)).FirstOrDefault();
}
}
@@ -129,14 +129,22 @@ namespace ReallifeGamemode.Server.Managers
House house = GetHouseById(houseId);
User user = client.GetUser();
var userHouseStatus = -1;
client.SendChatMessage("SERVER:ShowHouseMenu");
SendClientHouseData(client, house);
}
private static void SendClientHouseData(Client player, House house)
{
User user = player.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;
else if (dbContext.HouseRentals.Where(h => h.HouseId == house.Id && h.UserId == user.Id).Count() == 1) userHouseStatus = 2;
var rentals = dbContext.HouseRentals.Where(h => h.HouseId == house.Id).Include(h => h.User).Select(h => h.User.Name).ToList();
var newHouse = new
{
@@ -144,13 +152,10 @@ namespace ReallifeGamemode.Server.Managers
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()
Rentals = rentals
};
client.TriggerEvent("SERVER:ShowHouseMenu", JsonConvert.SerializeObject(newHouse), userHouseStatus);
player.TriggerEvent("SERVER:SetHouseData", JsonConvert.SerializeObject(newHouse), userHouseStatus);
}
}
@@ -182,7 +187,7 @@ namespace ReallifeGamemode.Server.Managers
{
User user = player.GetUser(dbContext);
if(user.HouseId != null)
if (user.HouseId != null)
{
ChatService.ErrorMessage(player, "Du kann nicht mehrere Häuser besitzen");
return;
@@ -227,9 +232,118 @@ namespace ReallifeGamemode.Server.Managers
house.RentalFee = rentalFee;
dbContext.SaveChanges();
player.SendNotification($"Der Mietpreis wurde auf ~g~{rentalFee.ToMoneyString()}~s~ gesetzt");
RemoveHouse(house);
LoadHouse(house);
}
}
[RemoteEvent("CLIENT:House_CancelUserRental")]
public void HouseManagerCancelUserRentalEvent(Client player, string userName)
{
using (var dbContext = new DatabaseContext())
{
User user = player.GetUser(dbContext);
if (user.HouseId == null)
{
ChatService.ErrorMessage(player, "Du besitzt kein Haus");
return;
}
User target = dbContext.Users.Where(u => u.Name == userName).FirstOrDefault();
if (target == null)
{
player.SendNotification("~r~Dieser Spieler wurde nicht gefunden.");
return;
}
House house = GetHouseById(user.HouseId.Value, dbContext);
if (user.HouseId == null)
{
ChatService.ErrorMessage(player, "Du besitzt kein Haus");
return;
}
HouseRental rental = dbContext.HouseRentals.Where(h => h.HouseId == house.Id && h.UserId == target.Id).FirstOrDefault();
if (rental == null)
{
player.SendNotification("~r~Der Spieler ist nicht in deinem Haus eingemietet");
return;
}
dbContext.HouseRentals.Remove(rental);
dbContext.SaveChanges();
player.SendNotification("Du hast dem Spieler ~y~" + target.Name + "~s~ den Mietvertrag gekündigt.");
SendClientHouseData(player, house);
}
}
[RemoteEvent("CLIENT:House_RentInHouse")]
public void HouseManagerRentInHouseEvent(Client player)
{
using (var dbContext = new DatabaseContext())
{
User user = player.GetUser(dbContext);
House house = GetNearHouse(player.Position, dbContext);
if (house == null)
{
ChatService.ErrorMessage(player, "In deiner Nähe ist kein Haus");
return;
}
HouseRental newRental = new HouseRental
{
HouseId = house.Id,
UserId = user.Id
};
dbContext.HouseRentals.Add(newRental);
dbContext.SaveChanges();
player.SendNotification("~g~Du hast dich in das Haus eingemietet");
house.User?.SendNotification($"~y~{player.Name}~s~ hat sich in dein Haus eingemietet.");
SendClientHouseData(player, house);
}
}
[RemoteEvent("CLIENT:House_CancelOwnRental")]
public void HouseManagerCancelOwnRentalEvent(Client player)
{
using (var dbContext = new DatabaseContext())
{
User user = player.GetUser(dbContext);
House house = GetNearHouse(player.Position, dbContext);
if (house == null)
{
ChatService.ErrorMessage(player, "In deiner Nähe ist kein Haus");
return;
}
HouseRental rental = dbContext.HouseRentals.Where(h => h.HouseId == house.Id && h.UserId == user.Id).FirstOrDefault();
if (rental == null)
{
player.SendNotification("~r~Du bist nin diesem Haus nicht eingemietet");
return;
}
dbContext.HouseRentals.Remove(rental);
dbContext.SaveChanges();
player.SendNotification("~g~Du hast den Mietvertrag gekündigt.");
house.User?.SendNotification($"~y~{player.Name}~s~ hat seinen Mietvertrag gekündigt.");
SendClientHouseData(player, house);
}
}
}

View File

@@ -106,7 +106,7 @@ namespace ReallifeGamemode.Server.Models
// Houses
public DbSet<Entities.House> Houses { get; set; }
public DbSet<Entities.HouseRentals> HouseRentals { get; set; }
public DbSet<Entities.HouseRental> HouseRentals { get; set; }
// Bus Routes
public DbSet<Entities.BusRoute> BusRoutes { get; set; }