Fix non-ghosts and admins counting toward most followed (#28120)

* Fixed non-ghosts and admins counting toward most followed

* Redone to better leverage EntityQueryEnumerator

* Remember to test your code before you commit it, kids

* Review revisions

* Update Content.Shared/Follower/FollowerSystem.cs

Co-authored-by: ShadowCommander <shadowjjt@gmail.com>

* Update Content.Shared/Follower/FollowerSystem.cs

Co-authored-by: ShadowCommander <shadowjjt@gmail.com>

* Update Content.Shared/Follower/FollowerSystem.cs

* Clean up

---------

Co-authored-by: ShadowCommander <shadowjjt@gmail.com>
Co-authored-by: ShadowCommander <10494922+ShadowCommander@users.noreply.github.com>
This commit is contained in:
Tayrtahn
2024-05-20 10:12:05 -04:00
committed by GitHub
parent fa06783986
commit 8995810ade
2 changed files with 26 additions and 11 deletions

View File

@@ -306,7 +306,7 @@ namespace Content.Server.Ghost
return; return;
} }
if (_followerSystem.GetMostFollowed() is not {} target) if (_followerSystem.GetMostGhostFollowed() is not {} target)
return; return;
WarpTo(uid, target); WarpTo(uid, target);

View File

@@ -1,11 +1,11 @@
using System.Numerics; using System.Numerics;
using Content.Shared.Administration.Managers;
using Content.Shared.Database; using Content.Shared.Database;
using Content.Shared.Follower.Components; using Content.Shared.Follower.Components;
using Content.Shared.Ghost; using Content.Shared.Ghost;
using Content.Shared.Hands; using Content.Shared.Hands;
using Content.Shared.Movement.Events; using Content.Shared.Movement.Events;
using Content.Shared.Movement.Pulling.Events; using Content.Shared.Movement.Pulling.Events;
using Content.Shared.Movement.Systems;
using Content.Shared.Tag; using Content.Shared.Tag;
using Content.Shared.Verbs; using Content.Shared.Verbs;
using Robust.Shared.Containers; using Robust.Shared.Containers;
@@ -14,6 +14,7 @@ using Robust.Shared.Map.Events;
using Robust.Shared.Network; using Robust.Shared.Network;
using Robust.Shared.Physics; using Robust.Shared.Physics;
using Robust.Shared.Physics.Systems; using Robust.Shared.Physics.Systems;
using Robust.Shared.Player;
using Robust.Shared.Utility; using Robust.Shared.Utility;
namespace Content.Shared.Follower; namespace Content.Shared.Follower;
@@ -26,6 +27,7 @@ public sealed class FollowerSystem : EntitySystem
[Dependency] private readonly SharedJointSystem _jointSystem = default!; [Dependency] private readonly SharedJointSystem _jointSystem = default!;
[Dependency] private readonly SharedPhysicsSystem _physicsSystem = default!; [Dependency] private readonly SharedPhysicsSystem _physicsSystem = default!;
[Dependency] private readonly INetManager _netMan = default!; [Dependency] private readonly INetManager _netMan = default!;
[Dependency] private readonly ISharedAdminManager _adminManager = default!;
public override void Initialize() public override void Initialize()
{ {
@@ -249,20 +251,33 @@ public sealed class FollowerSystem : EntitySystem
} }
/// <summary> /// <summary>
/// Get the most followed entity. /// Gets the entity with the most non-admin ghosts following it.
/// </summary> /// </summary>
public EntityUid? GetMostFollowed() public EntityUid? GetMostGhostFollowed()
{ {
EntityUid? picked = null; EntityUid? picked = null;
int most = 0; var most = 0;
var query = EntityQueryEnumerator<FollowedComponent>();
while (query.MoveNext(out var uid, out var comp)) // Keep a tally of how many ghosts are following each entity
var followedEnts = new Dictionary<EntityUid, int>();
// Look for followers that are ghosts and are player controlled
var query = EntityQueryEnumerator<FollowerComponent, GhostComponent, ActorComponent>();
while (query.MoveNext(out _, out var follower, out _, out var actor))
{ {
var count = comp.Following.Count; // Exclude admins
if (count > most) if (_adminManager.IsAdmin(actor.PlayerSession))
continue;
var followed = follower.Following;
// Add new entry or increment existing
followedEnts.TryGetValue(followed, out var currentValue);
followedEnts[followed] = currentValue + 1;
if (followedEnts[followed] > most)
{ {
picked = uid; picked = followed;
most = count; most = followedEnts[followed];
} }
} }