~ubuntu-branches/ubuntu/edgy/ggz-client-libs/edgy

« back to all changes in this revision

Viewing changes to ggzcore/table.c

  • Committer: Bazaar Package Importer
  • Author(s): Peter Eisentraut, Josef Spillner, Peter Eisentraut
  • Date: 2006-09-09 13:37:14 UTC
  • mfrom: (2.1.2 edgy)
  • Revision ID: james.westby@ubuntu.com-20060909133714-q49a9kvjfkc0wcc3
Tags: 0.0.13-3
[ Josef Spillner ]
* Change ggzcore-bin dependency from ggzmod to recommends from ggzcore
  (closes: #384671).

[ Peter Eisentraut ]
* Make package dependencies binNMU-safe through use of ${binary:Version}
  (closes: #386126)

Show diffs side-by-side

added added

removed removed

Lines of Context:
3
3
 * Author: Justin Zaun
4
4
 * Project: GGZ Core Client Lib
5
5
 * Date: 6/5/00
6
 
 * $Id: table.c,v 1.28 2002/11/03 19:37:36 jdorje Exp $
 
6
 * $Id: table.c 7889 2006-03-07 09:57:32Z josef $
7
7
 *
8
8
 * This fils contains functions for handling tables
9
9
 *
10
10
 * Copyright (C) 1998 Brent Hendricks.
11
11
 *
12
 
 * This program is free software; you can redistribute it and/or modify
13
 
 * it under the terms of the GNU General Public License as published by
14
 
 * the Free Software Foundation; either version 2 of the License, or
15
 
 * (at your option) any later version.
16
 
 *
17
 
 * This program is distributed in the hope that it will be useful,
 
12
 * This library is free software; you can redistribute it and/or
 
13
 * modify it under the terms of the GNU Lesser General Public
 
14
 * License as published by the Free Software Foundation; either
 
15
 * version 2.1 of the License, or (at your option) any later version.
 
16
 * 
 
17
 * This library is distributed in the hope that it will be useful,
18
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
 
 * GNU General Public License for more details.
21
 
 *
22
 
 * You should have received a copy of the GNU General Public License
23
 
 * along with this program; if not, write to the Free Software
24
 
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
 
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
20
 * Lesser General Public License for more details.
 
21
 * 
 
22
 * You should have received a copy of the GNU Lesser General Public
 
23
 * License along with this library; if not, write to the Free Software
 
24
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
25
25
 */
26
26
 
27
27
#ifdef HAVE_CONFIG_H
46
46
 */
47
47
 
48
48
 
49
 
/* Publicly exported functions */
 
49
/* Table Information */
 
50
struct _GGZTable {
 
51
 
 
52
        /* Pointer to room this table resides in */
 
53
        GGZRoom *room;
 
54
 
 
55
        /* Server ID of table */
 
56
        int id;
 
57
 
 
58
        /* Game Type */
 
59
        const GGZGameType *gametype;
 
60
 
 
61
        /* Table description */
 
62
        const char * desc;        
 
63
 
 
64
        /* Table state */
 
65
        GGZTableState state;
 
66
 
 
67
        /* Total seats */
 
68
        unsigned int num_seats;
 
69
 
 
70
        /* Seats */
 
71
        GGZTableSeat *seats;
 
72
 
 
73
        /* Total spectator seats */
 
74
        unsigned int num_spectator_seats;
 
75
 
 
76
        /* Spectator seats - "type" is unused; player name is
 
77
           NULL for empty seat. */
 
78
        GGZTableSeat *spectator_seats;
 
79
};
 
80
 
 
81
 
 
82
/*
 
83
 * Private functions - either static or declared in table.h
 
84
 */
 
85
 
 
86
static const GGZRoom *_ggzcore_table_get_room(const GGZTable *table)
 
87
{
 
88
        return table->room;
 
89
}
 
90
 
 
91
 
 
92
static int _ggzcore_table_get_id(const GGZTable *table)
 
93
{
 
94
        return table->id;
 
95
}
 
96
 
 
97
 
 
98
static const GGZGameType* _ggzcore_table_get_type(const GGZTable *table)
 
99
{
 
100
        return table->gametype;
 
101
}
 
102
 
 
103
 
 
104
static const char * _ggzcore_table_get_desc(const GGZTable *table)
 
105
{
 
106
        return table->desc;
 
107
}
 
108
 
 
109
 
 
110
static GGZTableState _ggzcore_table_get_state(const GGZTable *table)
 
111
{
 
112
        return table->state;
 
113
}
 
114
 
 
115
 
 
116
static int _ggzcore_table_get_num_seats(const GGZTable *table)
 
117
{
 
118
        return table->num_seats;
 
119
}
 
120
 
 
121
 
 
122
static int _ggzcore_table_get_seat_count(const GGZTable *table,
 
123
                                         GGZSeatType type)
 
124
{
 
125
        int i, count = 0;
 
126
 
 
127
        for (i = 0; i < table->num_seats; i++)
 
128
                if (table->seats[i].type == type)
 
129
                        count++;
 
130
        
 
131
        return count;
 
132
}
 
133
 
 
134
 
 
135
GGZTableSeat _ggzcore_table_get_nth_seat(const GGZTable *table,
 
136
                                          unsigned int num)
 
137
{
 
138
        return table->seats[num];
 
139
}
 
140
 
 
141
 
 
142
static const char *_ggzcore_table_get_nth_player_name(const GGZTable *table,
 
143
                                                      unsigned int num)
 
144
{
 
145
        return table->seats[num].name;
 
146
}
 
147
 
 
148
 
 
149
static GGZSeatType _ggzcore_table_get_nth_player_type(const GGZTable *table,
 
150
                                                      unsigned int num)
 
151
{
 
152
        return table->seats[num].type;
 
153
}
 
154
 
 
155
 
 
156
static int _ggzcore_table_get_num_spectator_seats(const GGZTable *table)
 
157
{
 
158
        return table->num_spectator_seats;
 
159
}
 
160
 
 
161
 
 
162
GGZTableSeat _ggzcore_table_get_nth_spectator_seat(const GGZTable *table,
 
163
                                                    unsigned int num)
 
164
{
 
165
        return table->spectator_seats[num];
 
166
}
 
167
 
 
168
 
 
169
static const char *_ggzcore_table_get_nth_spectator_name(const GGZTable *table,
 
170
                                                         unsigned int num)
 
171
{
 
172
        return table->spectator_seats[num].name;
 
173
}
 
174
 
 
175
 
 
176
/*
 
177
 * Publicly exported functions
 
178
 */
50
179
 
51
180
GGZTable* ggzcore_table_new(void)
52
181
{
55
184
 
56
185
 
57
186
int ggzcore_table_init(GGZTable *table,
58
 
                       GGZGameType *gametype,
 
187
                       const GGZGameType *gametype,
59
188
                       const char *desc,
60
189
                       const unsigned int num_seats)
61
190
{
 
191
        /* A NULL desc is allowed. */
62
192
        if (table && gametype) {
63
193
                _ggzcore_table_init(table, gametype, desc,
64
194
                                    num_seats,
65
195
                                    GGZ_TABLE_CREATED, -1);
66
196
                return 0;
67
 
        }
68
 
        else
 
197
        } else
69
198
                return -1;
70
199
}
71
200
 
82
211
                           GGZSeatType type,
83
212
                           const char *name)
84
213
{
85
 
        GGZRoom *room;
86
214
        GGZServer *server;
87
215
        GGZNet *net;
88
 
        struct _GGZSeat seat = {index, type, name};
 
216
        GGZTableSeat seat = {index, type, name};
89
217
 
90
218
        ggz_debug(GGZCORE_DBG_TABLE, "User changing seats... on %p", table);
91
219
 
109
237
                _ggzcore_table_set_seat(table, &seat);
110
238
        else {
111
239
                /* Otherwise we have to do it through the server */
112
 
                if (!(room = _ggzcore_table_get_room(table)))
 
240
                if (!table->room)
113
241
                        return -1;
114
242
 
115
 
                if (!(server = _ggzcore_room_get_server(room)))
 
243
                if (!(server = ggzcore_room_get_server(table->room)))
116
244
                        return -1;
117
245
 
118
246
                if (!(net = _ggzcore_server_get_net(server)))
128
256
 
129
257
int ggzcore_table_set_desc(GGZTable *table, const char *desc)
130
258
{
131
 
        GGZRoom *room;
132
259
        GGZServer *server;
133
260
        GGZNet *net;
134
261
 
144
271
                _ggzcore_table_set_desc(table, desc);
145
272
        else {
146
273
                /* Otherwise we have to do it through the server */
147
 
                if (!(room = _ggzcore_table_get_room(table)))
 
274
                if (!table->room)
148
275
                        return -1;
149
276
 
150
 
                if (!(server = _ggzcore_room_get_server(room)))
 
277
                if (!(server = ggzcore_room_get_server(table->room)))
151
278
                        return -1;
152
279
 
153
280
                if (!(net = _ggzcore_server_get_net(server)))
161
288
}
162
289
 
163
290
 
164
 
int ggzcore_table_remove_player(GGZTable *table, char *name)
 
291
int ggzcore_table_remove_player(GGZTable *table, const char *name)
165
292
{
166
293
        int i, status = -1;
167
 
        struct _GGZSeat seat;
168
294
 
169
295
        if (table && name) {
170
296
                for (i = 0; i < table->num_seats; i++)
171
297
                        if (table->seats[i].name != NULL 
172
298
                            && strcmp(table->seats[i].name, name) == 0) {
173
 
                                seat.index = i;
174
 
                                seat.type = GGZ_SEAT_OPEN;
175
 
                                seat.name = NULL;
 
299
                                GGZTableSeat seat = {i, GGZ_SEAT_OPEN, NULL};
 
300
 
176
301
                                _ggzcore_table_set_seat(table, &seat);
177
302
                                status = 0;
178
303
                        }
182
307
}
183
308
 
184
309
 
185
 
GGZGameType* ggzcore_table_get_type(GGZTable *table)
 
310
const GGZGameType* ggzcore_table_get_type(const GGZTable *table)
186
311
{
187
312
        if (table)
188
313
                return _ggzcore_table_get_type(table);
191
316
}
192
317
 
193
318
 
194
 
int ggzcore_table_get_id(GGZTable *table)
 
319
int ggzcore_table_get_id(const GGZTable *table)
195
320
{
196
321
        if (table)
197
322
                return _ggzcore_table_get_id(table);
199
324
                return -1;
200
325
}
201
326
 
 
327
 
 
328
const GGZRoom *ggzcore_table_get_room(const GGZTable *table)
 
329
{
 
330
        if (table)
 
331
                return _ggzcore_table_get_room(table);
 
332
        else
 
333
                return NULL;
 
334
}
 
335
 
202
336
     
203
 
const char * ggzcore_table_get_desc(GGZTable *table)
 
337
const char * ggzcore_table_get_desc(const GGZTable *table)
204
338
{
205
339
        if (table)
206
340
                return _ggzcore_table_get_desc(table);
209
343
}
210
344
 
211
345
 
212
 
GGZTableState ggzcore_table_get_state(GGZTable *table)
 
346
GGZTableState ggzcore_table_get_state(const GGZTable *table)
213
347
{
214
348
        if (table)
215
349
                return _ggzcore_table_get_state(table);
218
352
}
219
353
 
220
354
 
221
 
int ggzcore_table_get_num_seats(GGZTable *table)
 
355
int ggzcore_table_get_num_seats(const GGZTable *table)
222
356
{
223
357
        if (table)
224
358
                return _ggzcore_table_get_num_seats(table);
227
361
}
228
362
 
229
363
 
230
 
int ggzcore_table_get_seat_count(GGZTable *table, GGZSeatType type)
 
364
int ggzcore_table_get_seat_count(const GGZTable *table, GGZSeatType type)
231
365
{
232
366
        if (table)
233
367
                return _ggzcore_table_get_seat_count(table, type);
235
369
                return -1;
236
370
}
237
371
 
238
 
const char * ggzcore_table_get_nth_player_name(GGZTable *table,
 
372
 
 
373
const char * ggzcore_table_get_nth_player_name(const GGZTable *table,
239
374
                                               const unsigned int num)
240
375
{
241
376
        if (table && num < table->num_seats)
245
380
}
246
381
 
247
382
 
248
 
GGZSeatType ggzcore_table_get_nth_player_type(GGZTable *table, const unsigned int num)
 
383
GGZSeatType ggzcore_table_get_nth_player_type(const GGZTable *table,
 
384
                                              const unsigned int num)
249
385
{
250
386
        if (table && num < table->num_seats)
251
387
                return _ggzcore_table_get_nth_player_type(table, num);
254
390
}
255
391
 
256
392
 
257
 
 
258
 
int ggzcore_table_get_num_spectator_seats(GGZTable *table)
 
393
int ggzcore_table_get_num_spectator_seats(const GGZTable *table)
259
394
{
260
395
        if (!table)
261
396
                return -1;
263
398
        return _ggzcore_table_get_num_spectator_seats(table);
264
399
}
265
400
 
266
 
const char *ggzcore_table_get_nth_spectator_name(GGZTable *table,
 
401
 
 
402
const char *ggzcore_table_get_nth_spectator_name(const GGZTable *table,
267
403
                                                 const unsigned int num)
268
404
{
269
405
        if (!table || num >= table->num_spectator_seats)
278
414
 * NOTE:All of these functions assume valid inputs!
279
415
 */
280
416
 
281
 
struct _GGZTable* _ggzcore_table_new(void)
 
417
GGZTable* _ggzcore_table_new(void)
282
418
{
283
 
        struct _GGZTable *table;
 
419
        GGZTable *table;
284
420
 
285
 
        table = ggz_malloc(sizeof(struct _GGZTable));
 
421
        table = ggz_malloc(sizeof(*table));
286
422
 
287
423
        /* FIXME: anything we should mark invalid? */
288
424
        return table;
289
425
}
290
426
 
291
427
 
292
 
void _ggzcore_table_init(struct _GGZTable *table, 
293
 
                         struct _GGZGameType *gametype,
 
428
void _ggzcore_table_init(GGZTable *table, 
 
429
                         const GGZGameType *gametype,
294
430
                         const char *desc,
295
431
                         const unsigned int num_seats,
296
432
                         const GGZTableState state,
307
443
        table->num_seats = num_seats;
308
444
        ggz_debug(GGZCORE_DBG_TABLE, "Allocating %d seats", num_seats);
309
445
        if (num_seats)
310
 
                table->seats = ggz_malloc(num_seats * sizeof(struct _GGZSeat));
 
446
                table->seats = ggz_malloc(num_seats * sizeof(GGZTableSeat));
311
447
        for (i = 0; i < num_seats; i++) {
 
448
                /* FIXME: We should probably initialize seats to
 
449
                 * GGZ_SEAT_NONE.  Some code in netxml has to reset it
 
450
                 * manually. */
312
451
                table->seats[i].index = i;
313
452
                table->seats[i].type = GGZ_SEAT_OPEN;
314
 
                table->seats[i].name =  NULL;
 
453
                table->seats[i].name = NULL;
315
454
        }
316
455
 
317
456
        /* Allocated on demand later */
320
459
}
321
460
 
322
461
 
323
 
void _ggzcore_table_free(struct _GGZTable *table)
 
462
void _ggzcore_table_free(GGZTable *table)
324
463
{
325
464
        int i;
326
465
 
345
484
 
346
485
}
347
486
 
348
 
void _ggzcore_table_set_room(struct _GGZTable *table, struct _GGZRoom *room)
 
487
 
 
488
void _ggzcore_table_set_room(GGZTable *table, GGZRoom *room)
349
489
{
350
490
        table->room = room;
351
491
}
352
492
 
353
493
 
354
 
void _ggzcore_table_set_id(struct _GGZTable *table, const int id)
 
494
void _ggzcore_table_set_id(GGZTable *table, const int id)
355
495
{
356
496
        table->id = id;
357
497
}
358
498
 
359
499
 
360
 
void _ggzcore_table_set_state(struct _GGZTable *table, const GGZTableState state)
 
500
void _ggzcore_table_set_state(GGZTable *table, const GGZTableState state)
361
501
{
362
502
        ggz_debug(GGZCORE_DBG_TABLE, "Table %d new state %d",
363
503
                  table->id, state);
369
509
}
370
510
 
371
511
 
372
 
void _ggzcore_table_set_desc(struct _GGZTable *table, const char *desc)
 
512
void _ggzcore_table_set_desc(GGZTable *table, const char *desc)
373
513
{
374
514
        ggz_debug(GGZCORE_DBG_TABLE, "Table %d new desc %s", table->id, desc);
375
515
 
383
523
                _ggzcore_room_table_event(table->room, GGZ_TABLE_UPDATE, NULL);
384
524
}
385
525
 
386
 
void _ggzcore_table_set_seat(struct _GGZTable *table, struct _GGZSeat *seat)
 
526
 
 
527
void _ggzcore_table_set_seat(GGZTable *table, GGZTableSeat *seat)
387
528
{
388
529
        /* Set up the new seat. */
389
 
        struct _GGZSeat oldseat;
 
530
        GGZTableSeat oldseat;
390
531
        GGZServer *server;
391
532
        GGZGame *game;
392
533
 
407
548
                ggz_debug(GGZCORE_DBG_TABLE, "%s joining seat %d at table %d",
408
549
                              seat->name, seat->index, table->id);
409
550
                if (table->room)
410
 
                        _ggzcore_room_player_set_table(table->room, seat->name, table->id);
411
 
        }
412
 
        else if (oldseat.type == GGZ_SEAT_PLAYER) {
 
551
                        _ggzcore_room_player_set_table(table->room,
 
552
                                                       seat->name, table->id);
 
553
        } else if (oldseat.type == GGZ_SEAT_PLAYER) {
413
554
                ggz_debug(GGZCORE_DBG_TABLE, "%s leaving seat %d at table %d",
414
555
                          oldseat.name, oldseat.index, table->id);
415
556
                if (table->room)
416
 
                        _ggzcore_room_player_set_table(table->room, oldseat.name, -1);
417
 
        }
418
 
        else {
 
557
                        _ggzcore_room_player_set_table(table->room,
 
558
                                                       oldseat.name, -1);
 
559
        } else {
419
560
                if (table->room) 
420
 
                        _ggzcore_room_table_event(table->room, GGZ_TABLE_UPDATE, NULL);
 
561
                        _ggzcore_room_table_event(table->room,
 
562
                                                  GGZ_TABLE_UPDATE, NULL);
421
563
        }
422
564
        
423
565
        /* Get rid of the old seat. */
426
568
 
427
569
        /* If this is our table, alert the game module. */
428
570
        if (table->room
429
 
            && (server = _ggzcore_room_get_server(table->room))
430
 
            && (game = _ggzcore_server_get_cur_game(server))
431
 
            && (_ggzcore_room_get_id(table->room)
432
 
                == _ggzcore_game_get_room_id(game))) {
 
571
            && (server = ggzcore_room_get_server(table->room))
 
572
            && (game = ggzcore_server_get_cur_game(server))
 
573
            && ggzcore_room_get_id(table->room)
 
574
                == _ggzcore_game_get_room_id(game)) {
433
575
                const char *me = _ggzcore_server_get_handle(server);
434
576
                int game_table = _ggzcore_game_get_table_id(game);
435
577
 
448
590
}
449
591
 
450
592
 
451
 
void _ggzcore_table_set_spectator_seat(struct _GGZTable *table,
452
 
                                       struct _GGZSeat *seat)
 
593
void _ggzcore_table_set_spectator_seat(GGZTable *table,
 
594
                                       GGZTableSeat *seat)
453
595
{
454
 
        struct _GGZSeat oldseat;
 
596
        GGZTableSeat oldseat;
455
597
        GGZServer *server;
456
598
        GGZGame *game;
457
599
 
505
647
 
506
648
        /* If this is our table, alert the game module. */
507
649
        if (table->room
508
 
            && (server = _ggzcore_room_get_server(table->room))
 
650
            && (server = ggzcore_room_get_server(table->room))
509
651
            && (game = _ggzcore_server_get_cur_game(server))
510
 
            && (_ggzcore_room_get_id(table->room)
 
652
            && (ggzcore_room_get_id(table->room)
511
653
                == _ggzcore_game_get_room_id(game))) {
512
654
                const char *me = _ggzcore_server_get_handle(server);
513
655
                int game_table = _ggzcore_game_get_table_id(game);
526
668
}
527
669
 
528
670
 
529
 
struct _GGZRoom* _ggzcore_table_get_room(struct _GGZTable *table)
530
 
{
531
 
        return table->room;
532
 
}
533
 
 
534
 
 
535
 
int _ggzcore_table_get_id(struct _GGZTable *table)
536
 
{
537
 
        return table->id;
538
 
}
539
 
 
540
 
 
541
 
struct _GGZGameType* _ggzcore_table_get_type(struct _GGZTable *table)
542
 
{
543
 
        return table->gametype;
544
 
}
545
 
 
546
 
 
547
 
const char * _ggzcore_table_get_desc(struct _GGZTable *table)
548
 
{
549
 
        return table->desc;
550
 
}
551
 
 
552
 
 
553
 
GGZTableState _ggzcore_table_get_state(struct _GGZTable *table)
554
 
{
555
 
        return table->state;
556
 
}
557
 
 
558
 
 
559
 
int _ggzcore_table_get_num_seats(struct _GGZTable *table)
560
 
{
561
 
        return table->num_seats;
562
 
}
563
 
 
564
 
 
565
 
int _ggzcore_table_get_seat_count(GGZTable *table, GGZSeatType type)
566
 
{
567
 
        int i, count = 0;
568
 
 
569
 
        for (i = 0; i < table->num_seats; i++)
570
 
                if (table->seats[i].type == type)
571
 
                        count++;
572
 
        
573
 
        return count;
574
 
}
575
 
 
576
 
 
577
 
struct _GGZSeat* _ggzcore_table_get_nth_seat(struct _GGZTable *table, const unsigned int num)
578
 
{
579
 
        return &table->seats[num];
580
 
}
581
 
 
582
 
 
583
 
const char * _ggzcore_table_get_nth_player_name(struct _GGZTable *table, const unsigned int num)
584
 
{
585
 
        return table->seats[num].name;
586
 
}
587
 
 
588
 
 
589
 
GGZSeatType _ggzcore_table_get_nth_player_type(struct _GGZTable *table, const unsigned int num)
590
 
{
591
 
        return table->seats[num].type;
592
 
}
593
 
 
594
 
int _ggzcore_table_get_num_spectator_seats(GGZTable *table)
595
 
{
596
 
        return table->num_spectator_seats;
597
 
}
598
 
 
599
 
struct _GGZSeat *_ggzcore_table_get_nth_spectator_seat(GGZTable *table,
600
 
                                                       const unsigned int num)
601
 
{
602
 
        return &table->spectator_seats[num];
603
 
}
604
 
 
605
 
const char *_ggzcore_table_get_nth_spectator_name(GGZTable *table,
606
 
                                                  const unsigned int num)
607
 
{
608
 
        return table->spectator_seats[num].name;
609
 
}
610
 
 
611
 
 
612
 
int _ggzcore_table_compare(void* p, void* q)
613
 
{
614
 
        if(((struct _GGZTable*)p)->id == ((struct _GGZTable*)q)->id)
615
 
                return 0;
616
 
        if(((struct _GGZTable*)p)->id > ((struct _GGZTable*)q)->id)
617
 
                return 1;
618
 
        if(((struct _GGZTable*)p)->id < ((struct _GGZTable*)q)->id)
619
 
                return -1;
620
 
 
621
 
        return 0;
 
671
int _ggzcore_table_compare(const void* p, const void* q)
 
672
{
 
673
        const GGZTable *table1 = p, *table2 = q;
 
674
 
 
675
        return table1->id - table2->id;
622
676
}
623
677
 
624
678
 
625
679
void* _ggzcore_table_create(void* p)
626
680
{
627
 
        struct _GGZTable *new, *src = p;
 
681
        GGZTable *new, *src = p;
628
682
 
629
683
        new = _ggzcore_table_new();
630
684
        _ggzcore_table_init(new, src->gametype, src->desc,
633
687
 
634
688
        /* FIXME: copy players as well */
635
689
        
636
 
        return (void*)new;
 
690
        return new;
637
691
}
638
692
 
639
693
 
641
695
{
642
696
        _ggzcore_table_free(p);
643
697
}
644
 
 
645
 
 
646
 
 
647
 
/* Static functions internal to this file */
648