-
Notifications
You must be signed in to change notification settings - Fork 0
/
Turns.cpp
174 lines (150 loc) · 3.97 KB
/
Turns.cpp
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
#include "Turns.h"
#include <iostream>
#include <algorithm>
#include <sstream>
#include <iomanip>
FischerTurnManager::FischerTurnManager(void) :
clock (),
indices (),
increments (),
allocations (),
participants (),
index ( 0 ),
running ( false )
{
}
void FischerTurnManager::addParticipant(int id, double seconds, double increment) {
using namespace std;
increments[id] = increment;
allocations[id] = seconds;
indices[id] = participants.size();
participants.push_back( id );
}
void FischerTurnManager::addTime(int id, double seconds) {
if( participants[id] < 0 ) {
// you can never go into negative time
// if you get, say, 30 seconds when you're
// technically 10 seconds past, you still
// have 30 seconds to move, not 20
participants[ id ] = seconds;
} else {
participants[ id ] += seconds;
}
}
double FischerTurnManager::getRemainingTime(int id) {
flushTime();
return allocations[ indices[ id ] ];
}
double FischerTurnManager::getCurrentRemainingTime(void) {
flushTime();
return allocations[participants[index]];
}
void FischerTurnManager::wrapIndex(void) {
if( index >= (int) participants.size() ) {
index = 0;
}
}
int FischerTurnManager::skip(void) {
using namespace std;
if( !participants.size() ) {
return -1;
}
++index;
wrapIndex();
return current();
}
int FischerTurnManager::next(void) {
using namespace std;
if( !participants.size() ) {
return -1;
}
allocations[participants[index]] += increments[participants[index]];
++index;
wrapIndex();
return current();
}
int FischerTurnManager::current(void) {
if( !participants.size() ) {
return -1;
}
return participants[index];
}
void FischerTurnManager::start(void) {
flushTime();
running = true;
}
void FischerTurnManager::stop(void) {
flushTime();
running = false;
}
void FischerTurnManager::flushTime(void) {
if( running ) {
allocations[ participants[index] ] -= clock.getElapsedTime();
}
clock.reset();
}
void FischerTurnManager::removeParticipant(int id) {
// because this is so terribly useful? yeah, I don't know
// hackish because it's pretty much only useful for test code
std::vector<int>::iterator i = find( participants.begin(), participants.end(), id );
if( i == participants.end() ) {
return;
}
participants.erase( i );
increments.erase( id );
allocations.erase( id );
indices.clear();
for(std::map<int,double>::iterator j = allocations.begin(); j != allocations.end(); j++) {
indices[j->second] = j->first;
}
wrapIndex();
}
Timer::Timer(void) {
reset();
}
void Timer::reset(void) {
gettimeofday( &t0, 0 );
}
double Timer::getElapsedTime(void) {
struct timeval t1, dt;
gettimeofday( &t1, 0 );
timersub( &t1, &t0, &dt );
return dt.tv_sec + 0.000001 * dt.tv_usec;
}
std::string formatTimeCoarse(double seconds) {
int t = (int)(0.5 + seconds * 1000);
const int k[] = { 1000 * 60 * 60, 1000 * 60, 1000 };
const int kn = 3;
int r[kn];
std::ostringstream oss;
for(int i=0;i<kn;i++) {
r[i] = t / k[i];
t -= r[i] * k[i];
}
for(int i=0;i<kn;i++) {
if( i != 0 ) oss << ":";
oss << std::setfill('0') << std::setw(2);
oss << r[i];
}
return oss.str();
}
std::string formatTime(double seconds) {
int t = (int)(0.5 + seconds * 1000);
const int k[] = { 1000 * 60 * 60 * 24, 1000 * 60 * 60, 1000 * 60, 1000, 1 };
const int kn = 5;
const std::string sx[] = { "d", "h", "m", "s", "ms" };
int r[kn];
std::ostringstream oss;
for(int i=0;i<kn;i++) {
r[i] = t / k[i];
t -= r[i] * k[i];
}
for(int i=0;i<kn;i++) if( r[i] ) {
if( i != 0 ) oss << " ";
oss << r[i] << sx[i];
}
return oss.str();
}
int FischerTurnManager::getNumberOfParticipants(void) {
return participants.size();
}