Skip to content

Commit

Permalink
Merge pull request #53 from GravityWolfNotAmused/dev
Browse files Browse the repository at this point in the history
Creation of Status Formats
  • Loading branch information
GravityWolfNotAmused authored Sep 28, 2022
2 parents 468bbfe + c2a67f5 commit 078a43c
Show file tree
Hide file tree
Showing 69 changed files with 548 additions and 580 deletions.
40 changes: 30 additions & 10 deletions DiscordPlayerCountBot.Tests/DockerConfigurationTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using DiscordPlayerCountBot.Tests.Environment;
using System.Collections.Generic;
using EnvironmentHelper = PlayerCountBot.Tests.Environment.EnvironmentHelper;

namespace DiscordPlayerCountBot.Tests;
namespace PlayerCountBot.Tests;

[Collection("Configuration Test Suite")]
public class DockerConfigurationTests
Expand All @@ -9,7 +10,7 @@ public class DockerConfigurationTests
[Fact(DisplayName = "Test Docker Configuration with all data", Timeout = 30)]
public async Task DockerConfigurationTestWithAllData()
{
EnivronmentHelper.SetTestEnvironmentWithAllVariables();
EnvironmentHelper.SetTestEnvironmentWithAllVariables();

var bots = new Dictionary<string, Bot>();
var time = -1;
Expand All @@ -20,7 +21,7 @@ public async Task DockerConfigurationTestWithAllData()
bots = configuration.Item1;
time = configuration.Item2;

EnivronmentHelper.ClearTestEnvironmentVariables();
EnvironmentHelper.ClearTestEnvironmentVariables();

Assert.True(time > 0, $"Time was returned from config and not zero. Actual: {time}");
Assert.True(bots.Count > 0, $"More than one bot was created. Actual: {bots.Count}");
Expand All @@ -29,10 +30,29 @@ public async Task DockerConfigurationTestWithAllData()
Assert.True(bots.ToList()[0].Value.ApplicationTokens.ContainsKey("BattleMetricsKey"), $"Battle Metrics Key should be present.");
}

[Fact(DisplayName = "Test Docker Configuration with duplicate addresses")]
public async Task DockerConfigurationWithDuplicateAddresses()
{
EnvironmentHelper.SetTestEnvironmentWithDuplicateAddresses();

var dockerConfiguration = new DockerConfiguration();
var configuration = await dockerConfiguration.Configure(false);

EnvironmentHelper.ClearTestEnvironmentVariables();
var duplicateAddressCount = configuration.Item1.GroupBy(x => x.Value.Information!.Address)
.Where(g => g.Count() > 1)
.Select(y => new { Element = y.Key, Counter = y.Count() })
.ToList();

Assert.NotNull(duplicateAddressCount);
Assert.True(configuration.Item1.Values.Count > 0, "Should have created more than 0 bots.");
Assert.True(duplicateAddressCount.Any(grouping => grouping.Counter > 1), $"Should be two addresses that are the same.");
}

