From e242975d75c0690619956eea0bd808eecd805784 Mon Sep 17 00:00:00 2001 From: XtraCube <72575280+XtraCube@users.noreply.github.com> Date: Wed, 9 Apr 2025 22:06:28 -0400 Subject: [PATCH] fix modifier errors in end game win patch --- MiraAPI/Modifiers/Types/GameModifier.cs | 2 +- .../Patches/Modifiers/EndGameDidWinPatch.cs | 43 ++++++++++++------- 2 files changed, 29 insertions(+), 16 deletions(-) diff --git a/MiraAPI/Modifiers/Types/GameModifier.cs b/MiraAPI/Modifiers/Types/GameModifier.cs index abcd1f7..13f6754 100644 --- a/MiraAPI/Modifiers/Types/GameModifier.cs +++ b/MiraAPI/Modifiers/Types/GameModifier.cs @@ -34,7 +34,7 @@ public abstract class GameModifier : BaseModifier public virtual bool IsModifierValidOn(RoleBehaviour role) => true; /// - /// Determines whether the player won the game with this modifier. + /// Determines whether the player won the game with this modifier. The modifier with the highest priority will override other modifiers. /// /// The reason why the game ended. /// True if the player won, false if they lost. Return null to use the player's default win condition. diff --git a/MiraAPI/Patches/Modifiers/EndGameDidWinPatch.cs b/MiraAPI/Patches/Modifiers/EndGameDidWinPatch.cs index 1a7046c..eee933c 100644 --- a/MiraAPI/Patches/Modifiers/EndGameDidWinPatch.cs +++ b/MiraAPI/Patches/Modifiers/EndGameDidWinPatch.cs @@ -1,19 +1,20 @@ -using System.Linq; +using System.Collections.Generic; +using System.Linq; using HarmonyLib; using MiraAPI.Modifiers; using MiraAPI.Modifiers.Types; -using MiraAPI.Utilities; -using Reactor.Utilities; namespace MiraAPI.Patches.Modifiers; [HarmonyPatch(typeof(AmongUsClient), nameof(AmongUsClient.OnGameEnd))] -public static class EndGameDidWinPatch +internal static class EndGameDidWinPatch { - public static void Postfix() + private static List CachedWinners { get; set; } = []; + + public static void Prefix(EndGameResult endGameResult) { - var gameOverReason = EndGameResult.CachedGameOverReason; - EndGameResult.CachedWinners.Clear(); + CachedWinners.Clear(); + var gameOverReason = endGameResult.GameOverReason; var players = GameData.Instance.AllPlayers.ToArray(); for (var i = 0; i < GameData.Instance.PlayerCount; i++) @@ -26,25 +27,37 @@ public static class EndGameDidWinPatch var didWin = networkedPlayerInfo.Role.DidWin(gameOverReason); - bool? modifierDidWin = null; + if (!networkedPlayerInfo.Object) + { + if (didWin) + { + CachedWinners.Add(new CachedPlayerData(networkedPlayerInfo)); + } + continue; + } + foreach (var modifier in networkedPlayerInfo.Object.GetModifiers().OrderByDescending(x => x.Priority())) { var result = modifier.DidWin(gameOverReason); if (!result.HasValue) continue; - modifierDidWin = result.Value; + didWin = result.Value; break; } - if (modifierDidWin.HasValue) - { - didWin = modifierDidWin.Value; - } - if (didWin) { - EndGameResult.CachedWinners.Add(new CachedPlayerData(networkedPlayerInfo)); + CachedWinners.Add(new CachedPlayerData(networkedPlayerInfo)); } } } + + public static void Postfix(EndGameResult endGameResult) + { + EndGameResult.CachedWinners.Clear(); + foreach (var winner in CachedWinners) + { + EndGameResult.CachedWinners.Add(winner); + } + } }