Exemplo n.º 1
0
        void CaptureMumu()
        {
            viewportRect = mumuTools.GetMumuViewportRect();
            var viewportCapture = Tools.GetInstance().DoCaptureScreen(viewportRect);

            viewportMat = viewportCapture.ToOpenCvMat();
            //Cv2.ImShow("viewportMat", viewportMat);
        }
Exemplo n.º 2
0
        public void SelectBattleTeam(Mat viewportMat, RECT viewportRect, List <PCRUnit> units)
        {
            for (var i = 1; i <= 5; i++)
            {
                var key = $"Battle_Team_Slot_{i}";
                mumuTools.DoClick(key);
                Thread.Sleep(500);
            }

            viewportRect = mumuTools.GetMumuViewportRect();
            var viewportCapture = Tools.GetInstance().DoCaptureScreen(viewportRect);

            viewportMat = viewportCapture.ToOpenCvMat();

            var rectRate       = GetMatchSourceRectRate(BATTLE_UNIT_LIST_VIEW_MKEY);
            var threshold      = GetMatchTemplateThreshold(BATTLE_UNIT_LIST_VIEW_MKEY);
            var vis            = new bool[5];
            var dragTimesLimit = 5;

            for (var i = 0; i < dragTimesLimit; i++)
            {
                var matchSourceMat = viewportMat.GetChildMatByRectRate(rectRate);
                for (var k = 0; k < units.Count; k++)
                {
                    if (vis[k])
                    {
                        continue;
                    }
                    var unit          = units[k];
                    var avatar        = unit.GetResizedAvatar();
                    var avatarPartial = avatar.GetChildMatByRectRate(unitAvatarPartialRectRate);
                    var matchRes      = opencvTools.MatchImage(matchSourceMat, avatarPartial, threshold);
                    if (matchRes.Success)
                    {
                        var rectToViewport = matchRes.GetMatchedAbsoluteRect(viewportRect, rectRate);
                        var centerPos      = rectToViewport.GetCenterPos();
                        var emulatorPoint  = mumuTools.GetEmulatorPoint(viewportRect, centerPos);
                        mumuTools.DoClick(emulatorPoint);
                        vis[k] = true;
                        Thread.Sleep(500);
                    }
                }

                var allOk = true;
                for (var k = 0; k < units.Count; k++)
                {
                    allOk = allOk && vis[k];
                }
                if (allOk)
                {
                    break;
                }

                var milliSeconds = 2000;
                mumuTools.DoDrag(new Vec2f(0.5f, 0.6344f), new Vec2f(0.5f, 0.2450f), milliSeconds);
                Thread.Sleep(milliSeconds);

                if (i != dragTimesLimit - 1)
                {
                    viewportRect    = mumuTools.GetMumuViewportRect();
                    viewportCapture = Tools.GetInstance().DoCaptureScreen(viewportRect);
                    viewportMat     = viewportCapture.ToOpenCvMat();
                }
            }

            OnBattleTeamUnitNotFound(units, vis);
        }
Exemplo n.º 3
0
        public CreateScriptTaskResult CreateScriptTask(ScriptBase script)
        {
            var tokenSource = new CancellationTokenSource();
            var token       = tokenSource.Token;
            var task        = new Task(() =>
            {
                var viewportRect    = mumuTools.GetMumuViewportRect();
                var viewportCapture = tools.DoCaptureScreen(viewportRect);
                logTools.Info("ScriptStart", $"Script: {script.Name} OnStart");
                script.OnStart(viewportCapture.ToOpenCvMat(), viewportRect);
                while (true)
                {
                    if (token.IsCancellationRequested)
                    {
                        logTools.Info("ScriptLoop", Trans.T("脚本: {0} 被终止", script.Name));
                        break;
                    }
                    try
                    {
                        Thread.Sleep(script.Interval);
                        viewportRect    = mumuTools.GetMumuViewportRect();
                        viewportCapture = tools.DoCaptureScreen(viewportRect);
                        //logTools.Info("ScriptLoop", $"Script: {script.Name} Tick");
                        var viewportMat = viewportCapture.ToOpenCvMat();
                        var startTime   = DateTime.Now;
                        script.Tick(viewportMat, viewportRect);
                        var endTime = DateTime.Now;
                        //logTools.Info("ScriptLoop", $"Tick Takes {(endTime - startTime).TotalMilliseconds}ms");
                    }
                    catch (Exception e)
                    {
                        var needBreak = logTools.IsSelfOrChildrenBreakException(e);
                        if (!script.CanKeepOnWhenException || needBreak)
                        {
                            logTools.Error("ScriptLoop", Trans.T("脚本: {0} 因发生错误或主动结束而被终止", script.Name), false);
                            throw e;
                        }
                        else
                        {
                            logTools.Error("ScriptLoop", Trans.T("脚本: {0} 发生错误", script.Name), false);
                            logTools.Error("ScriptLoop", e);
                        }
                    }
                }
            }, tokenSource.Token);

            task.ContinueWith((t) =>
            {
                if (t.IsFaulted)
                {
                    logTools.Error("ScriptTask", t.Exception);
                }
                else if (t.IsCanceled)
                {
                    logTools.Error("ScriptTask", Trans.T("脚本: {0} 被终止", script.Name));
                }
                OnScriptEnded?.Invoke(script);
            });
            return(new CreateScriptTaskResult()
            {
                Task = task,
                Script = script,
                TaskTokenSource = tokenSource,
                TaskToken = token,
            });
        }