-
-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathAccountTaskBehavior.cs
More file actions
62 lines (53 loc) · 2.69 KB
/
Copy pathAccountTaskBehavior.cs
File metadata and controls
62 lines (53 loc) · 2.69 KB
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
using MainCore.Tasks.Base;
namespace MainCore.Behaviors
{
public sealed class AccountTaskBehavior<TRequest, TResponse>
: Behavior<TRequest, TResponse>
where TRequest : AccountTask
where TResponse : Result
{
private readonly ITaskManager _taskManager;
private readonly IChromeBrowser _browser;
private readonly UpdateAccountInfoCommand.Handler _updateAccountInfoCommand;
private readonly UpdateVillageListCommand.Handler _updateVillageListCommand;
private readonly UpdateAdventureCommand.Handler _updateAdventureCommand;
public AccountTaskBehavior(IChromeBrowser browser, ITaskManager taskManager, UpdateAccountInfoCommand.Handler updateAccountInfoCommand, UpdateVillageListCommand.Handler updateVillageListCommand, UpdateAdventureCommand.Handler updateAdventureCommand)
{
_browser = browser;
_taskManager = taskManager;
_updateAccountInfoCommand = updateAccountInfoCommand;
_updateVillageListCommand = updateVillageListCommand;
_updateAdventureCommand = updateAdventureCommand;
}
public override async ValueTask<TResponse> HandleAsync(TRequest request, CancellationToken cancellationToken)
{
var accountId = request.AccountId;
if (!LoginParser.IsIngamePage(_browser.Html))
{
if (!LoginParser.IsLoginPage(_browser.Html))
{
return (TResponse)Stop.Error.WithError("Travian is not ingame nor login page. Please check browser");
}
if (request is not LoginTask.Task)
{
_taskManager.AddOrUpdate<LoginTask.Task>(new(accountId), first: true);
request.ExecuteAt = request.ExecuteAt.AddSeconds(1);
return (TResponse)Skip.Error.WithError("Account is logout. Re-login now");
}
}
if (LoginParser.IsIngamePage(_browser.Html))
{
await _updateAccountInfoCommand.HandleAsync(new(accountId), cancellationToken);
await _updateVillageListCommand.HandleAsync(new(accountId), cancellationToken);
}
var response = await Next(request, cancellationToken);
if (LoginParser.IsIngamePage(_browser.Html))
{
await _updateAccountInfoCommand.HandleAsync(new(accountId), cancellationToken);
await _updateVillageListCommand.HandleAsync(new(accountId), cancellationToken);
await _updateAdventureCommand.HandleAsync(new(accountId), cancellationToken);
}
return response;
}
}
}