asmrobot
2019-10-16 5597c0b354f881994a75878731c3a02183e9c970
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
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;
 
        Int32 runningStep = RunningStep.None;
 
        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();
        }
 
        /// <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 = GetDNFJob();
                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();
        }
 
        /// <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<TestMap>(this.config);
            }
            throw new Exception("this task index is error");
        }
    }
}