1 /**********************************************************************
2 Freeciv - Copyright (C) 2005 - Freeciv Development Team
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; either version 2, or (at your option)
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12 ***********************************************************************/
15 #include <fc_config.h>
24 #include "gui_main_g.h"
30 #include "ggzclient.h"
38 /****************************************************************************
39 Initializations for GGZ, including checks to see if we are being run from
41 ****************************************************************************/
42 void ggz_initialize(void)
45 with_ggz
= ggzmod_is_ggz_mode();
52 gui_ggz_embed_ensure_server();
57 static GGZMod
*ggzmod
;
59 /****************************************************************************
60 A callback that GGZ calls when we are connected to the freeciv
62 ****************************************************************************/
63 static void handle_ggzmod_server(GGZMod
* ggzmod
, GGZModEvent e
,
66 const int *socket
= data
;
67 const char *username
= ggzmod_get_player(ggzmod
, NULL
, NULL
);
72 make_connection(*socket
, username
);
75 /****************************************************************************
76 Callback for when the GGZ client tells us player stats.
77 ****************************************************************************/
78 static void handle_ggzmod_stats(GGZMod
*ggzmod
, GGZModEvent e
,
81 conn_list_dialog_update();
84 /****************************************************************************
85 Connect to the GGZ client.
86 ****************************************************************************/
91 /* We're in GGZ mode */
92 ggzmod
= ggzmod_new(GGZMOD_GAME
);
93 ggzmod_set_handler(ggzmod
, GGZMOD_EVENT_SERVER
, &handle_ggzmod_server
);
94 ggzmod_set_handler(ggzmod
, GGZMOD_EVENT_STATS
, &handle_ggzmod_stats
);
95 if (ggzmod_connect(ggzmod
) < 0) {
98 ggz_socket
= ggzmod_get_fd(ggzmod
);
100 fc_fprintf(stderr
, _("Only the GGZ client must call freeciv-client"
104 add_ggz_input(ggz_socket
);
107 /****************************************************************************
108 Called when the ggz socket has data pending.
109 ****************************************************************************/
110 void input_from_ggz(int socket
)
112 if (ggzmod_dispatch(ggzmod
) < 0) {
113 disconnect_from_server();
117 /****************************************************************************
118 Find the seat for the given player (in the seat parameter), or returns
119 FALSE if no seat is found.
120 ****************************************************************************/
121 static bool user_get_seat(const char *name
, GGZSeat
*seat
)
124 int num
= ggzmod_get_num_seats(ggzmod
);
126 for (i
= 0; i
< num
; i
++) {
127 *seat
= ggzmod_get_seat(ggzmod
, i
);
129 if (seat
->type
== GGZ_SEAT_PLAYER
130 && strcasecmp(seat
->name
, name
) == 0) {
138 /****************************************************************************
139 Find the player's rating, or return FALSE if there is no rating.
140 ****************************************************************************/
141 bool user_get_rating(const char *name
, int *rating
)
145 if (user_get_seat(name
, &seat
)) {
146 return ggzmod_player_get_rating(ggzmod
, &seat
, rating
);
152 /****************************************************************************
153 Find the player's record, or return FALSE if there is no record.
154 ****************************************************************************/
155 bool user_get_record(const char *name
,
156 int *wins
, int *losses
, int *ties
, int *forfeits
)
160 if (user_get_seat(name
, &seat
)) {
161 return ggzmod_player_get_record(ggzmod
, &seat
,
162 wins
, losses
, ties
, forfeits
);
169 #endif /* GGZ_CLIENT */