-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreferee.cpp
342 lines (292 loc) · 8.36 KB
/
referee.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/**
* Referee
*
* Send a start position into 2 game communication channels,
* observe positions sent in one channel, and if valid, broadcast
* them into other channel.
* For a time limited game, times are updated by referee itself.
*
* (C) 2005-2015, Josef Weidendorfer, GPLv2+
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/time.h>
#include "board.h"
#include "network.h"
/* Global, static vars */
static NetworkLoop l;
static Board myBoard;
/* Time of last draw */
static struct timeval t1;
class MyDomain;
static MyDomain *d1 = 0, *d2 = 0;
/*
* Do other members in the channels exist at all?
* Only start time if there are players.
*
* Note: We can not distinguish between observers and players,
* so game always start when another member is seen.
*/
static bool d1MemberExists = false;
static bool d2MemberExists = false;
/* time limit in seconds (0: no time limit) */
static int secsToPlay = 0;
static int msecsToPlay[3] = {0,0,0};
/* to set verbosity of NetworkLoop implementation */
extern int verbose;
/* show board on stdout after each move? */
static int showBoard = 1;
#define DEFAULT_DOMAIN_PORT 23412
#define DEFAULT_DOMAIN_DIFF 50
/* remote channel */
static char* host[2] = {0,0}; /* not used per default */
static int rport[2] = {DEFAULT_DOMAIN_PORT, DEFAULT_DOMAIN_PORT + DEFAULT_DOMAIN_DIFF};
/* local channel */
static int lport[2] = {DEFAULT_DOMAIN_PORT, DEFAULT_DOMAIN_PORT + DEFAULT_DOMAIN_DIFF};
/* Where to read position to broadcast from? (0: start position) */
static FILE* file = 0;
class MyDomain: public NetworkDomain
{
public:
MyDomain(int p) : NetworkDomain(p) { sent = 0; }
void sendBoard(Board*);
protected:
void received(char* str);
void newConnection(Connection*);
private:
Board* sent;
};
void MyDomain::sendBoard(Board* b)
{
if (b) {
static char tmp[500];
sprintf(tmp, "pos %s\n", b->getState());
broadcast(tmp);
}
sent = b;
}
void MyDomain::received(char* str)
{
if (strncmp(str, "quit", 4)==0) {
l.exit();
return;
}
if (strncmp(str, "pos ", 4)!=0) return;
// on receiving remote position, do not broadcast own board any longer
sent = 0;
if (myBoard.validState() != Board::empty) {
Board newBoard;
newBoard.setState(str+4);
Move m = myBoard.moveToReach(&newBoard, false);
if (m.type == Move::none) {
printf("WARNING: Got a board which is not reachable via a valid move !?\n");
return;
}
else {
struct timeval t2;
gettimeofday(&t2,0);
int msecsPassed =
(1000* t2.tv_sec + t2.tv_usec / 1000) -
(1000* t1.tv_sec + t1.tv_usec / 1000);
t1 = t2;
int* pMSecs;
if (myBoard.actColor() == Board::color1) {
pMSecs = &(msecsToPlay[Board::color1]);
printf("O");
}
else {
pMSecs = &(msecsToPlay[Board::color2]);
printf("X");
}
printf(" draws '%s' (after %d.%03d secs)...\n",
m.name(), msecsPassed/1000, msecsPassed%1000);
if (*pMSecs > msecsPassed)
*pMSecs -= msecsPassed;
else
*pMSecs = 0;
}
}
myBoard.setState(str+4);
/* force our objective view regarding time */
myBoard.setMSecsToPlay(Board::color1, msecsToPlay[Board::color1] );
myBoard.setMSecsToPlay(Board::color2, msecsToPlay[Board::color2] );
int state = myBoard.validState();
if (showBoard)
printf("%s%s\n", myBoard.getState(), Board::stateDescription(state));
else
printf("%s - %s\n", myBoard.getShortState(), Board::stateDescription(state));
switch(state) {
case Board::timeout1:
case Board::timeout2:
case Board::win1:
case Board::win2:
l.exit();
default:
break;
}
/* send to other domain */
if (d1 == this) if (d2) d2->sendBoard(&myBoard);
if (d2 == this) if (d1) d1->sendBoard(&myBoard);
}
void MyDomain::newConnection(Connection* c)
{
NetworkDomain::newConnection(c);
if (sent) {
static char tmp[500];
int len = sprintf(tmp, "pos %s\n", sent->getState());
c->sendString(tmp, len);
}
/* adjust time if this is first connection for the channel */
if (!d1MemberExists && (this == d1)) {
d1MemberExists = true;
gettimeofday(&t1,0);
}
if (!d2MemberExists && (this == d2)) {
d2MemberExists = true;
gettimeofday(&t1,0);
}
}
static void printHelp(char* prg, bool printHeader)
{
if (printHeader)
printf("Referee V 0.2\n"
"Broadcast a game position into 2 domains, observe moves played in one domain,\n"
"and if valid, broadcast to other domain. Stops playing times itself.\n\n");
printf("Usage: %s [options] [<file>|-]\n\n"
" <file> File containing game position (default: start position)\n"
" - Position is read from standard input\n\n",
prg);
printf(" Options:\n"
" -h / --help Print this help text\n"
" -v / -vv Be verbose / more verbose\n"
" -n Do not show board\n"
" -t <timeToPlay> Start in tournament modus (limited time)\n"
" -p [host:][port] Connection to first (second) broadcast channel\n"
" (default: %d / %d)\n\n",
DEFAULT_DOMAIN_PORT, DEFAULT_DOMAIN_PORT + DEFAULT_DOMAIN_DIFF);
exit(1);
}
static void parseArgs(int argc, char* argv[])
{
int domainsSet = 0;
int arg=0;
while(arg+1<argc) {
arg++;
if (argv[arg][0] == '-') {
if (strcmp(argv[arg],"-h")==0 ||
strcmp(argv[arg],"--help")==0) printHelp(argv[0], true);
if (strcmp(argv[arg],"-v")==0) {
verbose = 1;
continue;
}
if (strcmp(argv[arg],"-vv")==0) {
verbose = 2;
continue;
}
if (strcmp(argv[arg],"-n")==0) {
showBoard = 0;
continue;
}
if ((strcmp(argv[arg],"-t")==0) && (arg+1<argc)) {
arg++;
secsToPlay = atoi(argv[arg]);
if (secsToPlay == 0) {
printf("%s: WARNING - Ignoring tournament; %d secs to play\n",
argv[0], secsToPlay);
}
continue;
}
if ((strcmp(argv[arg],"-p")==0) && (arg+1<argc)) {
arg++;
if (domainsSet>1) {
printf("%s: WARNING - Domain specification %s ignored.\n",
argv[0], argv[arg]);
continue;
}
if (argv[arg][0]>'0' && argv[arg][0]<='9') {
lport[domainsSet] = atoi(argv[arg]);
domainsSet++;
continue;
}
char* c = strrchr(argv[arg],':');
int p = 0;
if (c != 0) {
*c = 0;
p = atoi(c+1);
}
host[domainsSet] = argv[arg];
if (p) rport[domainsSet] = p;
domainsSet++;
continue;
}
if (strcmp(argv[arg],"-")==0) {
file = stdin;
continue;
}
printf("%s: ERROR - Unknown option %s\n", argv[0], argv[arg]);
printHelp(argv[0], false);
}
file = fopen(argv[arg], "r");
if (!file) {
printf("%s: ERROR - Can not open '%s' for reading start position\n",
argv[0], argv[arg]);
printHelp(argv[0], false);
}
break;
}
if (lport[0] == lport[1]) {
lport[1] = lport[0] + DEFAULT_DOMAIN_DIFF;
printf("Local port for domain 2 set to %d\n", lport[1]);
}
}
int main(int argc, char* argv[])
{
parseArgs(argc, argv);
myBoard.setVerbose(verbose);
if (file) {
char tmp[500];
int len = 0, c;
while( len<499 && (c=fgetc(file)) != EOF)
tmp[len++] = (char) c;
tmp[len++]=0;
if (!myBoard.setState(tmp)) {
printf("%s: WARNING - Can not parse given position; using start position\n", argv[0]);
myBoard.begin(Board::color1);
}
}
else
myBoard.begin(Board::color1);
if (secsToPlay >= 0) {
msecsToPlay[Board::color1] = 1000 * secsToPlay;
msecsToPlay[Board::color2] = 1000 * secsToPlay;
}
else {
msecsToPlay[Board::color1] = myBoard.msecsToPlay(Board::color1);
msecsToPlay[Board::color2] = myBoard.msecsToPlay(Board::color2);
}
myBoard.setMSecsToPlay(Board::color1, msecsToPlay[Board::color1] );
myBoard.setMSecsToPlay(Board::color2, msecsToPlay[Board::color2] );
/*
* Register domains at NetworkLoop.
*/
d1 = new MyDomain(lport[0]);
l.install(d1);
d2 = new MyDomain(lport[1]);
l.install(d2);
if (host[0]) d1->addConnection(host[0], rport[0]);
if (host[1]) d2->addConnection(host[1], rport[1]);
d1MemberExists = d1->count() >0;
d2MemberExists = d2->count() >0;
gettimeofday(&t1,0);
/* send board to both domains */
d1->sendBoard(&myBoard);
d2->sendBoard(&myBoard);
int state = myBoard.validState();
if (showBoard)
printf("%s%s\n", myBoard.getState(), Board::stateDescription(state));
else
printf("%s - %s\n", myBoard.getShortState(), Board::stateDescription(state));
return l.run();
}