~ubuntu-branches/ubuntu/saucy/ricochet/saucy-proposed

« back to all changes in this revision

Viewing changes to ricochet-0.1/server-dispatch.5c

  • Committer: Package Import Robot
  • Author(s): Keith Packard
  • Date: 2012-06-11 13:37:57 UTC
  • Revision ID: package-import@ubuntu.com-20120611133757-zn0ukd22vz56ymto
Tags: 0.3
* Improve appearance of board
* Fix user list when removing/adding same user

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * $Id$
3
 
 *
4
 
 * Copyright © 2003 Keith Packard
5
 
 *
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.
15
 
 *
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.
23
 
 */
24
 
 
25
 
autoload RR
26
 
autoload RR::Send
27
 
autoload Server
28
 
autoload Server::Clients
29
 
autoload Server::Readreq
30
 
autoload Server::Show
31
 
autoload Server::Games
32
 
 
33
 
extend namespace Server {
34
 
    public namespace Dispatch {
35
 
 
36
 
        string function upper (string s)
37
 
        {
38
 
            string  r = "";
39
 
            for (int i = 0; i < String::length (s); i++)
40
 
                r = r + String::new (Ctype::toupper (s[i]));
41
 
            return r;
42
 
        }
43
 
 
44
 
        void client_locked (file f, &Client c) {
45
 
            void        respond (string fmt, poly args...) {
46
 
                Clients::client_send (&c, fmt, args...);
47
 
            }
48
 
 
49
 
            exception   closed ();
50
 
 
51
 
            void assert_user ()
52
 
            {
53
 
                if (c.user == User.none)
54
 
                    raise rr_error (Error.NONAMESET);
55
 
            }
56
 
 
57
 
            void assert_game ()
58
 
            {
59
 
                if (c.game == GameRef.none)
60
 
                    raise rr_error (Error.NOTINGAME);
61
 
            }
62
 
 
63
 
            void print_client_score (&Client o) {
64
 
                Clients::print_client_score (&c, &o);
65
 
            }
66
 
 
67
 
            void print_client_games (&Client o) {
68
 
                Clients::print_client_games (&c, &o);
69
 
            }
70
 
 
71
 
            void print_client (&Client o) {
72
 
                Clients::print_client (&c, &o);
73
 
            }
74
 
 
75
 
            typedef struct {
76
 
                string      command;
77
 
                string      usage;
78
 
                string      describe;
79
 
                void(string[])  f;
80
 
            } Cmd;
81
 
 
82
 
            string random_user () {
83
 
                const string    consonant = "bcdfghjklmnpqrstvwxz";
84
 
                const string    vowel = "aeiou";
85
 
                
86
 
                string randc (string a)
87
 
                {
88
 
                    int i = PRNG::randint (String::length (a));
89
 
                    return String::substr (a, i, 1);
90
 
                }
91
 
                string randthree ()
92
 
                {
93
 
                    return (randc (consonant) + 
94
 
                            randc (vowel) + 
95
 
                            randc (consonant));
96
 
                }
97
 
 
98
 
                return randthree () + randthree ();
99
 
            }
100
 
            
101
 
            Cmd[] commands = {
102
 
                {       
103
 
                    command = "HELO", 
104
 
                    usage = "HELO <username>",
105
 
                    describe = 
106
 
                    "Sets the session username.  Must be used before\n"+
107
 
                    "any other command (aside from QUIT)\n",
108
 
                    f = void func (string[] w) {
109
 
                        string username;
110
 
                        if (dim (w) == 0) {
111
 
                            do
112
 
                                username = random_user ();
113
 
                            while (Clients::find (username) != ClientRef.none);
114
 
                        } else
115
 
                            username = w[0];
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",
121
 
                                 server_id,
122
 
                                 username,
123
 
                                 Sockets::addr_to_string (s.addr),
124
 
                                 s.port);
125
 
                        Clients::server_send ("NOTICE USER %s\n", username);
126
 
                        File::fprintf(stderr, "Client connect %s\n", username);
127
 
                    }
128
 
                },
129
 
                {
130
 
                    command = "WHO",
131
 
                    usage = "WHO",
132
 
                    describe =
133
 
                    "List connected users and the number of games they've won.\n",
134
 
                    f = void func (string[] w) {
135
 
                        assert_user ();
136
 
                        respond ("WHO");
137
 
                        Clients::iterate (print_client_games);
138
 
                        respond ("\n");
139
 
                    }
140
 
                },
141
 
                {
142
 
                    command = "GAMES",
143
 
                    usage = "GAMES",
144
 
                    describe =
145
 
                    "List available games.\n",
146
 
                    f = void func (string[] w) {
147
 
                        assert_user ();
148
 
                        respond ("GAMES");
149
 
                        Games::iterate (void func(&Game g) {
150
 
                            respond (" %s", g.name);
151
 
                        });
152
 
                        respond ("\n");
153
 
                    }
154
 
                },
155
 
                {
156
 
                    command = "PLAYERS",
157
 
                    usage = "PLAYERS <game>",
158
 
                    describe =
159
 
                    "List players in the indicated game and their current score.\n",
160
 
                    f = void func (string[] w) {
161
 
                        if (dim (w) == 0)
162
 
                            raise rr_error (Error.SYNTAX);
163
 
                        string game = w[0];
164
 
                        assert_user ();
165
 
                        &Game g = &Games::find (game);
166
 
                        respond ("PLAYERS");
167
 
                        Games::iterate_client (&g, print_client_score, true, false);
168
 
                        respond ("\n");
169
 
                    }
170
 
                },
171
 
                {
172
 
                    command = "WATCHERS",
173
 
                    usage = "WATCHERS <game>",
174
 
                    describe =
175
 
                    "List users watching the indicated game.\n",
176
 
                    f = void func (string[] w) {
177
 
                        if (dim (w) == 0)
178
 
                            raise rr_error (Error.SYNTAX);
179
 
                        string game = w[0];
180
 
                        assert_user ();
181
 
                        &Game   g = &Games::find (game);
182
 
                        respond ("WATCHERS");
183
 
                        Games::iterate_client (&g, print_client, false, true);
184
 
                        respond ("\n");
185
 
                    }
186
 
                },
187
 
                {
188
 
                    command = "GAMEINFO",
189
 
                    usage = "GAMEINFO <game>",
190
 
                    describe =
191
 
                    "GAMEINFO <turn> <color> <shape> <state> <time> <bid> <active>\n"+
192
 
                    "\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) {
208
 
                        if (dim (w) == 0)
209
 
                            raise rr_error (Error.SYNTAX);
210
 
                        string game = w[0];
211
 
                        assert_user ();
212
 
                        &Game   g = &Games::find (game);
213
 
 
214
 
                        int         turn = 17 - dim (g.targets);
215
 
 
216
 
                        int         remain = 0;
217
 
                        if (g.state == GameState.BID)
218
 
                        {
219
 
                            remain = g.expire_time - time();
220
 
                            if (remain < 0) remain = 0;
221
 
                        }
222
 
 
223
 
                        int         bid = 0;
224
 
                        union switch (Games::lowest_bidder (&g)) {
225
 
                        case none: break;
226
 
                        case client c: bid = c.bid.bid.number; break;
227
 
                        }
228
 
 
229
 
                        string      active = "";
230
 
                        if (g.state == GameState.SHOW)
231
 
                            active = g.active.client.user.username;
232
 
 
233
 
                        respond ("GAMEINFO %d %C %S %G %d %d %s\n",
234
 
                                 turn, 
235
 
                                 g.target.color,
236
 
                                 g.target.shape,
237
 
                                 g.state,
238
 
                                 remain,
239
 
                                 bid,
240
 
                                 active);
241
 
                    }
242
 
                },
243
 
                {
244
 
                    command = "USERINFO",
245
 
                    usage = "USERINFO <username>",
246
 
                    describe =
247
 
                    "USERINFO <game> <playing> <games> <score> <bid>\n"+
248
 
                    "\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"+
251
 
                    "false 0 0.\n"+
252
 
                    "\n"+
253
 
                    "<playing> is true if the user is playing and false if watching.\n"+
254
 
                    "\n"+
255
 
                    "<games> is the number of games won by this user\n"+
256
 
                    "\n"+
257
 
                    "<score> is a number from 0 to 17 indicating this players score\n"+
258
 
                    "\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) {
262
 
                        if (dim (w) == 0)
263
 
                            raise rr_error (Error.SYNTAX);
264
 
                        string username = w[0];
265
 
                        assert_user ();
266
 
                        ClientRef       cr = Clients::find (username);
267
 
                        union switch (cr) {
268
 
                        case none:
269
 
                            raise rr_error (Error.NOUSER);
270
 
                        case client c:
271
 
                            string  game = "";
272
 
                            bool    playing = false;
273
 
                            int     games = c.games;
274
 
                            int     score = 0;
275
 
                            int     bid = 0;
276
 
 
277
 
                            union switch (c.game) {
278
 
                            case none:  break;
279
 
                            case game g:
280
 
                                game = g.name;
281
 
                                playing = c.playing;
282
 
                                if (playing)
283
 
                                {
284
 
                                    score = c.score;
285
 
                                    if (c.bid != Bid.none)
286
 
                                        bid = c.bid.bid.number;
287
 
                                }
288
 
                            }
289
 
                            respond ("USERINFO %s %b %d %d %d\n",
290
 
                                     game, playing, games, score, bid);
291
 
                        }
292
 
                    }
293
 
                },
294
 
                {
295
 
                    command = "NEW",
296
 
                    usage = "NEW <game-suggestion>",
297
 
                    describe =
298
 
                    "Create new game.  <game-suggestion> will be used to construct\n"+
299
 
                    "the game name.\n",
300
 
                    f = void func (string[] w) {
301
 
                        if (dim (w) == 0)
302
 
                            raise rr_error (Error.SYNTAX);
303
 
                        string game=w[0];
304
 
                        assert_user ();
305
 
                        &Game g = &Games::new (game);
306
 
                        Games::add_client (&g, &c, true);
307
 
                        respond ("NEW %s\n", g.name);
308
 
                    }
309
 
                },
310
 
                {
311
 
                    command = "JOIN",
312
 
                    usage = "JOIN <game>",
313
 
                    describe =
314
 
                    "Join an existing game.\n",
315
 
                    f = void func (string[] w) {
316
 
                        if (dim (w) == 0)
317
 
                            raise rr_error (Error.SYNTAX);
318
 
                        string game=w[0];
319
 
                        assert_user ();
320
 
                        &Game g = &Games::find (game);
321
 
                        Games::add_client (&g, &c, true);
322
 
                        respond ("JOIN\n");
323
 
                    }
324
 
                },
325
 
                {
326
 
                    command = "WATCH",
327
 
                    usage = "WATCH <game>",
328
 
                    describe =
329
 
                    "Watch an existing game.\n",
330
 
                    f = void func (string[] w) {
331
 
                        if (dim (w) == 0)
332
 
                            raise rr_error (Error.SYNTAX);
333
 
                        string game=w[0];
334
 
                        assert_user ();
335
 
                        &Game g = &Games::find (game);
336
 
                        Games::add_client (&g, &c, false);
337
 
                        respond ("WATCH\n");
338
 
                    }
339
 
                },
340
 
                {
341
 
                    command = "DISPOSE",
342
 
                    usage = "DISPOSE <game>",
343
 
                    describe =
344
 
                    "Dispose an empty game.\n",
345
 
                    f = void func (string[] w) {
346
 
                        if (dim (w) == 0)
347
 
                            raise rr_error (Error.SYNTAX);
348
 
                        string game=w[0];
349
 
                        assert_user ();
350
 
                        &Game g = &Games::find (game);
351
 
                        Games::dispose (&g);
352
 
                        respond ("DISPOSE\n");
353
 
                    }
354
 
                },
355
 
                {
356
 
                    command = "SHOW",
357
 
                    usage = "SHOW",
358
 
                    describe =
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"+
362
 
                    "\n"+
363
 
                    " HHH \n"+
364
 
                    "VrcsV\n"+
365
 
                    " HHH\n"+
366
 
                    "\n"+
367
 
                    "H = ' ' or '='\n"+
368
 
                    "V = ' ' or '|'\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"+
372
 
                    "\n"+
373
 
                    "The goal robot and target (color and shape) are indicated with\n"+
374
 
                    "capital letters.\n"+
375
 
                    "\n"+
376
 
                    "For example:\n"+
377
 
                    "\n"+
378
 
                    "SHOW \"\n"+
379
 
                    " ===\n"+
380
 
                    "|R.. ... .gs\n"+
381
 
                    "\n"+
382
 
                    " byc|... .RT|\n"+
383
 
                    " ===    === \"\n"+
384
 
                    "\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) {
390
 
                        assert_user ();
391
 
                        assert_game ();
392
 
                        File::fprintf (f, "SHOW \"\n");
393
 
                        Show::show (f, &c.game.game.board);
394
 
                        File::fprintf (f, "\"\n");
395
 
                    }
396
 
                },
