voting API improvements

This commit is contained in:
XtraCube committed 2025-03-10 22:29:33 -04:00
1 parent bad6648f44
commit 0b9e54eb50
9 files changed
+70 -46

No files matched your search

+4 -5
View File
@@ -1,5 +1,4 @@
using System.Linq;
using HarmonyLib;
using MiraAPI.Events;
using MiraAPI.Events.Mira;
using MiraAPI.Events.Vanilla.Gameplay;
@@ -43,7 +42,7 @@ public static class ExampleEventHandlers
{
if (plr.Data.Role is MayorRole)
{
plr.GetVoteData().AddVotes(1);
plr.GetVoteData().IncreaseRemainingVotes(1);
}
}
}
@@ -52,9 +51,9 @@ public static class ExampleEventHandlers
[RegisterEvent(15)]
public static void HandleVoteEvent(HandleVoteEvent @event)
{
if (@event.VoteData.Owner?.Data.Role is not NeutralKillerRole) return;
if (@event.VoteData.Owner.Data.Role is not NeutralKillerRole) return;
@event.VoteData.SetVotesRemaining(0);
@event.VoteData.SetRemainingVotes(0);
for (var i = 0; i < 5; i++)
{
@@ -63,7 +62,7 @@ public static class ExampleEventHandlers
foreach (var plr in PlayerControl.AllPlayerControls.ToArray().Where(player => player != @event.VoteData.Owner))
{
plr.GetVoteData().VotedPlayers.Clear();
plr.GetVoteData().Votes.Clear();
plr.GetVoteData().VotesRemaining = 0;
}
@@ -1,5 +1,4 @@
using MiraAPI.Utilities;
using MiraAPI.Voting;
using MiraAPI.Voting;
namespace MiraAPI.Events.Vanilla.Meeting.Voting;
@@ -1,5 +1,4 @@
using MiraAPI.Utilities;
using MiraAPI.Voting;
using MiraAPI.Voting;
namespace MiraAPI.Events.Vanilla.Meeting.Voting;
+8
View File
@@ -39,6 +39,14 @@ public enum MiraRpc : uint
/// Custom Game Over RPC.
/// </summary>
CustomGameOver,
/// <summary>
/// Custom RPC to handle populating vote results.
/// </summary>
PopulateResults,
/// <summary>
/// Custom RPC to undo a player's vote.
/// </summary>
RemoveVote,
}
-1
View File
@@ -6,7 +6,6 @@ using MiraAPI.Events.Vanilla.Player;
using MiraAPI.Hud;
using MiraAPI.Modifiers;
using MiraAPI.Roles;
using MiraAPI.Utilities;
using MiraAPI.Voting;
using Reactor.Utilities.Extensions;
+11 -12
View File
@@ -10,7 +10,7 @@ using Reactor.Networking.Rpc;
namespace MiraAPI.Patches.Voting;
[HarmonyPatch(typeof(MeetingHud))]
public static class MeetingHudPatches
internal static class MeetingHudPatches
{
[HarmonyPostfix]
[HarmonyPatch(nameof(MeetingHud.Start))]
@@ -19,12 +19,12 @@ public static class MeetingHudPatches
foreach (var plr in PlayerControl.AllPlayerControls)
{
var voteData = plr.GetVoteData();
voteData.VotedPlayers.Clear();
voteData.SetVotesRemaining(1);
voteData.Votes.Clear();
voteData.SetRemainingVotes(1);
if (plr.Data.IsDead || plr.Data.Disconnected)
{
voteData.SetVotesRemaining(0);
voteData.SetRemainingVotes(0);
}
}
var @event = new StartMeetingEvent(__instance);
@@ -46,7 +46,7 @@ public static class MeetingHudPatches
}
var hasVotes = voteData.VotesRemaining > 0;
var hasVotedFor = voteData.VotedPlayers.Contains((byte)suspectStateIdx);
var hasVotedFor = voteData.VotedFor((byte)suspectStateIdx);
return hasVotes && !hasVotedFor;
}
@@ -69,12 +69,12 @@ public static class MeetingHudPatches
var pva = __instance.playerStates.First(pv => pv.TargetPlayerId == player.PlayerId);
var voteData = player.GetVoteData();
if (pva.AmDead || !voteData.VotedPlayers.Contains(pc.PlayerId))
if (pva.AmDead || !voteData.VotedFor(pc.PlayerId))
{
continue;
}
voteData.VotedPlayers.Remove(pc.PlayerId);
voteData.Votes.RemoveAll(x=>x.Suspect==pc.PlayerId);
voteData.VotesRemaining += 1;
VotingUtils.RpcRemoveVote(PlayerControl.LocalPlayer, player.PlayerId, pc.PlayerId);
@@ -104,7 +104,7 @@ public static class MeetingHudPatches
var voteData = plr.Object.GetVoteData();
if (voteData == null || voteData.VotesRemaining == 0 ||
voteData.VotedPlayers.Contains(suspectPlayerId))
voteData.VotedFor(suspectPlayerId))
{
return false;
}
@@ -135,7 +135,7 @@ public static class MeetingHudPatches
if (!AmongUsClient.Instance.AmHost)
{
if (voteData == null || voteData.VotesRemaining == 0 ||
voteData.VotedPlayers.Contains(suspectIdx))
voteData.VotedFor(suspectIdx))
{
return;
}
@@ -173,7 +173,7 @@ public static class MeetingHudPatches
[HarmonyPatch(nameof(MeetingHud.CheckForEndVoting))]
public static bool EndCheck(MeetingHud __instance)
{
if (Helpers.GetAlivePlayers().Any(plr => plr.GetVoteData()!.VotesRemaining > 0))
if (Helpers.GetAlivePlayers().Exists(plr => plr.GetVoteData()!.VotesRemaining > 0))
{
return false;
}
@@ -193,7 +193,6 @@ public static class MeetingHudPatches
return false;
}
[HarmonyPrefix]
[HarmonyPatch(nameof(MeetingHud.PopulateResults))]
public static bool PopulateResultsPatch(MeetingHud __instance)
@@ -204,7 +203,7 @@ public static class MeetingHudPatches
}
var votes = VotingUtils.CalculateVotes();
Rpc<PopulateResultsRpc>.Instance.Send(votes.ToArray());
Rpc<PopulateResultsRpc>.Instance.Send([.. votes]);
return false;
}
+34 -15
View File
@@ -9,17 +9,17 @@ namespace MiraAPI.Voting;
/// Handles player votes, and removing/adding additional votes.
/// </summary>
[RegisterInIl2Cpp]
public class PlayerVoteData(nint ptr) : MonoBehaviour(ptr)
public class PlayerVoteData(nint cppPtr) : MonoBehaviour(cppPtr)
{
/// <summary>
/// Gets the owner of this component.
/// </summary>
public PlayerControl? Owner { get; private set; }
public PlayerControl Owner { get; private set; } = null!;
/// <summary>
/// Gets the players which the owner has voted for.
/// Gets the list of votes the owner has.
/// </summary>
public List<byte> VotedPlayers { get; private set; } = [];
public List<CustomVote> Votes { get; private set; } = [];
/// <summary>
/// Gets or sets the amount of votes the owner has left.
@@ -43,29 +43,48 @@ public class PlayerVoteData(nint ptr) : MonoBehaviour(ptr)
}
/// <summary>
/// Adds the voted player to the list.
/// Returns whether the owner has voted for the specified player.
/// </summary>
/// <param name="playerId">The target's playerId.</param>
public void VoteForPlayer(byte playerId)
/// <returns>True if the owner voted for the specified player, false otherwise.</returns>
public bool VotedFor(byte playerId)
{
VotedPlayers.Add(playerId);
return Votes.Exists(x => x.Suspect == playerId);
}
/// <summary>
/// Removes the voted player from the list.
/// Adds the voted player to the list.
/// </summary>
/// <param name="playerId">The target's playerId.</param>
/// <param name="weight">The weight of the vote.</param>
public void VoteForPlayer(byte playerId, float weight=1f)
{
Votes.Add(new CustomVote(Owner.PlayerId, playerId, weight));
}
/// <summary>
/// Removes the specified vote.
/// </summary>
/// <param name="vote">The vote you would like to remove.</param>
public void RemovePlayerVote(CustomVote vote)
{
Votes.Remove(vote);
}
/// <summary>
/// Removes a single vote from the owner.
/// </summary>
/// <param name="playerId">The target's playerId.</param>
public void RemovePlayerVote(byte playerId)
{
if (!VotedPlayers.Contains(playerId)) return;
VotedPlayers.Remove(playerId);
Votes.Remove(Votes.Find(x=>x.Suspect==playerId));
}
/// <summary>
/// Sets the player's votes remaining.
/// </summary>
/// <param name="votesRemaining">The amount of votes you would like to set it to.</param>
public void SetVotesRemaining(int votesRemaining)
public void SetRemainingVotes(int votesRemaining)
{
VotesRemaining = votesRemaining;
}
@@ -74,17 +93,17 @@ public class PlayerVoteData(nint ptr) : MonoBehaviour(ptr)
/// Adds votes to the owner.
/// </summary>
/// <param name="amount">The amount of votes you would like to add.</param>
public void AddVotes(int amount)
public void IncreaseRemainingVotes(int amount)
{
SetVotesRemaining(VotesRemaining + amount);
SetRemainingVotes(VotesRemaining + amount);
}
/// <summary>
/// Removes votes from the owner.
/// </summary>
/// <param name="amount">The amount of votes you would like to remove.</param>
public void RemoveVotes(int amount)
public void DecreaseRemainingVotes(int amount)
{
SetVotesRemaining(VotesRemaining - amount);
SetRemainingVotes(VotesRemaining - amount);
}
}
+3 -3
View File
@@ -1,5 +1,4 @@
using System.Linq;
using Hazel;
using Hazel;
using MiraAPI.Networking;
using Reactor.Networking.Attributes;
using Reactor.Networking.Rpc;
@@ -29,6 +28,7 @@ public class PopulateResultsRpc(MiraApiPlugin plugin, uint id) : PlayerCustomRpc
{
writer.Write(t.Voter);
writer.Write(t.Suspect);
writer.Write(t.Weight);
}
}
@@ -39,7 +39,7 @@ public class PopulateResultsRpc(MiraApiPlugin plugin, uint id) : PlayerCustomRpc
for (var i = 0; i < votes.Length; i++)
{
votes[i] = new CustomVote(reader.ReadByte(), reader.ReadByte());
votes[i] = new CustomVote(reader.ReadByte(), reader.ReadByte(), reader.ReadSingle());
}
return votes;
+8 -6
View File
@@ -23,7 +23,7 @@ public static class VotingUtils
/// <param name="suspectIdx">Who the player voted for.</param>
public static void HandleVote(PlayerVoteData voteData, byte suspectIdx)
{
HandleVoteEvent @event = new HandleVoteEvent(voteData, suspectIdx);
var @event = new HandleVoteEvent(voteData, suspectIdx);
MiraEventManager.InvokeEvent(@event);
if (@event.IsCancelled)
@@ -31,7 +31,7 @@ public static class VotingUtils
return;
}
voteData.RemoveVotes(1);
voteData.DecreaseRemainingVotes(1);
voteData.VoteForPlayer(suspectIdx);
}
@@ -61,7 +61,7 @@ public static class VotingUtils
if (!AmongUsClient.Instance.AmHost)
{
var voteData = PlayerControl.LocalPlayer.GetVoteData();
voteData.RemoveVotes(1);
voteData.DecreaseRemainingVotes(1);
voteData.RemovePlayerVote(votedFor);
}
@@ -100,9 +100,11 @@ public static class VotingUtils
/// <returns>The list of votes.</returns>
public static List<CustomVote> CalculateVotes()
{
return (from player in Helpers.GetAlivePlayers()
from vote in player.GetVoteData()!.VotedPlayers
select new CustomVote(player.PlayerId, vote)).ToList();
return
[
.. Helpers.GetAlivePlayers()
.SelectMany(player => player.GetVoteData().Votes)
];
}
/// <summary>