93
97
static void handle_ggz_state_event(GGZdMod * ggz, GGZdModEvent event,
97
100
const GGZdModState *old_state = data;
98
101
GGZdModState new_state = ggzdmod_get_state(ggz);
100
/* Currently no handling is done. */
103
freelog(LOG_DEBUG, "ggz changed state to %d.", new_state);
105
if (*old_state == GGZDMOD_STATE_CREATED) {
106
const char *savegame = ggzdmod_get_savedgame(ggz);
108
/* If a savegame is given, load it. */
109
freelog(LOG_DEBUG, "Instructed to load \"%s\".", savegame);
111
if (!load_command(NULL, savegame, FALSE)) {
112
/* no error handling? */
117
/* If we loaded a game that'll include the serverid. If not we
118
* generate one here. */
119
if (strlen(srvarg.serverid) == 0) {
120
strcpy(srvarg.serverid, "ggz-civ-XXXXXX");
121
if (!mkdtemp(srvarg.serverid)) {
123
_("Unable to make temporary directory for GGZ game.\n"));
128
/* Change into the server directory */
129
if (chdir(srvarg.serverid) < 0) {
131
_("Unable to change into temporary server "
132
"directory %s.\n"), srvarg.serverid);
135
freelog(LOG_DEBUG, "Changed into directory %s.", srvarg.serverid);
104
139
/****************************************************************************
105
140
Handles a seat-change event as reported by the GGZ server.
142
This typically happens when a human player joins or leaves the game.
143
This function links the GGZ seat player (which is just a seat number) to
144
the correct player. In the case of a join event we also get the socket
145
for the player's connection, which is treated just like a new connection.
106
146
****************************************************************************/
107
147
static void handle_ggz_seat_event(GGZdMod *ggz, GGZdModEvent event,
108
148
const void *data)
142
182
} players_iterate_end;
145
printf("%s is leaving.\n", old_seat->name);
185
freelog(LOG_DEBUG, "%s is leaving.", old_seat->name);
146
186
leaving->sock = -1;
147
187
lost_connection_to_client(leaving);
148
188
close_connection(leaving);
150
printf("Couldn't match player %s.\n", old_seat->name);
190
freelog(LOG_ERROR, "Couldn't match player %s.", old_seat->name);
175
215
/****************************************************************************
216
Handles a ggzdmod error. This simply exits the server with an error
218
****************************************************************************/
219
static void handle_ggz_error(GGZdMod * ggz, GGZdModEvent event,
222
const char *err = data;
224
freelog(LOG_ERROR, "Error in ggz: %s", err);
228
/****************************************************************************
176
229
Connect to the GGZ server, if GGZ is being used.
177
230
****************************************************************************/
178
231
void ggz_initialize(void)
199
252
&handle_ggz_seat_event);
200
253
ggzdmod_set_handler(ggzdmod, GGZDMOD_EVENT_SPECTATOR_JOIN,
201
254
&handle_ggz_spectator_seat_event);
255
ggzdmod_set_handler(ggzdmod, GGZDMOD_EVENT_SPECTATOR_LEAVE,
256
&handle_ggz_spectator_seat_event);
257
ggzdmod_set_handler(ggzdmod, GGZDMOD_EVENT_SPECTATOR_SEAT,
258
&handle_ggz_spectator_seat_event);
259
ggzdmod_set_handler(ggzdmod, GGZDMOD_EVENT_ERROR,
202
261
if (ggzdmod_connect(ggzdmod) < 0) {
203
262
exit(EXIT_FAILURE);
332
391
num_victors = 0; /* In case there's another game. */
394
/****************************************************************************
395
Reports a savegame file to the GGZ server. GGZ will allow
396
reloading from a file later by providing the savegame at launch time
397
(in the STATE event when leaving the CREATED state).
398
****************************************************************************/
399
void ggz_game_saved(const char *filename)
401
char full_filename[strlen(filename) + strlen(srvarg.serverid) + 2];
403
if (!path_is_absolute(filename)) {
404
snprintf(full_filename, sizeof(full_filename), "%s/%s",
405
srvarg.serverid, filename);
407
sz_strlcpy(full_filename, filename);
409
freelog(LOG_DEBUG, "Reporting filename %s => %s to ggz.",
410
filename, full_filename);
413
ggzdmod_report_savegame(ggzdmod, full_filename);