Skip to content

Commit

Permalink
Merge pull request #68 from GravityWolfNotAmused/serilog-logging
Browse files Browse the repository at this point in the history
  • Loading branch information
GravityWolfNotAmused authored Jul 22, 2024
2 parents 64a4a15 + 257e540 commit 7873c22
Show file tree
Hide file tree
Showing 88 changed files with 805 additions and 281 deletions.
87 changes: 87 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,90 @@

# CS8618: Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
dotnet_diagnostic.CS8618.severity = suggestion
csharp_using_directive_placement = outside_namespace:silent
csharp_prefer_simple_using_statement = true:suggestion
csharp_prefer_braces = true:silent
csharp_style_namespace_declarations = block_scoped:silent
csharp_style_prefer_method_group_conversion = true:silent
csharp_style_prefer_top_level_statements = true:silent
csharp_style_expression_bodied_methods = false:silent
csharp_style_expression_bodied_constructors = false:silent
csharp_style_expression_bodied_operators = false:silent
csharp_style_expression_bodied_properties = true:silent
csharp_style_expression_bodied_indexers = true:silent
csharp_style_expression_bodied_accessors = true:silent
csharp_style_expression_bodied_lambdas = true:silent
csharp_style_expression_bodied_local_functions = false:silent
csharp_style_throw_expression = true:suggestion
csharp_style_prefer_null_check_over_type_check = true:suggestion
csharp_prefer_simple_default_expression = true:suggestion
csharp_indent_labels = one_less_than_current
csharp_space_around_binary_operators = before_and_after

[*.{cs,vb}]
#### Naming styles ####

# Naming rules

dotnet_naming_rule.interface_should_be_begins_with_i.severity = suggestion
dotnet_naming_rule.interface_should_be_begins_with_i.symbols = interface
dotnet_naming_rule.interface_should_be_begins_with_i.style = begins_with_i

dotnet_naming_rule.types_should_be_pascal_case.severity = suggestion
dotnet_naming_rule.types_should_be_pascal_case.symbols = types
dotnet_naming_rule.types_should_be_pascal_case.style = pascal_case

dotnet_naming_rule.non_field_members_should_be_pascal_case.severity = suggestion
dotnet_naming_rule.non_field_members_should_be_pascal_case.symbols = non_field_members
dotnet_naming_rule.non_field_members_should_be_pascal_case.style = pascal_case

# Symbol specifications

dotnet_naming_symbols.interface.applicable_kinds = interface
dotnet_naming_symbols.interface.applicable_accessibilities = public, internal, private, protected, protected_internal, private_protected
dotnet_naming_symbols.interface.required_modifiers =

dotnet_naming_symbols.types.applicable_kinds = class, struct, interface, enum
dotnet_naming_symbols.types.applicable_accessibilities = public, internal, private, protected, protected_internal, private_protected
dotnet_naming_symbols.types.required_modifiers =

dotnet_naming_symbols.non_field_members.applicable_kinds = property, event, method
dotnet_naming_symbols.non_field_members.applicable_accessibilities = public, internal, private, protected, protected_internal, private_protected
dotnet_naming_symbols.non_field_members.required_modifiers =

# Naming styles

dotnet_naming_style.begins_with_i.required_prefix = I
dotnet_naming_style.begins_with_i.required_suffix =
dotnet_naming_style.begins_with_i.word_separator =
dotnet_naming_style.begins_with_i.capitalization = pascal_case

dotnet_naming_style.pascal_case.required_prefix =
dotnet_naming_style.pascal_case.required_suffix =
dotnet_naming_style.pascal_case.word_separator =
dotnet_naming_style.pascal_case.capitalization = pascal_case

