1
- using System . Threading . Tasks ;
1
+ using HtmlAgilityPack ;
2
+ using OpenQA . Selenium ;
3
+ using System . Linq ;
4
+ using System . Threading . Tasks ;
2
5
using TbsCore . Helpers ;
3
6
using TbsCore . Models . AccModels ;
4
7
@@ -8,26 +11,48 @@ public class LoginTask : BotTask
8
11
{
9
12
public override async Task < TaskRes > Execute ( Account acc )
10
13
{
11
- if ( ! TaskExecutor . IsLoginScreen ( acc ) )
14
+ var usernameNode = GetUsernameNode ( acc ) ;
15
+ if ( usernameNode == null )
12
16
{
13
- await Task . Delay ( AccountHelper . Delay ( acc ) * 2 ) ;
14
- return TaskRes . Executed ;
17
+ acc . Logger . Warning ( "Cannot find username box" ) ;
18
+ acc . TaskTimer . Stop ( ) ;
19
+ acc . Status = Status . Paused ;
20
+ return TaskRes . Retry ;
15
21
}
16
22
17
- var access = acc . Access . GetCurrentAccess ( ) ;
18
-
19
- if ( acc . AccInfo . ServerUrl . Contains ( "ttwars" ) )
23
+ var passwordNode = GetPasswordNode ( acc ) ;
24
+ if ( passwordNode == null )
20
25
{
21
- await DriverHelper . WriteByName ( acc , "user" , acc . AccInfo . Nickname ) ;
22
- await DriverHelper . WriteByName ( acc , "pw" , access . Password ) ;
26
+ acc . Logger . Warning ( "Cannot find password box" ) ;
27
+ acc . TaskTimer . Stop ( ) ;
28
+ acc . Status = Status . Paused ;
29
+ return TaskRes . Retry ;
23
30
}
24
- else
31
+
32
+ var buttonNode = GetLoginButton ( acc ) ;
33
+ if ( buttonNode == null )
25
34
{
26
- await DriverHelper . WriteByName ( acc , "name" , acc . AccInfo . Nickname ) ;
27
- await DriverHelper . WriteByName ( acc , "password" , access . Password ) ;
35
+ acc . Logger . Warning ( "Cannot find login button" ) ;
36
+ acc . TaskTimer . Stop ( ) ;
37
+ acc . Status = Status . Paused ;
38
+ return TaskRes . Retry ;
28
39
}
29
40
30
- await DriverHelper . ClickByName ( acc , "s1" ) ;
41
+ var access = acc . Access . GetCurrentAccess ( ) ;
42
+
43
+ var usernameElement = acc . Wb . Driver . FindElement ( By . XPath ( usernameNode . XPath ) ) ;
44
+
45
+ usernameElement . SendKeys ( Keys . Home ) ;
46
+ usernameElement . SendKeys ( Keys . Shift + Keys . End ) ;
47
+ usernameElement . SendKeys ( acc . AccInfo . Nickname ) ;
48
+
49
+ var passwordElement = acc . Wb . Driver . FindElement ( By . XPath ( passwordNode . XPath ) ) ;
50
+ passwordElement . SendKeys ( Keys . Home ) ;
51
+ passwordElement . SendKeys ( Keys . Shift + Keys . End ) ;
52
+ passwordElement . SendKeys ( access . Password ) ;
53
+
54
+ var buttonElement = acc . Wb . Driver . FindElement ( By . XPath ( buttonNode . XPath ) ) ;
55
+ buttonElement . Click ( ) ;
31
56
32
57
acc . Wb . UpdateHtml ( ) ;
33
58
@@ -36,6 +61,7 @@ public override async Task<TaskRes> Execute(Account acc)
36
61
// Wrong password/nickname
37
62
acc . Logger . Warning ( "Password is incorrect!" ) ;
38
63
acc . TaskTimer . Stop ( ) ;
64
+ acc . Status = Status . Paused ;
39
65
return TaskRes . Retry ;
40
66
}
41
67
else
@@ -48,5 +74,53 @@ public override async Task<TaskRes> Execute(Account acc)
48
74
return TaskRes . Executed ;
49
75
}
50
76
}
77
+
78
+ private HtmlNode GetUsernameNode ( Account acc )
79
+ {
80
+ switch ( acc . AccInfo . ServerVersion )
81
+ {
82
+ case Classificator . ServerVersionEnum . TTwars :
83
+ return acc . Wb . Html . DocumentNode . Descendants ( "input" ) . FirstOrDefault ( x => x . GetAttributeValue ( "name" , "" ) . Equals ( "user" ) ) ;
84
+
85
+ case Classificator . ServerVersionEnum . T4_5 :
86
+ return acc . Wb . Html . DocumentNode . Descendants ( "input" ) . FirstOrDefault ( x => x . GetAttributeValue ( "name" , "" ) . Equals ( "name" ) ) ;
87
+
88
+ default :
89
+ return null ;
90
+ } ;
91
+ }
92
+
93
+ private HtmlNode GetPasswordNode ( Account acc )
94
+ {
95
+ switch ( acc . AccInfo . ServerVersion )
96
+ {
97
+ case Classificator . ServerVersionEnum . TTwars :
98
+ return acc . Wb . Html . DocumentNode . Descendants ( "input" ) . FirstOrDefault ( x => x . GetAttributeValue ( "name" , "" ) . Equals ( "pw" ) ) ;
99
+
100
+ case Classificator . ServerVersionEnum . T4_5 :
101
+ return acc . Wb . Html . DocumentNode . Descendants ( "input" ) . FirstOrDefault ( x => x . GetAttributeValue ( "name" , "" ) . Equals ( "password" ) ) ;
102
+
103
+ default :
104
+ return null ;
105
+ } ;
106
+ }
107
+
108
+ private HtmlNode GetLoginButton ( Account acc )
109
+ {
110
+ switch ( acc . AccInfo . ServerVersion )
111
+ {
112
+ case Classificator . ServerVersionEnum . TTwars :
113
+ return acc . Wb . Html . GetElementbyId ( "s1" ) ;
114
+
115
+ case Classificator . ServerVersionEnum . T4_5 :
116
+ {
117
+ var trNode = acc . Wb . Html . DocumentNode . Descendants ( "tr" ) . FirstOrDefault ( x => x . HasClass ( "loginButtonRow" ) ) ;
118
+ if ( trNode == null ) return null ;
119
+ return trNode . Descendants ( "button" ) . FirstOrDefault ( x => x . HasClass ( "green" ) ) ;
120
+ }
121
+ default :
122
+ return null ;
123
+ } ;
124
+ }
51
125
}
52
126
}
0 commit comments