forked from HearthSim/Hearthstone-Deck-Tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TurnTimer.cs
133 lines (117 loc) · 3.35 KB
/
TurnTimer.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
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#region
using System;
using System.Media;
using System.Threading.Tasks;
using System.Timers;
using HearthDb.Enums;
using Hearthstone_Deck_Tracker.Enums;
using Hearthstone_Deck_Tracker.Hearthstone;
using Hearthstone_Deck_Tracker.Utility.Logging;
#endregion
namespace Hearthstone_Deck_Tracker
{
internal class TimerState
{
public TimerState(double seconds, int playerSeconds, int opponentSeconds)
{
Seconds = seconds;
PlayerSeconds = playerSeconds;
OpponentSeconds = opponentSeconds;
}
public double Seconds { get; private set; }
public int PlayerSeconds { get; private set; }
public int OpponentSeconds { get; private set; }
}
internal class TurnTimer
{
private readonly Timer _timer = new Timer(1000) {AutoReset = true};
private GameV2? _game;
private TurnTimer()
{
_timer.Elapsed += TimerOnElapsed;
}
static TurnTimer()
{
}
public double Seconds { get; private set; }
public int PlayerSeconds { get; private set; }
public int OpponentSeconds { get; private set; }
private bool IsPlayersTurn => _game?.PlayerEntity?.HasTag(GameTag.CURRENT_PLAYER) ?? false;
public static TurnTimer Instance { get; } = new TurnTimer();
private void TimerOnElapsed(object sender, ElapsedEventArgs elapsedEventArgs)
{
Seconds--;
if(_game?.IsMulliganDone ?? false)
{
if(IsPlayersTurn)
PlayerSeconds++;
else
OpponentSeconds++;
}
TimerTick(new TimerState(Seconds, PlayerSeconds, OpponentSeconds));
}
public async Task Start(GameV2 game)
{
Log.Info("Starting turn timer");
if(_game != null)
{
Log.Warn("Turn timer is already running");
return;
}
_game = game;
PlayerSeconds = 0;
OpponentSeconds = 0;
Seconds = 75;
if(game.PlayerEntity == null)
Log.Warn("Waiting for player entity");
while(game.PlayerEntity == null)
await Task.Delay(100);
if(game.OpponentEntity == null)
Log.Warn("Waiting for player entity");
while(game.OpponentEntity == null)
await Task.Delay(100);
TimerTick(new TimerState(Seconds, PlayerSeconds, OpponentSeconds));
_timer.Start();
}
public void Stop()
{
if(_game == null)
return;
Log.Info("Stopping turn timer");
_timer.Stop();
_game = null;
}
private void TimerTick(TimerState timerState)
{
Core.Overlay.Dispatcher.BeginInvoke(new Action(() => Core.Overlay.UpdateTurnTimer(timerState)));
Core.Windows.TimerWindow.Dispatcher.BeginInvoke(new Action(() => Core.Windows.TimerWindow.Update(timerState)));
if(IsPlayersTurn)
CheckForTimerAlarm();
}
private void CheckForTimerAlarm()
{
if(!Config.Instance.TimerAlert || Seconds != Config.Instance.TimerAlertSeconds)
return;
SystemSounds.Asterisk.Play();
User32.FlashHs();
}
public void SetPlayer(ActivePlayer player)
{
if(_game == null)
{
Seconds = 75;
Log.Warn("Set timer to 75, game is null");
return;
}
if(player == ActivePlayer.Player && _game.PlayerEntity != null)
Seconds = _game.PlayerEntity.HasTag(GameTag.TIMEOUT) ? _game.PlayerEntity.GetTag(GameTag.TIMEOUT) : double.PositiveInfinity;
else if(player == ActivePlayer.Opponent && _game.OpponentEntity != null)
Seconds = _game.OpponentEntity.HasTag(GameTag.TIMEOUT) ? _game.OpponentEntity.GetTag(GameTag.TIMEOUT) : double.PositiveInfinity;
else
{
Seconds = 75;
Log.Warn("Set timer to 75, both player entities are null");
}
}
}
}