Files
better-genshin-impact/BetterGenshinImpact/Core/Script/Dependence/Dispatcher.cs
kaedelcb ad6c854c06 一条龙菜单、自动秘境、切换队伍相关修改 (#1465)
* 1.一条龙菜单增加配置组,每周7天单独配置。
2.自动秘境执行条件,到达秘境向前走的时间加长。
3.切换队伍API,失败重试3次,切换识别不退出任务。

* Update AutoFightTask.cs

恢复错误提示

* 一条龙配置名称的函数误删恢复

* 格式化代码,消除警告

* 修改配置组的启动调用

* 命名修改 & 如果任务已经被取消,中断所有任务

---------

Co-authored-by: 辉鸭蛋 <huiyadanli@gmail.com>
2025-04-26 18:49:00 +08:00

174 lines
5.7 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using BetterGenshinImpact.Core.Script.Dependence.Model;
using BetterGenshinImpact.GameTask;
using BetterGenshinImpact.ViewModel.Pages;
using System;
using System.Threading.Tasks;
using BetterGenshinImpact.GameTask.AutoDomain;
using BetterGenshinImpact.GameTask.AutoFishing;
using BetterGenshinImpact.GameTask.AutoWood;
using BetterGenshinImpact.GameTask.AutoGeniusInvokation;
using BetterGenshinImpact.GameTask.AutoPathing.Handler;
using System.Threading;
namespace BetterGenshinImpact.Core.Script.Dependence;
public class Dispatcher
{
private object _config = null;
public Dispatcher(object config)
{
_config = config;
}
public void RunTask()
{
}
/// <summary>
/// 添加实时任务,会清理之前的所有任务
/// </summary>
/// <param name="timer">实时任务触发器</param>
/// <exception cref="ArgumentNullException"></exception>
public void AddTimer(RealtimeTimer timer)
{
ClearAllTriggers();
try
{
AddTrigger(timer);
}
catch (ArgumentException e)
{
if (e is ArgumentNullException)
{
throw;
}
}
}
/// <summary>
/// 清理所有实时任务
/// </summary>
public void ClearAllTriggers()
{
TaskTriggerDispatcher.Instance().ClearTriggers();
}
/// <summary>
/// 添加实时任务,不会清理之前的任务
/// </summary>
/// <param name="timer"></param>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="ArgumentException"></exception>
public void AddTrigger(RealtimeTimer timer)
{
var realtimeTimer = timer;
if (realtimeTimer == null)
{
throw new ArgumentNullException(nameof(realtimeTimer), "实时任务对象不能为空");
}
if (string.IsNullOrEmpty(realtimeTimer.Name))
{
throw new ArgumentNullException(nameof(realtimeTimer.Name), "实时任务名称不能为空");
}
if (!TaskTriggerDispatcher.Instance().AddTrigger(realtimeTimer.Name, realtimeTimer.Config))
{
throw new ArgumentException($"添加实时任务失败: {realtimeTimer.Name}", nameof(realtimeTimer.Name));
}
}
/// <summary>
/// 运行独立任务
/// </summary>
/// <param name="soloTask">
/// 支持的任务名称:
/// - AutoGeniusInvokation: 启动自动七圣召唤任务
/// - AutoWood: 启动自动伐木任务
/// - AutoFight: 启动自动战斗任务
/// - AutoDomain: 启动自动秘境任务
/// </param>
/// <param name="customCts">自定义取消令牌源允许从JS控制任务取消</param>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="ArgumentException"></exception>
public async Task RunTask(SoloTask soloTask, System.Threading.CancellationTokenSource customCts = null)
{
if (soloTask == null)
{
throw new ArgumentNullException(nameof(soloTask), "独立任务对象不能为空");
}
var taskSettingsPageViewModel = App.GetService<TaskSettingsPageViewModel>();
if (taskSettingsPageViewModel == null)
{
throw new ArgumentNullException(nameof(taskSettingsPageViewModel), "内部视图模型对象为空");
}
// 创建一个链接的取消令牌源,同时监听自定义令牌和全局令牌
CancellationTokenSource linkedCts = null;
CancellationToken cancellationToken;
if (customCts != null)
{
// 创建链接的取消令牌源,任何一个取消都会触发
linkedCts = CancellationTokenSource.CreateLinkedTokenSource(
customCts.Token,
CancellationContext.Instance.Cts.Token);
cancellationToken = linkedCts.Token;
}
else
{
// 如果没有自定义令牌,就使用全局令牌
cancellationToken = CancellationContext.Instance.Cts.Token;
}
try
{
// 根据名称执行任务
switch (soloTask.Name)
{
case "AutoGeniusInvokation":
if (taskSettingsPageViewModel.GetTcgStrategy(out var content))
{
return;
}
await new AutoGeniusInvokationTask(new GeniusInvokationTaskParam(content)).Start(cancellationToken);
break;
case "AutoWood":
await new AutoWoodTask(new WoodTaskParam(taskSettingsPageViewModel.AutoWoodRoundNum,
taskSettingsPageViewModel.AutoWoodDailyMaxCount)).Start(cancellationToken);
break;
case "AutoFight":
await new AutoFightHandler().RunAsyncByScript(cancellationToken, null, _config);
break;
case "AutoDomain":
if (taskSettingsPageViewModel.GetFightStrategy(out var path))
{
return;
}
await new AutoDomainTask(new AutoDomainParam(0, path)).Start(cancellationToken);
break;
case "AutoFishing":
await new AutoFishingTask(AutoFishingTaskParam.BuildFromSoloTaskConfig(soloTask.Config)).Start(
cancellationToken);
break;
default:
throw new ArgumentException($"未知的任务名称: {soloTask.Name}", nameof(soloTask.Name));
}
}
finally
{
// 释放链接的取消令牌源
linkedCts?.Dispose();
}
}
}