mirror of
https://github.com/All-Of-Us-Mods/MiraAPI.git
synced 2026-10-19 15:51:48 +00:00
add ProcessVotesEvent.cs and remove "weighting"
This commit is contained in:
1 parent
a04e0d28db
commit
3cba870980
7 files changed
+88
-17
No files matched your search
@@ -17,7 +17,7 @@ public class HandleVoteEvent : MiraCancelableEvent
|
||||
/// <summary>
|
||||
/// Gets the player who voted.
|
||||
/// </summary>
|
||||
public PlayerControl? Player { get; }
|
||||
public PlayerControl Player { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the player id of the target.
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using MiraAPI.Voting;
|
||||
|
||||
namespace MiraAPI.Events.Vanilla.Meeting.Voting;
|
||||
|
||||
/// <summary>
|
||||
/// Ran after calculating votes and before displaying the results. Only ran on the host.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Initializes a new instance of the <see cref="ProcessVotesEvent"/> class.
|
||||
/// </remarks>
|
||||
/// <param name="votes">>The list of votes that are being processed.</param>
|
||||
/// <param name="exiledPlayer">The player to be exiled. Will be null if no player is to be exiled.</param>
|
||||
public class ProcessVotesEvent(List<CustomVote> votes, NetworkedPlayerInfo? exiledPlayer = null) : MiraEvent
|
||||
{
|
||||
private readonly List<CustomVote> _originalVotes = [.. votes];
|
||||
|
||||
private NetworkedPlayerInfo? _exiledPlayer = exiledPlayer;
|
||||
|
||||
/// <summary>
|
||||
/// Gets a value indicating whether the exiled player has been modified by the event.
|
||||
/// </summary>
|
||||
public bool ExiledPlayerModified { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets a value indicating whether the votes have been modified by the event.
|
||||
/// </summary>
|
||||
public bool VotesModified => !Votes.SequenceEqual(_originalVotes);
|
||||
|
||||
/// <summary>
|
||||
/// Gets the list of votes that are being processed.
|
||||
/// </summary>
|
||||
public List<CustomVote> Votes { get; } = votes;
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the player to be exiled. Will be null if no player is to be exiled.
|
||||
/// </summary>
|
||||
public NetworkedPlayerInfo? ExiledPlayer
|
||||
{
|
||||
get => _exiledPlayer;
|
||||
set
|
||||
{
|
||||
_exiledPlayer = value;
|
||||
ExiledPlayerModified = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -173,23 +173,29 @@ internal static class MeetingHudPatches
|
||||
[HarmonyPatch(nameof(MeetingHud.CheckForEndVoting))]
|
||||
public static bool EndCheck(MeetingHud __instance)
|
||||
{
|
||||
if (Helpers.GetAlivePlayers().Exists(plr => plr.GetVoteData()!.VotesRemaining > 0))
|
||||
if (Helpers.GetAlivePlayers().Exists(plr => plr.GetVoteData().VotesRemaining > 0))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
var votes = VotingUtils.CalculateVotes();
|
||||
var exiled = VotingUtils.GetExiled(votes, out var isTie);
|
||||
|
||||
var max = VotingUtils.CalculateNumVotes(votes).MaxPair(out var isTie);
|
||||
var exiled = GameData.Instance.AllPlayers.ToArray().FirstOrDefault(v => !isTie && v.PlayerId == max.Key);
|
||||
var @event = new ProcessVotesEvent(votes, exiled);
|
||||
MiraEventManager.InvokeEvent(@event);
|
||||
|
||||
if (exiled is null || exiled.IsDead || exiled.Disconnected)
|
||||
if (@event.VotesModified)
|
||||
{
|
||||
exiled = null;
|
||||
votes = @event.Votes;
|
||||
exiled = VotingUtils.GetExiled(votes, out isTie);
|
||||
}
|
||||
|
||||
if (@event.ExiledPlayerModified)
|
||||
{
|
||||
exiled = @event.ExiledPlayer;
|
||||
}
|
||||
|
||||
__instance.RpcVotingComplete(new MeetingHud.VoterState[__instance.playerStates.Length], exiled, isTie);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@@ -5,5 +5,4 @@
|
||||
/// </summary>
|
||||
/// <param name="Voter">The player that voted.</param>
|
||||
/// <param name="Suspect">The suspect being voted for.</param>
|
||||
/// <param name="Weight">The weight of the vote for calculations.</param>
|
||||
public record struct CustomVote(byte Voter, byte Suspect, float Weight = 1f);
|
||||
public record struct CustomVote(byte Voter, byte Suspect);
|
||||
@@ -56,10 +56,9 @@ public class PlayerVoteData(nint cppPtr) : MonoBehaviour(cppPtr)
|
||||
/// 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)
|
||||
public void VoteForPlayer(byte playerId)
|
||||
{
|
||||
Votes.Add(new CustomVote(Owner.PlayerId, playerId, weight));
|
||||
Votes.Add(new CustomVote(Owner.PlayerId, playerId));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -28,7 +28,6 @@ public class PopulateResultsRpc(MiraApiPlugin plugin, uint id) : PlayerCustomRpc
|
||||
{
|
||||
writer.Write(t.Voter);
|
||||
writer.Write(t.Suspect);
|
||||
writer.Write(t.Weight);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -39,7 +38,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(), reader.ReadSingle());
|
||||
votes[i] = new CustomVote(reader.ReadByte(), reader.ReadByte());
|
||||
}
|
||||
|
||||
return votes;
|
||||
|
||||
@@ -16,6 +16,26 @@ public static class VotingUtils
|
||||
{
|
||||
private const byte SkipVoteId = 253;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the exiled player from the list of votes. Returns null if no player is to be exiled.
|
||||
/// </summary>
|
||||
/// <param name="votes">>The list of votes to check.</param>
|
||||
/// <param name="isTie">Whether the vote is a tie.</param>
|
||||
/// <returns>The player to be exiled. Will be null if no player is to be exiled.</returns>
|
||||
public static NetworkedPlayerInfo? GetExiled(List<CustomVote> votes, out bool isTie)
|
||||
{
|
||||
var max = CalculateNumVotes(votes).MaxPair(out var tie);
|
||||
isTie = tie;
|
||||
var exiled = GameData.Instance.AllPlayers.ToArray().FirstOrDefault(v => !tie && v.PlayerId == max.Key);
|
||||
|
||||
if (exiled is null || exiled.IsDead || exiled.Disconnected)
|
||||
{
|
||||
exiled = null;
|
||||
}
|
||||
|
||||
return exiled;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Handles when a vote is added and allows for other mods to override/modify.
|
||||
/// </summary>
|
||||
@@ -83,11 +103,11 @@ public static class VotingUtils
|
||||
{
|
||||
var dictionary = new Dictionary<byte, float>();
|
||||
|
||||
foreach (var vote in votes)
|
||||
foreach (var vote in votes.Select(v=>v.Suspect))
|
||||
{
|
||||
if (!dictionary.TryAdd(vote.Suspect, vote.Weight))
|
||||
if (!dictionary.TryAdd(vote, 1))
|
||||
{
|
||||
dictionary[vote.Suspect] += vote.Weight;
|
||||
dictionary[vote] += 1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user