From a9158b4803457de4fb685ee76595e09f5f6ddedf Mon Sep 17 00:00:00 2001 From: Nemanja <98561806+EmoGarbage404@users.noreply.github.com> Date: Thu, 30 May 2024 22:06:21 -0400 Subject: [PATCH 1/2] Fix latejoin antag preferences not being respected (#28430) * Fix latejoin antag preferences not being respected * thank fuck for tests --- .../Antag/AntagSelectionSystem.API.cs | 31 ++++++++++++++++ Content.Server/Antag/AntagSelectionSystem.cs | 36 ++++++++++--------- 2 files changed, 50 insertions(+), 17 deletions(-) diff --git a/Content.Server/Antag/AntagSelectionSystem.API.cs b/Content.Server/Antag/AntagSelectionSystem.API.cs index 59bf05fe03f..77f543cdcf1 100644 --- a/Content.Server/Antag/AntagSelectionSystem.API.cs +++ b/Content.Server/Antag/AntagSelectionSystem.API.cs @@ -5,6 +5,7 @@ using Content.Server.Objectives; using Content.Shared.Chat; using Content.Shared.Mind; +using Content.Shared.Preferences; using JetBrains.Annotations; using Robust.Shared.Audio; using Robust.Shared.Enums; @@ -156,6 +157,36 @@ public List GetAntagMindEntityUids(Entity e return ent.Comp.SelectedMinds.Select(p => p.Item1).ToList(); } + /// + /// Checks if a given session has the primary antag preferences for a given definition + /// + public bool HasPrimaryAntagPreference(ICommonSession? session, AntagSelectionDefinition def) + { + if (session == null) + return true; + + if (def.PrefRoles.Count == 0) + return false; + + var pref = (HumanoidCharacterProfile) _pref.GetPreferences(session.UserId).SelectedCharacter; + return pref.AntagPreferences.Any(p => def.PrefRoles.Contains(p)); + } + + /// + /// Checks if a given session has the fallback antag preferences for a given definition + /// + public bool HasFallbackAntagPreference(ICommonSession? session, AntagSelectionDefinition def) + { + if (session == null) + return true; + + if (def.FallbackRoles.Count == 0) + return false; + + var pref = (HumanoidCharacterProfile) _pref.GetPreferences(session.UserId).SelectedCharacter; + return pref.AntagPreferences.Any(p => def.FallbackRoles.Contains(p)); + } + /// /// Returns all the antagonists for this rule who are currently alive /// diff --git a/Content.Server/Antag/AntagSelectionSystem.cs b/Content.Server/Antag/AntagSelectionSystem.cs index d74824dd2d5..a112e0ad63d 100644 --- a/Content.Server/Antag/AntagSelectionSystem.cs +++ b/Content.Server/Antag/AntagSelectionSystem.cs @@ -17,7 +17,7 @@ using Content.Shared.Ghost; using Content.Shared.Humanoid; using Content.Shared.Players; -using Content.Shared.Preferences; +using Content.Shared.Whitelist; using Robust.Server.Audio; using Robust.Server.GameObjects; using Robust.Server.Player; @@ -41,6 +41,7 @@ public sealed partial class AntagSelectionSystem : GameRuleSystem(); while (query.MoveNext(out var uid, out _, out var antag, out _)) { - // TODO ANTAG - // what why aasdiuhasdopiuasdfhksad - // stop this insanity please - // probability of antag assignment shouldn't depend on the order in which rules are returned by the query. + rules.Add((uid, antag)); + } + RobustRandom.Shuffle(rules); + + foreach (var (uid, antag) in rules) + { if (!RobustRandom.Prob(LateJoinRandomChance)) continue; @@ -221,13 +225,13 @@ public void ChooseAntags(Entity ent, IList /// Tries to makes a given player into the specified antagonist. /// - public bool TryMakeAntag(Entity ent, ICommonSession? session, AntagSelectionDefinition def, bool ignoreSpawner = false) + public bool TryMakeAntag(Entity ent, ICommonSession? session, AntagSelectionDefinition def, bool ignoreSpawner = false, bool checkPref = true) { - if (!IsSessionValid(ent, session, def) || - !IsEntityValid(session?.AttachedEntity, def)) - { + if (checkPref && !HasPrimaryAntagPreference(session, def)) + return false; + + if (!IsSessionValid(ent, session, def) || !IsEntityValid(session?.AttachedEntity, def)) return false; - } MakeAntag(ent, session, def, ignoreSpawner); return true; @@ -324,16 +328,14 @@ public AntagSelectionPlayerPool GetPlayerPool(Entity en var fallbackList = new List(); foreach (var session in sessions) { - if (!IsSessionValid(ent, session, def) || - !IsEntityValid(session.AttachedEntity, def)) + if (!IsSessionValid(ent, session, def) || !IsEntityValid(session.AttachedEntity, def)) continue; - var pref = (HumanoidCharacterProfile) _pref.GetPreferences(session.UserId).SelectedCharacter; - if (def.PrefRoles.Count != 0 && pref.AntagPreferences.Any(p => def.PrefRoles.Contains(p))) + if (HasPrimaryAntagPreference(session, def)) { preferredList.Add(session); } - else if (def.FallbackRoles.Count != 0 && pref.AntagPreferences.Any(p => def.FallbackRoles.Contains(p))) + else if (HasFallbackAntagPreference(session, def)) { fallbackList.Add(session); } @@ -404,13 +406,13 @@ public bool IsEntityValid(EntityUid? entity, AntagSelectionDefinition def) if (def.Whitelist != null) { - if (!def.Whitelist.IsValid(entity.Value, EntityManager)) + if (!_whitelist.IsValid(def.Whitelist, entity.Value)) return false; } if (def.Blacklist != null) { - if (def.Blacklist.IsValid(entity.Value, EntityManager)) + if (_whitelist.IsValid(def.Blacklist, entity.Value)) return false; } From 309eed8e7f653da4373da0b299dd8c81bbe5b722 Mon Sep 17 00:00:00 2001 From: Remuchi Date: Fri, 11 Oct 2024 14:02:06 +0700 Subject: [PATCH 2/2] fix: simple patch as we have no EntityWhitelistSystem yet --- Content.Server/Antag/AntagSelectionSystem.cs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/Content.Server/Antag/AntagSelectionSystem.cs b/Content.Server/Antag/AntagSelectionSystem.cs index a112e0ad63d..cd4d836e683 100644 --- a/Content.Server/Antag/AntagSelectionSystem.cs +++ b/Content.Server/Antag/AntagSelectionSystem.cs @@ -17,7 +17,6 @@ using Content.Shared.Ghost; using Content.Shared.Humanoid; using Content.Shared.Players; -using Content.Shared.Whitelist; using Robust.Server.Audio; using Robust.Server.GameObjects; using Robust.Server.Player; @@ -41,7 +40,6 @@ public sealed partial class AntagSelectionSystem : GameRuleSystem