Skip to content

Commit

Permalink
fix: TeamInterestManagement OnDestroyed logic
Browse files Browse the repository at this point in the history
  • Loading branch information
MrGadget1024 committed Apr 18, 2022
1 parent 0e9d86f commit 2db726d
Showing 1 changed file with 16 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ public override void OnSpawned(NetworkIdentity identity)
string currentTeam = networkTeam.teamId;
lastObjectTeam[identity] = currentTeam;

// string.Empty is never a valid teamId...do not add to teamObjects collection
if (currentTeam == string.Empty)
// Null / Empty string is never a valid teamId...do not add to teamObjects collection
if (string.IsNullOrWhiteSpace(currentTeam))
return;

// Debug.Log($"MatchInterestManagement.OnSpawned({identity.name}) currentMatch: {currentTeam}");
Expand All @@ -38,10 +38,14 @@ public override void OnSpawned(NetworkIdentity identity)

public override void OnDestroyed(NetworkIdentity identity)
{
lastObjectTeam.TryGetValue(identity, out string currentTeam);
lastObjectTeam.Remove(identity);
if (currentTeam != string.Empty && teamObjects.TryGetValue(currentTeam, out HashSet<NetworkIdentity> objects) && objects.Remove(identity))
RebuildTeamObservers(currentTeam);
if (lastObjectTeam.TryGetValue(identity, out string currentTeam))
{
lastObjectTeam.Remove(identity);
if (!string.IsNullOrWhiteSpace(currentTeam)
&& teamObjects.TryGetValue(currentTeam, out HashSet<NetworkIdentity> objects)
&& objects.Remove(identity))
RebuildTeamObservers(currentTeam);
}
}

// internal so we can update from tests
Expand All @@ -62,7 +66,7 @@ internal void Update()
if (!lastObjectTeam.TryGetValue(netIdentity, out string currentTeam))
continue;

// string.Empty is never a valid teamId
// Null / Empty string is never a valid teamId
// Nothing to do if teamId hasn't changed
if (string.IsNullOrWhiteSpace(newTeam) || newTeam == currentTeam)
continue;
Expand All @@ -84,8 +88,8 @@ internal void Update()

void UpdateDirtyTeams(string newTeam, string currentTeam)
{
// string.Empty is never a valid teamId
if (currentTeam != string.Empty)
// Null / Empty string is never a valid teamId
if (!string.IsNullOrWhiteSpace(currentTeam))
dirtyTeams.Add(currentTeam);

dirtyTeams.Add(newTeam);
Expand Down Expand Up @@ -136,7 +140,7 @@ public override bool OnCheckObserver(NetworkIdentity identity, NetworkConnection
if (newObserverNetworkTeam.forceShown)
return true;

// string.Empty is never a valid teamId
// Null / Empty string is never a valid teamId
if (string.IsNullOrWhiteSpace(newObserverNetworkTeam.teamId))
return false;

Expand All @@ -160,8 +164,8 @@ public override void OnRebuildObservers(NetworkIdentity identity, HashSet<Networ
return;
}

// string.Empty is never a valid teamId
if (networkTeam.teamId == string.Empty)
// Null / Empty string is never a valid teamId
if (string.IsNullOrWhiteSpace(networkTeam.teamId))
return;

// Abort if this team hasn't been created yet by OnSpawned or UpdateTeamObjects
Expand Down

0 comments on commit 2db726d

Please sign in to comment.