397
 
                {
398
 
                    command = "BID",
399
 
                    usage = "BID <number>",
400
 
                    describe =
401
 
                    "Make a bid on the current turn.\n",
402
 
                    f = void func (string[] w) {
403
 
                        if (dim (w) == 0)
404
 
                            raise rr_error (Error.SYNTAX);
405
 
                        int number = string_to_integer (w[0]);
406
 
                        assert_user ();
407
 
                        assert_game ();
408
 
                        Games::bid (&c.game.game, &c, number);
409
 
                        respond ("BID\n");
410
 
                    }
411
 
                },
412
 
                {
413
 
                    command = "REVOKE",
414
 
                    usage = "REVOKE",
415
 
                    describe =
416
 
                    "Revoke the current bid.  This makes it appear as if the user has\n"+
417
 
                    "never bid.\n",
418
 
                    f = void func (string[] w) {
419
 
                        assert_user ();
420
 
                        assert_game ();
421
 
                        Games::revoke (&c.game.game, &c);
422
 
                        respond ("REVOKE\n");
423
 
                    }
424
 
                },
425
 
                {
426
 
                    command = "ABANDON",
427
 
                    usage = "ABANDON",
428
 
                    describe =
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) {
432
 
                        assert_user ();
433
 
                        assert_game ();
434
 
                        Games::abandon (&c.game.game, &c);
435
 
                        respond ("ABANDON\n");
436
 
                    }
