-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from Mishin870/dev/scheduler
Tasks scheduler
- Loading branch information
Showing
91 changed files
with
800 additions
and
387 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
namespace HuTaoHelper.Console.Commands; | ||
|
||
/// <summary> | ||
/// Special command version for easy async operations handling | ||
/// </summary> | ||
public abstract class AsyncCommand : ICommand { | ||
public void Execute(List<string> args) { | ||
Task.Run(() => ExecuteAsync(args)).Wait(); | ||
} | ||
|
||
/// <summary> | ||
/// Need to override this in order to execute async operations | ||
/// </summary> | ||
/// <param name="args">Command line arguments</param> | ||
/// <returns></returns> | ||
protected abstract Task ExecuteAsync(List<string> args); | ||
|
||
public abstract string Help { get; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
namespace HuTaoHelper.Console.Commands; | ||
|
||
/// <summary> | ||
/// Any executable command for HuTaoHelper.Console | ||
/// </summary> | ||
public interface ICommand { | ||
/// <summary> | ||
/// Run command with arguments | ||
/// </summary> | ||
/// <param name="args">Command line arguments</param> | ||
void Execute(List<string> args); | ||
|
||
/// <summary> | ||
/// Get help on command for displaying if something goes wrong | ||
/// </summary> | ||
string Help { get; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
using HuTaoHelper.Console.Commands.Impl; | ||
using HuTaoHelper.Core.Core; | ||
|
||
namespace HuTaoHelper.Console.Commands; | ||
|
||
/// <summary> | ||
/// All commands database | ||
/// </summary> | ||
public static class CommandsRegistry { | ||
private static readonly Dictionary<string, ICommand> Commands = new() { | ||
{ "help", new CommandHelp() }, | ||
{ "daily", new CommandDaily() }, | ||
}; | ||
|
||
/// <summary> | ||
/// Get command by name or null if it doesn't exist | ||
/// </summary> | ||
/// <param name="name">Command name</param> | ||
/// <returns>Command or null if there is no such command</returns> | ||
public static ICommand? Get(string name) { | ||
return Commands.GetValueOrDefault(name.ToLowerInvariant()); | ||
} | ||
|
||
/// <summary> | ||
/// Print help for selected commands to console | ||
/// </summary> | ||
/// <param name="forCommand">Command name filter or null to display all commands</param> | ||
public static void PrintHelp(string? forCommand = null) { | ||
Logging.PostEvent(@"All commands:"); | ||
Logging.PostEvent(""); | ||
|
||
foreach (var (commandName, command) in Commands) { | ||
if (forCommand != null && commandName != forCommand) continue; | ||
|
||
Logging.PostEvent(@$"{commandName} {command.Help}"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
using HuTaoHelper.Core.Core; | ||
using HuTaoHelper.Core.Web.Tools; | ||
|
||
namespace HuTaoHelper.Console.Commands.Impl; | ||
|
||
/// <summary> | ||
/// Command to do daily check-ins for all accounts automatically | ||
/// </summary> | ||
public class CommandDaily : AsyncCommand { | ||
protected override async Task ExecuteAsync(List<string> args) { | ||
var count = Settings.Instance.Accounts.Count; | ||
var current = 1; | ||
var random = new Random(); | ||
|
||
foreach (var (_, account) in Settings.Instance.Accounts) { | ||
Logging.PostEvent($"Processing \"{account.Name}\" account [{current} / {count}]"); | ||
await DailyCheckIn.DoCheckInAsync(account); | ||
|
||
var seconds = 2 + random.Next(3); | ||
Logging.PostEvent($"Wait random time to be safe: {seconds} seconds"); | ||
await Task.Delay(TimeSpan.FromSeconds(seconds)); | ||
|
||
Logging.PostEvent(""); | ||
current++; | ||
} | ||
} | ||
|
||
public override string Help => "- do daily check-in for all accounts"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
namespace HuTaoHelper.Console.Commands.Impl; | ||
|
||
/// <summary> | ||
/// Show help for another commands | ||
/// </summary> | ||
public class CommandHelp : ICommand { | ||
public void Execute(List<string> args) { | ||
if (args.Count >= 1) { | ||
CommandsRegistry.PrintHelp(args[0]); | ||
} else { | ||
CommandsRegistry.PrintHelp(); | ||
} | ||
} | ||
|
||
public string Help => "[command_name] - get help for commands"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>WinExe</OutputType> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
<LangVersion>default</LangVersion> | ||
<PackageId /> | ||
<PackageVersion /> | ||
<Authors /> | ||
<Company>Mishin870</Company> | ||
<Product>HuTaoHelper.Console</Product> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\HuTaoHelper.Core\HuTaoHelper.Core.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
using System.Reflection; | ||
using HuTaoHelper.Console.Commands; | ||
using HuTaoHelper.Core.Core; | ||
|
||
namespace HuTaoHelper.Console; | ||
|
||
public class Program { | ||
public static void Main(string[] args) { | ||
Logging.EventsProcessor = (text, _) => { | ||
var path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); | ||
if (path == null) return; | ||
|
||
var content = $"[{DateTime.Now.ToString("HH:mm:ss")}] {text}\n"; | ||
File.AppendAllText(Path.Combine(path, "HuTaoHelper.Console.log"), content); | ||
}; | ||
|
||
if (args.Length >= 1) { | ||
var commandName = args[0]; | ||
var command = CommandsRegistry.Get(commandName); | ||
|
||
if (command == null) { | ||
Logging.PostEvent(@$"Command not found: {commandName}"); | ||
} else { | ||
Settings.Load(); | ||
command.Execute(args.Skip(1).ToList()); | ||
} | ||
} else { | ||
Logging.PostEvent(@"No command. Usage: HuTaoHelper.Console %command%"); | ||
Logging.PostEvent(""); | ||
CommandsRegistry.PrintHelp(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
Core/AutologinSettings.cs → HuTaoHelper.Core/Core/AutologinSettings.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 1 addition & 4 deletions
5
Core/Migrations/Migrator.cs → HuTaoHelper.Core/Core/Migrations/Migrator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 1 addition & 2 deletions
3
Core/Migrations/SettingsMigrator.cs → ....Core/Core/Migrations/SettingsMigrator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
namespace HuTaoHelper.Core.Core; | ||
|
||
/// <summary> | ||
/// Core module utility class for GUI callbacks\ | ||
/// Will do nothing if the application is running without a GUI | ||
/// </summary> | ||
public static class ViewCallbacks { | ||
/// <summary> | ||
/// GUI callback for refreshing accounts list | ||
/// </summary> | ||
public static Action CallbackRefreshAccountsList = () => { }; | ||
} |
Oops, something went wrong.