-
-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathRxQueue.cs
More file actions
105 lines (92 loc) · 4.26 KB
/
Copy pathRxQueue.cs
File metadata and controls
105 lines (92 loc) · 4.26 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
using Microsoft.Extensions.DependencyInjection;
using System.Reactive.Concurrency;
using System.Reactive.Subjects;
namespace MainCore.Services
{
[RegisterSingleton<IRxQueue, RxQueue>]
public class RxQueue : IRxQueue
{
private readonly Subject<INotification> _notifications = new Subject<INotification>();
private readonly IConnectableObservable<INotification> _connectableObservable;
private readonly ICustomServiceScopeFactory _serviceScopeFactory;
public RxQueue(ICustomServiceScopeFactory serviceScopeFactory)
{
_serviceScopeFactory = serviceScopeFactory;
_connectableObservable = _notifications.ObserveOn(Scheduler.Default).Publish();
_connectableObservable.Connect();
}
public void Enqueue(INotification notification)
{
_notifications.OnNext(notification);
}
public void Setup()
{
RegisterHandler<AccountInit>(AccountInitHandler);
RegisterHandler<VillageTaskAdded>(VillageTaskAddedHandler);
}
private void AccountInitHandler(AccountInit notification)
{
var accountId = notification.AccountId;
using var scope = _serviceScopeFactory.CreateScope();
var context = scope.ServiceProvider.GetRequiredService<AppDbContext>();
var taskManager = scope.ServiceProvider.GetRequiredService<ITaskManager>();
taskManager.Add(new LoginTask.Task(accountId), first: true);
var workTime = context.ByName(accountId, AccountSettingEnums.WorkTimeMin, AccountSettingEnums.WorkTimeMax);
var sleepTask = new SleepTask.Task(accountId);
sleepTask.ExecuteAt = DateTime.Now.AddMinutes(workTime);
taskManager.AddOrUpdate<SleepTask.Task>(sleepTask);
var startAdventureTask = new StartAdventureTask.Task(accountId);
if (startAdventureTask.CanStart(context) && !taskManager.IsExist<StartAdventureTask.Task>(accountId))
{
taskManager.Add(startAdventureTask);
}
var villagesSpec = new VillagesSpec(accountId);
var villages = context.Villages
.WithSpecification(villagesSpec)
.ToList();
foreach (var village in villages)
{
var updateVillageTask = new UpdateVillageTask.Task(accountId, village);
if (updateVillageTask.CanStart(context) && !taskManager.IsExist<UpdateVillageTask.Task>(accountId, village))
{
taskManager.Add(updateVillageTask);
}
var trainTroopTask = new TrainTroopTask.Task(accountId, village);
if (trainTroopTask.CanStart(context) && !taskManager.IsExist<TrainTroopTask.Task>(accountId, village))
{
taskManager.Add(trainTroopTask);
}
}
var hasBuildJobVillagesSpec = new HasBuildJobVillagesSpec(accountId);
var hasBuildJobVillages = context.Villages
.WithSpecification(hasBuildJobVillagesSpec)
.ToList();
foreach (var village in hasBuildJobVillages)
{
var upgradeBuildingTask = new UpgradeBuildingTask.Task(accountId, village);
if (!taskManager.IsExist<UpgradeBuildingTask.Task>(accountId, village))
{
taskManager.Add(upgradeBuildingTask);
}
}
}
private void VillageTaskAddedHandler(VillageTaskAdded notification)
{
using var scope = _serviceScopeFactory.CreateScope();
var context = scope.ServiceProvider.GetRequiredService<AppDbContext>();
notification.Task.SetVillageName(context);
}
public void RegisterHandler<T>(Action<T> handleAction) where T : INotification
{
_connectableObservable.OfType<T>().Subscribe(handleAction);
}
public void RegisterCommand<T>(ReactiveCommand<T, Unit> command) where T : INotification
{
GetObservable<T>().InvokeCommand(command);
}
public IObservable<T> GetObservable<T>() where T : INotification
{
return _connectableObservable.OfType<T>();
}
}
}