dotnet_naming_style.pascal_case.required_prefix =
dotnet_naming_style.pascal_case.required_suffix =
dotnet_naming_style.pascal_case.word_separator =
dotnet_naming_style.pascal_case.capitalization = pascal_case
dotnet_style_coalesce_expression = true:suggestion
dotnet_style_null_propagation = true:suggestion
dotnet_style_prefer_is_null_check_over_reference_equality_method = true:suggestion
dotnet_style_prefer_auto_properties = true:silent
dotnet_style_object_initializer = true:suggestion
dotnet_style_collection_initializer = true:suggestion
dotnet_style_prefer_simplified_boolean_expressions = true:suggestion
dotnet_style_prefer_conditional_expression_over_assignment = true:silent
dotnet_style_prefer_conditional_expression_over_return = true:silent
dotnet_style_explicit_tuple_names = true:suggestion
dotnet_style_prefer_inferred_tuple_names = true:suggestion
dotnet_style_prefer_inferred_anonymous_type_member_names = true:suggestion
dotnet_style_prefer_compound_assignment = true:suggestion
dotnet_style_prefer_simplified_interpolation = true:suggestion
dotnet_style_namespace_match_folder = true:suggestion
dotnet_style_operator_placement_when_wrapping = beginning_of_line
tab_width = 4
indent_size = 4
end_of_line = lf
insert_final_newline = false
4 changes: 2 additions & 2 deletions .github/workflows/CreateReleaseApps.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
run: echo "::set-output name=date::$(date +'%Y-%m-%d')"
- name: Get branch name
id: branch-name
uses: tj-actions/branch-names@v6
uses: tj-actions/branch-names@v7.0.7
- name: Setup .NET
uses: actions/setup-dotnet@v2
with:
Expand Down Expand Up @@ -64,7 +64,7 @@ jobs:
run: echo "::set-output name=date::$(date +'%Y-%m-%d')"
- name: Get branch name
id: branch-name
uses: tj-actions/branch-names@v6
uses: tj-actions/branch-names@v7.0.7
- name: Setup .NET
uses: actions/setup-dotnet@v2
with:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/docker-image.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
- uses: actions/checkout@v3
- name: Get branch name
id: branch-name
uses: tj-actions/branch-names@v6
uses: tj-actions/branch-names@v7.0.7
- name: Login
env:
DOCKER_USER: ${{secrets.DOCKER_USER}}
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/dotnet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
run: echo "::set-output name=date::$(date +'%Y-%m-%d')"
- name: Get branch name
id: branch-name
uses: tj-actions/branch-names@v6
uses: tj-actions/branch-names@v7.0.7
- name: Setup .NET
uses: actions/setup-dotnet@v2
with:
Expand Down
26 changes: 21 additions & 5 deletions DiscordPlayerCountBot.Tests/DockerConfigurationTests.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Microsoft.Extensions.DependencyInjection;
using EnvironmentHelper = DiscordPlayerCountBot.Tests.Environment.EnvironmentHelper;

namespace DiscordPlayerCountBot.Tests;
Expand All @@ -14,7 +15,10 @@ public async Task DockerConfigurationTestWithAllData()
var bots = new Dictionary<string, Bot>();
var time = -1;

var dockerConfiguration = new DockerConfiguration();
var serviceProvider = new ServiceCollection()
.BuildServiceProvider();

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

bots = configuration.Item1;
Expand All @@ -34,7 +38,10 @@ public async Task DockerConfigurationWithDuplicateAddresses()
{
EnvironmentHelper.SetTestEnvironmentWithDuplicateAddresses();

var dockerConfiguration = new DockerConfiguration();
var services = new ServiceCollection()
.BuildServiceProvider();

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

EnvironmentHelper.ClearTestEnvironmentVariables();
Expand All @@ -56,7 +63,10 @@ public async Task DockerConfigurationTestWithoutBattleMetrics()
var bots = new Dictionary<string, Bot>();
var time = -1;

var dockerConfiguration = new DockerConfiguration();
var services = new ServiceCollection()
.BuildServiceProvider();

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

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

var dockerConfiguration = new DockerConfiguration();
var services = new ServiceCollection()
.BuildServiceProvider();

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

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

var dockerConfiguration = new DockerConfiguration();
var services = new ServiceCollection()
.BuildServiceProvider();

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

bots = configuration.Item1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,4 @@ public static void ClearTestEnvironmentVariables()
System.Environment.SetEnvironmentVariable("BOT_APPLICATION_VARIABLES", null);
}
}
}
}
2 changes: 1 addition & 1 deletion DiscordPlayerCountBot.Tests/JsonTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace DiscordPlayerCountBot.Tests;
namespace PlayerCountBot.Tests;

[Collection("Json Serialization Test Suite")]
public class JsonTests
Expand Down
4 changes: 2 additions & 2 deletions DiscordPlayerCountBot.Tests/SunMoonTagTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace DiscordPlayerCountBot.Tests
namespace PlayerCountBot.Tests
{
[Collection("Sun & Moon Tag Test Suite")]
public class SunMoonTagTests
Expand Down Expand Up @@ -75,4 +75,4 @@ public void ShouldOutputCorrectValue(string time)
Assert.Equal(sunMoonPhase, output);
}
}
}
}
2 changes: 1 addition & 1 deletion DiscordPlayerCountBot.Tests/Usings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

