Skip to content

Commit

Permalink
Added ability to start the generation of the stats through the bot.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeroen Heijster committed May 15, 2022
1 parent 44e5b70 commit 0d31d07
Show file tree
Hide file tree
Showing 13 changed files with 210 additions and 16 deletions.
2 changes: 1 addition & 1 deletion StatBot/Classes/LogFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// Created : 12-11-2017
//
// Last Modified By : Jeroen Heijster
// Last Modified On : 13-05-2022
// Last Modified On : 14-05-2022
// ***********************************************************************
// <copyright file="LogFile.cs">
// Copyright © 2022
Expand Down
2 changes: 1 addition & 1 deletion StatBot/Handlers/ConnectionHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ internal class ConnectionHandler
/// </summary>
BotSettings _botSettings;
/// <summary>
/// Initializes a new instance of the <see cref="ConnectionHandler"/> class.
/// Initializes a new instance of the <see cref="ConnectionHandler" /> class.
/// </summary>
/// <param name="client">The client.</param>
/// <param name="logHandler">The log handler.</param>
Expand Down
6 changes: 3 additions & 3 deletions StatBot/Handlers/FileHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
// Created : 12-11-2017
//
// Last Modified By : Jeroen Heijster
// Last Modified On : 13-05-2022
// Last Modified On : 14-05-2022
// ***********************************************************************
// <copyright file="FileHelper.cs">
// Copyright © 2017
// <copyright file="FileHandler.cs">
// Copyright © 2022
// </copyright>
// <summary></summary>
// ***********************************************************************
Expand Down
4 changes: 2 additions & 2 deletions StatBot/Handlers/LogHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// Created : 13-05-2022
//
// Last Modified By : Jeroen Heijster
// Last Modified On : 13-05-2022
// Last Modified On : 14-05-2022
// ***********************************************************************
// <copyright file="LogHandler.cs">
// Copyright © 2022
Expand Down Expand Up @@ -41,7 +41,7 @@ internal class LogHandler
/// </summary>
private PushoverMessageHandler _pushoverMessageHandler;
/// <summary>
/// Initializes a new instance of the <see cref="LogHandler"/> class.
/// Initializes a new instance of the <see cref="LogHandler" /> class.
/// </summary>
/// <param name="botSettings">The bot settings.</param>
public LogHandler(BotSettings botSettings)
Expand Down
98 changes: 98 additions & 0 deletions StatBot/Handlers/mIRCStatsHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
// ***********************************************************************
// Assembly : StatBot
// Author : Jeroen Heijster
// Created : 15-05-2022
//
// Last Modified By : Jeroen Heijster
// Last Modified On : 15-05-2022
// ***********************************************************************
// <copyright file="mIRCStatsHandler.cs">
// Copyright © 2022
// </copyright>
// <summary></summary>
// ***********************************************************************
using StatBot.Settings;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StatBot.Handlers
{
/// <summary>
/// Class mIRCStatsHandler.
/// </summary>
internal class mIRCStatsHandler
{
/// <summary>
/// The bot settings
/// </summary>
private static BotSettings _botSettings;
/// <summary>
/// The folder
/// </summary>
private static string folder;
/// <summary>
/// The sleeping time
/// </summary>
private static int sleepingTime;

/// <summary>
/// Initializes a new instance of the <see cref="mIRCStatsHandler"/> class.
/// </summary>
/// <param name="botSettings">The bot settings.</param>
public mIRCStatsHandler(BotSettings botSettings)
{
_botSettings = botSettings;
}
/// <summary>
/// Generate stats as an asynchronous operation.
/// </summary>
/// <returns>A Task representing the asynchronous operation.</returns>
public async Task GenerateStatsAsync()
{
if (!string.IsNullOrEmpty(_botSettings.mIRCStats.GeneratorFile))
{
System.IO.FileInfo fileinfo = new System.IO.FileInfo(_botSettings.mIRCStats.GeneratorFile);
folder = fileinfo.Directory.FullName;
sleepingTime = _botSettings.mIRCStats.LaunchEveryMinutes * 60000; //convert from minutes to ms.
while (true)
if (StatsGenerator().IsFaulted)
System.Threading.Thread.Sleep(1800000); // Sleep for half an hour if the process couldn't be started.
}
}

/// <summary>
/// Runs the statistics generator
/// </summary>
/// <returns>Task.</returns>
private Task StatsGenerator()
{
System.Threading.Thread.Sleep(sleepingTime);
try
{
ProcessStartInfo pInfo = new ProcessStartInfo();
pInfo.FileName = _botSettings.mIRCStats.GeneratorFile;
pInfo.WorkingDirectory = folder;
pInfo.WindowStyle = ProcessWindowStyle.Maximized;
//Start the process.
Process p = Process.Start(pInfo);
Console.WriteLine("");

if (_botSettings.mIRCStats.WaitUntilCompleted)
{
//Wait for the process to end.
p.WaitForExit();
}
}
catch (Exception ex)
{
Console.WriteLine("mIRCStats couldn't launch with GeneratorFile. Exception: " + ex.Message);
return Task.FromException(ex);
}
return Task.CompletedTask;
}
}
}
5 changes: 4 additions & 1 deletion StatBot/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// Created : 12-11-2017
//
// Last Modified By : Jeroen Heijster
// Last Modified On : 13-05-2022
// Last Modified On : 15-05-2022
// ***********************************************************************
// <copyright file="Program.cs">
// Copyright © 2022
Expand Down Expand Up @@ -41,6 +41,8 @@ public Task MainAsync()
IServiceProvider provider = serviceScope.ServiceProvider;
var workerInstance = provider.GetRequiredService<Worker>();
workerInstance.DoWork();
var mircStatsGeneratorInstance = provider.GetRequiredService<mIRCStatsGenerator>();
mircStatsGeneratorInstance.DoWork();
host.Run();
return Task.CompletedTask;
}
Expand All @@ -63,6 +65,7 @@ static IHostBuilder CreateDefaultBuilder()
})
.ConfigureServices(services =>
{
services.AddSingleton<mIRCStatsGenerator>();
services.AddSingleton<Worker>();
});
}
Expand Down
2 changes: 1 addition & 1 deletion StatBot/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// Created : 05-11-2022
//
// Last Modified By : Jeroen Heijster
// Last Modified On : 13-05-2022
// Last Modified On : 14-05-2022
// ***********************************************************************
// <copyright file="AssemblyInfo.cs">
// Copyright © 2022
Expand Down
2 changes: 1 addition & 1 deletion StatBot/PushoverMessaging/PushoverMessageHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// Created : 12-05-2022
//
// Last Modified By : Jeroen Heijster
// Last Modified On : 13-05-2022
// Last Modified On : 14-05-2022
// ***********************************************************************
// <copyright file="PushoverMessageHandler.cs">
// Copyright © 2022
Expand Down
25 changes: 24 additions & 1 deletion StatBot/Settings/BotSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// Created : 13-05-2022
//
// Last Modified By : Jeroen Heijster
// Last Modified On : 13-05-2022
// Last Modified On : 15-05-2022
// ***********************************************************************
// <copyright file="BotSettings.cs">
// Copyright © 2022
Expand Down Expand Up @@ -38,6 +38,11 @@ public BotSettings(IConfiguration configuration)
VerifySettings();
}

