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,45 +11,114 @@ 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 ) ) ;
31
44
32
- acc . Wb . UpdateHtml ( ) ;
45
+ usernameElement . SendKeys ( Keys . Home ) ;
46
+ usernameElement . SendKeys ( Keys . Shift + Keys . End ) ;
47
+ usernameElement . SendKeys ( acc . AccInfo . Nickname ) ;
33
48
34
- if ( TaskExecutor . IsLoginScreen ( acc ) )
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 ( ) ;
56
+
57
+ var result = await DriverHelper . WaitPageChange ( acc , "dorf" ) ;
58
+
59
+ if ( ! result && TaskExecutor . IsLoginScreen ( acc ) )
35
60
{
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
42
68
{
43
- await DriverHelper . WaitPageChange ( acc , "dorf" ) ;
44
69
// check sitter account
45
- var auction = acc . Wb . Html . DocumentNode . SelectSingleNode ( "//a[contains(@class,'auction')]" ) ;
46
-
70
+ var auction = acc . Wb . Html . DocumentNode . Descendants ( "a" ) . FirstOrDefault ( x => x . HasClass ( "auction" ) ) ;
47
71
acc . Access . GetCurrentAccess ( ) . IsSittering = ( auction != null && auction . HasClass ( "disable" ) ) ;
48
72
return TaskRes . Executed ;
49
73
}
50
74
}
75
+
76
+ private HtmlNode GetUsernameNode ( Account acc )
77
+ {
78
+ switch ( acc . AccInfo . ServerVersion )
79
+ {
80
+ case Classificator . ServerVersionEnum . TTwars :
81
+ return acc . Wb . Html . DocumentNode . Descendants ( "input" ) . FirstOrDefault ( x => x . GetAttributeValue ( "name" , "" ) . Equals ( "user" ) ) ;
82
+
83
+ case Classificator . ServerVersionEnum . T4_5 :
84
+ return acc . Wb . Html . DocumentNode . Descendants ( "input" ) . FirstOrDefault ( x => x . GetAttributeValue ( "name" , "" ) . Equals ( "name" ) ) ;
85
+
86
+ default :
87
+ return null ;
88
+ } ;
89
+ }
90
+
91
+ private HtmlNode GetPasswordNode ( Account acc )
92
+ {
93
+ switch ( acc . AccInfo . ServerVersion )
94
+ {
95
+ case Classificator . ServerVersionEnum . TTwars :
96
+ return acc . Wb . Html . DocumentNode . Descendants ( "input" ) . FirstOrDefault ( x => x . GetAttributeValue ( "name" , "" ) . Equals ( "pw" ) ) ;
97
+
98
+ case Classificator . ServerVersionEnum . T4_5 :
99
+ return acc . Wb . Html . DocumentNode . Descendants ( "input" ) . FirstOrDefault ( x => x . GetAttributeValue ( "name" , "" ) . Equals ( "password" ) ) ;
100
+
101
+ default :
102
+ return null ;
103
+ } ;
104
+ }
105
+
106
+ private HtmlNode GetLoginButton ( Account acc )
107
+ {
108
+ switch ( acc . AccInfo . ServerVersion )
109
+ {
110
+ case Classificator . ServerVersionEnum . TTwars :
111
+ return acc . Wb . Html . GetElementbyId ( "s1" ) ;
112
+
113
+ case Classificator . ServerVersionEnum . T4_5 :
114
+ {
115
+ var trNode = acc . Wb . Html . DocumentNode . Descendants ( "tr" ) . FirstOrDefault ( x => x . HasClass ( "loginButtonRow" ) ) ;
116
+ if ( trNode == null ) return null ;
117
+ return trNode . Descendants ( "button" ) . FirstOrDefault ( x => x . HasClass ( "green" ) ) ;
118
+ }
119
+ default :
120
+ return null ;
121
+ } ;
122
+ }
51
123
}
52
124
}
0 commit comments