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
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
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.StateMachines;
using RichCreator.Maps;
using RichCreator.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using ZTImage.Configuration;
using RichCreator.StateMachines;
using RichCreator.Utility.Maps;
using RichCreator.Maps.Kalete;
using RichCreator.Maps.Lindong;
using RichCreator.Maps.SkillSetting;
using RichCreator.Maps.Test;
using RichCreator.Dnf;
 
namespace RichCreator.Jobs
{
    public class DNFJob : JobBase
    {
        public DNFJob(MapType mapType,RichCreatorConfig config) : base(config)
        {
            this.MapType = mapType;
        }
        
        /// <summary>
        /// 游戏窗口
        /// </summary>
        public ZTRectangle GameRect;
 
        /// <summary>
        /// 地图类型
        /// </summary>
        public MapType MapType { get; private set; }
 
        private Int32 runningStep = 0;
 
        public override ZTResult Do(CancellationToken cancellationToken, Int32 runningStep)
        {
            this.runningStep = runningStep;
            RunningModel runningModel = ConfigHelper.GetInstance<RunningModel>();
            RichCreatorConfig config = ConfigHelper.GetInstance<RichCreatorConfig>();
            
            ZTRectangle startGameButtonRect = ZTRectangle.Empty;
            if (runningStep <= RunningStep.SelectRole)
            {
                //查找游戏窗口
                bool result = FuncUtils.TimeoutCancelableWrap(config.StartWaitSecond * 1000, cancellationToken, () =>
                {
                    return DnfCVHelper.IsSelectRoleWindow( out startGameButtonRect);
                });
 
                if (!result)
                {
                    G.Instance.InfoWriter("启动DNF游戏失败");
                    return ZTResult.Failed;
                }
                G.Instance.InfoWriter($"已进入DNF角色界面,{startGameButtonRect.ToString()}");
            }
            
            WindowUtils.GetDnfRect(out this.GameRect);
 
            //循环刷所有角色
            ZTResult playResult = ZTResult.Success;
            Int32 roleIndex = runningModel.RoleIndex>0? runningModel.RoleIndex - 1:0;
            for (; roleIndex < config.RoleCount; roleIndex++)
            {
                RunningModel.UpdateRoleIndex(roleIndex+1);
                if (cancellationToken.IsCancellationRequested)
                {
                    return ZTResult.Cancel;
                }
                DateTime startRoleTime = DateTime.Now;
                G.Instance.InfoWriter($"Play Role:{roleIndex+1}");
 
                playResult = PlayRole(cancellationToken, roleIndex, startGameButtonRect);
 
                StateProvider.Instance.PreRoleTime = (Int32)(DateTime.Now - startRoleTime).TotalSeconds;
                StateProvider.Instance.RoleTimePassed = 0;
 
                if (playResult != ZTResult.Success)
                {
                    G.Instance.InfoWriter($"角色刷图失败,第{roleIndex+1}个角色");
                    return playResult;
                }
                G.Instance.InfoWriter($"角色刷图完成");
            }
            
            RunningModel.UpdateRoleIndex(1);
            return ZTResult.Success;
        }
 
        /// <summary>
        /// 刷单一角色
        /// </summary>
        /// <param name="cancellationToken"></param>
        /// <param name="roleIndex"></param>
        /// <returns></returns>
        private ZTResult PlayRole(CancellationToken cancellationToken, Int32 roleIndex, ZTRectangle startGameButtonRect)
        {
            bool result = true;
            Int32 pilaozhi = 1;
            RichCreatorConfig config = ConfigHelper.GetInstance<RichCreatorConfig>();
            if (this.runningStep < RunningStep.War)
            {
                //查找游戏窗口
                result = FuncUtils.TimeoutCancelableWrap(5 * 60 * 1000, cancellationToken, () =>
                {
                    return DnfCVHelper.IsSelectRoleWindow(out startGameButtonRect);
                });
 
                if (!result)
                {
                    G.Instance.InfoWriter("非角色选择界面");
                    return ZTResult.Failed;
                }
 
                //进入赛丽亚的房间
                result = IntoSailiyaRoom(cancellationToken, startGameButtonRect, roleIndex);
                if (!result)
                {
                    G.Instance.InfoWriter("未进入赛丽亚的房间");
                    return ZTResult.Failed;
                }
                G.Instance.InfoWriter("进入赛丽亚的房间");
 
 
                //频道选择和网络断开重试
                ChannelStateMachine csm = new ChannelStateMachine();
                ZTResult workResult = csm.Work(this.GameRect, config.IsGroup, cancellationToken, 5 * 60 * 1000);
                if (workResult != ZTResult.Success)
                {
                    G.Instance.InfoWriter("切换频道没有成功," + workResult);
                    Thread.Sleep(5 * 60 * 1000);
                    return workResult;
                }
                G.Instance.InfoWriter("切换频道成功");
                
                //查询疲劳值
                pilaozhi = DnfCVHelper.GetPiLaoZhi(this.GameRect);
                G.Instance.InfoWriter("疲劳值:" + pilaozhi);
            }
            
 
            ZTResult mapResult = ZTResult.Success;
            if (pilaozhi > 0)
            {
                //刷图,并保持在可按esc退出的状态
                MapInfo map = CreateMap( cancellationToken);
                mapResult = map.Start(this.runningStep);
            }
            runningStep = RunningStep.None;
 
 
            if (mapResult==ZTResult.Success)
            {
                //按ESC,退到选角色界面
                if (ReturnToSelectRole(cancellationToken))
                {
                    return ZTResult.Success;
                }
                return ZTResult.Failed;
            }
 
            return mapResult;
        }
 
