From 02f7ddc63a59e67cef8d69512f69c682de2472e2 Mon Sep 17 00:00:00 2001 From: Ape <155788660+ApeMV@users.noreply.github.com> Date: Mon, 2 Feb 2026 20:41:26 +0100 Subject: [PATCH] Custom role fixes, small setting fix, winner text change --- AmongUsRevamped.cs | 2 +- Modules/CustomRoleManagement.cs | 88 +++++++++++++++++++++++++-------- Patches/ChatPatches.cs | 8 +-- Patches/EndGamePatches.cs | 11 +++-- Patches/ExilePatch.cs | 35 +++++++++++++ 5 files changed, 115 insertions(+), 29 deletions(-) create mode 100644 Patches/ExilePatch.cs diff --git a/AmongUsRevamped.cs b/AmongUsRevamped.cs index c09e2f7..037edb3 100644 --- a/AmongUsRevamped.cs +++ b/AmongUsRevamped.cs @@ -39,7 +39,7 @@ public partial class Main : BasePlugin public static HideNSeekGameOptionsV10 HideNSeekOptions => GameOptionsManager.Instance != null ? GameOptionsManager.Instance.currentHideNSeekGameOptions : null; public static bool HasArgumentException; public static string CredentialsText; - public const string ModVersion = "v1.5.0"; + public const string ModVersion = "v1.5.1"; public static float GameTimer; diff --git a/Modules/CustomRoleManagement.cs b/Modules/CustomRoleManagement.cs index a375ec0..67e1851 100644 --- a/Modules/CustomRoleManagement.cs +++ b/Modules/CustomRoleManagement.cs @@ -14,50 +14,47 @@ public static class CustomRoleManagement { // crewmateRoles = assigned with crewmate role base // impostorRoles = assigned with impostor role base - List<(string roleName, int percentage)> crewmateRoles = new List<(string, int)> + List<(string roleName, int percentage)> crewmateRoles = new() { ("Jester", Options.JesterPerc.GetInt()), ("Mayor", Options.MayorPerc.GetInt()) }; - List<(string roleName, int percentage)> impostorRoles = new List<(string, int)> + List<(string roleName, int percentage)> impostorRoles = new() { }; - List availablePlayers = new List(); - foreach (var player in PlayerControl.AllPlayerControls) - { - availablePlayers.Add(player); - } - + List availablePlayers = new(); + foreach (var player in PlayerControl.AllPlayerControls) availablePlayers.Add(player); availablePlayers = availablePlayers.OrderBy(x => random.Next()).ToList(); PlayerRoles.Clear(); + HashSet assignedRoles = new HashSet(); + HashSet attemptedRoles = new HashSet(); foreach (var player in availablePlayers) { bool isCrewmate = !player.Data.Role.IsImpostor; - - List<(string roleName, int percentage)> rolesToAssign = isCrewmate ? crewmateRoles : impostorRoles; + var rolesToAssign = isCrewmate ? crewmateRoles : impostorRoles; foreach (var (roleName, percentage) in rolesToAssign) { - if (assignedRoles.Contains(roleName)) - continue; + if (attemptedRoles.Contains(roleName)) continue; + + attemptedRoles.Add(roleName); int randomValue = random.Next(0, 101); - Logger.Info($"{roleName}, Value: {randomValue}, Percentage: {percentage}", "StartGameCustomRole1"); - if (randomValue <= percentage && !PlayerRoles.ContainsKey(player.PlayerId)) - { - PlayerRoles[player.PlayerId] = roleName; - assignedRoles.Add(roleName); - Logger.Info($"({player.PlayerId}) {player.Data.PlayerName} -> {roleName}", "StartGameCustomRole2"); + if (randomValue >= percentage) continue; + if (PlayerRoles.ContainsKey(player.PlayerId)) continue; - break; - } + PlayerRoles[player.PlayerId] = roleName; + assignedRoles.Add(roleName); + + Logger.Info($"({player.PlayerId}) {player.Data.PlayerName} -> {roleName}", "StartGameCustomRole2"); + break; } } } @@ -90,6 +87,8 @@ public static class CustomRoleManagement return lines.Count > 0 ? string.Join("\n", lines) : string.Empty; } + public static bool HandlingRoleMessages = false; + private static int PendingRoleMessages = 0; public static void SendRoleMessages(Dictionary roleMessages) { HashSet sentRoles = new HashSet(); @@ -97,19 +96,68 @@ public static class CustomRoleManagement var players = PlayerControl.AllPlayerControls.ToArray().ToList(); float delay = 0f; + PendingRoleMessages = 0; + HandlingRoleMessages = false; foreach (var player in players) { if (!PlayerRoles.TryGetValue(player.PlayerId, out string role)) continue; if (!roleMessages.ContainsKey(role)) continue; if (sentRoles.Contains(role)) continue; + PendingRoleMessages++; new LateTask(() => { Utils.SendPrivateMessage(player, roleMessages[role]); + PendingRoleMessages--; + + if (PendingRoleMessages <= 0) + { + PendingRoleMessages = 0; + HandlingRoleMessages = false; + } }, delay, "SendRoleMessage"); sentRoles.Add(role); delay += 2.2f; } } + + public static string PlayerToCustomRole() + { + if (CustomRoleManagement.PlayerRoles.Count == 0) return string.Empty; + + Dictionary> roleToPlayers = new Dictionary>(); + + foreach (var kvp in CustomRoleManagement.PlayerRoles) + { + byte playerId = kvp.Key; + string role = kvp.Value; + + PlayerControl pc = null; + foreach (var p in PlayerControl.AllPlayerControls) + { + if (p.PlayerId == playerId) + { + pc = p; + break; + } + } + + if (pc == null) continue; + + if (!roleToPlayers.ContainsKey(role)) roleToPlayers[role] = new List(); + roleToPlayers[role].Add(pc.Data.PlayerName); + } + + if (roleToPlayers.Count == 0) return string.Empty; + + var lines = new List(); + + foreach (var entry in roleToPlayers) + { + string players = string.Join(", ", entry.Value); + lines.Add($"{entry.Key}: {players}"); + } + return string.Join("\n", lines); + } } \ No newline at end of file diff --git a/Patches/ChatPatches.cs b/Patches/ChatPatches.cs index 31e1f08..e882899 100644 --- a/Patches/ChatPatches.cs +++ b/Patches/ChatPatches.cs @@ -104,7 +104,7 @@ internal static class SendChatPatch return false; } - if (__instance.timeSinceLastMessage < 3f || OnGameJoinedPatch.WaitingForChat) return false; + if (__instance.timeSinceLastMessage < 3f || OnGameJoinedPatch.WaitingForChat || CustomRoleManagement.HandlingRoleMessages) return false; if (text == "/l" || text == "/lastgame" || text == "/win" || text == "/winner") { @@ -142,7 +142,7 @@ internal static class SendChatPatch switch (Options.Gamemode.GetValue()) { case 0: - Utils.ChatCommand(__instance, $"ENABLED CUSTOM ROLES:\n\n{CustomRoleManagement.GetActiveRoles()}", "", false); + Utils.ChatCommand(__instance, $"Enabled Custom Roles:\n\n{CustomRoleManagement.GetActiveRoles()}", "", false); break; case 1: @@ -266,7 +266,7 @@ public static class RPCHandlerPatch { c = keywords.Any(k => text == k); } - if (c && !Utils.IsPlayerModerator(__instance.Data.FriendCode)) + if (c && !Utils.IsPlayerModerator(__instance.Data.FriendCode) && Options.AutoKickStart.GetBool()) { int clientId = __instance.Data.ClientId; @@ -372,7 +372,7 @@ public static class RPCHandlerPatch { if (!Utils.IsPlayerModerator(__instance.Data.FriendCode)) return; - // Banning works by name and color. Commands are seperated incase someone has a color as their name + // Banning works by name and color. Commands are separated incase someone has a color as their name bool isKick = text.StartsWith("/kick "); bool isBan = text.StartsWith("/ban "); diff --git a/Patches/EndGamePatches.cs b/Patches/EndGamePatches.cs index e4f1429..7d3631a 100644 --- a/Patches/EndGamePatches.cs +++ b/Patches/EndGamePatches.cs @@ -39,21 +39,24 @@ class NormalGameEndChecker ImpCheckComplete = true; } + var customRoles = CustomRoleManagement.PlayerToCustomRole(); + var impostorList = string.Join(", ", imps.Select(p => p.Data.PlayerName)); + if (Utils.AliveImpostors == 0) { - LastWinReason = $"★ Crewmates win!\n\nImpostors:\n" + string.Join("\n", imps.Select(p => p.Data.PlayerName)); + LastWinReason = $"Crewmates win!\n\nImpostors: {impostorList}" + (string.IsNullOrEmpty(customRoles) ? "" : "\n\n" + customRoles); } else if (Utils.AliveImpostors >= Utils.AliveCrewmates) { - LastWinReason = $"★ Impostors win!\n\nImpostors:\n★" + string.Join("\n★", imps.Select(p => p.Data.PlayerName)); + LastWinReason = $"Impostors win!\n\nImpostor: {impostorList}" + (string.IsNullOrEmpty(customRoles) ? "" : "\n\n" + customRoles); } else if (GameData.Instance != null && GameData.Instance.TotalTasks > 0 && GameData.Instance.CompletedTasks >= GameData.Instance.TotalTasks) { - LastWinReason = $"★ Crewmates win! (Tasks)\n\nImpostors:\n" + string.Join("\n", imps.Select(p => p.Data.PlayerName)); + LastWinReason = $"Crewmates win! (Tasks)\n\nImpostors: {impostorList}" + (string.IsNullOrEmpty(customRoles) ? "" : "\n\n" + customRoles); } else if (Options.Gamemode.GetValue() < 2) { - LastWinReason = $"★ Impostors win! (Sabotage)\n\nImpostors:\n★" + string.Join("\n★", imps.Select(p => p.Data.PlayerName)); + LastWinReason = $"Impostors win! (Sabotage)\n\nImpostors: {impostorList}" + (string.IsNullOrEmpty(customRoles) ? "" : "\n\n" + customRoles); } return true; } diff --git a/Patches/ExilePatch.cs b/Patches/ExilePatch.cs new file mode 100644 index 0000000..9b8783a --- /dev/null +++ b/Patches/ExilePatch.cs @@ -0,0 +1,35 @@ +namespace AmongUsRevamped; + +class ExileControllerWrapUpPatch +{ + [HarmonyPatch(typeof(ExileController), nameof(ExileController.WrapUp))] + class ExileControllerPatch + { + public static void Postfix(ExileController __instance) + { + if (!AmongUsClient.Instance.AmHost) return; + + var ejectedPlayer = __instance.initData?.networkedPlayer; + if (ejectedPlayer == null) return; + + PlayerControl pc = null; + foreach (var p in PlayerControl.AllPlayerControls) + { + if (p.PlayerId == ejectedPlayer.PlayerId) + { + pc = p; + break; + } + } + + Logger.Info($" {ejectedPlayer.PlayerName} was ejected", "ExileController"); + + if (!CustomRoleManagement.PlayerRoles.TryGetValue(ejectedPlayer.PlayerId, out var role)) return; + + if (role == "Jester") + { + Utils.CustomWinnerEndGame(pc, 1); + } + } + } +} \ No newline at end of file