global using PlayerCountBot;
global using PlayerCountBot.Configuration;
global using DiscordPlayerCountBot.Extensions;
global using PlayerCountBot.Extensions;
global using PlayerCountBot.Json;
2 changes: 1 addition & 1 deletion DiscordPlayerCountBot/Attributes/AttributeHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ public static string GetNameFromAttribute(object obj)
return label;
}
}
}
}
3 changes: 1 addition & 2 deletions DiscordPlayerCountBot/Attributes/NameAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
namespace PlayerCountBot.Attributes
{

[AttributeUsage(AttributeTargets.Property | AttributeTargets.Class)]
public class NameAttribute : Attribute
{
Expand All @@ -11,4 +10,4 @@ public NameAttribute(string name)
Name = name;
}
}
}
}
41 changes: 22 additions & 19 deletions DiscordPlayerCountBot/Bot/Bot.cs
Original file line number Diff line number Diff line change
@@ -1,34 +1,32 @@
namespace PlayerCountBot
using PlayerCountBot.Extensions;
using Microsoft.Extensions.DependencyInjection;

namespace PlayerCountBot
{

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

public Bot(BotInformation info, Dictionary<string, string> applicationTokens) : base(info)
public Bot(BotInformation info, Dictionary<string, string> applicationTokens, IServiceProvider services)
{
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()
{
HandlerTimeout = null
});

InitDataProviders();
}

public void InitDataProviders()
{
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!));
DataProviders = services.GetServices<IServerInformationProvider>()
.ToDictionary(value => value.GetRequiredProviderType());
}

public async Task StartAsync(bool shouldStart)
Expand All @@ -38,7 +36,7 @@ public async Task StartAsync(bool shouldStart)
Information.Address = await AddressHelper.ResolveAddress(Information.Address);
}

Info($"Loaded {Information.Name} at address and port: {Information.Address}, {Information.ProviderType}");
Info($"Loaded {Information.Name} ({Information.Id}) at address and port: {Information.Address}, {(DataProvider)Information.ProviderType}");
await DiscordClient.LoginAndStartAsync(Information.Token, Information.Address, shouldStart);
}

Expand All @@ -49,20 +47,25 @@ public async Task StopAsync()

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

if (dataProviderType != Information.ProviderType)
if (dataProviderInt != Information.ProviderType)
{
Warn($"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}", Information.Id.ToString());
}

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}");
Warn($"Config for bot at address: {Information.Address} has an invalid activity type: {Information.Status}", Information.Id.ToString());
}

var dataProviderType = (DataProvider)dataProviderInt;

if (!DataProviders.ContainsKey(dataProviderType))
throw new Exception($"Missing Data Provider for Type: {dataProviderType}");

var dataProvider = DataProviders[dataProviderType];
var serverInformation = await dataProvider.GetServerInformation(Information, ApplicationTokens);

Expand Down
3 changes: 1 addition & 2 deletions DiscordPlayerCountBot/Bot/BotConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ public class BotConfig
public Dictionary<string, string> ApplicationTokens { get; set; } = new();
public void CreateDefaults()
{

ServerInformation.Add(new()
{
Name = "TestBot",
Expand All @@ -22,4 +21,4 @@ public void CreateDefaults()
ApplicationTokens.Add("BattleMetricsKey", "Here");
}
}
}
}
3 changes: 2 additions & 1 deletion DiscordPlayerCountBot/Bot/BotInformation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class BotInformation
public string? StatusFormat { get; set; }
public int? SunriseHour { get; set; }
public int? SunsetHour { get; set; }
public string? RconServiceName { get; set; }

public Tuple<string, ushort> GetAddressAndPort()
{
Expand All @@ -32,4 +33,4 @@ public Tuple<string, ushort> GetAddressAndPort()
}
}
}
}
}
3 changes: 2 additions & 1 deletion DiscordPlayerCountBot/Configuration/Base/IConfigurable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
{
public interface IConfigurable
{
public HostEnvironment GetRequiredEnvironment();
Task<Tuple<Dictionary<string, Bot>, int>> Configure(bool shouldStart = true);
}
}
}
Loading

0 comments on commit 7873c22

Please sign in to comment.