From c928eae183262af9ce146993f4dd3058ecaa6f73 Mon Sep 17 00:00:00 2001
From: XtraCube <72575280+XtraCube@users.noreply.github.com>
Date: Sun, 22 Sep 2024 20:38:41 -0400
Subject: [PATCH] modifier didwins
---
MiraAPI/Modifiers/Types/GameModifier.cs | 6 +++
.../Patches/Modifiers/EndGameDidWinPatch.cs | 40 +++++++++++++++++++
2 files changed, 46 insertions(+)
create mode 100644 MiraAPI/Patches/Modifiers/EndGameDidWinPatch.cs
diff --git a/MiraAPI/Modifiers/Types/GameModifier.cs b/MiraAPI/Modifiers/Types/GameModifier.cs
index 9ec6f28..60c5f56 100644
--- a/MiraAPI/Modifiers/Types/GameModifier.cs
+++ b/MiraAPI/Modifiers/Types/GameModifier.cs
@@ -23,4 +23,10 @@ public abstract class GameModifier : BaseModifier
/// The role to be checked.
/// True if the modifier is valid on the role, otherwise false.
public virtual bool IsModifierValidOn(RoleBehaviour role) => true;
+
+ ///
+ /// Determines whether the player won the game with this modifier.
+ ///
+ /// True if the player won, false if they lost. Return null to use the player's default win condition.
+ public virtual bool? DidWin(GameOverReason reason) => null;
}
diff --git a/MiraAPI/Patches/Modifiers/EndGameDidWinPatch.cs b/MiraAPI/Patches/Modifiers/EndGameDidWinPatch.cs
new file mode 100644
index 0000000..d7a99d5
--- /dev/null
+++ b/MiraAPI/Patches/Modifiers/EndGameDidWinPatch.cs
@@ -0,0 +1,40 @@
+using System.Linq;
+using HarmonyLib;
+using MiraAPI.Modifiers.Types;
+using MiraAPI.Utilities;
+
+namespace MiraAPI.Patches.Modifiers;
+
+[HarmonyPatch(typeof(AmongUsClient), nameof(AmongUsClient.CoEndGame))]
+public static class EndGameDidWinPatch
+{
+ public static void Prefix()
+ {
+ var gameOverReason = EndGameResult.CachedGameOverReason;
+ EndGameResult.CachedWinners.Clear();
+
+ var players = GameData.Instance.AllPlayers.ToArray();
+ for (var i = 0; i < GameData.Instance.PlayerCount; i++)
+ {
+ var networkedPlayerInfo = players[i];
+ if (networkedPlayerInfo == null)
+ {
+ continue;
+ }
+
+ var didWin = networkedPlayerInfo.Role.DidWin(gameOverReason);
+
+ var modifierWin = networkedPlayerInfo.Object?.GetModifierComponent()?.ActiveModifiers.OfType()
+ .FirstOrDefault(x => x.DidWin(gameOverReason) != null)?.DidWin(gameOverReason);
+ if (modifierWin != null)
+ {
+ didWin = modifierWin.Value;
+ }
+
+ if (didWin)
+ {
+ EndGameResult.CachedWinners.Add(new CachedPlayerData(networkedPlayerInfo));
+ }
+ }
+ }
+}