Skip to content

Commit

Permalink
Add Steam Query, and Rcon as data providers
Browse files Browse the repository at this point in the history
  • Loading branch information
GravityWolfNotAmused committed Jan 14, 2024
1 parent 588bfc9 commit b42f2b7
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 5 deletions.
3 changes: 2 additions & 1 deletion DiscordPlayerCountBot/Enums/DataProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public enum DataProvider
SCUM,
MINECRAFT,
BATTLEMETRICS,
RCONClient
RCONClient,
SteamQuery
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Net;
using SteamServerQuery;

Check failure on line 1 in DiscordPlayerCountBot/Providers/Base/ServerInformationProvider.cs

View workflow job for this annotation

GitHub Actions / test

The type or namespace name 'SteamServerQuery' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 1 in DiscordPlayerCountBot/Providers/Base/ServerInformationProvider.cs

View workflow job for this annotation

GitHub Actions / test

The type or namespace name 'SteamServerQuery' could not be found (are you missing a using directive or an assembly reference?)
using System.Net;
using System.Net.Http;

namespace PlayerCountBot.Providers.Base
Expand All @@ -17,7 +18,6 @@ protected void HandleLastException(BotInformation information)
{
if (WasLastExecutionAFailure)
{

Info($"Bot named: {information.Name} at address: {information.Address} successfully fetched data after failure.");
LastException = null;
WasLastExecutionAFailure = false;
Expand Down Expand Up @@ -76,6 +76,12 @@ protected void HandleException(Exception e)
return;
}

if(e is SteamException steamException)

Check failure on line 79 in DiscordPlayerCountBot/Providers/Base/ServerInformationProvider.cs

View workflow job for this annotation

GitHub Actions / dotnet_format

DiscordPlayerCountBot/Providers/Base/ServerInformationProvider.cs#L79

WHITESPACE: Fix whitespace formatting. Insert '\s'.
{
Error($"There was an issue speaking with Steam Query Server.", e);
return;
}

Error($"There was an error speaking with {Label}.", e);
throw e;
}
Expand Down
1 change: 0 additions & 1 deletion DiscordPlayerCountBot/Providers/RconProvider.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using DiscordPlayerCountBot.Enums;
using DiscordPlayerCountBot.Enums;
using DiscordPlayerCountBot.Exceptions;
using DiscordPlayerCountBot.Services;

Expand Down
37 changes: 37 additions & 0 deletions DiscordPlayerCountBot/Providers/SteamQueryProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using DiscordPlayerCountBot.Services.SteamQuery;

namespace PlayerCountBot.Providers
{
[Name("Steam Query")]
public class SteamQueryProvider : ServerInformationProvider
{
public SteamQueryProvider(BotInformation info) : base(info)
{
}

public async override Task<BaseViewModel?> GetServerInformation(BotInformation information, Dictionary<string, string> applicationVariables)
{
var service = new SteamQueryService();

try
{
var addressAndPort = information.GetAddressAndPort();
var response = await service.GetQueryResponse(addressAndPort.Item1, addressAndPort.Item2);

if (response == null)
{
throw new ApplicationException($" Failed to get a Server Information response from Steam Query.");
}

HandleLastException(information);

Check failure on line 26 in DiscordPlayerCountBot/Providers/SteamQueryProvider.cs

View workflow job for this annotation

GitHub Actions / dotnet_format

DiscordPlayerCountBot/Providers/SteamQueryProvider.cs#L26

WHITESPACE: Fix whitespace formatting. Replace 34 characters with '\r\n\r\n\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s'.

return response;
}
catch (Exception e)
{
HandleException(e);
return null;
}
}
}
}
2 changes: 1 addition & 1 deletion DiscordPlayerCountBot/Services/Rcon/RconService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace DiscordPlayerCountBot.Services
{
public class RconService : IRconService
{
private Dictionary<RconServiceType, RconServiceInformation> PlayerCommands = new()
private readonly Dictionary<RconServiceType, RconServiceInformation> PlayerCommands = new()
{
{RconServiceType.CSGO, new RconServiceInformation("status", new CSGOInformationParser())},
{RconServiceType.Minecraft, new RconServiceInformation("list", new MinecraftInformationParser()) },
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using DiscordPlayerCountBot.Enums;

namespace DiscordPlayerCountBot.Services.SteamQuery
{
internal interface ISteamQueryService
{
public Task<BaseViewModel> GetQueryResponse(string address, int port);
}
}
29 changes: 29 additions & 0 deletions DiscordPlayerCountBot/Services/SteamQuery/SteamQueryService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using SteamServerQuery;

Check failure on line 1 in DiscordPlayerCountBot/Services/SteamQuery/SteamQueryService.cs

View workflow job for this annotation

GitHub Actions / test

The type or namespace name 'SteamServerQuery' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 1 in DiscordPlayerCountBot/Services/SteamQuery/SteamQueryService.cs

View workflow job for this annotation

GitHub Actions / test

The type or namespace name 'SteamServerQuery' could not be found (are you missing a using directive or an assembly reference?)

namespace DiscordPlayerCountBot.Services.SteamQuery
{
public class SteamQueryService : ISteamQueryService

Check failure on line 5 in DiscordPlayerCountBot/Services/SteamQuery/SteamQueryService.cs

View workflow job for this annotation

GitHub Actions / test

'SteamQueryService' does not implement interface member 'ISteamQueryService.GetQueryResponse(string, int)'

Check failure on line 5 in DiscordPlayerCountBot/Services/SteamQuery/SteamQueryService.cs

View workflow job for this annotation

GitHub Actions / test

'SteamQueryService' does not implement interface member 'ISteamQueryService.GetQueryResponse(string, int)'
{
public async Task<BaseViewModel> GetQueryResponse(string address, int port, string authorizationToken)
{
var model = new BaseViewModel()
{
Address = address,
Port = port
};

try
{
var serverInformation = await SteamServer.QueryServerAsync(address, port = 0);
model.MaxPlayers = serverInformation.MaxPlayers;
model.Players = serverInformation.Players;
model.QueuedPlayers = 0;
}catch

Check failure on line 21 in DiscordPlayerCountBot/Services/SteamQuery/SteamQueryService.cs

View workflow job for this annotation

GitHub Actions / dotnet_format

DiscordPlayerCountBot/Services/SteamQuery/SteamQueryService.cs#L21

WHITESPACE: Fix whitespace formatting. Insert '\r\n\s\s\s\s\s\s\s\s\s\s\s\s'.
{
throw;
}

return model;
}
}
}

0 comments on commit b42f2b7

Please sign in to comment.