1
- using Newtonsoft . Json ;
1
+ using OpenQA . Selenium ;
2
2
using System ;
3
- using System . Collections . Generic ;
4
3
using System . Linq ;
5
4
using System . Threading . Tasks ;
6
-
7
- using OpenQA . Selenium ;
8
-
9
5
using TbsCore . Extensions ;
10
- using TbsCore . Helpers ;
11
6
using TbsCore . Models . AccModels ;
12
- using TbsCore . Models . JsObjects ;
13
7
using TbsCore . Models . VillageModels ;
14
8
using TbsCore . Parsers ;
15
9
using TbsCore . Tasks ;
@@ -81,50 +75,57 @@ public static async Task Execute(Account acc, BotTask task)
81
75
await Task . Delay ( AccountHelper . Delay ( acc ) ) ;
82
76
83
77
if ( task . Vill == null ) task . Vill = acc . Villages . FirstOrDefault ( x => x . Active ) ;
84
-
85
- acc . Logger . Information ( $ "Executing task { task . GetName ( ) } " + ( task . Vill == null ? "" : $ " in village { task . Vill . Name } ") ) ;
86
-
87
- try
78
+ acc . Logger . Information ( $ "Executing task { task . GetName ( ) } in village { task . Vill ? . Name } ") ;
79
+ TaskRes taskRes = TaskRes . Executed ;
80
+ do
88
81
{
89
- switch ( await task . Execute ( acc ) )
82
+ try
90
83
{
91
- case TaskRes . Retry :
92
- task . RetryCounter ++ ;
93
- if ( task . NextExecute == null ) task . NextExecute = DateTime . Now . AddMinutes ( 3 ) ;
94
- break ;
95
-
96
- default :
97
- task . RetryCounter = 0 ;
98
- if ( task . NextTask != null )
99
- {
100
- task . NextTask . ExecuteAt = DateTime . MinValue . AddHours ( 5 ) ;
101
- task . NextTask . Stage = TaskStage . Start ;
102
- acc . Tasks . Add ( task . NextTask ) ;
103
- task . NextTask = null ;
104
- }
105
- break ;
84
+ taskRes = await task . Execute ( acc ) ;
85
+ break ;
106
86
}
107
- }
108
- catch ( WebDriverException e ) when ( e . Message . Contains ( "chrome not reachable" ) || e . Message . Contains ( "no such window:" ) )
109
- {
110
- acc . Logger . Warning ( $ "Chrome has problem while executing task { task . GetName ( ) } ! Vill { task . Vill ? . Name } . Try reopen Chrome") ;
111
-
112
- acc . Tasks . Add ( new ReopenDriver ( )
87
+ catch ( WebDriverException e ) when ( e . Message . Contains ( "chrome not reachable" ) || e . Message . Contains ( "no such window:" ) )
113
88
{
114
- ExecuteAt = DateTime . MinValue ,
115
- Priority = TaskPriority . High ,
116
- ReopenAt = DateTime . MinValue
117
- } ) ;
118
-
119
- //try exccute task after we reopen chrome 1 mintues
89
+ acc . Logger . Warning ( $ "Chrome has problem while executing task { task . GetName ( ) } ! Vill { task . Vill ? . Name } . Try reopen Chrome") ;
120
90
121
- if ( task . NextExecute == null ) task . NextExecute = DateTime . MinValue . AddMinutes ( 1 ) ; // make sure current task is excuted after reopen driver
91
+ acc . Wb . Close ( ) ;
92
+ await acc . Wb . Init ( acc ) ;
93
+ }
94
+ catch ( Exception e )
95
+ {
96
+ acc . Logger . Error ( e , $ "Error executing task { task . GetName ( ) } ! Vill { task . Vill ? . Name } ") ;
97
+ task . RetryCounter ++ ;
98
+ if ( task . NextExecute == null )
99
+ {
100
+ task . NextExecute = DateTime . Now . AddMinutes ( 3 ) ;
101
+ acc . Tasks . ReOrder ( ) ;
102
+ }
103
+ break ;
104
+ }
122
105
}
123
- catch ( Exception e )
106
+ while ( true ) ;
107
+
108
+ switch ( taskRes )
124
109
{
125
- acc . Logger . Error ( e , $ "Error executing task { task . GetName ( ) } ! Vill { task . Vill ? . Name } ") ;
126
- task . RetryCounter ++ ;
127
- if ( task . NextExecute == null ) task . NextExecute = DateTime . Now . AddMinutes ( 3 ) ;
110
+ case TaskRes . Retry :
111
+ task . RetryCounter ++ ;
112
+ if ( task . NextExecute == null )
113
+ {
114
+ task . NextExecute = DateTime . Now . AddMinutes ( 3 ) ;
115
+ acc . Tasks . ReOrder ( ) ;
116
+ }
117
+ break ;
118
+
119
+ default :
120
+ task . RetryCounter = 0 ;
121
+ if ( task . NextTask != null )
122
+ {
123
+ task . NextTask . ExecuteAt = DateTime . MinValue . AddHours ( 5 ) ;
124
+ task . NextTask . Stage = TaskStage . Start ;
125
+ acc . Tasks . Add ( task . NextTask ) ;
126
+ task . NextTask = null ;
127
+ }
128
+ break ;
128
129
}
129
130
130
131
//We want to re-execute the same task later
@@ -135,18 +136,18 @@ public static async Task Execute(Account acc, BotTask task)
135
136
acc . Tasks . ReOrder ( ) ;
136
137
137
138
task . Stage = TaskStage . Start ;
138
- acc . Logger . Warning ( $ "Task { task . GetName ( ) } " + ( task . Vill == null ? "" : $ " in village { task . Vill . Name } will be re-executed at { task . ExecuteAt } ") ) ;
139
+ acc . Logger . Warning ( $ "Task { task . GetName ( ) } in village { task . Vill ? . Name } will be re-executed at { task . ExecuteAt } ") ;
139
140
return ;
140
141
}
141
142
// Remove the task from the task list
142
143
acc . Tasks . Remove ( task ) ;
143
144
if ( task . RetryCounter >= 3 )
144
145
{
145
- acc . Logger . Warning ( $ "Task { task . GetName ( ) } " + ( task . Vill == null ? "" : $ " in village { task . Vill . Name } is already re-executed 3 times. Ignore it") ) ;
146
+ acc . Logger . Warning ( $ "Task { task . GetName ( ) } in village { task . Vill ? . Name } is already re-executed 3 times. Ignore it") ;
146
147
}
147
148
else
148
149
{
149
- acc . Logger . Information ( $ "Task { task . GetName ( ) } " + ( task . Vill == null ? "" : $ " in village { task . Vill . Name } is done.") ) ;
150
+ acc . Logger . Information ( $ "Task { task . GetName ( ) } in village { task . Vill ? . Name } is done.") ;
150
151
}
151
152
}
152
153
@@ -180,8 +181,6 @@ public static void UpdateDorf2Info(Account acc)
180
181
//remove any further UpdateDorf2 BotTasks for this village (if below 5min)
181
182
acc . Tasks . Remove ( typeof ( UpdateDorf2 ) , vill , 5 ) ;
182
183
183
- UpdateCurrentlyBuilding ( acc , vill ) ;
184
-
185
184
var buildings = InfrastructureParser . GetBuildings ( acc , acc . Wb . Html ) ;
186
185
foreach ( var field in buildings )
187
186
{
@@ -190,6 +189,8 @@ public static void UpdateDorf2Info(Account acc)
190
189
building . Type = field . Type ;
191
190
building . UnderConstruction = field . UnderConstruction ;
192
191
}
192
+
193
+ UpdateCurrentlyBuilding ( acc , vill ) ;
193
194
}
194
195
195
196
public static void UpdateDorf1Info ( Account acc )
@@ -200,8 +201,6 @@ public static void UpdateDorf1Info(Account acc)
200
201
//remove any further UpdateDorf1 BotTasks for this village (if below 5min)
201
202
acc . Tasks . Remove ( typeof ( UpdateDorf1 ) , vill , 5 ) ;
202
203
203
- UpdateCurrentlyBuilding ( acc , vill ) ;
204
-
205
204
var dorf1Movements = TroopsMovementParser . ParseDorf1Movements ( acc . Wb . Html ) ;
206
205
207
206
// Check attacks if there are incoming attacks and alerts aren't disabled and task isn't already on task list
@@ -224,16 +223,21 @@ public static void UpdateDorf1Info(Account acc)
224
223
building . Type = field . Type ;
225
224
building . UnderConstruction = field . UnderConstruction ;
226
225
}
226
+
227
+ UpdateCurrentlyBuilding ( acc , vill ) ;
227
228
}
228
229
229
230
private static void UpdateCurrentlyBuilding ( Account acc , Village vill )
230
231
{
231
232
vill . Build . CurrentlyBuilding . Clear ( ) ;
232
- var cb = InfrastructureParser . CurrentlyBuilding ( acc . Wb . Html , acc ) ;
233
+ var cb = InfrastructureParser . CurrentlyBuilding ( acc . Wb . Html ) ;
233
234
if ( cb == null ) return ; // Nothing is currently building
234
235
235
236
for ( int i = 0 ; i < cb . Count ; i ++ )
236
237
{
238
+ var build = vill . Build . Buildings . FirstOrDefault ( x => x . Type == cb [ i ] . Building && x . Level - cb [ i ] . Level < 3 ) ;
239
+ if ( build == null ) continue ;
240
+ cb [ i ] . Location = build . Id ;
237
241
vill . Build . CurrentlyBuilding . Add ( cb [ i ] ) ;
238
242
}
239
243
}
0 commit comments