mirror of
https://github.com/space-wizards/RobustToolbox.git
synced 2026-02-14 19:29:36 +01:00
* Toolshed Rejig * shorten hint string * Try fix conflicts. Ill make with work later * bodge * Fix ProtoIdTypeParser assert * comment * AllEntities * Remove more linq from WhereCommand * better help strings * Add ContainsCommand * loc strings * Add contains command description * Add $self variable * Errors for writing to readonly variables * A
39 lines
1.2 KiB
C#
39 lines
1.2 KiB
C#
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using Robust.Shared.IoC;
|
|
using Robust.Shared.Random;
|
|
using Robust.Shared.Toolshed.TypeParsers;
|
|
|
|
namespace Robust.Shared.Toolshed.Commands.Generic;
|
|
|
|
[ToolshedCommand]
|
|
public sealed class SelectCommand : ToolshedCommand
|
|
{
|
|
[Dependency] private readonly IRobustRandom _random = default!;
|
|
|
|
[CommandImplementation, TakesPipedTypeAsGeneric]
|
|
public IEnumerable<TR> Select<TR>([PipedArgument] IEnumerable<TR> enumerable, Quantity quantity, [CommandInverted] bool inverted)
|
|
{
|
|
var arr = enumerable.ToArray();
|
|
_random.Shuffle(arr);
|
|
|
|
if (quantity is {Amount: { } amount})
|
|
{
|
|
|
|
var taken = (int) System.Math.Ceiling(amount);
|
|
if (inverted)
|
|
taken = System.Math.Max(0, arr.Length - taken);
|
|
|
|
return arr.Take(taken);
|
|
}
|
|
else
|
|
{
|
|
var percent = inverted
|
|
? (int) System.Math.Floor(arr.Length * System.Math.Clamp(1 - (double) quantity.Percentage!, 0, 1))
|
|
: (int) System.Math.Floor(arr.Length * System.Math.Clamp((double) quantity.Percentage!, 0, 1));
|
|
|
|
return arr.Take(percent);
|
|
}
|
|
}
|
|
}
|