using RichCreator.Utility; using RichCreator.Utility.Captures; using RichCreator.Utility.CV; using RichCreator.Utility.InputControl; using RichCreator.Utility.Structs; using RichCreator.Utility.Utilitys; using Emgu.CV; using Emgu.CV.Structure; using RichCreator.Jobs; using RichCreator.Models; using RichCreator.Utilitys; using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using ZTImage.Configuration; using RichCreator.Maps; namespace RichCreator { public class JobMonitor { private RichCreatorConfig config; private CancellationTokenSource cancelTokenSource; private bool canRun = true;//是否可运行 private WeGameJob wegameJob; private JobBase dnfJob; private Int32 taskIndex; public JobMonitor(Int32 taskIndex) { this.config = ConfigHelper.GetInstance(); this.cancelTokenSource = new CancellationTokenSource(); this.taskIndex = taskIndex; } /// /// 开始 /// public void Start() { if (!canRun) { throw new Exception("不能多次运行"); } canRun = false; ThreadPool.QueueUserWorkItem(MonitorBusinessThread); } //停止 public void Stop() { this.cancelTokenSource.Cancel(); canRun = true; this.cancelTokenSource = new CancellationTokenSource(); } private void MonitorBusinessThread(object obj) { Int32 accountCount = GetAccountCount(); Int32 runningStep = RunningStep.GetStep(); G.Instance.InfoWriter("当前步骤:" + runningStep.ToString()); RunningModel runModel = ConfigHelper.GetInstance(); if (runModel.AccountIndex > accountCount) { G.Instance.InfoWriter("开始角色比已配置角色还多,如果没有那么多角色请清空运行模型"); return; } bool tryAgain = false; int accountIndex = runModel.AccountIndex > 0 ? runModel.AccountIndex - 1 : 0; for (; accountIndex < accountCount; accountIndex++) { RunningModel.UpdateAccountIndex(accountIndex+1); if (runningStep < RunningStep.SelectRole) { string username, password; GetAccount(accountIndex, out username, out password); this.wegameJob = new WeGameJob(this.config, username, password); if (!this.wegameJob.DoWork(out tryAgain,this.cancelTokenSource.Token, runningStep)) { if (G.Instance.CancelConfirm("需要重试吗?", 8000)) { if (config.NotificationWechat) { if (!ServiceMonitor.Instance.NotificationWechat("失败,正在重新尝试")) { ZTImage.Log.Trace.Error("发送消息失败"); } } accountIndex--; runningStep = RunningStep.None; ProcessUtils.CloseDnf(); ProcessUtils.CloseWeGame(); continue; } else { break; } } } //设为顶层 WindowUtils.SetDnfToTop(); this.dnfJob = GetDNFJob(); if (this.dnfJob == null) { break; } if (!this.dnfJob.DoWork(out tryAgain,this.cancelTokenSource.Token, runningStep)) { if (G.Instance.CancelConfirm("需要重试吗?", 8000)) { if (config.NotificationWechat) { if (!ServiceMonitor.Instance.NotificationWechat("失败,正在重新尝试")) { ZTImage.Log.Trace.Error("发送消息失败"); } } accountIndex--; runningStep = RunningStep.None; ProcessUtils.CloseDnf(); ProcessUtils.CloseWeGame(); continue; } else { break; } } runningStep = RunningStep.None; ProcessUtils.CloseDnf(); ProcessUtils.CloseWeGame(); Thread.Sleep(5000); } if (config.NotificationWechat) { if (!ServiceMonitor.Instance.NotificationWechat("任务完成")) { ZTImage.Log.Trace.Error("发送消息失败"); } } runModel.RoleIndex = 1; runModel.AccountIndex = 1; ConfigHelper.SetInstance(runModel); this.Stop(); G.Instance.StopTaskUI(); G.Instance.InfoWriter("任务成功完成"); } /// /// 获取配置文件中账号的数量 /// /// private Int32 GetAccountCount() { Int32 accountCount = 0; if (!string.IsNullOrEmpty(this.config.UserName1)) { accountCount = 1; if (!string.IsNullOrEmpty(this.config.UserName2)) { accountCount = 2; if (!string.IsNullOrEmpty(this.config.UserName3)) { accountCount = 3; } } } return accountCount; } /// /// 获取指定编号 /// /// 从0开始 /// /// private void GetAccount(Int32 accountIndex, out string username, out string password) { username = this.config.UserName1; password = this.config.Password1; if (accountIndex == 1) { username = this.config.UserName2; password = this.config.Password2; } if (accountIndex == 2) { username = this.config.UserName3; password = this.config.Password3; } } /// /// 根据当前选择的地图实例化DNF地图 /// /// /// private JobBase GetDNFJob() { switch (this.taskIndex) { case 0: return new DNFJob(this.config); case 1: return new DNFJob(this.config); case 2: return new DNFJob(this.config); default: return null; } } } }