437
 
                },
438
 
                {
439
 
                    command = "NOBID",
440
 
                    usage = "NOBID",
441
 
                    describe =
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) {
445
 
                        assert_user ();
446
 
                        assert_game ();
447
 
                        Games::nobid (&c.game.game, &c);
448
 
                        respond ("NOBID\n");
449
 
                    }
450
 
                },
451
 
                {
452
 
                    command = "MOVE",
453
 
                    usage = "MOVE <color> <dir1> <dir2> ...",
454
 
                    describe =
455
 
                    "Move the robot indicated by <color> in the indicated\n"+
456
 
                    "sequence of directions.\n",
457
 
                    f = void func (string[] w) {
458
 
                        if (dim (w) == 0)
459
 
                            raise rr_error (Error.SYNTAX);
460
 
                        Color col = color(w[0]);
461
 
                        Direction[dim(w)-1] directions = { 
462
 
                            [i] = direction(w[i+1]) 
463
 
                        };
464
 
                        assert_user ();
465
 
                        assert_game ();
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));
469
 
                    }
470
 
                },
471
 
                {
472
 
                    command = "UNDO",
473
 
                    usage = "UNDO",
474
 
                    describe =
475
 
                    "Undo the last move.\n",
476
 
                    f = void func (string[] w) {
477
 
                        assert_user ();
478
 
                        assert_game ();
479
 
                        Games::undo (&c.game.game, &c);
480
 
                        respond ("UNDO\n");
481
 
                    }
482
 
                },
