asmrobot
2024-12-31 a8536c3c73a4445b1f1252a1d3271e0c73b35613
src/RichCreator/Jobs/WeGameJob.cs
@@ -13,7 +13,7 @@
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using RichCreator.Utility;
namespace RichCreator.Jobs
{
@@ -28,44 +28,67 @@
        private string UserName;
        private string Password;
        public override bool DoWork(out bool tryAgain, CancellationToken cancellationToken,Int32 runningStep)
        public override ZTResult Do(CancellationToken cancellationToken,Int32 runningStep)
        {
            tryAgain = true;
            
            Process.Start(this.Config.TGPDaemonPath);
            G.Instance.InfoWriter("等待We Game运行");
            ZTRectangle weGameRect = ZTRectangle.Empty;
            ZTRectangle changeLoginMethodButtonRect = ZTRectangle.Empty;
            ZTRectangle changeMethodRect = ZTRectangle.Empty;
            bool result = false;
            //查找游戏
            bool result = FindWeGame(cancellationToken, 5 * 60, out weGameRect,out changeLoginMethodButtonRect);
            if (!result)
            //查找游戏并设为帐号密码登陆
            Int32 counter = 3;
            while (counter>0)
            {
                G.Instance.InfoWriter("查找游戏失败");
                return false;
            }
                bool isQQFast = false;
                //查找游戏
                result = FindWeGame(out isQQFast, cancellationToken, 5 * 60, out weGameRect, out changeMethodRect);
                if (!result)
                {
                    G.Instance.InfoWriter("查找游戏失败");
                    return ZTResult.Failed;
                }
            G.Instance.InfoWriter("WeGame于:" + weGameRect.ToString() + ",切换按钮于:" + changeLoginMethodButtonRect.ToString());
                if (isQQFast)
                {
                    //qq快速登陆
                    G.Instance.InputControl.MoveToAndClick(changeMethodRect.GetCenterPoint());
                    Thread.Sleep(500);
                }
                else
                {
                    break;
                }
                counter--;
            }
            G.Instance.InfoWriter("WeGame于:" + weGameRect.ToString() + ",切换按钮于:" + changeMethodRect.ToString());
            
            //登陆
            result = LoginWeGame(cancellationToken, 30, weGameRect, changeLoginMethodButtonRect);
            result = LoginWeGame(cancellationToken, 30, weGameRect, changeMethodRect);
            if (!result)
            {
                G.Instance.InfoWriter("登陆失败");
                return false;
                return ZTResult.Failed;
            }
            G.Instance.InfoWriter("WeGame登陆成功");
            
            //登陆DNF游戏
            result = LoginDNF(cancellationToken, 30);
            result = LoginDNF(cancellationToken, 30,weGameRect);
            if (!result)
            {
                G.Instance.InfoWriter("DNF未成功打开");
                return false;
                G.Instance.InfoWriter("DNF启动未成功");
                return ZTResult.Failed;
            }
            G.Instance.InfoWriter("开始启动DNF游戏");
            return true;
            return ZTResult.Success;
        }
        
