From 3989f0e21ab93234a8241cd3b111dcacd46b29e9 Mon Sep 17 00:00:00 2001 From: White Date: Tue, 21 Feb 2017 22:16:08 +1030 Subject: [PATCH] Ban.BanDateTime and Ban.ExpirationDateTime are now nullable, in case of bad bans. Ban sorting now uses an IComparer to do its dirty work. --- TShockAPI/DB/BanManager.cs | 109 ++++++++++++++++++++++++++++++------- 1 file changed, 88 insertions(+), 21 deletions(-) diff --git a/TShockAPI/DB/BanManager.cs b/TShockAPI/DB/BanManager.cs index 2f71ac40..9566b142 100644 --- a/TShockAPI/DB/BanManager.cs +++ b/TShockAPI/DB/BanManager.cs @@ -107,25 +107,7 @@ namespace TShockAPI.DB banlist.Add(new Ban(reader.Get("IP"), reader.Get("Name"), reader.Get("UUID"), reader.Get("Reason"), reader.Get("BanningUser"), reader.Get("Date"), reader.Get("Expiration"))); } - Comparison comparer; - switch (sortMethod) - { - case BanSortMethod.AddedOldestToNewest: - comparer = (a, b) => b.BanDateTime.CompareTo(a.BanDateTime); - break; - case BanSortMethod.AddedNewestToOldest: - comparer = (a, b) => a.BanDateTime.CompareTo(b.BanDateTime); - break; - case BanSortMethod.ExpirationLatestToSoonest: - comparer = (a, b) => b.ExpirationDateTime.CompareTo(a.ExpirationDateTime); - break; - default: - //Default encompasses BanSortMethod.ExpirationSoonestToLatest - comparer = (a, b) => a.ExpirationDateTime.CompareTo(b.ExpirationDateTime); - break; - } - - banlist.Sort(comparer); + banlist.Sort(new BanComparer(sortMethod)); return banlist; } } @@ -290,6 +272,91 @@ namespace TShockAPI.DB AddedOldestToNewest } + /// + /// An used for sorting an enumerable of bans + /// + public class BanComparer : IComparer + { + private BanSortMethod _method; + + /// + /// Generates a new using the given + /// + /// + public BanComparer(BanSortMethod method) + { + _method = method; + } + + private int CompareDateTimes(DateTime? x, DateTime? y) + { + if (x == null) + { + if (y == null) + { + //If both bans have no BanDateTime they're considered equal + return 0; + } + //If we're sorting by a newest to oldest method, a null value will come after the valid value. + return _method == BanSortMethod.AddedNewestToOldest || _method == BanSortMethod.ExpirationSoonestToLatest ? 1 : -1; + } + + if (y == null) + { + return _method == BanSortMethod.AddedNewestToOldest || _method == BanSortMethod.ExpirationSoonestToLatest ? -1 : 1; + } + + //Newest to oldest sorting uses x compared to y. Oldest to newest uses y compared to x + return _method == BanSortMethod.AddedNewestToOldest || _method == BanSortMethod.ExpirationSoonestToLatest ? x.Value.CompareTo(y.Value) + : y.Value.CompareTo(x.Value); + } + + /// + /// Compares two ban objects + /// + /// + /// + /// 1 if x is less than y, 0 if x is equal to y, -1 if x is greater than y + public int Compare(Ban x, Ban y) + { + if (x == null) + { + if (y == null) + { + return 0; + } + + //If Ban y is null and Ban x is not, and we're sorting from newest to oldest, x goes before y. Else y goes before x + return _method == BanSortMethod.AddedNewestToOldest || _method == BanSortMethod.ExpirationSoonestToLatest ? -1 : 1; + } + + if (x == null) + { + if (y == null) + { + return 0; + } + + //If Ban y is null and Ban x is not, and we're sorting from newest to oldest, x goes before y. Else y goes before x + return _method == BanSortMethod.AddedNewestToOldest || _method == BanSortMethod.ExpirationSoonestToLatest ? -1 : 1; + } + + switch (_method) + { + case BanSortMethod.AddedNewestToOldest: + case BanSortMethod.AddedOldestToNewest: + return CompareDateTimes(x.BanDateTime, y.BanDateTime); + + case BanSortMethod.ExpirationSoonestToLatest: + case BanSortMethod.ExpirationLatestToSoonest: + return CompareDateTimes(x.ExpirationDateTime, y.ExpirationDateTime); + + default: + return 0; + } + } + } + /// /// Model class that represents a ban entry in the TShock database. /// @@ -334,7 +401,7 @@ namespace TShockAPI.DB /// /// Gets the object representation of the string. /// - public DateTime BanDateTime { get; } + public DateTime? BanDateTime { get; } /// /// Gets or sets the expiration date, in which the ban shall be lifted @@ -345,7 +412,7 @@ namespace TShockAPI.DB /// /// Gets the object representation of the string. /// - public DateTime ExpirationDateTime { get; } + public DateTime? ExpirationDateTime { get; } /// /// Initializes a new instance of the class.