483
 
                {
484
 
                    command = "RESET",
485
 
                    usage = "RESET",
486
 
                    describe =
487
 
                    "Reset all robots back to their positions at the\n"+
488
 
                    "start of the turn.\n",
489
 
                    f = void func (string[] w) {
490
 
                        assert_user ();
491
 
                        assert_game ();
492
 
                        Games::reset (&c.game.game, &c);
493
 
                        respond ("RESET\n");
494
 
                    }
495
 
                },
496
 
                {
497
 
                    command = "TURN",
498
 
                    usage = "TURN",
499
 
                    describe =
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"+
503
 
                    "\n"+
504
 
                    "TURN succeeds if one of the following is true:\n"+
505
 
                    "\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) {
510
 
                        assert_user ();
511
 
                        assert_game ();
512
 
                        Games::turn (&c.game.game, &c);
513
 
                        respond ("TURN\n");
514
 
                    }
515
 
                },
516
 
                {
517
 
                    command = "PASS",
518
 
                    usage = "PASS",
519
 
                    describe =
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) {
524
 
                        assert_user ();
525
 
                        assert_game ();
526
 
                        Games::pass (&c.game.game, &c);
527
 
                        respond ("PASS\n");
528
 
                    }
529
 
                },
530
 
                {
531
 
                    command = "MESSAGE",
532
 
                    usage = "MESSAGE <text>",
533
 
                    describe =
534
 
                    "Broadcast <text> to all users.\n",
535
 
                    f = void func (string[] w) {
536
 
                        if (dim (w) == 0)
537
 
                            raise rr_error (Error.SYNTAX);
538
 
                        assert_user ();
539
 
                        respond ("MESSAGE\n");
540
 
                        string text = w[0];
541
 
                        for (int i = 1; i < dim(w); i++)
542
 
                            text = text + " " + w[i];
543
 
                        string u = (c.user == User.none ? "anonymous" :
544
 
                                    c.user.username);
545
 
                        Clients::server_send ("NOTICE MESSAGE %s %s\n", u, text);
546
 
                    }
547
 
                },
