~ubuntu-branches/debian/lenny/ggz-server/lenny

« back to all changes in this revision

Viewing changes to game_servers/ggzcards/message.c

  • Committer: Bazaar Package Importer
  • Author(s): Neil Williams
  • Date: 2008-08-30 22:28:31 UTC
  • mfrom: (3.1.5 intrepid)
  • Revision ID: james.westby@ubuntu.com-20080830222831-lt2l462usl5opxfd
Tags: 0.0.14.1-1.2
* Non-maintainer upload.
* Improve the database.m4 fix to migrate to libdb4.6
  instead of 4.4 {request from Steve Langasek.}

Show diffs side-by-side

added added

removed removed

Lines of Context:
4
4
 * Project: GGZCards Server
5
5
 * Date: 06/20/2001
6
6
 * Desc: Functions and data for messaging system
7
 
 * $Id: message.c 6077 2004-07-11 04:28:48Z jdorje $
 
7
 * $Id: message.c 8456 2006-08-02 06:00:35Z jdorje $
8
8
 *
9
9
 * Right now GGZCards uses a super-generic "messaging" system.  Text
10
10
 * messages are generated by the server and sent to the client for
48
48
#include "common.h"
49
49
#include "message.h"
50
50
#include "net.h"
 
51
#include "score.h"
51
52
 
52
53
#include "message.h"
53
54
 
125
126
        assert(s >= 0 && s < game.num_seats);
126
127
 
127
128
        allplayers_iterate(p) {
128
 
                send_player_message(s, p);
 
129
                /* HACK: We don't want to send messages to disconnected
 
130
                   players. This check skips them: if the player doesn't
 
131
                   have a socket we just skip it. */
 
132
                if (get_player_dio(p)) {
 
133
                        send_player_message(s, p);
 
134
                }
129
135
        } allplayers_iterate_end;
130
136
}
131
137
 
144
150
        broadcast_player_message(game.players[p].seat);
145
151
}
146
152
 
147
 
void set_all_player_messages()
 
153
void set_all_player_messages(void)
148
154
{
149
155
        player_t p;
150
156
        for (p = 0; p < game.num_players; p++)
231
237
void add_player_score_message(player_t p)
232
238
{
233
239
        seat_t s = game.players[p].seat;
234
 
        add_player_message(s, "Score: %d\n", game.players[p].score);
 
240
        add_player_message(s, "Score: %d\n", get_player_score(p));
235
241
}
236
242
 
237
243
void add_player_bid_message(player_t p)
255
261
            && game.state != STATE_NEXT_PLAY)
256
262
                return;
257
263
 
258
 
        if (game.players[p].team >= 0) {
259
 
                int tricks = 0;
260
 
                player_t p2;
261
 
 
262
 
                for (p2 = 0; p2 < game.num_players; p2++)
263
 
                        if (game.players[p2].team == game.players[p].team)
264
 
                                tricks += game.players[p2].tricks;
265
 
 
266
 
                add_player_message(s, "Tricks: %d (%d)\n",
267
 
                                   game.players[p].tricks, tricks);
268
 
        } else {
269
 
                add_player_message(s, "Tricks: %d\n", game.players[p].tricks);
270
 
        }
 
264
#if 0 /* Disabled - this is deemed too confusing. */
 
265
        int tricks = 0;
 
266
        player_t p2;
 
267
 
 
268
        for (p2 = 0; p2 < game.num_players; p2++)
 
269
                if (game.players[p2].team == game.players[p].team)
 
270
                        tricks += game.players[p2].tricks;
 
271
 
 
272
        add_player_message(s, "Tricks: %d (%d)\n",
 
273
                           game.players[p].tricks, tricks);
 
274
#else
 
275
        add_player_message(s, "Tricks: %d\n", game.players[p].tricks);
 
276
#endif
271
277
}
272
278
 
273
279
void add_player_action_message(player_t p)
352
358
        ggz_free(cardlist2);
353
359
}
354
360
 
355
 
static int **cumulative_scores = NULL;
356
 
static int c_score_count = 0;
357
 
static int c_score_size = 0;
358
 
 
359
 
/* Surely there's some library function to do this... */
360
 
static int int_len(int x)
361
 
{
362
 
        int len = x < 0 ? 1 : 0;
363
 
 
364
 
        for (x = abs(x); x > 0; x /= 10)
365
 
                len++;
366
 
 
367
 
        return len;
368
 
}
369
 
 
370
 
/* This function is extra-ordinarily overcomplicated.  I hate string
371
 
   handling... */
372
 
static void send_cumulative_scores(void)
373
 
