Skip to content

Commit

Permalink
Merge pull request #3 from Mishin870/dev/scheduler
Browse files Browse the repository at this point in the history
Tasks scheduler
  • Loading branch information
Mishin870 authored Nov 7, 2022
2 parents 65ec8c5 + 08fcefd commit fe1ebbd
Show file tree
Hide file tree
Showing 91 changed files with 800 additions and 387 deletions.
4 changes: 0 additions & 4 deletions App.xaml.cs

This file was deleted.

19 changes: 19 additions & 0 deletions HuTaoHelper.Console/Commands/AsyncCommand.cs
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; }
}
17 changes: 17 additions & 0 deletions HuTaoHelper.Console/Commands/Command.cs
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; }
}
38 changes: 38 additions & 0 deletions HuTaoHelper.Console/Commands/CommandsRegistry.cs
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}");
}
}
}
29 changes: 29 additions & 0 deletions HuTaoHelper.Console/Commands/Impl/CommandDaily.cs
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";
}
16 changes: 16 additions & 0 deletions HuTaoHelper.Console/Commands/Impl/CommandHelp.cs
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";
}
20 changes: 20 additions & 0 deletions HuTaoHelper.Console/HuTaoHelper.Console.csproj
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>
33 changes: 33 additions & 0 deletions HuTaoHelper.Console/Program.cs
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();
}
}
}
6 changes: 2 additions & 4 deletions Core/Account.cs → HuTaoHelper.Core/Core/Account.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
using System;
using System.Threading.Tasks;
using HuTaoHelper.Web.Client;
using HuTaoHelper.Core.Web.Client;
using Newtonsoft.Json;

namespace HuTaoHelper.Core;
namespace HuTaoHelper.Core.Core;

/// <summary>
/// User account information
Expand Down
17 changes: 4 additions & 13 deletions Core/ApiCookies.cs → HuTaoHelper.Core/Core/ApiCookies.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
using System.Collections.Generic;
using System.Linq;
using Microsoft.Web.WebView2.Core;

namespace HuTaoHelper.Core;
namespace HuTaoHelper.Core.Core;

public class ApiCookies {
public Dictionary<string, string> Values { get; set; } = new();
Expand All @@ -20,19 +16,14 @@ public string ToCookie() {
return string.Join("; ", Values.Select(pair => $"{pair.Key}={pair.Value}"));
}

public bool ParseFrom(List<CoreWebView2Cookie> cookies) {
public bool ParseFrom(Dictionary<string, string> cookies) {
Values.Clear();

var remapped = new Dictionary<string, string>();
foreach (var cookie in cookies) {
remapped[cookie.Name] = cookie.Value;
}

foreach (var required in Constants.ApiRequiredCookies) {
if (!remapped.ContainsKey(required)) {
if (!cookies.ContainsKey(required)) {
return false;
} else {
Values[required] = remapped[required];
Values[required] = cookies[required];
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace HuTaoHelper.Core;
namespace HuTaoHelper.Core.Core;

/// <summary>
/// Settings for autologin process (e.g. delays between operations in ms)
Expand Down
3 changes: 1 addition & 2 deletions Core/Constants.cs → HuTaoHelper.Core/Core/Constants.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
using System;
using System.Diagnostics.CodeAnalysis;
using System.Text;

namespace HuTaoHelper.Core;
namespace HuTaoHelper.Core.Core;

/// <summary>
/// All constants. Strings are packed to prevent direct github search for them, sorry
Expand Down
15 changes: 6 additions & 9 deletions Core/Logging.cs → HuTaoHelper.Core/Core/Logging.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
using System;
using MaterialDesignThemes.Wpf;

namespace HuTaoHelper.Core;
namespace HuTaoHelper.Core.Core;

/// <summary>
/// Logging utilities
/// </summary>
public static class Logging {
public static readonly SnackbarMessageQueue EventQueue = new();
public static Action<object?, int> EventsProcessor = (text, durationMs) => {
Console.WriteLine(@$"{text}");
};

/// <summary>
/// Post a text message to application event log. By default it's a SnackBar at the bottom
Expand All @@ -16,9 +15,7 @@ public static class Logging {
/// <param name="durationMs">Duration of the message in milliseconds</param>
public static void PostEvent(object? text, int durationMs = 1000) {
if (text == null) return;

EventQueue.Enqueue($"{text}", null, null,
null, false, false,
TimeSpan.FromMilliseconds(durationMs));

EventsProcessor(text, durationMs);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using System;
using System.Collections.Generic;

namespace HuTaoHelper.Core.Migrations;
namespace HuTaoHelper.Core.Core.Migrations;

/// <summary>
/// Smart migration service for any system entity
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
using System;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace HuTaoHelper.Core.Migrations;
namespace HuTaoHelper.Core.Core.Migrations;

/// <summary>
/// Migrator for <c>Settings</c>
Expand Down
5 changes: 1 addition & 4 deletions Core/Scheduler.cs → HuTaoHelper.Core/Core/Scheduler.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using System;
using System.Collections.Generic;

namespace HuTaoHelper.Core;
namespace HuTaoHelper.Core.Core;

/// <summary>
/// Scheduler for binding to delayed events
Expand Down
17 changes: 7 additions & 10 deletions Core/Settings.cs → HuTaoHelper.Core/Core/Settings.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Diagnostics.CodeAnalysis;
using System.Text;
using HuTaoHelper.Core.Migrations;
using HuTaoHelper.View.Utils;
using HuTaoHelper.Core.Core.Migrations;
using Newtonsoft.Json;

namespace HuTaoHelper.Core;
namespace HuTaoHelper.Core.Core;

/// <summary>
/// App settings including user accounts
Expand All @@ -22,7 +19,7 @@ public class Settings {
/// <summary>
/// Current selected app language
/// </summary>
public string Language;
public string Language = "";

/// <summary>
/// Current free id for new accounts
Expand All @@ -43,7 +40,7 @@ private static string MakeFilePath() {
/// </summary>
public static void Save() {
File.WriteAllText(MakeFilePath(), JsonConvert.SerializeObject(Instance), Encoding.UTF8);
ViewUtils.CallbackRefreshAccountsList();
ViewCallbacks.CallbackRefreshAccountsList();
}

/// <summary>
Expand All @@ -63,13 +60,13 @@ public static void Load() {

if (settings != null) {
Instance = settings;
ViewUtils.CallbackRefreshAccountsList();
ViewCallbacks.CallbackRefreshAccountsList();
return;
}
}

Instance = new Settings();
ViewUtils.CallbackRefreshAccountsList();
ViewCallbacks.CallbackRefreshAccountsList();
}

/// <summary>
Expand Down
12 changes: 12 additions & 0 deletions HuTaoHelper.Core/Core/ViewCallbacks.cs
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 = () => { };
}
Loading

0 comments on commit fe1ebbd

Please sign in to comment.