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;
using RichCreator.Utility;
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 changeMethodRect = ZTRectangle.Empty;
bool result = false;
//查找游戏并设为帐号密码登陆
Int32 counter = 3;
while (counter>0)
{
bool isQQFast = false;
//查找游戏
result = FindWeGame(out isQQFast, cancellationToken, 5 * 60, out weGameRect, out changeMethodRect);
if (!result)
{
G.Instance.InfoWriter("查找游戏失败");
return ZTResult.Failed;
}
if (isQQFast)
{
//qq快速登陆
G.Instance.InputControl.MoveToAndClick(changeMethodRect.GetCenterPoint());
Thread.Sleep(500);
}
else
{
break;
}
counter--;
}
G.Instance.InfoWriter("WeGame于:" + weGameRect.ToString() + ",切换按钮于:" + changeMethodRect.ToString());
//登陆
result = LoginWeGame(cancellationToken, 30, weGameRect, changeMethodRect);
if (!result)
{
G.Instance.InfoWriter("登陆失败");
return ZTResult.Failed;
}
G.Instance.InfoWriter("WeGame登陆成功");
//登陆DNF游戏
result = LoginDNF(cancellationToken, 30,weGameRect);
if (!result)
{
G.Instance.InfoWriter("DNF启动未成功");
return ZTResult.Failed;
}
G.Instance.InfoWriter("开始启动DNF游戏");
return ZTResult.Success;
}
///
/// 查找we game运行框
///
/// 取消令牌
/// 超时时间,秒
/// 程序区域
/// 当前登陆方式,0:QQ快速登陆,1:码密登陆
/// 切换登陆方式按钮区域
///
private bool FindWeGame(out bool isQQFastLogin,CancellationToken cancelToken, Int64 timeoutSecond, out ZTRectangle wegameRect, out ZTRectangle changeButtonRect)
{
ZTRectangle weGameRect = ZTRectangle.Empty;
ZTRectangle changeLoginMethodButtonRect = ZTRectangle.Empty;
bool isfl = false;
bool result=FuncUtils.TimeoutCancelableWrap((Int32)timeoutSecond * 1000, cancelToken, () => {
return WeGameCVHelper.IsLoginPage(out isfl,out weGameRect, out changeLoginMethodButtonRect);
});
isQQFastLogin = isfl;
wegameRect = weGameRect;
changeButtonRect = changeLoginMethodButtonRect;
return result;
}
///
/// 登陆WeGame
///
/// wegame登陆框区域
/// 切换模式区域
///
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;
}
///
/// 查找DNF项的区域
///
///
///
///
///
private bool LoginDNF(CancellationToken cancelToken, Int64 timeoutSecond,ZTRectangle wegameRect)
{
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;
//todo:发通知
}
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;
//todo:发通知
}
G.Instance.InfoWriter("Dnf项已找到");
G.Instance.InputControl.MoveToAndClick(dnfItemRect.GetCenterPoint());
//是否需要更新,如需要则更新
ZTRectangle buttonRect = ZTRectangle.Empty;
bool isUpdate = false;
result = FuncUtils.TimeoutCancelableWrap((Int32)timeoutSecond*1000, cancelToken, () => {
return FindGameButton(out buttonRect, out isUpdate);
});
if (!result)
{
G.Instance.InfoWriter("未找到开始或升级游戏按钮");
return false;
}
if (isUpdate)
{
//更新
G.Instance.InputControl.MoveToAndClick(buttonRect.GetCenterPoint());
G.Instance.InfoWriter("需要更新,更新超时时长为30分钟");
result = FuncUtils.TimeoutCancelableWrap(30 * 60 * 1000, cancelToken, () => {
G.Instance.InputControl.MoveTo(0, 0, false, false, false);
if (FindGameButton(out buttonRect, out isUpdate))
{
if (isUpdate)
{
//如果没点上更新按钮则再点一次
G.Instance.InputControl.MoveToAndClick(buttonRect.GetCenterPoint());
}
else
{
return true;
}
}
return false;
});
if (!result)
{
G.Instance.InfoWriter("更新未成功,请手动更新");
//todo:发通知
return false;
}
}
G.Instance.InfoWriter("开始游戏");
G.Instance.InputControl.MoveToAndClick(buttonRect.GetCenterPoint());
return true;
}
///
/// 查找游戏按钮
///
///
///
private bool FindGameButton(out ZTRectangle buttonRect,out bool isUpdate)
{
buttonRect = ZTRectangle.Empty;
isUpdate = false;
Image image = ScreenCapture.Instance.CaptureScreenReturnImage();
if (WeGameCVHelper.FindUpdateButton(out buttonRect, image))
{
isUpdate = true;
return true;
}
if (WeGameCVHelper.FindStartGameButton(out buttonRect, image))
{
isUpdate = false;
return true;
}
return false;
}
#region Tools
///
/// 删除左右指定数量的字符
///
///
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
}
}