userbankaccount balancechanged dont crash server

This commit is contained in:
hydrant
2021-04-11 14:20:29 +02:00
parent 4ec4875701
commit e82d467d4e
2 changed files with 26 additions and 14 deletions

View File

@@ -187,11 +187,14 @@ namespace ReallifeGamemode.Server.Finance
public static void Timer_Elapsed()
{
using var dbContext = new DatabaseContext();
foreach (var player in NAPI.Pools.GetAllPlayers())
foreach (var player in NAPI.Pools.GetAllPlayers().Where(p => p.IsLoggedIn()))
{
User user = player.GetUser(dbContext);
if (player.IsLoggedIn())
if(user == null)
{
continue;
}
user.PlayedMinutes += 1;
if (user.PaydayTimer <= 0)
{
@@ -203,7 +206,6 @@ namespace ReallifeGamemode.Server.Finance
user.PaydayTimer -= 1;
}
}
}
dbContext.SaveChanges();
}
}

View File

@@ -172,8 +172,18 @@ namespace ReallifeGamemode.Server
using (var dbContext = new DatabaseContext())
{
var user = dbContext.Users.Where(u => u.BankAccountId == account.Id).Select(u => u.Name).FirstOrDefault();
if (user == null) return;
PlayerService.GetPlayerByNameOrId(user).TriggerEvent("updateMoney", account.Balance);
if (user == null)
{
return;
}
Player player = PlayerService.GetPlayerByNameOrId(user);
if(player == null)
{
return;
}
player.TriggerEvent("updateMoney", account.Balance);
}
};