/// <summary>
/// Verifies the settings.
/// </summary>
/// <exception cref="System.Exception">Discord bot token missing.</exception>
/// <exception cref="System.Exception">Logging file name missing.</exception>
private void VerifySettings()
{
if (String.IsNullOrEmpty(Discord.Token))
Expand Down Expand Up @@ -188,6 +193,9 @@ public MIRCStats(IConfiguration configuration)
Path = configuration.GetValue<string>("MIRCStats:Path");
NicksFile = configuration.GetValue<string>("MIRCStats:NicksFile");
NickSection = configuration.GetValue<string>("MIRCStats:NickSection");
LaunchEveryMinutes = configuration.GetValue<int>("MIRCStats:LaunchEveryMinutes");
GeneratorFile = configuration.GetValue<string>("MIRCStats:GeneratorFile");
WaitUntilCompleted = configuration.GetValue<bool>("MIRCStats:WaitUntilCompleted");
}

/// <summary>
Expand All @@ -205,6 +213,21 @@ public MIRCStats(IConfiguration configuration)
/// </summary>
/// <value>The nick section.</value>
public string NickSection { get; }
/// <summary>
/// Gets how often it should launch mIRCStats in minutes.
/// </summary>
/// <value>The value of how often it should launch mIRCStats in minutes.</value>
public int LaunchEveryMinutes { get; }
/// <summary>
/// Gets the generator file.
/// </summary>
/// <value>The generator file.</value>
public string GeneratorFile { get; }
/// <summary>
/// Gets a value indicating whether [wait until completed].
/// </summary>
/// <value><c>true</c> if [wait until completed]; otherwise, <c>false</c>.</value>
public bool WaitUntilCompleted { get; }
}

