Skip to content

Commit

Permalink
fix: null checks for sessions
Browse files Browse the repository at this point in the history
  • Loading branch information
oscar-wos committed May 26, 2024
1 parent aa6d903 commit 6799c07
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions Sessions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,11 @@ public override void Load(bool hotReload)

_server.Map = _database.GetMapAsync(Server.MapName).GetAwaiter().GetResult();

Utilities.GetPlayers().Where(IsValidPlayer).ToList().ForEach(player =>
foreach (CCSPlayerController player in Utilities.GetPlayers())
{
OnPlayerConnect(player.Slot, player.SteamID, NativeAPI.GetPlayerIpAddress(player.Slot).Split(":")[0]).GetAwaiter().GetResult();
CheckAlias(player.Slot, player.PlayerName).GetAwaiter().GetResult();
});
}
}

public async Task OnPlayerConnect(int playerSlot, ulong steamId, string ip)
Expand All @@ -80,28 +80,29 @@ public async Task OnPlayerConnect(int playerSlot, ulong steamId, string ip)

public async Task CheckAlias(int playerSlot, string alias)
{
if (!_players.TryGetValue(playerSlot, out PlayerSQL? value))
if (!_players.TryGetValue(playerSlot, out PlayerSQL? value) || value.Session == null)
return;

AliasSQL? recentAlias = await _database.GetAliasAsync(value.Id);

if (recentAlias == null || recentAlias.Alias != alias)
_database.InsertAlias(value.Session!.Id, value.Id, alias);
_database.InsertAlias(value.Session.Id, value.Id, alias);
}

public void Timer_Repeat()
{
IEnumerable<CCSPlayerController> players = Utilities.GetPlayers().Where(IsValidPlayer);
List<int> playerIds = [];
List<long> sessionIds = [];

foreach (CCSPlayerController player in players)
foreach (CCSPlayerController player in Utilities.GetPlayers())
{
if (!_players.TryGetValue(player.Slot, out PlayerSQL? value))
continue;

playerIds.Add(value.Id);
sessionIds.Add(value.Session!.Id);

if (value.Session != null)
sessionIds.Add(value.Session.Id);
}

_database.UpdateSessions(playerIds, sessionIds);
Expand Down

0 comments on commit 6799c07

Please sign in to comment.