From 246e40539ccf0e4f8780855c024187f0473b0d76 Mon Sep 17 00:00:00 2001 From: AlchlcDvl <56699741+AlchlcDvl@users.noreply.github.com> Date: Sun, 6 Oct 2024 10:08:54 -0400 Subject: [PATCH] finally works --- MiraAPI/Modifiers/ModifierComponent.cs | 20 +++++++++++ MiraAPI/Modifiers/ModifierManager.cs | 49 ++++++++++++-------------- MiraAPI/Utilities/Extensions.cs | 4 +-- 3 files changed, 44 insertions(+), 29 deletions(-) diff --git a/MiraAPI/Modifiers/ModifierComponent.cs b/MiraAPI/Modifiers/ModifierComponent.cs index 7558562..35a4d83 100644 --- a/MiraAPI/Modifiers/ModifierComponent.cs +++ b/MiraAPI/Modifiers/ModifierComponent.cs @@ -255,4 +255,24 @@ public class ModifierComponent(IntPtr cppPtr) : MonoBehaviour(cppPtr) { return AddModifier(typeof(T)) as T; } + + /// + /// Checks if a player has an active or queued modifier by its ID. + /// + /// The Modifier ID. + /// True if the Modifier is present, false otherwise. + public bool HasModifier(uint id) + { + return ActiveModifiers.Exists(x => x.ModifierId == id) || _toAdd.Exists(x => x.ModifierId == id); + } + + /// + /// Checks if a player has an active or queued modifier by its type. + /// + /// The Type of the Modifier. + /// True if the Modifier is present, false otherwise. + public bool HasModifier() + { + return ActiveModifiers.Exists(x => x is T) || _toAdd.Exists(x => x is T); + } } diff --git a/MiraAPI/Modifiers/ModifierManager.cs b/MiraAPI/Modifiers/ModifierManager.cs index 65ef738..927a9ee 100644 --- a/MiraAPI/Modifiers/ModifierManager.cs +++ b/MiraAPI/Modifiers/ModifierManager.cs @@ -78,73 +78,68 @@ public static class ModifierManager internal static void AssignModifiers(List plrs) { - foreach (var player in plrs) - { - Logger.Message(player.Data.PlayerName); - } - var rand = new Random(); - var sequence = PrioritiesToIdsMap.Keys.OrderByDescending(x => x); - foreach (var priority in sequence) + foreach (var priority in PrioritiesToIdsMap.Keys.OrderByDescending(x => x)) { var filteredModifiers = new List(); - Logger.Message(priority); foreach (var id in PrioritiesToIdsMap[priority]) { var mod = Activator.CreateInstance(IdToTypeModifierMap[id]) as GameModifier; + var chance = Math.Clamp(mod!.GetAssignmentChance(), 0, 100); var maxCount = plrs.Count(x => IsGameModifierValid(x, mod!, id)); - Logger.Message(maxCount); + + if (chance == 0 || mod!.GetAmountPerGame() == 0) + { + continue; + } if (maxCount == 0) { - Logger.Warning("No players are valid for modifier: " + mod!.ModifierName); + Logger.Warning($"No players are valid for {mod!.ModifierName}"); continue; } var num = Math.Clamp(mod!.GetAmountPerGame(), 0, maxCount); - var chance = mod.GetAssignmentChance(); for (var i = 0; i < num; i++) { var randomNum = rand.Next(100); - if (randomNum < Math.Clamp(chance, 0, 100)) + if (randomNum < chance) { filteredModifiers.Add(id); } } } - var shuffledList = filteredModifiers.ToList().Randomize(); + if (filteredModifiers.Count == 0) + { + Logger.Warning($"No filtered modifiers for priority {priority}"); + continue; + } + + var shuffledList = filteredModifiers.Randomize(); if (shuffledList.Count > plrs.Count) { shuffledList = shuffledList.GetRange(0, plrs.Count)!; } - while (shuffledList.Count > 0) + foreach (var id in shuffledList) { - var id = shuffledList[0]; var mod = Activator.CreateInstance(IdToTypeModifierMap[id]) as GameModifier; + var plr = plrs.Where(x => IsGameModifierValid(x, mod!, id)).Random(); - if (!plrs.Exists(x => IsGameModifierValid(x, mod!, id))) + if (plr == null) { - shuffledList.RemoveAt(0); - continue; + Logger.Warning($"Somehow valid players for modifier {mod!.ModifierName} disappeared"); } - - var plr = plrs.Random(); - - if (plr == null || !IsGameModifierValid(plr, mod!, id)) + else { - shuffledList.RemoveAt(0); // Just some insurance - continue; + plr.RpcAddModifier(id); } - - shuffledList.RemoveAt(0); - plr.RpcAddModifier(id); } } } diff --git a/MiraAPI/Utilities/Extensions.cs b/MiraAPI/Utilities/Extensions.cs index f7c667a..ef5d1f2 100644 --- a/MiraAPI/Utilities/Extensions.cs +++ b/MiraAPI/Utilities/Extensions.cs @@ -194,7 +194,7 @@ public static class Extensions public static bool HasModifier(this PlayerControl? player) where T : BaseModifier { return player?.GetModifierComponent() != null && - player.GetModifierComponent()!.ActiveModifiers.Exists(x => x is T); + player.GetModifierComponent()!.HasModifier(); } /// @@ -206,7 +206,7 @@ public static class Extensions public static bool HasModifier(this PlayerControl? player, uint id) { return player?.GetModifierComponent() != null && - player.GetModifierComponent()!.ActiveModifiers.Exists(x => x.ModifierId == id); + player.GetModifierComponent()!.HasModifier(id); } ///