miete alle 7 tage nachricht angepasst

improvements in db und haussystem
This commit is contained in:
hydrant
2020-03-29 18:51:18 +02:00
parent 0dc1235909
commit 5a3dc6de75
3 changed files with 59 additions and 39 deletions

View File

@@ -1,9 +1,8 @@
using System; using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema; using System.ComponentModel.DataAnnotations.Schema;
using GTANetworkAPI;
using ReallifeGamemode.Server.Core.API; using ReallifeGamemode.Server.Core.API;
using ReallifeGamemode.Server.Core.RageMP;
namespace ReallifeGamemode.Database.Entities namespace ReallifeGamemode.Database.Entities
{ {
@@ -33,5 +32,7 @@ namespace ReallifeGamemode.Database.Entities
[ForeignKey("Owner")] [ForeignKey("Owner")]
public int? OwnerId { get; set; } public int? OwnerId { get; set; }
public User Owner { get; set; } public User Owner { get; set; }
public ICollection<HouseRental> Rentals { get; set; }
} }
} }

View File

@@ -51,6 +51,25 @@ namespace ReallifeGamemode.Database.Models
modelBuilder.Entity<Entities.VehicleMod>() modelBuilder.Entity<Entities.VehicleMod>()
.HasIndex(vM => new { vM.ServerVehicleId, vM.Slot }).IsUnique(); .HasIndex(vM => new { vM.ServerVehicleId, vM.Slot }).IsUnique();
modelBuilder.Entity<Entities.HouseRental>()
.HasIndex(hR => new
{
hR.HouseId,
hR.UserId
}).IsUnique();
modelBuilder.Entity<Entities.House>()
.HasMany(h => h.Rentals)
.WithOne(hR => hR.House);
modelBuilder.Entity<Entities.User>()
.HasOne(u => u.House)
.WithOne(h => h.Owner);
modelBuilder.Entity<Entities.House>()
.HasOne(h => h.Owner)
.WithOne(u => u.House);
} }
//User //User

View File

@@ -113,33 +113,35 @@ namespace ReallifeGamemode.Server.Core.Managers
public House GetHouseById(int id, DatabaseContext dbContext) 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) 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); OwnerName = house.Owner?.Name,
var userHouseStatus = -1; house.RentalFee,
house.Price,
house.Type,
Rentals = rentals.Select(r => r.User.Name)
};
if (house.OwnerId == null) userHouseStatus = 0; player.TriggerEvent("SetHouseData", newHouse.SerializeJson(), userHouseStatus);
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);
}
} }
public void RemoveHouse(House house) public void RemoveHouse(House house)
@@ -181,19 +183,17 @@ namespace ReallifeGamemode.Server.Core.Managers
public void ReloadAllHouses() 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) private void HouseManagerBuyHouseEvent(IPlayer player, params object[] args)
{ {
using var dbContext = new DatabaseContext(); using var dbContext = GetDbContext();
User user = player.GetUser(dbContext, bankAccount: true); User user = player.GetUser(dbContext, bankAccount: true);
if (user.HouseId != null) if (user.HouseId != null)
@@ -243,8 +243,8 @@ namespace ReallifeGamemode.Server.Core.Managers
DateTime newPossibility = house.LastRentSetTime.AddDays(7); DateTime newPossibility = house.LastRentSetTime.AddDays(7);
string dateStr = newPossibility.ToLongDateString(); string dateStr = newPossibility.ToLongDateString();
string timeStr = newPossibility.ToShortTimeString(); string timeStr = newPossibility.ToShortTimeString();
player.SendNotification( player.SendMessage(
$"~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."); $"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; return;
} }
@@ -274,7 +274,7 @@ namespace ReallifeGamemode.Server.Core.Managers
private void HouseManagerCancelUserRentalEvent(IPlayer player, params object[] args) private void HouseManagerCancelUserRentalEvent(IPlayer player, params object[] args)
{ {
string userName = args[0] as string; string userName = args[0] as string;
using var dbContext = new DatabaseContext(); using var dbContext = GetDbContext();
User user = player.GetUser(dbContext); User user = player.GetUser(dbContext);
if (user.HouseId == null) if (user.HouseId == null)
{ {
@@ -291,14 +291,14 @@ namespace ReallifeGamemode.Server.Core.Managers
House house = GetHouseById(user.HouseId.Value, dbContext); 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) if (rental == null)
{ {
player.SendNotification("~r~Der Spieler ist nicht in deinem Haus eingemietet"); player.SendNotification("~r~Der Spieler ist nicht in deinem Haus eingemietet");
return; return;
} }
dbContext.HouseRentals.Remove(rental); house.Rentals.Remove(rental);
dbContext.SaveChanges(); dbContext.SaveChanges();
target.NewPlayer?.SendNotification($"~y~{player.Name}~s~ hat deinen Mietvertrag ~g~gekündigt~s~."); 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) private void HouseManagerRentInHouseEvent(IPlayer player, params object[] args)
{ {
using var dbContext = new DatabaseContext(); using var dbContext = GetDbContext();
User user = player.GetUser(dbContext); User user = player.GetUser(dbContext);
House house = GetNearHouse(player.Position, dbContext); House house = GetNearHouse(player.Position, dbContext);
@@ -331,7 +331,7 @@ namespace ReallifeGamemode.Server.Core.Managers
UserId = user.Id UserId = user.Id
}; };
dbContext.HouseRentals.Add(newRental); house.Rentals.Add(newRental);
dbContext.SaveChanges(); dbContext.SaveChanges();
@@ -353,7 +353,7 @@ namespace ReallifeGamemode.Server.Core.Managers
return; 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) if (rental == null)
{ {
@@ -361,7 +361,7 @@ namespace ReallifeGamemode.Server.Core.Managers
return; return;
} }
dbContext.HouseRentals.Remove(rental); house.Rentals.Remove(rental);
dbContext.SaveChanges(); dbContext.SaveChanges();