[Fact(DisplayName = "Test Docker Configuration without Battle Metrics", Timeout = 30)]
public async Task DockerConfigurationTestWithoutBattleMetrics()
{
EnivronmentHelper.SetTestEnvironmentWithoutBattleMetrics();
EnvironmentHelper.SetTestEnvironmentWithoutBattleMetrics();

var bots = new Dictionary<string, Bot>();
var time = -1;
Expand All @@ -43,7 +63,7 @@ public async Task DockerConfigurationTestWithoutBattleMetrics()
bots = configuration.Item1;
time = configuration.Item2;

EnivronmentHelper.ClearTestEnvironmentVariables();
EnvironmentHelper.ClearTestEnvironmentVariables();

Assert.True(time > 0, $"Time was returned from config and not zero. Actual: {time}");
Assert.True(bots.Count > 0, $"More than one bot was created. Actual: {bots.Count}");
Expand All @@ -55,7 +75,7 @@ public async Task DockerConfigurationTestWithoutBattleMetrics()
[Fact(DisplayName = "Test Docker Configuration without Application Variables", Timeout = 30)]
public async Task DockerConfigurationTestWithoutApplicationVariables()
{
EnivronmentHelper.SetTestEnvironmentWithoutApplicationVariables();
EnvironmentHelper.SetTestEnvironmentWithoutApplicationVariables();

var bots = new Dictionary<string, Bot>();
var time = -1;
Expand All @@ -66,7 +86,7 @@ public async Task DockerConfigurationTestWithoutApplicationVariables()
bots = configuration.Item1;
time = configuration.Item2;

EnivronmentHelper.ClearTestEnvironmentVariables();
EnvironmentHelper.ClearTestEnvironmentVariables();

Assert.True(time > 0, $"Time was returned from config and not zero. Actual: {time}");
Assert.True(bots.Count > 0, $"More than one bot was created. Actual: {bots.Count}");
Expand All @@ -76,7 +96,7 @@ public async Task DockerConfigurationTestWithoutApplicationVariables()
[Fact(DisplayName = "Test Docker Configuration without Steam variable", Timeout = 30)]
public async Task DockerConfigurationTestWithoutSteam()
{
EnivronmentHelper.SetTestEnvironmentWithoutSteam();
EnvironmentHelper.SetTestEnvironmentWithoutSteam();

var bots = new Dictionary<string, Bot>();
var time = -1;
Expand All @@ -87,7 +107,7 @@ public async Task DockerConfigurationTestWithoutSteam()
bots = configuration.Item1;
time = configuration.Item2;

EnivronmentHelper.ClearTestEnvironmentVariables();
EnvironmentHelper.ClearTestEnvironmentVariables();

Assert.True(time > 0, $"Time was returned from config and not zero. Actual: {time}");
Assert.True(bots.Count > 0, $"More than one bot was created. Actual: {bots.Count}");
Expand Down
35 changes: 24 additions & 11 deletions DiscordPlayerCountBot.Tests/Environment/EnivronmentHelper.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace DiscordPlayerCountBot.Tests.Environment
namespace PlayerCountBot.Tests.Environment
{
public static class EnivronmentHelper
public static class EnvironmentHelper
{
public static void SetTestEnvironmentWithAllVariables()
{
Expand All @@ -9,21 +9,34 @@ public static void SetTestEnvironmentWithAllVariables()
System.Environment.SetEnvironmentVariable("BOT_PUBADDRESSES", "51.222.191.212;96.43.133.194;178.63.74.20;minecraft.hypixel.net;15086629");
System.Environment.SetEnvironmentVariable("BOT_PORTS", "2403;30120;7042;25565;0");
System.Environment.SetEnvironmentVariable("BOT_DISCORD_TOKENS", "1;2;3;4;5");
System.Environment.SetEnvironmentVariable("BOT_USERNAMETAGS", "false;false;false;false;false");
System.Environment.SetEnvironmentVariable("BOT_STATUSES", "Steam;CFX;Scum;Minecraft;BattleMetrics");
System.Environment.SetEnvironmentVariable("BOT_USENAMETAGS", "false;false;false;false;false");
System.Environment.SetEnvironmentVariable("BOT_STATUSES", "0;0;0;0;0");
System.Environment.SetEnvironmentVariable("BOT_PROVIDERTYPES", "3;3;3;3;3");
System.Environment.SetEnvironmentVariable("BOT_UPDATE_TIME", "10");
System.Environment.SetEnvironmentVariable("BOT_APPLICATION_VARIABLES", "SteamAPIKey,12345;BattleMetricsKey,12345");
}
public static void SetTestEnvironmentWithDuplicateAddresses()
{
System.Environment.SetEnvironmentVariable("ISDOCKER", "true");
System.Environment.SetEnvironmentVariable("BOT_NAMES", "Steam;CFX;Scum;Minecraft;BattleMetrics;BattleMetrics2");
System.Environment.SetEnvironmentVariable("BOT_PUBADDRESSES", "51.222.191.212;96.43.133.194;178.63.74.20;minecraft.hypixel.net;15086629;15086629");
System.Environment.SetEnvironmentVariable("BOT_PORTS", "2403;30120;7042;25565;0;0");
System.Environment.SetEnvironmentVariable("BOT_DISCORD_TOKENS", "1;2;3;4;5;6");
System.Environment.SetEnvironmentVariable("BOT_USENAMETAGS", "false;false;false;false;false;false");
System.Environment.SetEnvironmentVariable("BOT_STATUSES", "0;0;0;0;0;0");
System.Environment.SetEnvironmentVariable("BOT_PROVIDERTYPES", "3;3;3;3;3;3");
System.Environment.SetEnvironmentVariable("BOT_UPDATE_TIME", "10");
System.Environment.SetEnvironmentVariable("BOT_APPLICATION_VARIABLES", "SteamAPIKey,12345;BattleMetricsKey,12345");
}
public static void SetTestEnvironmentWithoutSteam()
{
System.Environment.SetEnvironmentVariable("ISDOCKER", "true");
System.Environment.SetEnvironmentVariable("BOT_NAMES", "CFX;Scum;Minecraft;BattleMetrics");
System.Environment.SetEnvironmentVariable("BOT_PUBADDRESSES", "96.43.133.194;178.63.74.20;minecraft.hypixel.net;15086629");
System.Environment.SetEnvironmentVariable("BOT_PORTS", "30120;7042;25565;0");
System.Environment.SetEnvironmentVariable("BOT_DISCORD_TOKENS", "2;3;4;5");
System.Environment.SetEnvironmentVariable("BOT_USERNAMETAGS", "false;false;false;false");
System.Environment.SetEnvironmentVariable("BOT_STATUSES", "CFX;Scum;Minecraft;BattleMetrics");
System.Environment.SetEnvironmentVariable("BOT_USENAMETAGS", "false;false;false;false");
System.Environment.SetEnvironmentVariable("BOT_STATUSES", "0;0;0;0;0");
System.Environment.SetEnvironmentVariable("BOT_PROVIDERTYPES", "3;3;3;3");
System.Environment.SetEnvironmentVariable("BOT_UPDATE_TIME", "10");
System.Environment.SetEnvironmentVariable("BOT_APPLICATION_VARIABLES", "BattleMetricsKey,12345");
Expand All @@ -36,8 +49,8 @@ public static void SetTestEnvironmentWithoutBattleMetrics()
System.Environment.SetEnvironmentVariable("BOT_PUBADDRESSES", "51.222.191.212;96.43.133.194;178.63.74.20;minecraft.hypixel.net");
System.Environment.SetEnvironmentVariable("BOT_PORTS", "2403;30120;7042;25565");
System.Environment.SetEnvironmentVariable("BOT_DISCORD_TOKENS", "1;2;3;4");
System.Environment.SetEnvironmentVariable("BOT_USERNAMETAGS", "false;false;false;false");
System.Environment.SetEnvironmentVariable("BOT_STATUSES", "Steam;CFX;Scum;Minecraft");
System.Environment.SetEnvironmentVariable("BOT_USENAMETAGS", "false;false;false;false");
System.Environment.SetEnvironmentVariable("BOT_STATUSES", "0;0;0;0;0");
System.Environment.SetEnvironmentVariable("BOT_PROVIDERTYPES", "3;3;3;3");
System.Environment.SetEnvironmentVariable("BOT_UPDATE_TIME", "10");
System.Environment.SetEnvironmentVariable("BOT_APPLICATION_VARIABLES", "SteamAPIKey,12345");
Expand All @@ -50,8 +63,8 @@ public static void SetTestEnvironmentWithoutApplicationVariables()
System.Environment.SetEnvironmentVariable("BOT_PUBADDRESSES", "51.222.191.212;96.43.133.194;178.63.74.20;minecraft.hypixel.net;15086629");
System.Environment.SetEnvironmentVariable("BOT_PORTS", "2403;30120;7042;25565;0");
System.Environment.SetEnvironmentVariable("BOT_DISCORD_TOKENS", "1;2;3;4;5");
System.Environment.SetEnvironmentVariable("BOT_USERNAMETAGS", "false;false;false;false;false");
System.Environment.SetEnvironmentVariable("BOT_STATUSES", "Steam;CFX;Scum;Minecraft;BattleMetrics");
System.Environment.SetEnvironmentVariable("BOT_USENAMETAGS", "false;false;false;false;false");
System.Environment.SetEnvironmentVariable("BOT_STATUSES", "0;0;0;0;0");
System.Environment.SetEnvironmentVariable("BOT_PROVIDERTYPES", "3;3;3;3;3");
System.Environment.SetEnvironmentVariable("BOT_UPDATE_TIME", "10");
}
Expand All @@ -63,7 +76,7 @@ public static void ClearTestEnvironmentVariables()
System.Environment.SetEnvironmentVariable("BOT_PUBADDRESSES", null);
System.Environment.SetEnvironmentVariable("BOT_PORTS", null);
System.Environment.SetEnvironmentVariable("BOT_DISCORD_TOKENS", null);
System.Environment.SetEnvironmentVariable("BOT_USERNAMETAGS", null);
System.Environment.SetEnvironmentVariable("BOT_USENAMETAGS", null);
System.Environment.SetEnvironmentVariable("BOT_STATUSES", null);
System.Environment.SetEnvironmentVariable("BOT_PROVIDERTYPES", null);
System.Environment.SetEnvironmentVariable("BOT_UPDATE_TIME", null);
Expand Down
2 changes: 1 addition & 1 deletion DiscordPlayerCountBot.Tests/JsonTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using DiscordPlayerCountBot.Json;
using PlayerCountBot.Json;

namespace DiscordPlayerCountBot.Tests;

Expand Down
4 changes: 2 additions & 2 deletions DiscordPlayerCountBot.Tests/Usings.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
global using Xunit;
global using DiscordPlayerCountBot.Configuration;
global using PlayerCountBot;
global using PlayerCountBot.Configuration;
global using PlayerCountBot.Tests.Environment;
4 changes: 1 addition & 3 deletions DiscordPlayerCountBot/Attributes/AttributeHelper.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.Linq;

namespace DiscordPlayerCountBot.Attributes
namespace PlayerCountBot.Attributes
{
public static class AttributeHelper
{
Expand Down
7 changes: 3 additions & 4 deletions DiscordPlayerCountBot/Attributes/NameAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
using System;

namespace DiscordPlayerCountBot.Attributes
namespace PlayerCountBot.Attributes
{

[AttributeUsage(AttributeTargets.Property | AttributeTargets.Class)]
public class NameAttribute : Attribute
{
public string Name { get; set; }
public string Name { get; private set; }

public NameAttribute(string name)
{
Expand Down
59 changes: 22 additions & 37 deletions DiscordPlayerCountBot/Bot/Bot.cs
Original file line number Diff line number Diff line change
@@ -1,33 +1,19 @@
using Discord;
using Discord.WebSocket;
using DiscordPlayerCountBot;
using DiscordPlayerCountBot.Enum;
using DiscordPlayerCountBot.Http;
using DiscordPlayerCountBot.Providers;
using DiscordPlayerCountBot.Providers.Base;
using log4net;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace PlayerCountBot
namespace PlayerCountBot
{
public class Bot

[Name("Bot")]
public class Bot : LoggableClass
{
public BotInformation Information { get; set; }
public DiscordSocketClient DiscordClient { get; set; }
public Dictionary<int, IServerInformationProvider> DataProviders { get; set; } = new();
public Dictionary<string, string> ApplicationTokens { get; set; } = new();

private ILog Logger = LogManager.GetLogger(typeof(Bot));

public Bot(BotInformation info, Dictionary<string, string> applicationTokens)
public Bot(BotInformation info, Dictionary<string, string> applicationTokens) : base(info)
{
if (info is null) throw new ArgumentNullException(nameof(info));
if (applicationTokens is null) throw new ArgumentException(nameof(applicationTokens));

ApplicationTokens = applicationTokens;
Information = info;

DiscordClient = new DiscordSocketClient(new DiscordSocketConfig()
{
Expand All @@ -39,21 +25,20 @@ public Bot(BotInformation info, Dictionary<string, string> applicationTokens)

public void InitDataProviders()
{
DataProviders.Add((int)DataProvider.STEAM, new SteamProvider());
DataProviders.Add((int)DataProvider.CFX, new CFXProvider());
DataProviders.Add((int)DataProvider.SCUM, new ScumProvider());
DataProviders.Add((int)DataProvider.MINECRAFT, new MinecraftProvider());
DataProviders.Add((int)DataProvider.BATTLEMETRICS, new BattleMetricsProvider());
DataProviders.Add((int)DataProvider.STEAM, new SteamProvider(Information!));
DataProviders.Add((int)DataProvider.CFX, new CFXProvider(Information!));
DataProviders.Add((int)DataProvider.MINECRAFT, new MinecraftProvider(Information!));
DataProviders.Add((int)DataProvider.BATTLEMETRICS, new BattleMetricsProvider(Information!));
}

public async Task StartAsync(bool shouldStart)
{
if (Information.Address.Contains("hostname") || Information.Address.Contains("localhost"))
if (Information!.Address.Contains("hostname") || Information.Address.Contains("localhost"))
{
Information.Address = await AddressHelper.ResolveAddress(Information.Address);
}

Logger.Info($"[Bot] - Loaded {Information.Name} at address and port: {Information.Address}, {Information.ProviderType}");
Info($"Loaded {Information.Name} at address and port: {Information.Address}, {Information.ProviderType}");
await DiscordClient.LoginAndStartAsync(Information.Token, Information.Address, shouldStart);
}

Expand All @@ -64,11 +49,18 @@ public async Task StopAsync()

public async Task UpdateAsync()
{
var dataProviderType = EnumHelper.GetDataProvider(Information.ProviderType);
var dataProviderType = EnumHelper.GetDataProvider(Information!.ProviderType);

if (dataProviderType != Information.ProviderType)
{
Logger.Warn($"[Bot] - Config for bot at address: {Information.Address} has an invalid provider type: {Information.ProviderType}");
Warn($"Config for bot at address: {Information.Address} has an invalid provider type: {Information.ProviderType}");
}

var activityInteger = EnumHelper.GetActivityType(Information.Status);

if (Information.Status != activityInteger)
{
Warn($"Config for bot at address: {Information.Address} has an invalid activity type: {Information.Status}");
}

var dataProvider = DataProviders[dataProviderType];
Expand All @@ -79,16 +71,9 @@ public async Task UpdateAsync()
return;
}

var gameStatus = serverInformation.GetStatusString(Information.Name, Information.UseNameAsLabel);
var activityInteger = EnumHelper.GetActivityType(Information.Status);

if (Information.Status != activityInteger)
{
Logger.Warn($"[Bot] - Config for bot at address: {Information.Address} has an invalid activity type: {Information.Status}");
}
var gameStatus = serverInformation.ReplaceTagsWithValues(Information.StatusFormat, Information.UseNameAsLabel, Information.Name);

var activityType = (ActivityType)(activityInteger);
await DiscordClient.SetGameAsync(gameStatus, null, activityType);
await DiscordClient.SetGameAsync(gameStatus, null, (ActivityType)activityInteger);
await DiscordClient.SetChannelName(Information.ChannelID, gameStatus);
}
}
Expand Down
13 changes: 2 additions & 11 deletions DiscordPlayerCountBot/Bot/BotConfig.cs
Original file line number Diff line number Diff line change
@@ -1,23 +1,14 @@
using System.Collections.Generic;
using Newtonsoft.Json;

namespace PlayerCountBot
namespace PlayerCountBot
{
public class BotConfig
{
[JsonProperty]
public int UpdateTime { get; set; }

[JsonProperty]
public List<BotInformation> ServerInformation { get; set; } = new();

[JsonProperty]
public Dictionary<string, string> ApplicationTokens { get; set; } = new();

public void CreateDefaults()
{

ServerInformation.Add(new BotInformation()
ServerInformation.Add(new()
{
Name = "TestBot",
Address = "127.0.0.1:27014",
Expand Down
23 changes: 4 additions & 19 deletions DiscordPlayerCountBot/Bot/BotInformation.cs
Original file line number Diff line number Diff line change
@@ -1,32 +1,17 @@
using log4net.Repository.Hierarchy;
using Microsoft.VisualBasic;
using Newtonsoft.Json;
using System;

namespace PlayerCountBot
namespace PlayerCountBot
{
public class BotInformation
{
[JsonProperty]
[JsonIgnore]
public Guid Id { get; set; } = Guid.NewGuid();
public string Name { get; set; }

[JsonProperty]
public string Address { get; set; }

[JsonProperty]
public string Token { get; set; }

[JsonProperty]
public int Status { get; set; }

[JsonProperty]
public bool UseNameAsLabel { get; set; }

[JsonProperty]
public int ProviderType { get; set; } = 0;

[JsonProperty]
public ulong? ChannelID { get; set; }
public string? StatusFormat { get; set; }

public Tuple<string, ushort> GetAddressAndPort()
{
Expand Down
Loading

0 comments on commit 078a43c

Please sign in to comment.