mirror of
https://github.com/All-Of-Us-Mods/MiraAPI.git
synced 2026-10-19 15:51:48 +00:00
shuffle modifiers before assigning
This commit is contained in:
1 parent
4e1db039a0
commit
6b3038f397
2 files changed
+51
No files matched your search
@@ -5,6 +5,7 @@ using System.Runtime.Serialization;
|
|||||||
using MiraAPI.Modifiers.Types;
|
using MiraAPI.Modifiers.Types;
|
||||||
using MiraAPI.PluginLoading;
|
using MiraAPI.PluginLoading;
|
||||||
using MiraAPI.Roles;
|
using MiraAPI.Roles;
|
||||||
|
using MiraAPI.Utilities;
|
||||||
using Reactor.Utilities;
|
using Reactor.Utilities;
|
||||||
using Reactor.Utilities.Extensions;
|
using Reactor.Utilities.Extensions;
|
||||||
using Random = System.Random;
|
using Random = System.Random;
|
||||||
@@ -125,6 +126,7 @@ public static class ModifierManager
|
|||||||
.Select(x => Activator.CreateInstance(x.Value) as GameModifier)
|
.Select(x => Activator.CreateInstance(x.Value) as GameModifier)
|
||||||
.OfType<GameModifier>()
|
.OfType<GameModifier>()
|
||||||
.Where(x => x.GetAmountPerGame() > 0 && x.GetAssignmentChance() > 0)
|
.Where(x => x.GetAmountPerGame() > 0 && x.GetAssignmentChance() > 0)
|
||||||
|
.Shuffle()
|
||||||
.OrderByDescending(x => x.Priority())
|
.OrderByDescending(x => x.Priority())
|
||||||
.ToArray();
|
.ToArray();
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,49 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
|
||||||
|
namespace MiraAPI.Utilities;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Extensions for IEnumerable.
|
||||||
|
/// </summary>
|
||||||
|
public static class EnumerableExtensions
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Shuffle the elements of the source enumerable using a new instance of Random.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="source">The source enumerable.</param>
|
||||||
|
/// <typeparam name="T">The type of the elements.</typeparam>
|
||||||
|
/// <returns>>The shuffled enumerable.</returns>
|
||||||
|
public static IEnumerable<T> Shuffle<T>(this IEnumerable<T> source)
|
||||||
|
{
|
||||||
|
return source.Shuffle(new Random());
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Shuffle the elements of the source enumerable using the provided Random instance.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="source">The source enumerable.</param>
|
||||||
|
/// <param name="rng">The Random instance to use for shuffling.</param>
|
||||||
|
/// <typeparam name="T">The type of the elements.</typeparam>
|
||||||
|
/// <returns>>The shuffled enumerable.</returns>
|
||||||
|
public static IEnumerable<T> Shuffle<T>(this IEnumerable<T> source, Random rng)
|
||||||
|
{
|
||||||
|
ArgumentNullException.ThrowIfNull(source);
|
||||||
|
ArgumentNullException.ThrowIfNull(rng);
|
||||||
|
|
||||||
|
return source.ShuffleIterator(rng);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static IEnumerable<T> ShuffleIterator<T>(this IEnumerable<T> source, Random rng)
|
||||||
|
{
|
||||||
|
var buffer = source.ToList();
|
||||||
|
for (var i = 0; i < buffer.Count; i++)
|
||||||
|
{
|
||||||
|
var j = rng.Next(i, buffer.Count);
|
||||||
|
yield return buffer[j];
|
||||||
|
|
||||||
|
buffer[j] = buffer[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user