{
374
 
        char buf[4096];
375
 
        int buf_len = 0, r;
376
 
        int widths[game.num_players];
377
 
 
378
 
        if (!game.cumulative_scores)
379
 
                return;
380
 
 
381
 
        /* First, put up the list of players/teams and calculate the string
382
 
           width of each.  This is a pretty complicated process... */
383
 
        if (game.num_teams == 0) {
384
 
                player_t p;
385
 
                for (p = 0; p < game.num_players; p++) {
386
 
                        widths[p] = strlen(get_player_name(p));
387
 
                        if (widths[p] < 4)
388
 
                                widths[p] = 4;
389
 
                        buf_len +=
390
 
                                snprintf(buf + buf_len, sizeof(buf) - buf_len,
391
 
                                         "%*s%s", widths[p],
392
 
                                         get_player_name(p),
393
 
                                         p ==
394
 
                                         game.num_players - 1 ? "" : " ");
395
 
                }
396
 
        } else {
397
 
                team_t t;
398
 
 
399
 
                memset(widths, 0, sizeof(widths));
400
 
 
401
 
                for (t = 0; t < game.num_teams; t++) {
402
 
                        int num = 0;
403
 
                        player_t p;
404
 
                        char *orig = buf + buf_len;
405
 
 
406
 
                        for (p = 0; p < game.num_players; p++) {
407
 
                                if (game.players[p].team == t) {
408
 
                                        if (num != 0)
409
 
                                                buf_len +=
410
 
                                                        snprintf(buf +
411
 
                                                                 buf_len,
412
 
                                                                 sizeof(buf) -
413
 
                                                                 buf_len,
414
 
                                                                 "/");
415
 
 
416
 
                                        buf_len +=
417
 
                                                snprintf(buf + buf_len,
418
 
                                                         sizeof(buf) -
419
 
                                                         buf_len, "%s",
420
 
                                                         get_player_name(p));
421
 
 
422
 
                                        num++;
423
 
                                }
424
 
                        }
425
 
 
426
 
                        widths[t] = strlen(orig);
427
 
 
428
 
                        if (t != game.num_teams - 1) {
429
 
                                buf_len +=
430
 
                                        snprintf(buf + buf_len,
431
 
                                                 sizeof(buf) - buf_len, " ");
432
 
                        }
433
 
                }
434
 
        }
435
 
        buf_len += snprintf(buf + buf_len, sizeof(buf) - buf_len, "\n");
436
 
 
437
 
        for (r = 0; r < c_score_count; r++) {
438
 
                int max =
439
 
                        game.num_teams ==
440
 
                        0 ? game.num_players : game.num_teams;
441
 
                int x;          /* player or team... */
442
 
 
443
 
                for (x = 0; x < max; x++) {
444
 
                        int score = cumulative_scores[r][x];
445
 
                        int iw = int_len(score);
446
 
                        int extra = widths[x] - iw;
447
 
 
448
 
                        /* this overcomplicated bit of hackery is intended to
449
 
                           center the score under the name. Unfortunately, it
450
 
                           assumes the number isn't longer than 3 characters. */
451
 
                        buf_len +=
452
 
                                snprintf(buf + buf_len, sizeof(buf) - buf_len,
453
 
                                         "%*s%*d%*s%s", extra / 2, "", iw,
454
 
                                         score, (extra + 1) / 2, "",
455
 
                                         (x == max - 1) ? "" : " ");
456
 
                }
457
 
                buf_len +=
458
 
                        snprintf(buf + buf_len, sizeof(buf) - buf_len, "\n");
459
 
        }
460
 
 
461
 
        set_global_message("Scores", "%s", buf);
462
 
}
463
 
 
464
 
void init_cumulative_scores()
465
 
{
466
 
        int i;
467
 
 
468
 
        if (!game.cumulative_scores)
469
 
                return;
470
 
 
471
 
        /* Free the array. */
472
 
        if (cumulative_scores) {
473
 
                for (i = 0; i < c_score_count; i++)
474
 
                        ggz_free(cumulative_scores[i]);
475
 
                ggz_free(cumulative_scores);
476
 
        }
477
 
 
478
 
        cumulative_scores = NULL;
479
 
        c_score_count = c_score_size = 0;
480
 
 
481
 
        send_cumulative_scores();
482
 
}
483
 
 
484
 
/* add on this hand's scores */
485
 
void update_cumulative_scores()
486
 
{
487
 
        int num = game.num_teams > 0 ? game.num_teams : game.num_players;
488
 
        int *score_round;
489
 
 
490
 
        if (!game.cumulative_scores)
491
 
                return;
492
 
        c_score_count++;
493
 
        if (c_score_count > c_score_size) {
494
 
                c_score_size += 10;
495
 
                c_score_size = c_score_size * 3 / 2;
496
 
                cumulative_scores =
497
 
                        ggz_realloc(cumulative_scores,
498
 
                                    c_score_size *
499
 
                                    sizeof(*cumulative_scores));
500
 
        }
501
 
        score_round = ggz_malloc(num * sizeof(**cumulative_scores));
502
 
        cumulative_scores[c_score_count - 1] = score_round;
503
 
 
504
 
        if (game.num_teams == 0) {
505
 
                player_t p;
506
 
                for (p = 0; p < game.num_players; p++)
507
 
                        cumulative_scores[c_score_count - 1][p] =
508
 
                                game.players[p].score;
509
 
        } else {
510
 
                player_t p;
511
 
                for (p = 0; p < game.num_players; p++) {
512
 
                        cumulative_scores[c_score_count -
513
 
                                          1][game.players[p].team] =
514
 
                                game.players[p].score;
515
 
                }
516
 
        }
517
 
 
518
 
        send_cumulative_scores();
519
 
}
520
 
 
521
 
void send_bid_history()
 
361
void send_bid_history(void)
522
362
{
523
363
        int r, buf_len = 0, widths[game.num_players];
524
364
        player_t p;