asmrobot
2019-10-29 f25f89101a98ad815c0ae2d25e1a8dc35d53a5dd
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
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.Utilitys;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
 
 
namespace RichCreator.Jobs
{
    public class WeGameJob:JobBase
    {
        public WeGameJob(RichCreatorConfig config,string userName,string password):base(config)
        {
            this.UserName = userName;
            this.Password = password;
        }
 
        private string UserName;
        private string Password;
 
        public override ZTResult Do(CancellationToken cancellationToken,Int32 runningStep)
        {
            
            Process.Start(this.Config.TGPDaemonPath);
            G.Instance.InfoWriter("等待We Game运行");
            ZTRectangle weGameRect = ZTRectangle.Empty;
            ZTRectangle changeLoginMethodButtonRect = ZTRectangle.Empty;
 
            //查找游戏
            bool result = FindWeGame(cancellationToken, 5 * 60, out weGameRect,out changeLoginMethodButtonRect);
            if (!result)
            {
                G.Instance.InfoWriter("查找游戏失败");
                return ZTResult.Failed;
            }
 
            G.Instance.InfoWriter("WeGame于:" + weGameRect.ToString() + ",切换按钮于:" + changeLoginMethodButtonRect.ToString());
            
            //登陆
            result = LoginWeGame(cancellationToken, 30, weGameRect, changeLoginMethodButtonRect);
            if (!result)
            {
                G.Instance.InfoWriter("登陆失败");
                return ZTResult.Failed;
            }
            G.Instance.InfoWriter("WeGame登陆成功");
            
            //登陆DNF游戏
            result = LoginDNF(cancellationToken, 30);
            if (!result)
            {
                G.Instance.InfoWriter("DNF未成功打开");
                return ZTResult.Failed;
            }
            G.Instance.InfoWriter("开始启动DNF游戏");
 
            return ZTResult.Success;
        }
        
 
        /// <summary>
        /// 查找we game运行框 
        /// </summary>
        /// <param name="cancelToken">取消令牌</param>
        /// <param name="timeoutSecond">超时时间,秒</param>
        /// <param name="wegameRect">程序区域</param>
        /// <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)
        {
            ZTRectangle weGameRect = ZTRectangle.Empty;
            ZTRectangle changeLoginMethodButtonRect = ZTRectangle.Empty;
 
            bool result=FuncUtils.TimeoutCancelableWrap((Int32)timeoutSecond * 1000, cancelToken, () => {
                return WeGameCVHelper.IsLoginPage(out weGameRect, out changeLoginMethodButtonRect);
            });
 
            wegameRect = weGameRect;
            changeButtonRect = changeLoginMethodButtonRect;
            return result;
        }
        
 
        /// <summary>
        /// 登陆WeGame
        /// </summary>
        /// <param name="weGameRect">wegame登陆框区域</param>
        /// <param name="changButtonRect">切换模式区域</param>
        /// <returns></returns>
        private bool LoginWeGame(CancellationToken cancelToken, Int64 timeoutSecond, ZTRectangle weGameRect, ZTRectangle changButtonRect)
        {
 
            Utility.Structs.ZTPoint basePoint = changButtonRect.GetCenterPoint();
 
            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);
            DeleteAllChar(10);
            G.Instance.InputControl.InputString(this.UserName);
 
            //输入密码
            G.Instance.InputControl.MoveToAndClick(pwdPoint);
            DeleteAllChar(2);
            G.Instance.InputControl.InputString(this.Password);
 
            //点击登陆
            G.Instance.InputControl.MoveToAndClick(loginPoint);
 
 
            return true;
        }
 
        
        /// <summary>
        /// 查找DNF项的区域
        /// </summary>
        /// <param name="cancelToken"></param>
        /// <param name="timeoutSecond"></param>
        /// <param name="dnfItemRect"></param>
        /// <returns></returns>
        private bool LoginDNF(CancellationToken cancelToken, Int64 timeoutSecond)
        {
            ZTRectangle findRect = ZTRectangle.Empty;
            ZTRectangle pRect = ZTRectangle.Empty;
 
            G.Instance.InputControl.MoveTo(0, 0, false, false, false);
 
            //选中主页
            ZTRectangle unselectHomeButtonRect = ZTRectangle.Empty;
            bool result = FuncUtils.TimeoutCancelableWrap((Int32)timeoutSecond * 1000, cancelToken, () => {
                return WeGameCVHelper.FindUnselectHome(out unselectHomeButtonRect);
            });
            if (!result)
            {
                G.Instance.InfoWriter("未找到主页按钮");
                return false;
            }
            G.Instance.InfoWriter("主页按钮已找到");
            G.Instance.InputControl.MoveToAndClick(unselectHomeButtonRect.GetCenterPoint());
            
            //选中DNF项
            ZTRectangle dnfItemRect = ZTRectangle.Empty;
            result = FuncUtils.TimeoutCancelableWrap((Int32)timeoutSecond * 1000, cancelToken, () => {
                return WeGameCVHelper.FindDnfItem(out dnfItemRect);
            });
            if (!result)
            {
                G.Instance.InfoWriter("未找到DNF项");
                return false;
            }
            G.Instance.InfoWriter("Dnf项已找到");
            G.Instance.InputControl.MoveToAndClick(dnfItemRect.GetCenterPoint());
 
 
            //是否需要更新
            ZTRectangle updateButtonRect = ZTRectangle.Empty;
            result = FuncUtils.TimeoutCancelableWrap(3000, cancelToken, () => {
                return WeGameCVHelper.FindUpdateButton(out updateButtonRect);
            });
            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("未找到开始游戏按钮");
                return false;
            }
            G.Instance.InfoWriter("开始游戏按钮已找到");            
            G.Instance.InputControl.MoveToAndClick(startGameButtonRect.GetCenterPoint());
 
            return true;
        }
 
        #region Tools
        /// <summary>
        /// 删除左右指定数量的字符
        /// </summary>
        /// <param name="count"></param>
        private void DeleteAllChar(int count)
        {
            for (int i = 0; i < count; i++)
            {
                G.Instance.InputControl.PressKey(false, false, false, false, false, false, false, false, HIDCode.Delete);
                G.Instance.InputControl.PressKey(false, false, false, false, false, false, false, false, HIDCode.Backspace);
            }
        }
        #endregion
    }
}