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; using RichCreator.Utility.Maps; namespace RichCreator { public class JobMonitor { private RichCreatorConfig config; private CancellationTokenSource cancelTokenSource; private bool canRun = true;//是否可运行 private WeGameJob wegameJob; private JobBase dnfJob; private MapType mapType; Int32 runningStep = RunningStep.None; public JobMonitor(MapType maptype) { this.config = ConfigHelper.GetInstance(); this.cancelTokenSource = new CancellationTokenSource(); this.mapType = maptype; } /// /// 开始 /// public void Start() { if (!canRun) { throw new Exception("不能多次运行"); } canRun = false; Thread thread = new Thread(MonitorBusinessThread); thread.IsBackground = true; thread.Start(); //ThreadPool.QueueUserWorkItem(MonitorBusinessThread); } //停止 public void Stop() { this.cancelTokenSource.Cancel(); canRun = true; this.cancelTokenSource = new CancellationTokenSource(); } /// /// 业务逻辑线程 /// /// private void MonitorBusinessThread(object obj) { Int32 accountCount = this.config.GetAccountCount(); runningStep = RunningStep.GetStep(); G.Instance.InfoWriter("当前步骤:" + runningStep.ToString()); RunningModel runModel = ConfigHelper.GetInstance(); if (runModel.AccountIndex > accountCount) { G.Instance.InfoWriter("开始角色比已配置角色还多,如果没有那么多角色请清空运行模型"); return; } int accountIndex = runModel.AccountIndex > 0 ? runModel.AccountIndex - 1 : 0; ZTResult jobResult = ZTResult.Success; for (; accountIndex < accountCount; accountIndex++) { RunningModel.UpdateAccountIndex(accountIndex+1); if (runningStep < RunningStep.SelectRole) { string username, password; this.config.GetAccount(accountIndex, out username, out password); this.wegameJob = new WeGameJob(this.config, username, password); jobResult = this.wegameJob.Do(this.cancelTokenSource.Token, runningStep); if (jobResult != ZTResult.Success) { if (jobResult != ZTResult.Cancel) { if (G.Instance.CancelConfirm("需要重试吗?", 8000)) { ServiceProvider.Instance.NotificationWechat("失败,正在重新尝试"); accountIndex--; ShutdownGameProgram(); continue; } } break; } } //设为顶层 WindowUtils.SetDnfToTop(); this.dnfJob = new DNFJob(this.mapType, this.config); jobResult = this.dnfJob.Do(this.cancelTokenSource.Token, runningStep); if (jobResult != ZTResult.Success) { if (jobResult != ZTResult.Cancel) { if (G.Instance.CancelConfirm("需要重试吗?", 8000)) { ServiceProvider.Instance.NotificationWechat("失败,正在重新尝试"); accountIndex--; ShutdownGameProgram(); continue; } } break; } ShutdownGameProgram(); Thread.Sleep(5000); } if (jobResult == ZTResult.Success) { G.Instance.InfoWriter("任务完成"); ServiceProvider.Instance.NotificationWechat("任务完成"); runModel.RoleIndex = 1; runModel.AccountIndex = 1; ConfigHelper.SetInstance(runModel); } else { G.Instance.InfoWriter("任务中止"); } this.Stop(); G.Instance.StopTaskUI(); } /// /// 关闭游戏程序 /// private void ShutdownGameProgram() { runningStep = RunningStep.None; ProcessUtils.CloseDnf(); ProcessUtils.CloseWeGame(); } } }