diff --git a/ReallifeGamemode.Database/Entities/House.cs b/ReallifeGamemode.Database/Entities/House.cs index 666d74f1..09153026 100644 --- a/ReallifeGamemode.Database/Entities/House.cs +++ b/ReallifeGamemode.Database/Entities/House.cs @@ -1,9 +1,8 @@ using System; +using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; -using GTANetworkAPI; using ReallifeGamemode.Server.Core.API; -using ReallifeGamemode.Server.Core.RageMP; namespace ReallifeGamemode.Database.Entities { @@ -33,5 +32,7 @@ namespace ReallifeGamemode.Database.Entities [ForeignKey("Owner")] public int? OwnerId { get; set; } public User Owner { get; set; } + + public ICollection Rentals { get; set; } } } diff --git a/ReallifeGamemode.Database/Models/DatabaseContext.cs b/ReallifeGamemode.Database/Models/DatabaseContext.cs index 145888ca..ef188398 100644 --- a/ReallifeGamemode.Database/Models/DatabaseContext.cs +++ b/ReallifeGamemode.Database/Models/DatabaseContext.cs @@ -51,6 +51,25 @@ namespace ReallifeGamemode.Database.Models modelBuilder.Entity() .HasIndex(vM => new { vM.ServerVehicleId, vM.Slot }).IsUnique(); + + modelBuilder.Entity() + .HasIndex(hR => new + { + hR.HouseId, + hR.UserId + }).IsUnique(); + + modelBuilder.Entity() + .HasMany(h => h.Rentals) + .WithOne(hR => hR.House); + + modelBuilder.Entity() + .HasOne(u => u.House) + .WithOne(h => h.Owner); + + modelBuilder.Entity() + .HasOne(h => h.Owner) + .WithOne(u => u.House); } //User diff --git a/ReallifeGamemode.Server.Core/Managers/HouseManager.cs b/ReallifeGamemode.Server.Core/Managers/HouseManager.cs index a1cfada1..61adc7eb 100644 --- a/ReallifeGamemode.Server.Core/Managers/HouseManager.cs +++ b/ReallifeGamemode.Server.Core/Managers/HouseManager.cs @@ -113,33 +113,35 @@ namespace ReallifeGamemode.Server.Core.Managers public House GetHouseById(int id, DatabaseContext dbContext) { - return dbContext.Houses.Where(h => h.Id == id).Include(h => h.Owner).FirstOrDefault(); + return dbContext.Houses.Where(h => h.Id == id) + .Include(h => h.Owner) + .Include(h => h.Rentals) + .ThenInclude(hR => hR.User) + .FirstOrDefault(); } private void SendPlayerHouseData(IPlayer player, House house) { - using (var dbContext = new DatabaseContext()) + using var dbContext = GetDbContext(); + User user = player.GetUser(dbContext); + var userHouseStatus = -1; + + if (house.OwnerId == null) userHouseStatus = 0; + else if (house.OwnerId == user?.Id) userHouseStatus = 1; + else if (house.Rentals.Where(h => h.UserId == user.Id).Any()) userHouseStatus = 2; + + var rentals = house.Rentals; + + var newHouse = new { - User user = player.GetUser(dbContext); - var userHouseStatus = -1; + OwnerName = house.Owner?.Name, + house.RentalFee, + house.Price, + house.Type, + Rentals = rentals.Select(r => r.User.Name) + }; - if (house.OwnerId == null) userHouseStatus = 0; - else if (house.OwnerId == user?.Id) userHouseStatus = 1; - 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 - { - OwnerName = house.Owner?.Name, - house.RentalFee, - house.Price, - house.Type, - Rentals = rentals - }; - - player.TriggerEvent("SetHouseData", newHouse.SerializeJson(), userHouseStatus); - } + player.TriggerEvent("SetHouseData", newHouse.SerializeJson(), userHouseStatus); } public void RemoveHouse(House house) @@ -181,19 +183,17 @@ namespace ReallifeGamemode.Server.Core.Managers public void ReloadAllHouses() { - using (var dbContext = new DatabaseContext()) + using var dbContext = GetDbContext(); + foreach (House house in dbContext.Houses.Include(h => h.Owner).ToList()) { - foreach (House house in dbContext.Houses.Include(h => h.Owner).ToList()) - { - RemoveHouse(house); - LoadHouse(house); - } + RemoveHouse(house); + LoadHouse(house); } } private void HouseManagerBuyHouseEvent(IPlayer player, params object[] args) { - using var dbContext = new DatabaseContext(); + using var dbContext = GetDbContext(); User user = player.GetUser(dbContext, bankAccount: true); if (user.HouseId != null) @@ -243,8 +243,8 @@ namespace ReallifeGamemode.Server.Core.Managers DateTime newPossibility = house.LastRentSetTime.AddDays(7); string dateStr = newPossibility.ToLongDateString(); string timeStr = newPossibility.ToShortTimeString(); - player.SendNotification( - $"~r~Die Miete wurde in den letzten 7 Tagen schon verändert. Die nächste Änderung kann am {dateStr} um {timeStr} Uhr geändert werden."); + player.SendMessage( + $"Die Miete wurde in den letzten 7 Tagen schon verändert. Die Miete kann am ~y~{dateStr} um {timeStr}~s~ Uhr wieder geändert werden.", ChatPrefix.Info); return; } @@ -274,7 +274,7 @@ namespace ReallifeGamemode.Server.Core.Managers private void HouseManagerCancelUserRentalEvent(IPlayer player, params object[] args) { string userName = args[0] as string; - using var dbContext = new DatabaseContext(); + using var dbContext = GetDbContext(); User user = player.GetUser(dbContext); if (user.HouseId == null) { @@ -291,14 +291,14 @@ namespace ReallifeGamemode.Server.Core.Managers House house = GetHouseById(user.HouseId.Value, dbContext); - HouseRental rental = dbContext.HouseRentals.Where(h => h.HouseId == house.Id && h.UserId == target.Id).FirstOrDefault(); + HouseRental rental = house.Rentals.Where(h => h.UserId == target.Id).FirstOrDefault(); if (rental == null) { player.SendNotification("~r~Der Spieler ist nicht in deinem Haus eingemietet"); return; } - dbContext.HouseRentals.Remove(rental); + house.Rentals.Remove(rental); dbContext.SaveChanges(); target.NewPlayer?.SendNotification($"~y~{player.Name}~s~ hat deinen Mietvertrag ~g~gekündigt~s~."); @@ -309,7 +309,7 @@ namespace ReallifeGamemode.Server.Core.Managers private void HouseManagerRentInHouseEvent(IPlayer player, params object[] args) { - using var dbContext = new DatabaseContext(); + using var dbContext = GetDbContext(); User user = player.GetUser(dbContext); House house = GetNearHouse(player.Position, dbContext); @@ -331,7 +331,7 @@ namespace ReallifeGamemode.Server.Core.Managers UserId = user.Id }; - dbContext.HouseRentals.Add(newRental); + house.Rentals.Add(newRental); dbContext.SaveChanges(); @@ -353,7 +353,7 @@ namespace ReallifeGamemode.Server.Core.Managers return; } - HouseRental rental = dbContext.HouseRentals.Where(h => h.HouseId == house.Id && h.UserId == user.Id).FirstOrDefault(); + HouseRental rental = house.Rentals.Where(h => h.UserId == user.Id).FirstOrDefault(); if (rental == null) { @@ -361,7 +361,7 @@ namespace ReallifeGamemode.Server.Core.Managers return; } - dbContext.HouseRentals.Remove(rental); + house.Rentals.Remove(rental); dbContext.SaveChanges();