Skip to content

Commit

Permalink
modifier didwins
Browse files Browse the repository at this point in the history
  • Loading branch information
XtraCube committed Sep 23, 2024
1 parent 68afe58 commit c928eae
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
6 changes: 6 additions & 0 deletions MiraAPI/Modifiers/Types/GameModifier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,10 @@ public abstract class GameModifier : BaseModifier
/// <param name="role">The role to be checked.</param>
/// <returns>True if the modifier is valid on the role, otherwise false.</returns>
public virtual bool IsModifierValidOn(RoleBehaviour role) => true;

/// <summary>
/// Determines whether the player won the game with this modifier.
/// </summary>
/// <returns>True if the player won, false if they lost. Return null to use the player's default win condition.</returns>
public virtual bool? DidWin(GameOverReason reason) => null;
}
40 changes: 40 additions & 0 deletions MiraAPI/Patches/Modifiers/EndGameDidWinPatch.cs
Original file line number Diff line number Diff line change
@@ -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<GameModifier>()
.FirstOrDefault(x => x.DidWin(gameOverReason) != null)?.DidWin(gameOverReason);
if (modifierWin != null)
{
didWin = modifierWin.Value;
}

if (didWin)
{
EndGameResult.CachedWinners.Add(new CachedPlayerData(networkedPlayerInfo));
}
}
}
}

0 comments on commit c928eae

Please sign in to comment.