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.StateMachines; using RichCreator.Maps; using RichCreator.Models; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using ZTImage.Configuration; namespace RichCreator.Jobs { public class DNFJob : JobBase where T:MapInfo,new() { public DNFJob(RichCreatorConfig config) : base(config) { } /// /// 选角色时的角色选择偏移 /// private static ZTPoint[] RoleOffset = new ZTPoint[] { new ZTPoint (154,225), new ZTPoint (300,225), new ZTPoint (443,225), new ZTPoint (593,225), new ZTPoint (726,225), new ZTPoint (883,225), new ZTPoint (154,455), new ZTPoint (300,455), new ZTPoint (443,455), new ZTPoint (593,455), new ZTPoint (726,455), new ZTPoint (883,455) }; /// /// 游戏窗口 /// private ZTRectangle GameRect; private Int32 runningStep = 0; public override bool DoWork(out bool tryAgain,CancellationToken cancellationToken, Int32 runningStep) { this.runningStep = runningStep; tryAgain = true; RunningModel runningModel = ConfigHelper.GetInstance(); RichCreatorConfig config = ConfigHelper.GetInstance(); bool result = false; ZTRectangle startGameButtonRect = ZTRectangle.Empty; if (runningStep <= RunningStep.SelectRole) { //查找游戏窗口 result = FuncUtils.TimeoutCancelableWrap(config.StartWaitSecond * 1000, cancellationToken, () => { return DnfCVHelper.IsSelectRoleWindow( out startGameButtonRect); }); if (!result) { G.Instance.InfoWriter("启动DNF游戏失败"); return false; } G.Instance.InfoWriter($"已进入DNF角色界面,{startGameButtonRect.ToString()}"); } WindowUtils.GetDnfRect(out this.GameRect); //循环刷所有角色 Int32 roleIndex = runningModel.RoleIndex>0? runningModel.RoleIndex - 1:0; for (; roleIndex < config.RoleCount; roleIndex++) { RunningModel.UpdateRoleIndex(roleIndex+1); if (cancellationToken.IsCancellationRequested) { return false; } DateTime playOneRoleStartTime = DateTime.Now; G.Instance.InfoWriter($"Play Role:{roleIndex+1}"); if (!PlayRole(cancellationToken, roleIndex, startGameButtonRect)) { G.Instance.InfoWriter("角色刷图失败"); break; } Int32 playOneRoleTotalSecond = (Int32)(DateTime.Now - playOneRoleStartTime).TotalSeconds; G.Instance.InfoWriter($"角色刷图完成,用时:{(playOneRoleTotalSecond / 60)}分{(playOneRoleTotalSecond % 60)}秒"); } if (roleIndex < config.RoleCount) { string message = $"刷图失败,第{roleIndex+1}个角色"; G.Instance.InfoWriter(message); return false; } //按ESC,点退出程序 if (ExitGame(cancellationToken)) { G.Instance.InfoWriter("退出游戏失败"); return false; } RunningModel.UpdateRoleIndex(1); return true; } /// /// 刷单一角色 /// /// /// /// private bool PlayRole(CancellationToken cancellationToken, Int32 roleIndex, ZTRectangle startGameButtonRect) { bool result = true; Int32 pilaozhi = 1; if (this.runningStep < RunningStep.War) { //查找游戏窗口 result = FuncUtils.TimeoutCancelableWrap(5 * 60 * 1000, cancellationToken, () => { return DnfCVHelper.IsSelectRoleWindow(out startGameButtonRect); }); if (!result) { G.Instance.InfoWriter("非角色选择界面"); return false; } //进入赛丽亚的房间 result = IntoSailiyaRoom(cancellationToken, startGameButtonRect, roleIndex); if (!result) { G.Instance.InfoWriter("未进入赛丽亚的房间"); return false; } G.Instance.InfoWriter("进入赛丽亚的房间"); //关闭所有弹出窗 result = CloseAllAlertWindow(cancellationToken, this.GameRect); if (!result) { G.Instance.InfoWriter("关闭所有窗口出现问题"); return false; } G.Instance.InfoWriter("已关闭所有弹出窗"); //查询疲劳值 pilaozhi = DnfCVHelper.GetPiLaoZhi(this.GameRect); G.Instance.InfoWriter("疲劳值:" + pilaozhi); } result = true; if (pilaozhi > 0) { //刷图,并保持在可按esc退出的状态 MapInfo map = new T(); result = map.Start(this.GameRect, cancellationToken,this.runningStep); } runningStep = RunningStep.None; if (result) { //按ESC,退到选角色界面 if (ReturnToSelectRole(cancellationToken)) { return true; } return false; } else { return false; } } /// /// 开始游戏 /// /// /// /// /// /// private bool IntoSailiyaRoom(CancellationToken cancelToken, ZTRectangle startGameButtonRect, Int32 roleIndex) { bool result = FuncUtils.NoChangeRetryCallWrap( () => { WindowUtils.SetDnfToTop(); //选角色 G.Instance.InputControl.MoveToAndClick(this.GameRect.Start.Add(RoleOffset[roleIndex])); Thread.Sleep(500); //点进入游戏 G.Instance.InputControl.MoveToAndClick(startGameButtonRect.GetCenterPoint()); }, () => { return FuncUtils.TimeoutCancelableWrap(3 * 60 * 1000, cancelToken, () => { return DnfCVHelper.IsInSaiLiYaHouse(this.GameRect); }); }, () => { if (DnfCVHelper.IsSelectRoleWindow(out startGameButtonRect)) { return true; } return false; }); return result; } /// /// 关闭所有弹出窗 /// /// /// /// private bool CloseAllAlertWindow(CancellationToken cancelToken, ZTRectangle gameRect) { while (!cancelToken.IsCancellationRequested) { G.Instance.InputControl.MoveTo(0, 0, false, false, false); Thread.Sleep(10); //截图 System.Drawing.Bitmap bitmap = ScreenCapture.Instance.CaptureScreen(); Image image = new Image(bitmap); ZTRectangle closeButtonRect = ZTRectangle.Empty; if (!DnfCVHelper.GetAlertWindow(out closeButtonRect, image, gameRect)) { return true; } G.Instance.InputControl.MoveToAndClick(closeButtonRect.GetCenterPoint()); Thread.Sleep(500); } return false; } /// /// 返回选择角色界面 /// /// /// /// private bool ReturnToSelectRole(CancellationToken cancelToken) { //打开系统菜单 int counter = 10; bool result = false; WindowUtils.SetDnfToTop(); while (counter > 0) { G.Instance.InputControl.PressKey(RandomUtils.KeyPressDuration, HIDCode.Escape); bool findPanelResult = FuncUtils.TimeoutCancelableWrap(3000, cancelToken, () => { return DnfCVHelper.IsOpenSystemPanel(this.GameRect); }); if (findPanelResult) { result = true; break; } counter--; } if (result) { //点击切换角色按钮624,502 G.Instance.InputControl.MoveToAndClick(new ZTPoint(this.GameRect.Start.X + 624, this.GameRect.Start.Y + 502)); return true; } G.Instance.InfoWriter("退回选择角色界面失败"); return false; } /// /// 退出游戏 /// /// /// /// private bool ExitGame(CancellationToken cancelToken) { //打开系统菜单 int counter = 10; bool result = false; WindowUtils.SetDnfToTop(); while (counter > 0) { G.Instance.InputControl.PressKey(RandomUtils.KeyPressDuration, HIDCode.Escape); bool findPanelResult = FuncUtils.TimeoutCancelableWrap(3000, cancelToken, () => { return DnfCVHelper.IsOpenSystemPanel(this.GameRect); }); if (findPanelResult) { result = true; break; } counter--; } if (result) { //点击退出游戏783,505 G.Instance.InputControl.MoveToAndClick(new ZTPoint(this.GameRect.Start.X + 783, this.GameRect.Start.Y + 505)); Thread.Sleep(1000); ZTRectangle exitOkButtonRect = ZTRectangle.Empty; bool findExitPanelResult = FuncUtils.TimeoutCancelableWrap(3000, cancelToken, () => { return DnfCVHelper.FindExitPanelOkButton(out exitOkButtonRect, this.GameRect); }); if (findExitPanelResult) { //remove,现在是按查找到的按钮位置来判断的,点击确定按钮 612 379 //G.Instance.InputControl.MoveToAndClick(new ZTPoint(this.GameRect.Start.X + 612, this.GameRect.Start.Y + 379)); G.Instance.InputControl.MoveToAndClick(exitOkButtonRect.GetCenterPoint()); Thread.Sleep(6000); return true; } } return false; } } }