forked from HearthSim/Hearthstone-Deck-Tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BattlegroundsTeammateBoardStateWatcher.cs
59 lines (52 loc) · 1.57 KB
/
BattlegroundsTeammateBoardStateWatcher.cs
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
using HearthWatcher.EventArgs;
using HearthWatcher.Providers;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using HearthMirror.Objects;
namespace HearthWatcher
{
public class BattlegroundsTeammateBoardStateWatcher
{
public delegate void BattlegroundsTeammateBoardStateWatcherHandler(object sender, BattlegroundsTeammateBoardStateArgs args);
private readonly IBattlegroundsTeammateBoardStateProvider _provider;
private readonly int _delay;
private bool _running;
private bool _watch;
private BattlegroundsTeammateBoardStateArgs? _prev;
public BattlegroundsTeammateBoardStateWatcher(IBattlegroundsTeammateBoardStateProvider provider, int delay = 200)
{
_provider = provider ?? throw new ArgumentNullException(nameof(provider));
_delay = delay;
}
public event BattlegroundsTeammateBoardStateWatcherHandler Change;
public void Run()
{
_watch = true;
if(!_running)
Update();
}
public void Stop() => _watch = false;
private async void Update()
{
_running = true;
while(_watch)
{
await Task.Delay(_delay);
if(!_watch)
break;
var curr = new BattlegroundsTeammateBoardStateArgs(
_provider.BattlegroundsTeammateBoardState?.ViewingTeammate ?? false,
_provider.BattlegroundsTeammateBoardState?.MulliganHeroes ?? new List<string>(),
_provider.BattlegroundsTeammateBoardState?.Entities ?? new List<BattlegroundsTeammateBoardStateEntity>()
);
if(curr.Equals(_prev))
continue;
Change?.Invoke(this, curr);
_prev = curr;
}
_prev = null;
_running = false;
}
}
}