        /// <summary>
        /// 开始游戏
        /// </summary>
        /// <param name="cancelToken"></param>
        /// <param name="timeoutSecond"></param>
        /// <param name="startGameButtonRect"></param>
        /// <param name="roleIndex"></param>
        /// <returns></returns>
        private bool IntoSailiyaRoom(CancellationToken cancelToken,  ZTRectangle startGameButtonRect, Int32 roleIndex)
        {
            bool result = FuncUtils.NoChangeRetryCallWrap(
                () =>
                {
                    WindowUtils.SetDnfToTop();
 
                    if (SelectRoleIndex(roleIndex))
                    {
                        //点进入游戏
                        G.Instance.InputControl.MoveToAndClick(startGameButtonRect.GetCenterPoint());
                        return true;
                    }
                    Thread.Sleep(1000);
                    return false;
                    
                },
                () =>
                {
                    return FuncUtils.TimeoutCancelableWrap(3 * 60 * 1000, cancelToken, () =>
                          {
                              return DnfCVHelper.IsInSaiLiYaHouse(this.GameRect);
                          });
                },
                () =>
                {
                    if (DnfCVHelper.IsSelectRoleWindow(out startGameButtonRect))
                    {
                        return true;
                    }
                    return false;
                },3);
            return result;
        }
 
 
 
        /// <summary>
        /// 选角色时的角色选择偏移
        /// </summary>
        private static ZTPoint[] RoleOffset = new Utility.Structs.ZTPoint[] {
            new ZTPoint (133,191),
            new ZTPoint (249,191),
            new ZTPoint (371,191),
            new ZTPoint (499,191),
 
 
            new ZTPoint (133,398),
            new ZTPoint (249,398),
            new ZTPoint (371,398),
            new ZTPoint (499,398),
 
            new ZTPoint (133,398),
            new ZTPoint (249,398)
        };
 
        
        /// <summary>
        /// 选择角色
        /// </summary>
        /// <param name="roleIndex"></param>
        /// <returns></returns>
        private bool SelectRoleIndex(Int32 roleIndex)
        {
            if (roleIndex < 8)
            {
                //移到顶端
                if (!ScrollMoveToTop())
                {
                    return false;
                }
 
            }
            else
            {
                //移到顶端,向下一格
                if (!ScrollMoveToDownOne())
                {
                    return false;
                }
            }
 
            Thread.Sleep(1000);
            //选角色
            G.Instance.InputControl.MoveToAndClick(this.GameRect.Start.Add(RoleOffset[roleIndex]));
            Thread.Sleep(500);
            return true;
        }
 
        /// <summary>
        /// 滚动条移动到顶端
        /// </summary>
        /// <returns></returns>
        private bool ScrollMoveToTop()
        {
            ZTRectangle upButton = ZTRectangle.Empty;
            ZTRectangle downButton = ZTRectangle.Empty;
            ZTRectangle scrollButton = ZTRectangle.Empty;
 
 
            //查找滚动条位置
            if (DnfCVHelper.FindSelectRoleScroll(out upButton, out downButton, out scrollButton, this.GameRect))
            {
                //是在顶部
                if (Math.Abs(upButton.End.Y - scrollButton.Start.Y) < 3)
                {
                    return true;
                }
            }
 
            
 
            Int32 counter = 4;
            while(counter>0)
            {
                counter--;
                //查找滚动条位置
                if (!DnfCVHelper.FindSelectRoleScroll(out upButton, out downButton, out scrollButton, this.GameRect))
                {
                    continue;
                }
 
                //是在顶部
                if (Math.Abs(upButton.End.Y - scrollButton.Start.Y) < 3)
                {
                    return true;
                }
 
                ZTPoint targetPoint = scrollButton.GetCenterPoint();
                G.Instance.InputControl.MoveTo(targetPoint.X, targetPoint.Y, false, false, false);
                G.Instance.InputControl.Move(0, 0, true, false, false);
                targetPoint = upButton.GetCenterPoint();
                G.Instance.InputControl.MoveTo(targetPoint.X, targetPoint.Y, true, false, false);
                G.Instance.InputControl.Move(0, 0, false, false, false);
                Thread.Sleep(RandomUtils.KeyPressDuration);
                G.Instance.InputControl.MoveTo(this.GameRect.End.X, this.GameRect.Start.Y, false, false, false);
                Thread.Sleep(RandomUtils.KeyPressDuration);
            }
            
 
            return false;
        }
 
