3
3
* Author: Justin Zaun
4
4
* Project: GGZ Core Client Lib
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 $
8
8
* This fils contains functions for handling tables
10
10
* Copyright (C) 1998 Brent Hendricks.
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.
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.
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.
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.
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
27
27
#ifdef HAVE_CONFIG_H
49
/* Publicly exported functions */
49
/* Table Information */
52
/* Pointer to room this table resides in */
55
/* Server ID of table */
59
const GGZGameType *gametype;
61
/* Table description */
68
unsigned int num_seats;
73
/* Total spectator seats */
74
unsigned int num_spectator_seats;
76
/* Spectator seats - "type" is unused; player name is
77
NULL for empty seat. */
78
GGZTableSeat *spectator_seats;
83
* Private functions - either static or declared in table.h
86
static const GGZRoom *_ggzcore_table_get_room(const GGZTable *table)
92
static int _ggzcore_table_get_id(const GGZTable *table)
98
static const GGZGameType* _ggzcore_table_get_type(const GGZTable *table)
100
return table->gametype;
104
static const char * _ggzcore_table_get_desc(const GGZTable *table)
110
static GGZTableState _ggzcore_table_get_state(const GGZTable *table)
116
static int _ggzcore_table_get_num_seats(const GGZTable *table)
118
return table->num_seats;
122
static int _ggzcore_table_get_seat_count(const GGZTable *table,
127
for (i = 0; i < table->num_seats; i++)
128
if (table->seats[i].type == type)
135
GGZTableSeat _ggzcore_table_get_nth_seat(const GGZTable *table,
138
return table->seats[num];
142
static const char *_ggzcore_table_get_nth_player_name(const GGZTable *table,
145
return table->seats[num].name;
149
static GGZSeatType _ggzcore_table_get_nth_player_type(const GGZTable *table,
152
return table->seats[num].type;
156
static int _ggzcore_table_get_num_spectator_seats(const GGZTable *table)
158
return table->num_spectator_seats;
162
GGZTableSeat _ggzcore_table_get_nth_spectator_seat(const GGZTable *table,
165
return table->spectator_seats[num];
169
static const char *_ggzcore_table_get_nth_spectator_name(const GGZTable *table,
172
return table->spectator_seats[num].name;
177
* Publicly exported functions
51
180
GGZTable* ggzcore_table_new(void)
57
186
int ggzcore_table_init(GGZTable *table,
58
GGZGameType *gametype,
187
const GGZGameType *gametype,
60
189
const unsigned int num_seats)
191
/* A NULL desc is allowed. */
62
192
if (table && gametype) {
63
193
_ggzcore_table_init(table, gametype, desc,
65
195
GGZ_TABLE_CREATED, -1);
86
214
GGZServer *server;
88
struct _GGZSeat seat = {index, type, name};
216
GGZTableSeat seat = {index, type, name};
90
218
ggz_debug(GGZCORE_DBG_TABLE, "User changing seats... on %p", table);
109
237
_ggzcore_table_set_seat(table, &seat);
111
239
/* Otherwise we have to do it through the server */
112
if (!(room = _ggzcore_table_get_room(table)))
115
if (!(server = _ggzcore_room_get_server(room)))
243
if (!(server = ggzcore_room_get_server(table->room)))
118
246
if (!(net = _ggzcore_server_get_net(server)))
144
271
_ggzcore_table_set_desc(table, desc);
146
273
/* Otherwise we have to do it through the server */
147
if (!(room = _ggzcore_table_get_room(table)))
150
if (!(server = _ggzcore_room_get_server(room)))
277
if (!(server = ggzcore_room_get_server(table->room)))
153
280
if (!(net = _ggzcore_server_get_net(server)))
164
int ggzcore_table_remove_player(GGZTable *table, char *name)
291
int ggzcore_table_remove_player(GGZTable *table, const char *name)
166
293
int i, status = -1;
167
struct _GGZSeat seat;
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) {
174
seat.type = GGZ_SEAT_OPEN;
299
GGZTableSeat seat = {i, GGZ_SEAT_OPEN, NULL};
176
301
_ggzcore_table_set_seat(table, &seat);
328
const GGZRoom *ggzcore_table_get_room(const GGZTable *table)
331
return _ggzcore_table_get_room(table);
203
const char * ggzcore_table_get_desc(GGZTable *table)
337
const char * ggzcore_table_get_desc(const GGZTable *table)
206
340
return _ggzcore_table_get_desc(table);
230
int ggzcore_table_get_seat_count(GGZTable *table, GGZSeatType type)
364
int ggzcore_table_get_seat_count(const GGZTable *table, GGZSeatType type)
233
367
return _ggzcore_table_get_seat_count(table, type);
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)
250
386
if (table && num < table->num_seats)
251
387
return _ggzcore_table_get_nth_player_type(table, num);
263
398
return _ggzcore_table_get_num_spectator_seats(table);
266
const char *ggzcore_table_get_nth_spectator_name(GGZTable *table,
402
const char *ggzcore_table_get_nth_spectator_name(const GGZTable *table,
267
403
const unsigned int num)
269
405
if (!table || num >= table->num_spectator_seats)
278
414
* NOTE:All of these functions assume valid inputs!
281
struct _GGZTable* _ggzcore_table_new(void)
417
GGZTable* _ggzcore_table_new(void)
283
struct _GGZTable *table;
285
table = ggz_malloc(sizeof(struct _GGZTable));
421
table = ggz_malloc(sizeof(*table));
287
423
/* FIXME: anything we should mark invalid? */
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);
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
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;
317
456
/* Allocated on demand later */
348
void _ggzcore_table_set_room(struct _GGZTable *table, struct _GGZRoom *room)
488
void _ggzcore_table_set_room(GGZTable *table, GGZRoom *room)
350
490
table->room = room;
354
void _ggzcore_table_set_id(struct _GGZTable *table, const int id)
494
void _ggzcore_table_set_id(GGZTable *table, const int id)
360
void _ggzcore_table_set_state(struct _GGZTable *table, const GGZTableState state)
500
void _ggzcore_table_set_state(GGZTable *table, const GGZTableState state)
362
502
ggz_debug(GGZCORE_DBG_TABLE, "Table %d new state %d",
363
503
table->id, state);
372
void _ggzcore_table_set_desc(struct _GGZTable *table, const char *desc)
512
void _ggzcore_table_set_desc(GGZTable *table, const char *desc)
374
514
ggz_debug(GGZCORE_DBG_TABLE, "Table %d new desc %s", table->id, desc);
383
523
_ggzcore_room_table_event(table->room, GGZ_TABLE_UPDATE, NULL);
386
void _ggzcore_table_set_seat(struct _GGZTable *table, struct _GGZSeat *seat)
527
void _ggzcore_table_set_seat(GGZTable *table, GGZTableSeat *seat)
388
529
/* Set up the new seat. */
389
struct _GGZSeat oldseat;
530
GGZTableSeat oldseat;
390
531
GGZServer *server;
407
548
ggz_debug(GGZCORE_DBG_TABLE, "%s joining seat %d at table %d",
408
549
seat->name, seat->index, table->id);
410
_ggzcore_room_player_set_table(table->room, seat->name, table->id);
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);
416
_ggzcore_room_player_set_table(table->room, oldseat.name, -1);
557
_ggzcore_room_player_set_table(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);
423
565
/* Get rid of the old seat. */
427
569
/* If this is our table, alert the game module. */
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);
506
648
/* If this is our table, alert the game module. */
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);
529
struct _GGZRoom* _ggzcore_table_get_room(struct _GGZTable *table)
535
int _ggzcore_table_get_id(struct _GGZTable *table)
541
struct _GGZGameType* _ggzcore_table_get_type(struct _GGZTable *table)
543
return table->gametype;
547
const char * _ggzcore_table_get_desc(struct _GGZTable *table)
553
GGZTableState _ggzcore_table_get_state(struct _GGZTable *table)
559
int _ggzcore_table_get_num_seats(struct _GGZTable *table)
561
return table->num_seats;
565
int _ggzcore_table_get_seat_count(GGZTable *table, GGZSeatType type)
569
for (i = 0; i < table->num_seats; i++)
570
if (table->seats[i].type == type)
577
struct _GGZSeat* _ggzcore_table_get_nth_seat(struct _GGZTable *table, const unsigned int num)
579
return &table->seats[num];
583
const char * _ggzcore_table_get_nth_player_name(struct _GGZTable *table, const unsigned int num)
585
return table->seats[num].name;
589
GGZSeatType _ggzcore_table_get_nth_player_type(struct _GGZTable *table, const unsigned int num)
591
return table->seats[num].type;
594
int _ggzcore_table_get_num_spectator_seats(GGZTable *table)
596
return table->num_spectator_seats;
599
struct _GGZSeat *_ggzcore_table_get_nth_spectator_seat(GGZTable *table,
600
const unsigned int num)
602
return &table->spectator_seats[num];
605
const char *_ggzcore_table_get_nth_spectator_name(GGZTable *table,
606
const unsigned int num)
608
return table->spectator_seats[num].name;
612
int _ggzcore_table_compare(void* p, void* q)
614
if(((struct _GGZTable*)p)->id == ((struct _GGZTable*)q)->id)
616
if(((struct _GGZTable*)p)->id > ((struct _GGZTable*)q)->id)
618
if(((struct _GGZTable*)p)->id < ((struct _GGZTable*)q)->id)
671
int _ggzcore_table_compare(const void* p, const void* q)
673
const GGZTable *table1 = p, *table2 = q;
675
return table1->id - table2->id;
625
679
void* _ggzcore_table_create(void* p)
627
struct _GGZTable *new, *src = p;
681
GGZTable *new, *src = p;
629
683
new = _ggzcore_table_new();
630
684
_ggzcore_table_init(new, src->gametype, src->desc,