548
 
                {
549
 
                    command = "PART",
550
 
                    usage = "PART",
551
 
                    describe =
552
 
                    "Depart from the current game, but remain connected\n"+
553
 
                    "to the server.\n",
554
 
                    f = void func (string[] w) {
555
 
                        assert_user ();
556
 
                        assert_game ();
557
 
                        Games::remove_client (&c);
558
 
                        respond ("PART\n");
559
 
                    }
560
 
                },
561
 
                {
562
 
                    command = "QUIT",
563
 
                    usage = "QUIT",
564
 
                    describe =
565
 
                    "Disconnect from the server.\n",
566
 
                    f = void func (string[] w) {
567
 
                        respond ("QUIT\n");
568
 
                        raise closed();
569
 
                    }
570
 
                },
571
 
                {
572
 
                    command = "HELP",
573
 
                    usage = "HELP <command>",
574
 
                    describe =
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)
579
 
                        {
580
 
                            File::fprintf (f, "%-12.12s %s\n", cmd.command, cmd.usage);
581
 
                            if (describe)
582
 
                            {
583
 
                                File::fprintf (f, "\n%s\n", cmd.describe);
584
 
                            }
585
 
                        }
586
 
                        if (dim(w) > 0)
587
 
                        {
588
 
                            for (int j = 0; j < dim(w); j++)
589
 
                            {
590
 
                                string command = upper (w[j]);
591
 
                                int i;
592
 
                                for (i = 0; i < dim(commands); i++)
593
 
                                    if (command == commands[i].command)
594
 
                                    {
595
 
                                        print_help (&commands[i], true);
596
 
                                        break;
597
 
                                    }
598
 
                                if (i == dim(commands))
599
 
                                    raise rr_error (Error.COMMAND);
600
 
                            }
601
 
                        }
602
 
                        else
603
 
                        {
604
 
                            for (int i = 0; i < dim(commands); i++)
605
 
                                print_help (&commands[i], false);
606
 
                        }
607
 
                        respond ("HELP %s\n", File::string_string(f));
608
 
                    }
609
 
                },
610
 
                {
611
 
                    command = "VERSION",
612
 
                    usage = "VERSION <client-version>",
613
 
                    describe =
614
 
                    "Negotiate protocol version.\n",
615
 
                    f = void func (string[] w) {
616
 
                        if (dim (w) == 0)
617
 
                            raise rr_error (Error.SYNTAX);
618
 
                        int version = min (string_to_integer (w[0],
619
 
                                                              server_version));
620
 
                        c.version = version;
621
 
                        respond ("VERSION %d\n", version);
622
 
                    }
623
 
                }
624
 
            };
625
 
 
626
 
            File::fprintf (stderr, "New client\n");
627
 
 
628
 
            for (;;) {
629
 
                try {
630
 
                    File::flush (f);
631
 
                    string[]  words = Readreq::read (f);
632
 
                    
633
 
                    string  command = upper (words[0]);
634
 
                    string[dim(words)-1] args = { [i] = words[i+1] };
635
 
                    
636
 
                    if (words[0] == "") return;
637
 
                    int i;
638
 
                    for (i = 0; i < dim(commands); i++)
639
 
                        if (command == commands[i].command)
640
 
                        {
641
 
                            void f(string[*]);
642
 
                            try {
643
 
                                f = commands[i].f;
644
 
                            } catch invalid_array_bounds (string s,
645
 
                                                          poly a, poly i) {
646
 
                                raise rr_error (Error.SYNTAX);
647
 
                            }
648
 
                            f (args);
649
 
                            break;
650
 
                        }
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,
657
 
                                        file f) {
658
 
                    File::fprintf (stderr, "%v: %s\n",
659
 
                                   c.user, msg);
660
 
                    return;
661
 
                } catch File::io_eof (file f) {
662
 
                    return;
663
 
                } catch closed () {
664
 
                    return;
665
 
                }
666
 
            }
667
 
        }
668
 
 
669
 
        void client_cleanup (&Client c) {
670
 
            Games::remove_client (&c);
671
 
            Clients::dispose (&c);
672
 
        }
673
 
        
674
 
        public void client (file f)
675
 
        {
676
 
            twixt (lock (); unlock ())
677
 
                twixt ((&Client c = &Clients::new (f)), true;
678
 
                       client_cleanup (&c))
679
 
                    client_locked (f, &c);
680
 
        }
681
 
    }
682
 
}