Files
RobustToolbox/Robust.Shared/Toolshed/Commands/Generic/SelectCommand.cs
Leon Friedrich 9af119f57a Toolshed Rejig (#5455)
* 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
2024-12-21 17:49:11 +11:00

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);
}
}
}