From: Nemanja <98561806+EmoGarbage404@users.noreply.github.com>
Date: Fri, 31 May 2024 02:06:21 +0000 (-0400)
Subject: Fix latejoin antag preferences not being respected (#28430)
X-Git-Url: https://git.smokeofanarchy.ru/gitweb.cgi?a=commitdiff_plain;h=8422eaa07335dc4041b95ab7501253174b907b82;p=space-station-14.git
Fix latejoin antag preferences not being respected (#28430)
* Fix latejoin antag preferences not being respected
* thank fuck for tests
---
diff --git a/Content.Server/Antag/AntagSelectionSystem.API.cs b/Content.Server/Antag/AntagSelectionSystem.API.cs
index 16d83c4f3f..23e77c2102 100644
--- a/Content.Server/Antag/AntagSelectionSystem.API.cs
+++ b/Content.Server/Antag/AntagSelectionSystem.API.cs
@@ -5,6 +5,7 @@ using Content.Server.GameTicking.Rules.Components;
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 sealed partial class AntagSelectionSystem
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 b42831cbde..47da1b6475 100644
--- a/Content.Server/Antag/AntagSelectionSystem.cs
+++ b/Content.Server/Antag/AntagSelectionSystem.cs
@@ -18,7 +18,7 @@ using Content.Shared.GameTicking;
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;
@@ -42,6 +42,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;
@@ -232,13 +236,13 @@ public sealed partial class AntagSelectionSystem : GameRuleSystem
/// 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;
@@ -338,16 +342,14 @@ public sealed partial class AntagSelectionSystem : GameRuleSystem();
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);
}
@@ -418,13 +420,13 @@ public sealed partial class AntagSelectionSystem : GameRuleSystem