-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameTimer.cs
174 lines (162 loc) · 5.75 KB
/
GameTimer.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
using System;
using System.Collections.Generic;
using System.Text;
namespace SrcChess {
/// <summary>
/// Handle the timer for both player
/// </summary>
public class GameTimer {
/// <summary>true if timer is tickling</summary>
private bool m_bEnabled;
/// <summary>Time of last commit</summary>
private DateTime m_timerStart;
/// <summary>Commited time for the white</summary>
private TimeSpan m_timeSpanCommitedWhite;
/// <summary>Commited time for the black</summary>
private TimeSpan m_timeSpanCommitedBlack;
/// <summary>Player currently playing</summary>
private ChessBoard.PlayerColorE m_ePlayerColor;
//*********************************************************
//
/// <summary>
/// Class constructor
/// </summary>
//
//*********************************************************
public GameTimer() {
m_bEnabled = false;
Reset(ChessBoard.PlayerColorE.White);
}
//*********************************************************
//
/// <summary>
/// Commit the uncommited time to the current player
/// </summary>
//
//*********************************************************
private void Commit() {
DateTime now;
TimeSpan span;
if (m_bEnabled) {
now = DateTime.Now;
span = now - m_timerStart;
m_timerStart = now;
if (m_ePlayerColor == ChessBoard.PlayerColorE.White) {
m_timeSpanCommitedWhite += span;
} else {
m_timeSpanCommitedBlack += span;
}
}
}
//*********************************************************
//
/// <summary>
/// Enabled state of the timer
/// </summary>
//
//*********************************************************
public bool Enabled {
get {
return(m_bEnabled);
}
set {
if (value != m_bEnabled) {
if (value) {
m_timerStart = DateTime.Now;
} else {
Commit();
}
m_bEnabled = value;
}
}
}
//*********************************************************
//
/// <summary>
/// Reset the timer of both player
/// </summary>
/// <param name="ePlayerColor"> Playing color</param>
/// <param name="lWhiteTicks"> White Ticks</param>
/// <param name="lBlackTicks"> Black Ticks</param>
//
//*********************************************************
public void ResetTo(ChessBoard.PlayerColorE ePlayerColor, long lWhiteTicks, long lBlackTicks) {
m_ePlayerColor = ePlayerColor;
m_timeSpanCommitedWhite = new TimeSpan(lWhiteTicks);
m_timeSpanCommitedBlack = new TimeSpan(lBlackTicks);
m_timerStart = DateTime.Now;
}
//*********************************************************
//
/// <summary>
/// Reset the timer of both player
/// </summary>
/// <param name="ePlayerColor"> Playing color</param>
//
//*********************************************************
public void Reset(ChessBoard.PlayerColorE ePlayerColor) {
ResetTo(ePlayerColor, 0, 0);
}
//*********************************************************
//
/// <summary>
/// Color of the player playing
/// </summary>
//
//*********************************************************
public ChessBoard.PlayerColorE PlayerColor {
get {
return(m_ePlayerColor);
}
set {
m_ePlayerColor = value;
}
}
//*********************************************************
//
/// <summary>
/// Time spent by the white player
/// </summary>
//
//*********************************************************
public TimeSpan WhitePlayTime {
get {
Commit();
return(m_timeSpanCommitedWhite);
}
}
//*********************************************************
//
/// <summary>
/// Time spent by the black player
/// </summary>
//
//*********************************************************
public TimeSpan BlackPlayTime {
get {
Commit();
return(m_timeSpanCommitedBlack);
}
}
//*********************************************************
//
/// <summary>
/// Time span to string
/// </summary>
//
//*********************************************************
public static string GetHumanElapse(TimeSpan timeSpan) {
string strRetVal;
int iIndex;
strRetVal = timeSpan.ToString();
iIndex = strRetVal.IndexOf(':');
if (iIndex != -1) {
iIndex = strRetVal.IndexOf('.', iIndex);
if (iIndex != -1) {
strRetVal = strRetVal.Substring(0, iIndex);
}
}
return(strRetVal);
}
}
}