asmrobot
2024-12-31 a8536c3c73a4445b1f1252a1d3271e0c73b35613
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
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<RichCreatorConfig>();
            this.cancelTokenSource = new CancellationTokenSource();
            this.mapType = maptype;
        }
 
        /// <summary>
        /// 开始
        /// </summary>
        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();
        }
 
        /// <summary>
        /// 业务逻辑线程
        /// </summary>
        /// <param name="obj"></param>
        private void MonitorBusinessThread(object obj)
        {
            Int32 accountCount = this.config.GetAccountCount();            
            runningStep = RunningStep.GetStep();
            G.Instance.InfoWriter("当前步骤:" + runningStep.ToString());
            RunningModel runModel = ConfigHelper.GetInstance<RunningModel>();
            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<RunningModel>(runModel);
            }
            else
            {
                G.Instance.InfoWriter("任务中止");
            }
            
            this.Stop();
            G.Instance.StopTaskUI();
            
        }
 
        /// <summary>
        /// 关闭游戏程序
        /// </summary>
        private void ShutdownGameProgram()
        {
            runningStep = RunningStep.None;
            ProcessUtils.CloseDnf();
            ProcessUtils.CloseWeGame();
        }
 
    }
}