From 593fad138fab40765903fb7cc35a3ae7b7a6e03b Mon Sep 17 00:00:00 2001 From: hydrant Date: Sun, 5 Apr 2020 15:20:50 +0200 Subject: [PATCH] evtl fix payday --- .../Commands/AdminCommands.cs | 7 ++- ReallifeGamemode.Server/Finance/Economy.cs | 48 +++++++++---------- 2 files changed, 28 insertions(+), 27 deletions(-) diff --git a/ReallifeGamemode.Server/Commands/AdminCommands.cs b/ReallifeGamemode.Server/Commands/AdminCommands.cs index 64c50660..dcc69041 100644 --- a/ReallifeGamemode.Server/Commands/AdminCommands.cs +++ b/ReallifeGamemode.Server/Commands/AdminCommands.cs @@ -2719,9 +2719,12 @@ namespace ReallifeGamemode.Server.Commands return; } - foreach (var target in NAPI.Pools.GetAllPlayers().Select(c => c.GetUser())) + foreach (var target in NAPI.Pools.GetAllPlayers()) { - Economy.SetPaycheck(target.Player, target.Wage); + if (target.IsLoggedIn()) + { + Economy.SetPaycheck(target); + } } ChatService.SendMessage(player, "~b~[ADMIN]~s~ Du hast ein Payday gedroppt."); diff --git a/ReallifeGamemode.Server/Finance/Economy.cs b/ReallifeGamemode.Server/Finance/Economy.cs index 78bf8f45..e4fe8704 100644 --- a/ReallifeGamemode.Server/Finance/Economy.cs +++ b/ReallifeGamemode.Server/Finance/Economy.cs @@ -69,9 +69,10 @@ namespace ReallifeGamemode.Server.Finance } - public static void SetPaycheck(Player client, int wage) + public static void SetPaycheck(Player client) { User user = client.GetUser(); + int wage = user.Wage; (int bankAccount, float financialHelp, float financialInterest) = GetEconomyClass(client, wage); float propertyTax = GetPropertyTaxation(client); int vehicleTaxation = GetVehicleTaxation(client); @@ -97,12 +98,20 @@ namespace ReallifeGamemode.Server.Finance public static void ReleasePayDay(Player client, Paycheck paycheck) { + + if (client == null || paycheck == null) + { + return; + } + using (var dbContext = new DatabaseContext()) { User u = client.GetUser(dbContext); u.BankAccount.Balance += paycheck.Amount; u.Wage = 0; + u.PaydayTimer = 60; + var rentals = dbContext.HouseRentals .Include(hR => hR.House) .ThenInclude(h => h.BankAccount) @@ -110,9 +119,12 @@ namespace ReallifeGamemode.Server.Finance dbContext.Factions.Include(f => f.BankAccount).Where(f => f.Id == 2).First().BankAccount.Balance += (int)(paycheck.HealthInsurance * 0.1); - foreach (var rental in rentals) + if (rentals.Any()) { - rental.House.BankAccount.Balance += (int)(rental.House.RentalFee * 0.7); + foreach (var rental in rentals) + { + rental.House.BankAccount.Balance += (int)(rental.House.RentalFee * 0.7); + } } dbContext.SaveChanges(); @@ -123,41 +135,27 @@ namespace ReallifeGamemode.Server.Finance ChatService.SendMessage(client, "~g~[PAYDAY]~s~ Dein Stundenumsatz betrÃĪgt - $~r~" + paycheck.Amount + "~s~."); } - /* - public static void PaydayTimer() - { - System.Timers.Timer timer = new System.Timers.Timer(60000); - timer.Start(); - timer.Elapsed += Timer_Elapsed; - } - */ + public static void Timer_Elapsed() { + using var dbContext = new DatabaseContext(); foreach (var player in NAPI.Pools.GetAllPlayers()) { - User user = player.GetUser(); + User user = player.GetUser(dbContext); if (player.IsLoggedIn()) { if (user.PaydayTimer <= 0) { - Economy.SetPaycheck(player, user.Wage); - using (var dbContext = new DatabaseContext()) - { - player.GetUser(dbContext).PaydayTimer = 60; - dbContext.SaveChanges(); - } - return; + Economy.SetPaycheck(player); + user.PaydayTimer = 60; } - if (user.PaydayTimer > 0) + else if (user.PaydayTimer > 0) { - using (var dbContext = new DatabaseContext()) - { - player.GetUser(dbContext).PaydayTimer -= 1; - dbContext.SaveChanges(); - } + user.PaydayTimer -= 1; } } } + dbContext.SaveChanges(); } } }