asmrobot
2019-10-14 730fe7ea65bcadbe235e40bb54b2410d14495267
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
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;
 
namespace RichCreator
{
    public class JobMonitor
    {
        private RichCreatorConfig config;
        private CancellationTokenSource cancelTokenSource;
        private bool canRun = true;//是否可运行
 
        private WeGameJob wegameJob;
        private JobBase dnfJob;
 
        private Int32 taskIndex;
 
        public JobMonitor(Int32 taskIndex)
        {
            this.config = ConfigHelper.GetInstance<RichCreatorConfig>();
            this.cancelTokenSource = new CancellationTokenSource();
            this.taskIndex = taskIndex;
        }
 
        /// <summary>
        /// 开始
        /// </summary>
        public void Start()
        {
            if (!canRun)
            {
                throw new Exception("不能多次运行");
            }
            canRun = false;
            ThreadPool.QueueUserWorkItem(MonitorBusinessThread);
        }
 
        //停止
        public void Stop()
        {
            this.cancelTokenSource.Cancel();
            canRun = true;
            this.cancelTokenSource = new CancellationTokenSource();
        }
 
        private void MonitorBusinessThread(object obj)
        {
            Int32 accountCount = GetAccountCount();            
            Int32 runningStep = RunningStep.GetStep();
            G.Instance.InfoWriter("当前步骤:" + runningStep.ToString());
            RunningModel runModel = ConfigHelper.GetInstance<RunningModel>();
            if (runModel.AccountIndex > accountCount)
            {
                G.Instance.InfoWriter("开始角色比已配置角色还多,如果没有那么多角色请清空运行模型");
                return;
            }
 
            bool tryAgain = false;
            int accountIndex = runModel.AccountIndex > 0 ? runModel.AccountIndex - 1 : 0;
            for (; accountIndex < accountCount; accountIndex++)
            {
                RunningModel.UpdateAccountIndex(accountIndex+1);
                if (runningStep < RunningStep.SelectRole)
                {
                    string username, password;
                    GetAccount(accountIndex, out username, out password);
 
                    this.wegameJob = new WeGameJob(this.config, username, password);
                    if (!this.wegameJob.DoWork(out tryAgain,this.cancelTokenSource.Token, runningStep))
                    {
                        if (G.Instance.CancelConfirm("需要重试吗?", 8000))
                        {
                            if (config.NotificationWechat)
                            {
                                if (!ServiceMonitor.Instance.NotificationWechat("失败,正在重新尝试"))
                                {
                                    ZTImage.Log.Trace.Error("发送消息失败");
                                }
                            }
                            accountIndex--;
                            runningStep = RunningStep.None;
                            ProcessUtils.CloseDnf();
                            ProcessUtils.CloseWeGame();
                            continue;
                        }
                        else
                        {
                            break;
                        }
                    }
                }
 
                //设为顶层
                WindowUtils.SetDnfToTop();
                this.dnfJob = GetDNFJob();
                if (this.dnfJob == null)
                {
                    break;
                }
                if (!this.dnfJob.DoWork(out tryAgain,this.cancelTokenSource.Token, runningStep))
                {
                    if (G.Instance.CancelConfirm("需要重试吗?", 8000))
                    {
                        if (config.NotificationWechat)
                        {
                            if (!ServiceMonitor.Instance.NotificationWechat("失败,正在重新尝试"))
                            {
                                ZTImage.Log.Trace.Error("发送消息失败");
                            }
                        }
                        accountIndex--;
                        runningStep = RunningStep.None;
                        ProcessUtils.CloseDnf();
                        ProcessUtils.CloseWeGame();
                        continue;
                    }
                    else
                    {
                        break;
                    }
                }
                runningStep = RunningStep.None;
                ProcessUtils.CloseDnf();
                ProcessUtils.CloseWeGame();
                Thread.Sleep(5000);
            }
 
            if (config.NotificationWechat)
            {
                if (!ServiceMonitor.Instance.NotificationWechat("任务完成"))
                {
                    ZTImage.Log.Trace.Error("发送消息失败");
                }
            }
 
            runModel.RoleIndex = 1;
            runModel.AccountIndex = 1;
            ConfigHelper.SetInstance<RunningModel>(runModel);
            this.Stop();
            G.Instance.StopTaskUI();
            G.Instance.InfoWriter("任务成功完成");
        }
 
        /// <summary>
        /// 获取配置文件中账号的数量
        /// </summary>
        /// <returns></returns>
        private Int32 GetAccountCount()
        {
            Int32 accountCount = 0;
            if (!string.IsNullOrEmpty(this.config.UserName1))
            {
                accountCount = 1;
                if (!string.IsNullOrEmpty(this.config.UserName2))
                {
                    accountCount = 2;
                    if (!string.IsNullOrEmpty(this.config.UserName3))
                    {
                        accountCount = 3;
                    }
                }
            }
 
            return accountCount;
        }
 
        /// <summary>
        /// 获取指定编号
        /// </summary>
        /// <param name="accountIndex">从0开始</param>
        /// <param name="username"></param>
        /// <param name="password"></param>
        private void GetAccount(Int32 accountIndex, out string username, out string password)
        {
            username = this.config.UserName1;
            password = this.config.Password1;
            if (accountIndex == 1)
            {
                username = this.config.UserName2;
                password = this.config.Password2;
            }
 
            if (accountIndex == 2)
            {
                username = this.config.UserName3;
                password = this.config.Password3;
            }
        }
 
        /// <summary>
        /// 根据当前选择的地图实例化DNF地图
        /// </summary>
        /// <param name="config"></param>
        /// <returns></returns>
        private JobBase GetDNFJob()
        {
            switch (this.taskIndex)
            {
                case 0:
                    return new DNFJob<LindongMap>(this.config);
                case 1:
                    return new DNFJob<SkillMap>(this.config);
                case 2:
                    return new DNFJob<GroupMap>(this.config);
                default:
                    return null;
            }
        }
    }
}