4
* Copyright © 2003 Keith Packard
6
* Permission to use, copy, modify, distribute, and sell this software and its
7
* documentation for any purpose is hereby granted without fee, provided that
8
* the above copyright notice appear in all copies and that both that
9
* copyright notice and this permission notice appear in supporting
10
* documentation, and that the name of Keith Packard not be used in
11
* advertising or publicity pertaining to distribution of the software without
12
* specific, written prior permission. Keith Packard makes no
13
* representations about the suitability of this software for any purpose. It
14
* is provided "as is" without express or implied warranty.
16
* KEITH PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
17
* INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
18
* EVENT SHALL KEITH PACKARD BE LIABLE FOR ANY SPECIAL, INDIRECT OR
19
* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
20
* DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
21
* TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
22
* PERFORMANCE OF THIS SOFTWARE.
28
autoload Server::Clients
29
autoload Server::Readreq
31
autoload Server::Games
33
extend namespace Server {
34
public namespace Dispatch {
36
string function upper (string s)
39
for (int i = 0; i < String::length (s); i++)
40
r = r + String::new (Ctype::toupper (s[i]));
44
void client_locked (file f, &Client c) {
45
void respond (string fmt, poly args...) {
46
Clients::client_send (&c, fmt, args...);
53
if (c.user == User.none)
54
raise rr_error (Error.NONAMESET);
59
if (c.game == GameRef.none)
60
raise rr_error (Error.NOTINGAME);
63
void print_client_score (&Client o) {
64
Clients::print_client_score (&c, &o);
67
void print_client_games (&Client o) {
68
Clients::print_client_games (&c, &o);
71
void print_client (&Client o) {
72
Clients::print_client (&c, &o);
82
string random_user () {
83
const string consonant = "bcdfghjklmnpqrstvwxz";
84
const string vowel = "aeiou";
86
string randc (string a)
88
int i = PRNG::randint (String::length (a));
89
return String::substr (a, i, 1);
93
return (randc (consonant) +
98
return randthree () + randthree ();
104
usage = "HELO <username>",
106
"Sets the session username. Must be used before\n"+
107
"any other command (aside from QUIT)\n",
108
f = void func (string[] w) {
112
username = random_user ();
113
while (Clients::find (username) != ClientRef.none);
116
if (Clients::find (username) != ClientRef.none)
117
raise rr_error (Error.INVALIDNAME);
118
c.user.username = username;
119
Sockets::sockaddr s = Sockets::getsockname (c.f);
120
respond ("HELO %s %s %s %d\n",
123
Sockets::addr_to_string (s.addr),
125
Clients::server_send ("NOTICE USER %s\n", username);
126
File::fprintf(stderr, "Client connect %s\n", username);
133
"List connected users and the number of games they've won.\n",
134
f = void func (string[] w) {
137
Clients::iterate (print_client_games);
145
"List available games.\n",
146
f = void func (string[] w) {
149
Games::iterate (void func(&Game g) {
150
respond (" %s", g.name);
157
usage = "PLAYERS <game>",
159
"List players in the indicated game and their current score.\n",
160
f = void func (string[] w) {
162
raise rr_error (Error.SYNTAX);
165
&Game g = &Games::find (game);
167
Games::iterate_client (&g, print_client_score, true, false);
172
command = "WATCHERS",
173
usage = "WATCHERS <game>",
175
"List users watching the indicated game.\n",
176
f = void func (string[] w) {
178
raise rr_error (Error.SYNTAX);
181
&Game g = &Games::find (game);
182
respond ("WATCHERS");
183
Games::iterate_client (&g, print_client, false, true);
188
command = "GAMEINFO",
189
usage = "GAMEINFO <game>",
191
"GAMEINFO <turn> <color> <shape> <state> <time> <bid> <active>\n"+
193
"<turn> is a number from 1 to 17 indicating the current turn\n"+
194
"<color> <shape> indicate the active piece\n"+
195
"<state> is one of:\n"+
196
" new Turn just started, no bids yet\n"+
197
" bid Bidding opened. <time> indicates time remaining,\n"+
198
" <bid> indicates the minimum bid\n"+
199
" show Bidding closed and solution being demonstrated\n"+
200
" <active> indicates the person demonstrating\n"+
201
" solved Solution succesfully demonstrated that\n"+
202
" is less than the active users bid. <active>\n"+
203
" indicates the winner.\n"+
204
"<time> is valid only in BID state, else it's 0\n"+
205
"<bid> is valid in all but NEW state where it's 0\n"+
206
"<active> is valid in SHOW and SOLVED states, else it's \"\".\n",
207
f = void func (string[] w) {
209
raise rr_error (Error.SYNTAX);
212
&Game g = &Games::find (game);
214
int turn = 17 - dim (g.targets);
217
if (g.state == GameState.BID)
219
remain = g.expire_time - time();
220
if (remain < 0) remain = 0;
224
union switch (Games::lowest_bidder (&g)) {
226
case client c: bid = c.bid.bid.number; break;
230
if (g.state == GameState.SHOW)
231
active = g.active.client.user.username;
233
respond ("GAMEINFO %d %C %S %G %d %d %s\n",
244
command = "USERINFO",
245
usage = "USERINFO <username>",
247
"USERINFO <game> <playing> <games> <score> <bid>\n"+
249
"<game> is any currently associated game, else \"\". If the user\n"+
250
"is not associated with any game, the remaining fields are\n"+
253
"<playing> is true if the user is playing and false if watching.\n"+
255
"<games> is the number of games won by this user\n"+
257
"<score> is a number from 0 to 17 indicating this players score\n"+
259
"<bid> is either \"0\" indicating no bid or a number indicating\n"+
260
"the users minimum bid.\n",
261
f = void func (string[] w) {
263
raise rr_error (Error.SYNTAX);
264
string username = w[0];
266
ClientRef cr = Clients::find (username);
269
raise rr_error (Error.NOUSER);
272
bool playing = false;
277
union switch (c.game) {
285
if (c.bid != Bid.none)
286
bid = c.bid.bid.number;
289
respond ("USERINFO %s %b %d %d %d\n",
290
game, playing, games, score, bid);
296
usage = "NEW <game-suggestion>",
298
"Create new game. <game-suggestion> will be used to construct\n"+
300
f = void func (string[] w) {
302
raise rr_error (Error.SYNTAX);
305
&Game g = &Games::new (game);
306
Games::add_client (&g, &c, true);
307
respond ("NEW %s\n", g.name);
312
usage = "JOIN <game>",
314
"Join an existing game.\n",
315
f = void func (string[] w) {
317
raise rr_error (Error.SYNTAX);
320
&Game g = &Games::find (game);
321
Games::add_client (&g, &c, true);
327
usage = "WATCH <game>",
329
"Watch an existing game.\n",
330
f = void func (string[] w) {
332
raise rr_error (Error.SYNTAX);
335
&Game g = &Games::find (game);
336
Games::add_client (&g, &c, false);
342
usage = "DISPOSE <game>",
344
"Dispose an empty game.\n",
345
f = void func (string[] w) {
347
raise rr_error (Error.SYNTAX);
350
&Game g = &Games::find (game);
352
respond ("DISPOSE\n");
359
"<game-board> is a quoted multi-line string containing an\n"+
360
"diagram of the game contents, (an array of cells). A single\n"+
361
"cell and its surrounding are indicated as:\n"+
369
"r = '.' or <robot-color> (one of 'r', 'g', 'b', or 'y')\n"+
370
"c = '.' or <target-color> (one of 'r', 'g', 'b', or 'y')\n"+
371
"s = '.' or <target-shape> (one of 'c', 's', 'o', or 't')\n"+
373
"The goal robot and target (color and shape) are indicated with\n"+
374
"capital letters.\n"+
385
"R.. = Red robot (goal robot)\n"+
386
".gs = Green square target\n"+
387
"byc = Blue robot on yellow circle target\n"+
388
".RT = Red triangle (goal target)\n",
389
f = void func (string[] w) {
392
File::fprintf (f, "SHOW \"\n");
393
Show::show (f, &c.game.game.board);
394
File::fprintf (f, "\"\n");
399
usage = "BID <number>",
401
"Make a bid on the current turn.\n",
402
f = void func (string[] w) {
404
raise rr_error (Error.SYNTAX);
405
int number = string_to_integer (w[0]);
408
Games::bid (&c.game.game, &c, number);
416
"Revoke the current bid. This makes it appear as if the user has\n"+
418
f = void func (string[] w) {
421
Games::revoke (&c.game.game, &c);
422
respond ("REVOKE\n");
429
"Give up trying to solve the current turn. When all users\n"+
430
"ABANDON, TURN can be used to move to the next turn.\n",
431
f = void func (string[] w) {
434
Games::abandon (&c.game.game, &c);
435
respond ("ABANDON\n");
442
"Give up trying to improve bids in the current turn.\n"+
443
"When all users NOBID, game moves to SHOW state.\n",
444
f = void func (string[] w) {
447
Games::nobid (&c.game.game, &c);
453
usage = "MOVE <color> <dir1> <dir2> ...",
455
"Move the robot indicated by <color> in the indicated\n"+
456
"sequence of directions.\n",
457
f = void func (string[] w) {
459
raise rr_error (Error.SYNTAX);
460
Color col = color(w[0]);
461
Direction[dim(w)-1] directions = {
462
[i] = direction(w[i+1])
466
for (int i = 0; i < dim(directions); i++)
467
Games::move (&c.game.game, &c, col, directions[i]);
468
respond ("MOVE %d\n", Games::count (&c.game.game));
475
"Undo the last move.\n",
476
f = void func (string[] w) {
479
Games::undo (&c.game.game, &c);
487
"Reset all robots back to their positions at the\n"+
488
"start of the turn.\n",
489
f = void func (string[] w) {
492
Games::reset (&c.game.game, &c);
500
"Advance the game to the next turn, (which will have a new\n"+
501
"target square). The robots will start in their final positions\n"+
502
"at the end of the first successfully demonstrated solution.\n"+
504
"TURN succeeds if one of the following is true:\n"+
506
" + a solution has been demonstrated\n"+
507
" + all players have passed\n"+
508
" + all players have abandoned the turn\n",
509
f = void func (string[] w) {
512
Games::turn (&c.game.game, &c);
520
"Give up attempting to demonstrate a solution. Control passes\n"+
521
"to the next lowest bidder. When all users have passed, TURN\n"+
522
"can be used to move to the next turn.\n",
523
f = void func (string[] w) {
526
Games::pass (&c.game.game, &c);
532
usage = "MESSAGE <text>",
534
"Broadcast <text> to all users.\n",
535
f = void func (string[] w) {
537
raise rr_error (Error.SYNTAX);
539
respond ("MESSAGE\n");
541
for (int i = 1; i < dim(w); i++)
542
text = text + " " + w[i];
543
string u = (c.user == User.none ? "anonymous" :
545
Clients::server_send ("NOTICE MESSAGE %s %s\n", u, text);
552
"Depart from the current game, but remain connected\n"+
554
f = void func (string[] w) {
557
Games::remove_client (&c);
565
"Disconnect from the server.\n",
566
f = void func (string[] w) {
573
usage = "HELP <command>",
575
"Provide help for <command>.\n",
576
f = void func (string[] w) {
577
file f = File::string_write ();
578
void print_help (&Cmd cmd, bool describe)
580
File::fprintf (f, "%-12.12s %s\n", cmd.command, cmd.usage);
583
File::fprintf (f, "\n%s\n", cmd.describe);
588
for (int j = 0; j < dim(w); j++)
590
string command = upper (w[j]);
592
for (i = 0; i < dim(commands); i++)
593
if (command == commands[i].command)
595
print_help (&commands[i], true);
598
if (i == dim(commands))
599
raise rr_error (Error.COMMAND);
604
for (int i = 0; i < dim(commands); i++)
605
print_help (&commands[i], false);
607
respond ("HELP %s\n", File::string_string(f));
612
usage = "VERSION <client-version>",
614
"Negotiate protocol version.\n",
615
f = void func (string[] w) {
617
raise rr_error (Error.SYNTAX);
618
int version = min (string_to_integer (w[0],
621
respond ("VERSION %d\n", version);
626
File::fprintf (stderr, "New client\n");
631
string[] words = Readreq::read (f);
633
string command = upper (words[0]);
634
string[dim(words)-1] args = { [i] = words[i+1] };
636
if (words[0] == "") return;
638
for (i = 0; i < dim(commands); i++)
639
if (command == commands[i].command)
644
} catch invalid_array_bounds (string s,
646
raise rr_error (Error.SYNTAX);
651
if (i == dim(commands))
652
raise rr_error (Error.COMMAND);
653
} catch rr_error (Error e) {
654
respond ("ERROR %E\n", e);
655
} catch File::io_error (string msg,
656
File::error_type error,
658
File::fprintf (stderr, "%v: %s\n",
661
} catch File::io_eof (file f) {
669
void client_cleanup (&Client c) {
670
Games::remove_client (&c);
671
Clients::dispose (&c);
674
public void client (file f)
676
twixt (lock (); unlock ())
677
twixt ((&Client c = &Clients::new (f)), true;
679
client_locked (f, &c);