/// <summary>
Expand Down
4 changes: 1 addition & 3 deletions StatBot/Worker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// Created : 05-13-2022
//
// Last Modified By : Jeroen Heijster
// Last Modified On : 14-05-2022
// Last Modified On : 15-05-2022
// ***********************************************************************
// <copyright file="Worker.cs">
// Copyright © 2022
Expand Down Expand Up @@ -82,9 +82,7 @@ public void DoWork()
_client.Disconnected += _connectionHandler.Client_Disconnected;
_client.LoginAsync(TokenType.Bot, _botSettings.Discord.Token);
_client.StartAsync();

_logHandler.LogMessage($"Connected to the server at {DateTime.Now}.", _client);

}
}
}
8 changes: 7 additions & 1 deletion StatBot/appsettings.dev.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,13 @@
"mIRCStats": {
"Path": "",
"NicksFile": "nicks.txt",
"NickSection": "[common]"
"NickSection": "[common]",
//Leave value empty if you don't want to use it.
"GeneratorFile": "C:\\Discord\\Statbot\\mircstats\\Generate Stats.bat",
//If true, it will wait until the generation of the stats is completed and then start the timer for LaunchEveryMinutes. If false, it will start a timer after starting generating, even if it is still running (which may cause multiple stats being generated at the same time).
"WaitUntilCompleted": true,
//Delay until the stats will be generated again.
"LaunchEveryMinutes": 0
},
"Application": {
// Valid options:
Expand Down
9 changes: 8 additions & 1 deletion StatBot/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,16 @@
}
},
"mIRCStats": {
//The path where mircstats is located. Required for the include and exclude from stats commands. Every backslash has to be escaped with a backslash. E.g. C:\mircstats would be c:\\mircstats.
"Path": "",
"NicksFile": "nicks.txt",
"NickSection": "[common]"
"NickSection": "[common]",
//Leave value empty if you don't want to use it. Every backslash has to be escaped with a backslash. E.g. C:\mircstats would be c:\\mircstats.
"GeneratorFile": "C:\\Discord\\Statbot\\mircstats\\Generate Stats.bat",
//If true, it will wait until the generation of the stats is completed and then start the timer for LaunchEveryMinutes. If false, it will start a timer after starting generating, even if it is still running (which may cause multiple stats being generated at the same time).
"WaitUntilCompleted": true,
//Delay until the stats will be generated again.
"LaunchEveryMinutes": 15
},
"Application": {
// Valid options:
Expand Down
59 changes: 59 additions & 0 deletions StatBot/mIRCStatsGenerator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// ***********************************************************************
// Assembly : StatBot
// Author : Jeroen Heijster
// Created : 15-05-2022
//
// Last Modified By : Jeroen Heijster
// Last Modified On : 15-05-2022
// ***********************************************************************
// <copyright file="mIRCStatsGenerator.cs">
// Copyright © 2022
// </copyright>
// <summary></summary>
// ***********************************************************************
using Microsoft.Extensions.Configuration;
using StatBot.Handlers;
using StatBot.Settings;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StatBot
{
/// <summary>
/// Class mIRCStatsGenerator.
/// </summary>
internal class mIRCStatsGenerator
{
/// <summary>
/// The bot settings
/// </summary>
private BotSettings _botSettings;

/// <summary>
/// The configuration
/// </summary>
private readonly IConfiguration _configuration;

/// <summary>
/// Initializes a new instance of the <see cref="Worker" /> class.
/// </summary>
/// <param name="configuration">The configuration.</param>
public mIRCStatsGenerator(IConfiguration configuration)
{
_configuration = configuration;
}

/// <summary>
/// Does the work.
/// </summary>
public void DoWork()
{
_botSettings = new BotSettings(_configuration);
mIRCStatsHandler mircStatsHandler = new mIRCStatsHandler(_botSettings);
_ = mircStatsHandler.GenerateStatsAsync();
}
}
}

0 comments on commit 0d31d07

Please sign in to comment.