forked from HearthSim/Hearthstone-Deck-Tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTurnTimer.cs
149 lines (130 loc) · 3.94 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#region
using System;
using System.Media;
using System.Timers;
using Hearthstone_Deck_Tracker.Enums;
#endregion
namespace Hearthstone_Deck_Tracker
{
public class TimerEventArgs : EventArgs
{
public TimerEventArgs(int seconds, int playerSeconds, int opponentSeconds, bool running, ActivePlayer activePlayer)
{
Seconds = seconds;
Running = running;
PlayerSeconds = playerSeconds;
OpponentSeconds = opponentSeconds;
CurrentActivePlayer = activePlayer;
}
public int Seconds { get; private set; }
public int PlayerSeconds { get; private set; }
public int OpponentSeconds { get; private set; }
public bool Running { get; private set; }
public ActivePlayer CurrentActivePlayer { get; private set; }
}
internal class TurnTimer
{
private bool _opponentMulliganed;
private bool _playerMulliganed;
private Timer _timer;
private int _turnTime;
public ActivePlayer CurrentActivePlayer;
private TurnTimer()
{
}
public int Seconds { get; private set; }
public int PlayerSeconds { get; private set; }
public int OpponentSeconds { get; private set; }
private static TurnTimer _instance;
public static TurnTimer Instance
{
get
{
if (_instance == null) Create(Config.Instance.TimerTurnTime);
return _instance;
}
}
public void SetTurnTime(int turnTime)
{
_turnTime = turnTime;
}
/// <summary>
///
/// </summary>
/// <param name="turnTime">Time of a turn in seconds</param>
private static void Create(int turnTime)
{
_instance = new TurnTimer
{
Seconds = turnTime,
PlayerSeconds = 0,
OpponentSeconds = 0,
_turnTime = turnTime,
_timer = new Timer(1000) {AutoReset = true, Enabled = true}
};
_instance._timer.Elapsed += Instance.TimerOnElapsed;
_instance._timer.Stop();
}
private void TimerOnElapsed(object sender, ElapsedEventArgs elapsedEventArgs)
{
if(Seconds > 0)
Seconds--;
if(_playerMulliganed && _opponentMulliganed)
{
if(CurrentActivePlayer == ActivePlayer.Player)
PlayerSeconds++;
else
OpponentSeconds++;
}
TimerTick(this, new TimerEventArgs(Seconds, PlayerSeconds, OpponentSeconds, true, CurrentActivePlayer));
}
public void Restart()
{
Seconds = _turnTime;
_timer.Stop();
_timer.Start();
if((!_playerMulliganed && _opponentMulliganed) || (!_opponentMulliganed && _playerMulliganed)
|| (!_opponentMulliganed && !_playerMulliganed && Seconds < 85))
_playerMulliganed = _opponentMulliganed = true;
TimerTick(this, new TimerEventArgs(Seconds, PlayerSeconds, OpponentSeconds, true, CurrentActivePlayer));
}
public void Stop()
{
_timer.Stop();
PlayerSeconds = 0;
OpponentSeconds = 0;
_playerMulliganed = false;
_opponentMulliganed = false;
TimerTick(this, new TimerEventArgs(Seconds, PlayerSeconds, OpponentSeconds, false, CurrentActivePlayer));
}
public void SetCurrentPlayer(ActivePlayer activePlayer)
{
CurrentActivePlayer = activePlayer;
}
public void MulliganDone(ActivePlayer activePlayer)
{
if(activePlayer.Equals(ActivePlayer.Player))
_playerMulliganed = true;
else if(activePlayer.Equals(ActivePlayer.Opponent))
_opponentMulliganed = true;
}
private void TimerTick(TurnTimer sender, TimerEventArgs timerEventArgs)
{
Core.Overlay.Dispatcher.BeginInvoke(new Action(() => Core.Overlay.UpdateTurnTimer(timerEventArgs)));
Core.Windows.TimerWindow.Dispatcher.BeginInvoke(new Action(() => Core.Windows.TimerWindow.Update(timerEventArgs)));
if(CurrentActivePlayer == ActivePlayer.Player)
CheckForTimerAlarm();
}
private void CheckForTimerAlarm()
{
if(Config.Instance.TimerAlert)
{
if(Seconds == Config.Instance.TimerAlertSeconds)
{
SystemSounds.Asterisk.Play();
User32.FlashHs();
}
}
}
}
}