fix modifier errors in end game win patch

This commit is contained in:
XtraCube committed 2025-04-09 22:06:28 -04:00
1 parent 54d85a6996
commit e242975d75
2 files changed
+29 -16

No files matched your search

+1 -1
View File
@@ -34,7 +34,7 @@ public abstract class GameModifier : BaseModifier
public virtual bool IsModifierValidOn(RoleBehaviour role) => true;
/// <summary>
/// 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.
/// </summary>
/// <param name="reason">The reason why the game ended.</param>
/// <returns>True if the player won, false if they lost. Return null to use the player's default win condition.</returns>
+28 -15
View File
@@ -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<CachedPlayerData> 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<GameModifier>().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);
}
}
}