mirror of
https://github.com/babalae/better-genshin-impact.git
synced 2026-05-23 09:55:48 +08:00
Refactor gear task handling and improve service retrieval
This commit is contained in:
@@ -184,6 +184,11 @@ public partial class App : Application
|
||||
{
|
||||
return _host.Services.GetService(typeof(T)) as T;
|
||||
}
|
||||
|
||||
public static T GetRequiredService<T>() where T : class
|
||||
{
|
||||
return _host.Services.GetRequiredService<T>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets registered service.
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
using System;
|
||||
|
||||
namespace BetterGenshinImpact.Model.Gear.Tasks;
|
||||
|
||||
/// <summary>
|
||||
/// 针对GearTask的引用类
|
||||
/// </summary>
|
||||
[Obsolete]
|
||||
public class GearTaskRefence
|
||||
{
|
||||
/// <summary>
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
using System;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Diagnostics;
|
||||
using System.Threading.Tasks;
|
||||
using BetterGenshinImpact.Model.Gear.Tasks;
|
||||
using BetterGenshinImpact.Service;
|
||||
|
||||
namespace BetterGenshinImpact.Model.Gear.Triggers;
|
||||
|
||||
@@ -11,10 +14,23 @@ public abstract class GearBaseTrigger
|
||||
/// </summary>
|
||||
public string Name { get; set; } = string.Empty;
|
||||
|
||||
public ObservableCollection<GearTaskRefence> GearTaskRefenceList { get; set; } = [];
|
||||
|
||||
/// <summary>
|
||||
/// 是否启用
|
||||
/// </summary>
|
||||
public bool IsEnabled { get; set; } = true;
|
||||
|
||||
/// <summary>
|
||||
/// 任务定义名称
|
||||
/// </summary>
|
||||
public string TaskDefinitionName { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// 执行任务
|
||||
/// </summary>
|
||||
public abstract Task Trigger();
|
||||
public async Task Trigger()
|
||||
{
|
||||
var gearTaskExecutor = App.GetRequiredService<GearTaskExecutor>();
|
||||
await gearTaskExecutor.ExecuteTaskDefinitionAsync(TaskDefinitionName);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -17,60 +17,16 @@ public class HotkeyGearTrigger : GearBaseTrigger
|
||||
/// 热键配置
|
||||
/// </summary>
|
||||
public HotKey? Hotkey { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 是否启用
|
||||
/// </summary>
|
||||
public bool IsEnabled { get; set; } = true;
|
||||
|
||||
/// <summary>
|
||||
/// 冷却时间(毫秒),防止重复触发
|
||||
/// </summary>
|
||||
public int CooldownMs { get; set; } = 1000;
|
||||
|
||||
|
||||
private DateTime _lastExecutionTime = DateTime.MinValue;
|
||||
private bool _isExecuting = false;
|
||||
|
||||
public override async Task Trigger()
|
||||
{
|
||||
// 热键触发器通常不直接调用Run方法
|
||||
// 而是通过热键事件触发ExecuteTasks方法
|
||||
await ExecuteTasks();
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 热键触发时执行任务
|
||||
/// </summary>
|
||||
public async Task OnHotkeyPressed()
|
||||
{
|
||||
if (!IsEnabled || _isExecuting)
|
||||
return;
|
||||
|
||||
// 检查冷却时间
|
||||
var now = DateTime.Now;
|
||||
if ((now - _lastExecutionTime).TotalMilliseconds < CooldownMs)
|
||||
return;
|
||||
|
||||
_lastExecutionTime = now;
|
||||
_isExecuting = true;
|
||||
|
||||
try
|
||||
{
|
||||
await ExecuteTasks();
|
||||
}
|
||||
finally
|
||||
{
|
||||
_isExecuting = false;
|
||||
}
|
||||
}
|
||||
|
||||
private async Task ExecuteTasks()
|
||||
{
|
||||
List<BaseGearTask> list = GearTaskRefenceList.Select(gearTask => gearTask.ToGearTask()).ToList();
|
||||
foreach (var gearTask in list)
|
||||
{
|
||||
await gearTask.Run(CancellationToken.None);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -1,4 +1,7 @@
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.Threading.Tasks;
|
||||
using BetterGenshinImpact.Service;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace BetterGenshinImpact.Model.Gear.Triggers;
|
||||
@@ -19,9 +22,4 @@ public class QuartzCronGearTrigger : GearBaseTrigger
|
||||
/// 使用 Cron 表达式(如果设置,将覆盖 IntervalMs 设置)
|
||||
/// </summary>
|
||||
public string? CronExpression { get; set; }
|
||||
|
||||
public override async Task Trigger()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
@@ -23,7 +23,7 @@ public class QuartzGearTaskJob : IJob
|
||||
{
|
||||
try
|
||||
{
|
||||
var jobDataMap = context.JobDetail.JobDataMap;
|
||||
var jobDataMap = context.MergedJobDataMap;
|
||||
var taskListJson = jobDataMap.GetString("GearTaskReferenceList");
|
||||
var triggerName = jobDataMap.GetString("TriggerName") ?? "Unknown";
|
||||
var triggerId = jobDataMap.GetString("TriggerId") ?? Guid.NewGuid().ToString();
|
||||
|
||||
@@ -11,12 +11,4 @@ namespace BetterGenshinImpact.Model.Gear.Triggers;
|
||||
/// </summary>
|
||||
public class SequentialGearTrigger : GearBaseTrigger
|
||||
{
|
||||
public override async Task Trigger()
|
||||
{
|
||||
List<BaseGearTask> list = GearTaskRefenceList.Select(gearTask => gearTask.ToGearTask()).ToList();
|
||||
foreach (var gearTask in list)
|
||||
{
|
||||
await gearTask.Run(CancellationToken.None);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -43,26 +43,4 @@ public partial class GearTaskDefinitionViewModel : ObservableObject
|
||||
Description = description;
|
||||
RootTask = new GearTaskViewModel(name, true);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取任务总数(包括所有子任务)
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public int GetTotalTaskCount()
|
||||
{
|
||||
if (RootTask == null) return 0;
|
||||
return 1 + Enumerable.Count<GearTaskViewModel>(RootTask.GetAllChildren());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取启用的任务数量
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public int GetEnabledTaskCount()
|
||||
{
|
||||
if (RootTask == null) return 0;
|
||||
var count = RootTask.IsEnabled ? 1 : 0;
|
||||
count += Enumerable.Count<GearTaskViewModel>(RootTask.GetAllChildren(), t => t.IsEnabled);
|
||||
return count;
|
||||
}
|
||||
}
|
||||
@@ -40,30 +40,15 @@ public partial class GearTriggerViewModel : ObservableObject
|
||||
/// 任务引用列表
|
||||
/// </summary>
|
||||
[ObservableProperty]
|
||||
private ObservableCollection<GearTaskRefence> _taskReferences = new();
|
||||
private string _taskDefinitionName = string.Empty;
|
||||
|
||||
// 顺序触发器属性(无额外属性)
|
||||
|
||||
// 定时触发器属性
|
||||
[ObservableProperty]
|
||||
private int _intervalMs = 5000;
|
||||
|
||||
[ObservableProperty]
|
||||
private bool _isRepeating = true;
|
||||
|
||||
[ObservableProperty]
|
||||
private int _delayMs = 0;
|
||||
|
||||
[ObservableProperty]
|
||||
private int _maxExecutions = 0;
|
||||
|
||||
|
||||
// 热键触发器属性
|
||||
[ObservableProperty]
|
||||
private HotKey? _hotkey;
|
||||
|
||||
[ObservableProperty]
|
||||
private int _cooldownMs = 1000;
|
||||
|
||||
public GearTriggerViewModel()
|
||||
{
|
||||
}
|
||||
@@ -105,23 +90,23 @@ public partial class GearTriggerViewModel : ObservableObject
|
||||
/// </summary>
|
||||
public string HotkeyDisplayText => Hotkey?.ToString() ?? "未设置";
|
||||
|
||||
/// <summary>
|
||||
/// 获取定时器配置显示文本
|
||||
/// </summary>
|
||||
public string TimerDisplayText
|
||||
{
|
||||
get
|
||||
{
|
||||
if (TriggerType != TriggerType.Timed) return "";
|
||||
|
||||
var text = $"间隔: {IntervalMs}ms";
|
||||
if (DelayMs > 0) text += $", 延迟: {DelayMs}ms";
|
||||
if (MaxExecutions > 0) text += $", 最大次数: {MaxExecutions}";
|
||||
if (!IsRepeating) text += ", 单次执行";
|
||||
|
||||
return text;
|
||||
}
|
||||
}
|
||||
// /// <summary>
|
||||
// /// 获取定时器配置显示文本
|
||||
// /// </summary>
|
||||
// public string TimerDisplayText
|
||||
// {
|
||||
// get
|
||||
// {
|
||||
// if (TriggerType != TriggerType.Timed) return "";
|
||||
//
|
||||
// var text = $"间隔: {IntervalMs}ms";
|
||||
// if (DelayMs > 0) text += $", 延迟: {DelayMs}ms";
|
||||
// if (MaxExecutions > 0) text += $", 最大次数: {MaxExecutions}";
|
||||
// if (!IsRepeating) text += ", 单次执行";
|
||||
//
|
||||
// return text;
|
||||
// }
|
||||
// }
|
||||
|
||||
/// <summary>
|
||||
/// 转换为对应的触发器实例
|
||||
@@ -141,14 +126,13 @@ public partial class GearTriggerViewModel : ObservableObject
|
||||
TriggerType.Hotkey => new HotkeyGearTrigger
|
||||
{
|
||||
Hotkey = Hotkey,
|
||||
CooldownMs = CooldownMs,
|
||||
IsEnabled = IsEnabled
|
||||
},
|
||||
_ => new SequentialGearTrigger()
|
||||
};
|
||||
|
||||
trigger.Name = Name;
|
||||
trigger.GearTaskRefenceList = new ObservableCollection<GearTaskRefence>(TaskReferences);
|
||||
trigger.TaskDefinitionName = _taskDefinitionName;
|
||||
|
||||
return trigger;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user