        /// <summary>
        /// 滚动条移动到下一格
        /// </summary>
        /// <returns></returns>
        private bool ScrollMoveToDownOne()
        {
            ZTRectangle upButton = ZTRectangle.Empty;
            ZTRectangle downButton = ZTRectangle.Empty;
            ZTRectangle scrollButton = ZTRectangle.Empty;
 
 
            //查找滚动条位置
            if (DnfCVHelper.FindSelectRoleScroll(out upButton, out downButton, out scrollButton, this.GameRect))
            {
                //是在下一格
                if (Math.Abs(upButton.End.Y - scrollButton.Start.Y) > 3&& Math.Abs(upButton.End.Y - scrollButton.Start.Y)<30)
                {
                    return true;
                }
            }
 
 
 
            Int32 counter = 4;
            while (counter > 0)
            {
                counter--;
                //查找滚动条位置
                if (!DnfCVHelper.FindSelectRoleScroll(out upButton, out downButton, out scrollButton, this.GameRect))
                {
                    continue;
                }
 
                //是在第一格
                if (Math.Abs(upButton.End.Y - scrollButton.Start.Y) > 3 && Math.Abs(upButton.End.Y - scrollButton.Start.Y) < 30)
                {
                    return true;
                }
 
                ZTPoint targetPoint = scrollButton.GetCenterPoint();
                G.Instance.InputControl.MoveTo(targetPoint.X, targetPoint.Y, false, false, false);
                G.Instance.InputControl.Move(0, 0, true, false, false);
                targetPoint = upButton.GetCenterPoint();
                G.Instance.InputControl.MoveTo(targetPoint.X, targetPoint.Y, true, false, false);
                G.Instance.InputControl.Move(0, 0, false, false, false);
                Thread.Sleep(RandomUtils.KeyPressDuration);
               
 
                G.Instance.InputControl.MoveToAndClick(downButton.GetCenterPoint());
                Thread.Sleep(RandomUtils.KeyPressDuration);
                G.Instance.InputControl.MoveTo(this.GameRect.End.X, this.GameRect.Start.Y, false, false, false);
                Thread.Sleep(RandomUtils.KeyPressDuration);
            }
 
 
            return false;
        }
 
 
        /// <summary>
        /// 返回选择角色界面
        /// </summary>
        /// <param name="cancelToken"></param>
        /// <param name="gameRect"></param>
        /// <returns></returns>
        private bool ReturnToSelectRole(CancellationToken cancelToken)
        {
            //打开系统菜单
            int counter = 10;
            bool result = false;
            WindowUtils.SetDnfToTop();
            while (counter > 0)
            {
                G.Instance.InputControl.PressKey(RandomUtils.KeyPressDuration, HIDCode.Escape);
                bool findPanelResult = FuncUtils.TimeoutCancelableWrap(3000, cancelToken, () =>
                {
                    return DnfCVHelper.IsOpenSystemPanel(this.GameRect);
                });
 
                if (findPanelResult)
                {
                    result = true;
                    break;
                }
                counter--;
            }
 
            if (result)
            {
                //点击切换角色按钮388,445
                G.Instance.InputControl.MoveToAndClick(new Utility.Structs.ZTPoint(this.GameRect.Start.X + 388, this.GameRect.Start.Y + 445));
                return true;
            }
            G.Instance.InfoWriter("退回选择角色界面失败");
            return false;
        }
        
        /// <summary>
        /// 根据当前选择的地图实例化DNF地图
        /// </summary>
        /// <param name="config"></param>
        /// <returns></returns>
        private MapInfo CreateMap(CancellationToken cancelToken)
        {
            switch (this.MapType)
            {
                case MapType.Lingdong:
                    return new LindongMap(this.GameRect,cancelToken);
                case MapType.Kalete:
                    return new KaleteMap(this.GameRect, cancelToken);
                case MapType.SkillSetting:
                    return new SkillSettingMap(this.GameRect, cancelToken);
                case MapType.Test:
                    return new TestMap(this.GameRect, cancelToken);
            }
            throw new Exception("this task index is error");
        }
    }
}