mirror of
https://github.com/All-Of-Us-Mods/MiraAPI.git
synced 2026-10-19 15:51:48 +00:00
Fix reimplemented Judge for good
This commit is contained in:
1 parent
f96f75cea1
commit
6b774500dd
6 files changed
+119
-5
No files matched your search
@@ -34,6 +34,11 @@ public class HandleVoteEvent : MiraCancelableEvent
|
||||
/// </summary>
|
||||
public NetworkedPlayerInfo TargetPlayerInfo { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets a value indicating whether the vote event is an Overrule caused by a Judge.
|
||||
/// </summary>
|
||||
public bool IsOverruling { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="HandleVoteEvent"/> class.
|
||||
/// </summary>
|
||||
@@ -47,4 +52,20 @@ public class HandleVoteEvent : MiraCancelableEvent
|
||||
PreventVote = false;
|
||||
TargetPlayerInfo = GameData.Instance.GetPlayerById(targetId);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="HandleVoteEvent"/> class.
|
||||
/// </summary>
|
||||
/// <param name="playerVoteData">The voter's <see cref="PlayerVoteData"/>.</param>
|
||||
/// <param name="targetId">The target's playerId.</param>
|
||||
/// <param name="isOverruling">Whether the vote is actually for a Judge.</param>
|
||||
public HandleVoteEvent(PlayerVoteData playerVoteData, byte targetId, bool isOverruling)
|
||||
{
|
||||
VoteData = playerVoteData;
|
||||
Player = playerVoteData.Owner;
|
||||
TargetId = targetId;
|
||||
PreventVote = false;
|
||||
IsOverruling = isOverruling;
|
||||
TargetPlayerInfo = GameData.Instance.GetPlayerById(targetId);
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,7 @@ using BepInEx;
|
||||
using BepInEx.Unity.IL2CPP;
|
||||
using HarmonyLib;
|
||||
using MiraAPI.PluginLoading;
|
||||
using MiraAPI.VanillaEvents;
|
||||
using Reactor;
|
||||
using Reactor.Networking;
|
||||
using Reactor.Networking.Attributes;
|
||||
@@ -49,6 +50,7 @@ public partial class MiraApiPlugin : BasePlugin
|
||||
public override void Load()
|
||||
{
|
||||
Harmony.PatchAll();
|
||||
JudgeEvents.Initialize();
|
||||
|
||||
ReactorCredits.Register("Mira API", Version, IsDevBuild, ReactorCredits.AlwaysShow);
|
||||
|
||||
|
||||
@@ -74,4 +74,9 @@ public enum MiraRpc : uint
|
||||
/// Custom RPC to replace Among Us CastVote.
|
||||
/// </summary>
|
||||
CastVote,
|
||||
|
||||
/// <summary>
|
||||
/// Custom RPC to replace Among Us QueueOverruleVotes.
|
||||
/// </summary>
|
||||
QueueOverruleVotes,
|
||||
}
|
||||
@@ -2,6 +2,7 @@
|
||||
using System.Linq;
|
||||
using HarmonyLib;
|
||||
using Il2CppInterop.Runtime.InteropTypes.Arrays;
|
||||
using InnerNet;
|
||||
using MiraAPI.Events;
|
||||
using MiraAPI.Events.Vanilla.Meeting;
|
||||
using MiraAPI.Events.Vanilla.Meeting.Voting;
|
||||
@@ -223,6 +224,16 @@ internal static class MeetingHudPatches
|
||||
return false;
|
||||
}
|
||||
|
||||
// TODO: figure out a way to do host-authorization since right now any player can send RpcQueueOverruleVotes
|
||||
[HarmonyPrefix]
|
||||
[HarmonyPatch(nameof(MeetingHud.CmdQueueOverruleVotes))]
|
||||
// Although this method is inlined in MeetingHud.Confirm, the next patch fixes that.
|
||||
public static bool CmdQueueOverruleVotesPatch(MeetingHud __instance, PlayerId judgePlayerId, PlayerId targetPlayerId, ushort overruleNonce)
|
||||
{
|
||||
VotingUtils.RpcQueueOverruleVotes(PlayerControl.LocalPlayer, judgePlayerId.Value, targetPlayerId.Value, overruleNonce);
|
||||
return false;
|
||||
}
|
||||
|
||||
[HarmonyPrefix]
|
||||
[HarmonyPatch(nameof(MeetingHud.Confirm))]
|
||||
public static bool ConfirmPatch(MeetingHud __instance, [HarmonyArgument(0)] byte suspect)
|
||||
|
||||
@@ -6,11 +6,17 @@ namespace MiraAPI.VanillaEvents;
|
||||
|
||||
public static class JudgeEvents
|
||||
{
|
||||
[RegisterEvent(100000)]
|
||||
public static void Initialize()
|
||||
{
|
||||
// This is required because MiraAPI isn't an IMiraPlugin (besides in the gamemodes branch)
|
||||
MiraEventManager.RegisterEventHandler<ProcessVotesEvent>(@event => ProcessVotesEventHandler(@event), -1000);
|
||||
}
|
||||
|
||||
public static void ProcessVotesEventHandler(ProcessVotesEvent @event)
|
||||
{
|
||||
if (MeetingHud.Instance.TryGetWinningOverrule(out var judgeOverrule, out var networkedPlayerInfo, out var networkedPlayerInfo2))
|
||||
{
|
||||
Error("Judge has overruled votes!");
|
||||
@event.OverruledVote = true;
|
||||
@event.OverruledNonce = judgeOverrule.OverruleNonce;
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using InnerNet;
|
||||
using MiraAPI.Events;
|
||||
using MiraAPI.Events.Vanilla.Meeting.Voting;
|
||||
using MiraAPI.Networking;
|
||||
@@ -41,9 +42,9 @@ public static class VotingUtils
|
||||
/// <param name="voteData">The player's <see cref="PlayerVoteData"/>.</param>
|
||||
/// <param name="suspectIdx">Who the player voted for.</param>
|
||||
/// <param name="cancelVote">Whether you want the vote to commence or not.</param>
|
||||
private static void HandleVote(PlayerVoteData voteData, byte suspectIdx, out bool cancelVote)
|
||||
private static void HandleVote(PlayerVoteData voteData, byte suspectIdx, bool byJudge, out bool cancelVote)
|
||||
{
|
||||
var @event = new HandleVoteEvent(voteData, suspectIdx);
|
||||
var @event = new HandleVoteEvent(voteData, suspectIdx, byJudge);
|
||||
MiraEventManager.InvokeEvent(@event);
|
||||
|
||||
cancelVote = @event.PreventVote;
|
||||
@@ -60,7 +61,14 @@ public static class VotingUtils
|
||||
return;
|
||||
}
|
||||
|
||||
voteData.DecreaseRemainingVotes(1);
|
||||
if (byJudge)
|
||||
{
|
||||
voteData.SetRemainingVotes(0);
|
||||
}
|
||||
else
|
||||
{
|
||||
voteData.DecreaseRemainingVotes(1);
|
||||
}
|
||||
voteData.VoteForPlayer(suspectIdx);
|
||||
}
|
||||
|
||||
@@ -100,6 +108,67 @@ public static class VotingUtils
|
||||
MeetingHud.Instance.SkipVoteButton.gameObject.SetActive(true);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Networks the casting of a vote. We replace the vanilla solution with a custom version that works for Judge specifically.
|
||||
/// </summary>
|
||||
/// <param name="source">The <see cref="PlayerControl"/> who sent this RPC.</param>
|
||||
/// <param name="srcPlayerId">The id of the player who casted the vote.</param>
|
||||
/// <param name="suspectPlayerId">The voted player's id.</param>
|
||||
/// <param name="overruleNonce">Data that is checked by the Judge role to determine which Judge takes priority.</param>
|
||||
[MethodRpc((uint)MiraRpc.QueueOverruleVotes)]
|
||||
public static void RpcQueueOverruleVotes(PlayerControl source, byte srcPlayerId, byte suspectPlayerId, ushort overruleNonce)
|
||||
{
|
||||
CustomCastJudgeVote(srcPlayerId, suspectPlayerId, overruleNonce);
|
||||
}
|
||||
|
||||
private static void CustomCastJudgeVote(PlayerId judgePlayerId, PlayerId targetPlayerId, ushort overruleNonce)
|
||||
{
|
||||
var plr = GameData.Instance.GetPlayerById(judgePlayerId.Value);
|
||||
if (!plr) return;
|
||||
|
||||
var voteData = plr.Object.GetVoteData();
|
||||
if (!voteData) return;
|
||||
|
||||
HandleVote(voteData, targetPlayerId.Value, true, out var cancelVote);
|
||||
|
||||
// Handle local behaviour for the voter (for some reason checking AmOwner does not work for host)
|
||||
if (PlayerControl.LocalPlayer.PlayerId == judgePlayerId.Value)
|
||||
{
|
||||
if (!cancelVote) SoundManager.Instance.PlaySound(MeetingHud.Instance.VoteLockinSound, false);
|
||||
|
||||
foreach (var playerVoteArea in MeetingHud.Instance.playerStates)
|
||||
{
|
||||
playerVoteArea.ClearButtons();
|
||||
}
|
||||
|
||||
MeetingHud.Instance.SkipVoteButton.ClearButtons();
|
||||
|
||||
var localVoteData = PlayerControl.LocalPlayer.GetVoteData();
|
||||
if (!localVoteData || localVoteData.VotesRemaining != 0) return;
|
||||
|
||||
MeetingHud.Instance.SkipVoteButton.VoteComplete = true;
|
||||
MeetingHud.Instance.SkipVoteButton.gameObject.SetActive(false);
|
||||
}
|
||||
|
||||
if (cancelVote) return;
|
||||
|
||||
// If player has no more votes, then make it show that the player has used all their votes.
|
||||
if (voteData.VotesRemaining == 0)
|
||||
{
|
||||
MeetingHud.Instance.AddOrUpdateJudgeOverrule(judgePlayerId, targetPlayerId, overruleNonce);
|
||||
MeetingHud.Instance.playerStates.First(x => x.PlayerId == judgePlayerId.Value).SetVote(targetPlayerId.Value);
|
||||
}
|
||||
|
||||
// If host, then check end voting, and set votes/send chat if applicable
|
||||
if (!PlayerControl.LocalPlayer.IsHost()) return;
|
||||
|
||||
MeetingHud.Instance.SetDirtyBit(1U);
|
||||
MeetingHud.Instance.CheckForEndVoting();
|
||||
|
||||
if (voteData.VotesRemaining != 0) return;
|
||||
PlayerControl.LocalPlayer.RpcSendChatNote(judgePlayerId.Value, ChatNoteTypes.DidVote);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Networks the casting of a vote. We replace the vanilla solution with a custom version that works for the use case.
|
||||
/// </summary>
|
||||
@@ -120,7 +189,7 @@ public static class VotingUtils
|
||||
var voteData = plr.Object.GetVoteData();
|
||||
if (!voteData) return;
|
||||
|
||||
HandleVote(voteData, suspectPlayerId, out var cancelVote);
|
||||
HandleVote(voteData, suspectPlayerId, false, out var cancelVote);
|
||||
|
||||
// Handle local behaviour for the voter (for some reason checking AmOwner does not work for host)
|
||||
if (PlayerControl.LocalPlayer.PlayerId == srcPlayerId)
|
||||
|
||||
Reference in new issue
Block a user