Skip to content

Commit

Permalink
Merge pull request #67 from GravityWolfNotAmused/sunmoon-tags-time-tags
Browse files Browse the repository at this point in the history
Fix logic for SunMoon
  • Loading branch information
GravityWolfNotAmused authored Jan 15, 2024
2 parents 88dd8db + be45ab2 commit 64a4a15
Show file tree
Hide file tree
Showing 8 changed files with 132 additions and 24 deletions.
5 changes: 2 additions & 3 deletions DiscordPlayerCountBot.Tests/DockerConfigurationTests.cs
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace PlayerCountBot.Tests.Environment
namespace DiscordPlayerCountBot.Tests.Environment
{
public static class EnvironmentHelper
{
Expand Down
4 changes: 1 addition & 3 deletions DiscordPlayerCountBot.Tests/JsonTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using PlayerCountBot.Json;

namespace DiscordPlayerCountBot.Tests;
namespace DiscordPlayerCountBot.Tests;

[Collection("Json Serialization Test Suite")]
public class JsonTests
Expand Down
78 changes: 78 additions & 0 deletions DiscordPlayerCountBot.Tests/SunMoonTagTests.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
5 changes: 4 additions & 1 deletion DiscordPlayerCountBot.Tests/Usings.cs
Original file line number Diff line number Diff line change
@@ -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;
34 changes: 34 additions & 0 deletions DiscordPlayerCountBot/Extensions/StringExtensions.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
12 changes: 5 additions & 7 deletions DiscordPlayerCountBot/Providers/BattleMetricsProvider.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace PlayerCountBot.Providers
using DiscordPlayerCountBot.Extensions;

namespace PlayerCountBot.Providers
{
[Name("BattleMetrics")]
public class BattleMetricsProvider : ServerInformationProvider
Expand All @@ -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;
Expand Down
16 changes: 7 additions & 9 deletions DiscordPlayerCountBot/Providers/SteamProvider.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace PlayerCountBot.Providers
using DiscordPlayerCountBot.Extensions;

namespace PlayerCountBot.Providers
{
[Name("Steam")]
public class SteamProvider : ServerInformationProvider
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 64a4a15

Please sign in to comment.