@@ -78,15 +101,16 @@
        /// <param name="loginMethod">当前登陆方式,0:QQ快速登陆,1:码密登陆</param>
        /// <param name="changeLMButtonRectangle">切换登陆方式按钮区域</param>
        /// <returns></returns>
        private bool FindWeGame(CancellationToken cancelToken, Int64 timeoutSecond, out ZTRectangle wegameRect, out ZTRectangle changeButtonRect)
        private bool FindWeGame(out bool isQQFastLogin,CancellationToken cancelToken, Int64 timeoutSecond, out ZTRectangle wegameRect, out ZTRectangle changeButtonRect)
        {
            ZTRectangle weGameRect = ZTRectangle.Empty;
            ZTRectangle changeLoginMethodButtonRect = ZTRectangle.Empty;
            bool isfl = false;
            bool result=FuncUtils.TimeoutCancelableWrap((Int32)timeoutSecond * 1000, cancelToken, () => {
                return WeGameCVHelper.IsLoginPage(out weGameRect, out changeLoginMethodButtonRect);
                return WeGameCVHelper.IsLoginPage(out isfl,out weGameRect, out changeLoginMethodButtonRect);
            });
            isQQFastLogin = isfl;
            wegameRect = weGameRect;
            changeButtonRect = changeLoginMethodButtonRect;
            return result;
@@ -102,11 +126,11 @@
        private bool LoginWeGame(CancellationToken cancelToken, Int64 timeoutSecond, ZTRectangle weGameRect, ZTRectangle changButtonRect)
        {
            ZTPoint basePoint = changButtonRect.GetCenterPoint();
            Utility.Structs.ZTPoint basePoint = changButtonRect.GetCenterPoint();
            ZTPoint userPoint = new ZTPoint(basePoint.X, basePoint.Y - 148);
            ZTPoint pwdPoint = new ZTPoint(basePoint.X, basePoint.Y - 118);
            ZTPoint loginPoint = new ZTPoint(basePoint.X, basePoint.Y - 42);
            Utility.Structs.ZTPoint userPoint = new Utility.Structs.ZTPoint(basePoint.X, basePoint.Y - 148);
            Utility.Structs.ZTPoint pwdPoint = new Utility.Structs.ZTPoint(basePoint.X, basePoint.Y - 118);
            Utility.Structs.ZTPoint loginPoint = new Utility.Structs.ZTPoint(basePoint.X, basePoint.Y - 42);
            
            //输入用户名
            G.Instance.InputControl.MoveToAndClick(userPoint);
@@ -133,7 +157,7 @@
        /// <param name="timeoutSecond"></param>
        /// <param name="dnfItemRect"></param>
        /// <returns></returns>
        private bool LoginDNF(CancellationToken cancelToken, Int64 timeoutSecond)
        private bool LoginDNF(CancellationToken cancelToken, Int64 timeoutSecond,ZTRectangle wegameRect)
        {
            ZTRectangle findRect = ZTRectangle.Empty;
            ZTRectangle pRect = ZTRectangle.Empty;
@@ -149,6 +173,7 @@
            {
                G.Instance.InfoWriter("未找到主页按钮");
                return false;
                //todo:发通知
            }
            G.Instance.InfoWriter("主页按钮已找到");
            G.Instance.InputControl.MoveToAndClick(unselectHomeButtonRect.GetCenterPoint());
@@ -162,39 +187,89 @@
            {
                G.Instance.InfoWriter("未找到DNF项");
                return false;
                //todo:发通知
            }
            G.Instance.InfoWriter("Dnf项已找到");
            G.Instance.InputControl.MoveToAndClick(dnfItemRect.GetCenterPoint());
            //是否需要更新
            ZTRectangle updateButtonRect = ZTRectangle.Empty;
            result = FuncUtils.TimeoutCancelableWrap(3000, cancelToken, () => {
                return WeGameCVHelper.FindUpdateButton(out updateButtonRect);
            //是否需要更新,如需要则更新
            ZTRectangle buttonRect = ZTRectangle.Empty;
            bool isUpdate = false;
            result = FuncUtils.TimeoutCancelableWrap((Int32)timeoutSecond*1000, cancelToken, () => {
                return FindGameButton(out buttonRect, out isUpdate);
            });
            if (result)
            {
                G.Instance.InfoWriter("需要更新请更新后再运行");
                G.Instance.InputControl.MoveToAndClick(updateButtonRect.GetCenterPoint());
                return false;
            }
            //开始游戏按钮
            ZTRectangle startGameButtonRect = ZTRectangle.Empty;
            result = FuncUtils.TimeoutCancelableWrap((Int32)timeoutSecond * 1000, cancelToken, () => {
                return WeGameCVHelper.FindStartGameButton(out startGameButtonRect);
            });
            if (!result)
            {
                G.Instance.InfoWriter("未找到开始游戏按钮");
                G.Instance.InfoWriter("未找到开始或升级游戏按钮");
                return false;
            }
            G.Instance.InfoWriter("开始游戏按钮已找到");
            G.Instance.InputControl.MoveToAndClick(startGameButtonRect.GetCenterPoint());
            if (isUpdate)
            {
                //更新
                G.Instance.InputControl.MoveToAndClick(buttonRect.GetCenterPoint());
                G.Instance.InfoWriter("需要更新,更新超时时长为30分钟");
                result = FuncUtils.TimeoutCancelableWrap(30 * 60 * 1000, cancelToken, () => {
                    G.Instance.InputControl.MoveTo(0, 0, false, false, false);
                    if (FindGameButton(out buttonRect, out isUpdate))
                    {
                        if (isUpdate)
                        {
                            //如果没点上更新按钮则再点一次
                            G.Instance.InputControl.MoveToAndClick(buttonRect.GetCenterPoint());
                        }
                        else
                        {
                            return true;
                        }
                    }
                    return false;
                });
                if (!result)
                {
                    G.Instance.InfoWriter("更新未成功,请手动更新");
                    //todo:发通知
                    return false;
                }
            }
            G.Instance.InfoWriter("开始游戏");
            G.Instance.InputControl.MoveToAndClick(buttonRect.GetCenterPoint());
            return true;
        }
        /// <summary>
        /// 查找游戏按钮
        /// </summary>
        /// <param name="buttonRect"></param>
        /// <returns></returns>
        private bool FindGameButton(out ZTRectangle buttonRect,out bool isUpdate)
        {
            buttonRect = ZTRectangle.Empty;
            isUpdate = false;
            Image<Rgb, byte> image = ScreenCapture.Instance.CaptureScreenReturnImage();
            if (WeGameCVHelper.FindUpdateButton(out buttonRect, image))
            {
                isUpdate = true;
                return true;
            }
            if (WeGameCVHelper.FindStartGameButton(out buttonRect, image))
            {
                isUpdate = false;
                return true;
            }
            return false;
        }
        #region Tools
        /// <summary>
        /// 删除左右指定数量的字符