From bc467c846a61797fb0258edf91624d624a6d53dc Mon Sep 17 00:00:00 2001 From: Lemirda <142801986+Lemirda@users.noreply.github.com> Date: Sat, 15 Jun 2024 11:43:53 +0300 Subject: [PATCH] SponsorAntag --- Content.Server/Antag/AntagSelectionSystem.cs | 129 +++++++++++-------- 1 file changed, 78 insertions(+), 51 deletions(-) diff --git a/Content.Server/Antag/AntagSelectionSystem.cs b/Content.Server/Antag/AntagSelectionSystem.cs index c6a207178bb..6d71df6ad33 100644 --- a/Content.Server/Antag/AntagSelectionSystem.cs +++ b/Content.Server/Antag/AntagSelectionSystem.cs @@ -1,4 +1,5 @@ using System.Linq; +using Content.Server.Andromeda.AndromedaSponsorService; // A-13 SponsorAntag using Content.Server.Antag.Components; using Content.Server.Chat.Managers; using Content.Server.GameTicking; @@ -27,8 +28,6 @@ using Robust.Shared.Player; using Robust.Shared.Random; using Robust.Shared.Utility; -using Content.Server.Andromeda.AndromedaSponsorService; //A-13 SponsorAntag -using Content.Server.Andromeda.Roles; //A-13 SponsorAntag namespace Content.Server.Antag; @@ -45,8 +44,7 @@ public sealed partial class AntagSelectionSystem : GameRuleSystem ent, IList public void ChooseAntags(Entity ent, IList pool, AntagSelectionDefinition def) { + Log.Info($"ChooseAntags: Начало для entity: {ent}"); // A-13 Full logging for error detection var playerPool = GetPlayerPool(ent, pool, def); var count = GetTargetAntagCount(ent, GetTotalPlayerCount(pool), def); + // A-13 SponsorAntag start + var totalAntagCount = 0; + + Log.Info($"ChooseAntags: Начинается проверка на спонсора entity: {ent}"); + + foreach (var session in pool.Where(s => _sponsorsManager.IsSponsor(s.UserId))) + { + if (totalAntagCount >= count) + { + Log.Error($"ChooseAntags: Мест для спонсоров не осталось"); // A-13 SponsorAntag + break; + } + + bool allowedAntag = _sponsorsManager.GetSponsorAllowedAntag(session.UserId); + + if (!allowedAntag) + { + Log.Error($"ChooseAntags: Спонсор {session} не может быть выбран из за отсутствия allowedAntag"); + break; + } + + if (!TryMakeAntag(ent, session, def)) + { + Log.Error($"MakeAntag: Не удалось создать антаг для сущности {ent} и определения {def}"); + continue; + } + else + { + Log.Info($"MakeAntag: Создан антаг для сущности {ent} и определения {def}"); + totalAntagCount++; + } + + Log.Info($"ChooseAntags: В ходе добавления спонсора, totalAntagCount = {totalAntagCount}"); + } + // A-13 SponsorAntag end + + Log.Info($"ChooseAntags: Начинается проверка для игроков entity: {ent}"); // A-13 Full logging for error detection - // if there is both a spawner and players getting picked, let it fall back to a spawner. var noSpawner = def.SpawnerPrototype == null; - for (var i = 0; i < count; i++) + for (var i = totalAntagCount; i < count; i++) // A-13 SponsorAntag { var session = (ICommonSession?) null; if (def.PickPlayer) @@ -226,6 +261,19 @@ public void ChooseAntags(Entity ent, IList ent, IList @@ -243,10 +293,16 @@ public void ChooseAntags(Entity ent, IList ent, ICommonSession? session, AntagSelectionDefinition def, bool ignoreSpawner = false, bool checkPref = true) { if (checkPref && !HasPrimaryAntagPreference(session, def)) + { + Log.Error($"MakeAntag: {session} does not have primary preference for {def}"); // A-13 Full logging for error detection return false; + } if (!IsSessionValid(ent, session, def) || !IsEntityValid(session?.AttachedEntity, def)) + { + Log.Error($"MakeAntag: {session} is not valid for {def}"); // A-13 Full logging for error detection return false; + } MakeAntag(ent, session, def, ignoreSpawner); return true; @@ -257,8 +313,6 @@ public bool TryMakeAntag(Entity ent, ICommonSession? se /// public void MakeAntag(Entity ent, ICommonSession? session, AntagSelectionDefinition def, bool ignoreSpawner = false) { - Log.Debug($"MakeAntag: ent={ent}, session={session}, def={def}, ignoreSpawner={ignoreSpawner}"); //A-13 Полное логирование - var antagEnt = (EntityUid?) null; var isSpawner = false; @@ -283,7 +337,6 @@ public void MakeAntag(Entity ent, ICommonSession? sessi if (!getEntEv.Handled) { - Log.Error($"Attempted to make {session} antagonist in gamerule {ToPrettyString(ent)} but there was no valid entity for player."); //A-13 Полное логирование throw new InvalidOperationException($"Attempted to make {session} antagonist in gamerule {ToPrettyString(ent)} but there was no valid entity for player."); } @@ -302,9 +355,22 @@ public void MakeAntag(Entity ent, ICommonSession? sessi _transform.SetMapCoordinates((player, playerXform), pos); } - // The following is where we apply components, equipment, and other changes to our antagonist entity. - Log.Debug($"Applying components to antag entity: {antagEnt}"); //A-13 Полное логирование + // If we want to just do a ghost role spawner, set up data here and then return early. + // This could probably be an event in the future if we want to be more refined about it. + if (isSpawner) + { + if (!TryComp(player, out var spawnerComp)) + { + Log.Error($"Antag spawner {player} does not have a GhostRoleAntagSpawnerComponent."); + return; + } + spawnerComp.Rule = ent; + spawnerComp.Definition = def; + return; + } + + // The following is where we apply components, equipment, and other changes to our antagonist entity. EntityManager.AddComponents(player, def.Components); _stationSpawning.EquipStartingGear(player, def.StartingGear); @@ -317,16 +383,12 @@ public void MakeAntag(Entity ent, ICommonSession? sessi _mind.SetUserId(curMind.Value, session.UserId); } - Log.Debug($"Transferring mind to antag entity: {antagEnt}"); //A-13 Полное логирование - _mind.TransferTo(curMind.Value, antagEnt, ghostCheckOverride: true); _role.MindAddRoles(curMind.Value, def.MindComponents); ent.Comp.SelectedMinds.Add((curMind.Value, Name(player))); SendBriefing(session, def.Briefing); } - Log.Debug($"Start apply logic to antag entity: {antagEnt}"); //A-13 Полное логирование - var afterEv = new AfterAntagEntitySelectedEvent(session, player, ent, def); RaiseLocalEvent(ent, ref afterEv, true); } @@ -338,36 +400,11 @@ public AntagSelectionPlayerPool GetPlayerPool(Entity en { var preferredList = new List(); var fallbackList = new List(); - /* A-13 disable - var unwantedList = new List(); - var invalidList = new List(); - var sponsorPrefList = new List(); // A-13 SponsorAntag - */ foreach (var session in sessions) { if (!IsSessionValid(ent, session, def) || !IsEntityValid(session.AttachedEntity, def)) continue; - /* A-13 disable - // A-13 SponsorAntag start - if (_sponsorsManager.IsSponsor(session.UserId)) - { - bool allowedAntag = _sponsorsManager.GetSponsorAllowedAntag(session.UserId); - if (allowedAntag) - { - sponsorPrefList.Add(session); - Log.Info($"Added sponsor: {session}"); - } - } - else - { - Log.Info($"Skipped: {session}"); - } - // A-13 SponsorAntag end - - 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); @@ -377,17 +414,7 @@ public AntagSelectionPlayerPool GetPlayerPool(Entity en fallbackList.Add(session); } } - /* A-13 disable - // A-13 SponsorAntag start - if (sponsorPrefList.Count > 0) - { - Log.Info($"Added sponsor pool: {sponsorPrefList.Count}"); - preferredList.AddRange(sponsorPrefList); - } - // A-13 SponsorAntag end - return new AntagSelectionPlayerPool(new() { preferredList, fallbackList, unwantedList, invalidList }); - */ return new AntagSelectionPlayerPool(new() { preferredList, fallbackList }); } @@ -508,4 +535,4 @@ public record struct AntagSelectLocationEvent(ICommonSession? Session, Entity [ByRefEvent] -public readonly record struct AfterAntagEntitySelectedEvent(ICommonSession? Session, EntityUid EntityUid, Entity GameRule, AntagSelectionDefinition Def); +public readonly record struct AfterAntagEntitySelectedEvent(ICommonSession? Session, EntityUid EntityUid, Entity GameRule, AntagSelectionDefinition Def); \ No newline at end of file