From 0032518b205f968acd5461a9b92486fdb011af76 Mon Sep 17 00:00:00 2001 From: Larry Date: Mon, 15 Jan 2024 10:45:04 -0700 Subject: [PATCH 1/2] Fix logic for SunMoon --- .../Extensions/StringExtensions.cs | 34 +++++++++++++++++++ .../Providers/BattleMetricsProvider.cs | 12 +++---- .../Providers/SteamProvider.cs | 16 ++++----- 3 files changed, 46 insertions(+), 16 deletions(-) create mode 100644 DiscordPlayerCountBot/Extensions/StringExtensions.cs diff --git a/DiscordPlayerCountBot/Extensions/StringExtensions.cs b/DiscordPlayerCountBot/Extensions/StringExtensions.cs new file mode 100644 index 0000000..fdd6370 --- /dev/null +++ b/DiscordPlayerCountBot/Extensions/StringExtensions.cs @@ -0,0 +1,34 @@ +using Microsoft.VisualBasic; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DiscordPlayerCountBot.Extensions +{ + public static class StringExtensions + { + public static bool TryGetSunMoonPhase(this string timeString, int? sunriseHour, int? sunsetHour, out string output) + { + if (string.IsNullOrWhiteSpace(timeString)) + { + output = string.Empty; + return false; + } + + if (!TimeOnly.TryParse(timeString, out var time)) + { + output = string.Empty; + return false; + } + + var sunrise = sunriseHour ?? 6; + var sunset = sunsetHour ?? 20; + var hour = time.Hour; + + output = hour >= sunrise && hour < sunset ? "☀️" : "🌙"; + return !string.IsNullOrEmpty(output); + } + } +} diff --git a/DiscordPlayerCountBot/Providers/BattleMetricsProvider.cs b/DiscordPlayerCountBot/Providers/BattleMetricsProvider.cs index 77b30f2..9a77261 100644 --- a/DiscordPlayerCountBot/Providers/BattleMetricsProvider.cs +++ b/DiscordPlayerCountBot/Providers/BattleMetricsProvider.cs @@ -1,4 +1,6 @@ -namespace PlayerCountBot.Providers +using DiscordPlayerCountBot.Extensions; + +namespace PlayerCountBot.Providers { [Name("BattleMetrics")] public class BattleMetricsProvider : ServerInformationProvider @@ -23,13 +25,9 @@ public BattleMetricsProvider(BotInformation info) : base(info) var model = server.GetViewModel(); - if (!string.IsNullOrEmpty(model.Time) && TimeOnly.TryParse(model.Time, out var time)) + if (model.Time.TryGetSunMoonPhase(information.SunriseHour, information.SunsetHour, out var sunMoon)) { - if (information.SunriseHour.HasValue && information.SunsetHour.HasValue) - model.SunMoon = time.Hour > information.SunriseHour && time.Hour < information.SunsetHour ? "☀️" : "🌙"; - - if (!information.SunriseHour.HasValue || !information.SunsetHour.HasValue) - model.SunMoon = time.Hour > 6 && time.Hour < 20 ? "☀️" : "🌙"; + model.SunMoon = sunMoon; } return model; diff --git a/DiscordPlayerCountBot/Providers/SteamProvider.cs b/DiscordPlayerCountBot/Providers/SteamProvider.cs index b4e8be2..8a4ce94 100644 --- a/DiscordPlayerCountBot/Providers/SteamProvider.cs +++ b/DiscordPlayerCountBot/Providers/SteamProvider.cs @@ -1,4 +1,6 @@ -namespace PlayerCountBot.Providers +using DiscordPlayerCountBot.Extensions; + +namespace PlayerCountBot.Providers { [Name("Steam")] public class SteamProvider : ServerInformationProvider @@ -38,16 +40,12 @@ public SteamProvider(BotInformation info) : base(info) if (!string.IsNullOrEmpty(serverTime)) { - if (TimeOnly.TryParse(serverTime, out var time)) + if (model.Time.TryGetSunMoonPhase(information.SunriseHour, information.SunsetHour, out var sunMoon)) { - if (information.SunriseHour.HasValue && information.SunsetHour.HasValue) - model.SunMoon = time.Hour > information.SunriseHour && time.Hour < information.SunsetHour ? "☀️" : "🌙"; - - if (!information.SunriseHour.HasValue || !information.SunsetHour.HasValue) - model.SunMoon = time.Hour > 6 && time.Hour < 20 ? "☀️" : "🌙"; - - model.Time = serverTime; + model.SunMoon = sunMoon; } + + model.Time = serverTime; } return model; From be45ab23b05e641e1c64e7069af4358adbfd0c9c Mon Sep 17 00:00:00 2001 From: Larry Date: Mon, 15 Jan 2024 11:39:46 -0700 Subject: [PATCH 2/2] Add test cases around sun and moon tag logic. --- .../DockerConfigurationTests.cs | 5 +- .../Environment/EnivronmentHelper.cs | 2 +- DiscordPlayerCountBot.Tests/JsonTests.cs | 4 +- .../SunMoonTagTests.cs | 78 +++++++++++++++++++ DiscordPlayerCountBot.Tests/Usings.cs | 5 +- 5 files changed, 86 insertions(+), 8 deletions(-) create mode 100644 DiscordPlayerCountBot.Tests/SunMoonTagTests.cs diff --git a/DiscordPlayerCountBot.Tests/DockerConfigurationTests.cs b/DiscordPlayerCountBot.Tests/DockerConfigurationTests.cs index 6d5b68f..1eb0484 100644 --- a/DiscordPlayerCountBot.Tests/DockerConfigurationTests.cs +++ b/DiscordPlayerCountBot.Tests/DockerConfigurationTests.cs @@ -1,7 +1,6 @@ -using System.Collections.Generic; -using EnvironmentHelper = PlayerCountBot.Tests.Environment.EnvironmentHelper; +using EnvironmentHelper = DiscordPlayerCountBot.Tests.Environment.EnvironmentHelper; -namespace PlayerCountBot.Tests; +namespace DiscordPlayerCountBot.Tests; [Collection("Configuration Test Suite")] public class DockerConfigurationTests diff --git a/DiscordPlayerCountBot.Tests/Environment/EnivronmentHelper.cs b/DiscordPlayerCountBot.Tests/Environment/EnivronmentHelper.cs index e0a56b4..e754ccd 100644 --- a/DiscordPlayerCountBot.Tests/Environment/EnivronmentHelper.cs +++ b/DiscordPlayerCountBot.Tests/Environment/EnivronmentHelper.cs @@ -1,4 +1,4 @@ -namespace PlayerCountBot.Tests.Environment +namespace DiscordPlayerCountBot.Tests.Environment { public static class EnvironmentHelper { diff --git a/DiscordPlayerCountBot.Tests/JsonTests.cs b/DiscordPlayerCountBot.Tests/JsonTests.cs index 05c3d88..4b960a1 100644 --- a/DiscordPlayerCountBot.Tests/JsonTests.cs +++ b/DiscordPlayerCountBot.Tests/JsonTests.cs @@ -1,6 +1,4 @@ -using PlayerCountBot.Json; - -namespace DiscordPlayerCountBot.Tests; +namespace DiscordPlayerCountBot.Tests; [Collection("Json Serialization Test Suite")] public class JsonTests diff --git a/DiscordPlayerCountBot.Tests/SunMoonTagTests.cs b/DiscordPlayerCountBot.Tests/SunMoonTagTests.cs new file mode 100644 index 0000000..dad15c2 --- /dev/null +++ b/DiscordPlayerCountBot.Tests/SunMoonTagTests.cs @@ -0,0 +1,78 @@ +namespace DiscordPlayerCountBot.Tests +{ + [Collection("Sun & Moon Tag Test Suite")] + public class SunMoonTagTests + { + [Theory(DisplayName = "Test If Should Be Moon", Timeout = 30)] + [InlineData("05:30")] + [InlineData("21:30")] + [InlineData("23:30")] + public void ShouldBeMoonPhase(string time) + { + var success = time.TryGetSunMoonPhase(null, null, out var output); + + Assert.True(success, "SunMoon Phase failed to parse"); + Assert.True(!string.IsNullOrEmpty(output)); + Assert.Equal("🌙", output); + } + + [Theory(DisplayName = "Test If Should Be Sun", Timeout = 30)] + [InlineData("06:04")] + [InlineData("15:42")] + [InlineData("19:06")] + public void ShouldBeSunPhase(string time) + { + var success = time.TryGetSunMoonPhase(null, null, out var output); + + Assert.True(success, "SunMoon Phase failed to parse"); + Assert.True(!string.IsNullOrEmpty(output)); + Assert.Equal("☀️", output); + } + + [Theory(DisplayName = "Test Invalid Values", Timeout = 30)] + [InlineData("abc")] + [InlineData("")] + [InlineData("not a time value")] + [InlineData("24:30")] + public void ShouldNotParse(string time) + { + var success = time.TryGetSunMoonPhase(null, null, out var output); + + Assert.False(success, "SunMoon Phase successfully parsed when it should have failed"); + Assert.True(string.IsNullOrEmpty(output), "SunMoon output is not null or empty."); + } + + [Theory(DisplayName = "Test custom sunrise and sunset", Timeout = 30)] + [InlineData("05:30")] + [InlineData("21:30")] + [InlineData("23:30")] + [InlineData("06:04")] + [InlineData("15:42")] + [InlineData("19:06")] + public void ShouldOutputCorrectValue(string time) + { + var information = new BotInformation() + { + SunriseHour = 1, + SunsetHour = 21 + }; + + var success = time.TryGetSunMoonPhase(information.SunriseHour, information.SunsetHour, out var output); + var manualProcessingSuccess = TimeOnly.TryParse(time, out var timeOutput); + + Assert.True(success, "SunMoon Phase parsing failed"); + Assert.True(manualProcessingSuccess, "Manual Parsing SunMoon Phase failed"); + + var isBetween = timeOutput.Hour >= information.SunriseHour && timeOutput.Hour < information.SunsetHour; + var sunMoonPhase = isBetween ? "☀️" : "🌙"; + + if (isBetween) + Assert.True(sunMoonPhase.Equals("☀️"), "Time was between sunrise and sunset, but was not a Sun."); + + if (!isBetween) + Assert.True(sunMoonPhase.Equals("🌙"), "Time not was between sunrise and sunset, but was not a Moon."); + + Assert.Equal(sunMoonPhase, output); + } + } +} diff --git a/DiscordPlayerCountBot.Tests/Usings.cs b/DiscordPlayerCountBot.Tests/Usings.cs index eaab6e2..faaf93f 100644 --- a/DiscordPlayerCountBot.Tests/Usings.cs +++ b/DiscordPlayerCountBot.Tests/Usings.cs @@ -1,3 +1,6 @@ global using Xunit; + +global using PlayerCountBot; global using PlayerCountBot.Configuration; -global using PlayerCountBot.Tests.Environment; +global using DiscordPlayerCountBot.Extensions; +global using PlayerCountBot.Json; \ No newline at end of file