From df7fdce14498ef73494cae20582a6a14be2a5699 Mon Sep 17 00:00:00 2001 From: acw0 Date: Tue, 4 Aug 2020 19:28:29 -0400 Subject: [PATCH] Add sorting member list randomly --- PluralKit.Bot/Commands/Lists/ContextListExt.cs | 3 ++- PluralKit.Bot/Commands/Lists/MemberListOptions.cs | 7 ++++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/PluralKit.Bot/Commands/Lists/ContextListExt.cs b/PluralKit.Bot/Commands/Lists/ContextListExt.cs index f1efadb0..8bee0927 100644 --- a/PluralKit.Bot/Commands/Lists/ContextListExt.cs +++ b/PluralKit.Bot/Commands/Lists/ContextListExt.cs @@ -40,7 +40,8 @@ namespace PluralKit.Bot if (ctx.MatchFlag("by-last-fronted", "by-last-front", "by-last-switch", "blf", "bls")) p.SortProperty = SortProperty.LastSwitch; if (ctx.MatchFlag("by-last-message", "blm", "blp")) p.SortProperty = SortProperty.LastMessage; if (ctx.MatchFlag("by-birthday", "by-birthdate", "bbd")) p.SortProperty = SortProperty.Birthdate; - + if (ctx.MatchFlag("random")) p.SortProperty = SortProperty.Random; + // Sort reverse? if (ctx.MatchFlag("r", "rev", "reverse")) p.Reverse = true; diff --git a/PluralKit.Bot/Commands/Lists/MemberListOptions.cs b/PluralKit.Bot/Commands/Lists/MemberListOptions.cs index 9e00b226..2f6a7aad 100644 --- a/PluralKit.Bot/Commands/Lists/MemberListOptions.cs +++ b/PluralKit.Bot/Commands/Lists/MemberListOptions.cs @@ -75,6 +75,8 @@ namespace PluralKit.Bot IComparer ReverseMaybe(IComparer c) => opts.Reverse ? Comparer.Create((a, b) => c.Compare(b, a)) : c; + var randGen = new global::System.Random(); + var culture = StringComparer.InvariantCultureIgnoreCase; return (opts.SortProperty switch { @@ -96,6 +98,8 @@ namespace PluralKit.Bot SortProperty.LastSwitch => input .OrderByDescending(m => m.LastSwitchTime.HasValue) .ThenByDescending(m => m.LastSwitchTime, ReverseMaybe(Comparer.Default)), + SortProperty.Random => input + .OrderBy(m => randGen.Next()), _ => throw new ArgumentOutOfRangeException($"Unknown sort property {opts.SortProperty}") }) // Lastly, add a by-name fallback order for collisions (generally hits w/ lots of null values) @@ -112,7 +116,8 @@ namespace PluralKit.Bot CreationDate, LastSwitch, LastMessage, - Birthdate + Birthdate, + Random } public enum ListType