~ubuntu-branches/debian/squeeze/freeciv/squeeze

« back to all changes in this revision

Viewing changes to server/ggzserver.c

  • Committer: Bazaar Package Importer
  • Author(s): Clint Adams
  • Date: 2008-11-29 22:25:59 UTC
  • mfrom: (1.2.9 upstream)
  • Revision ID: james.westby@ubuntu.com-20081129222559-6sqqdum8qnhykm4l
Tags: 2.1.8-1
* New upstream release.  closes: #495740.
* Disable GGZ support (can be re-enabled when ggz 1.0 is available).
* Change maintainer to Debian Games Team.

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
 
18
18
#ifdef GGZ_SERVER
19
19
 
 
20
#include <unistd.h>
 
21
 
20
22
#include <ggzdmod.h>
21
23
 
22
24
#include "fciconv.h"
31
33
#include "ggzserver.h"
32
34
#include "score.h"
33
35
#include "sernet.h"
 
36
#include "srv_main.h"
 
37
#include "stdinhand.h"
34
38
 
35
39
bool with_ggz = FALSE;
36
40
 
73
77
    return NULL;
74
78
  case GGZ_SEAT_PLAYER:
75
79
  case GGZ_SEAT_BOT:
76
 
  default: /* Works for GGZ_SEAT_ABANDONED. */
 
80
  case GGZ_SEAT_ABANDONED:
77
81
    break;
78
82
  }
79
83
 
93
97
static void handle_ggz_state_event(GGZdMod * ggz, GGZdModEvent event,
94
98
                                   const void *data)
95
99
{
96
 
#if 0
97
100
  const GGZdModState *old_state = data;
98
101
  GGZdModState new_state = ggzdmod_get_state(ggz);
99
102
 
100
 
  /* Currently no handling is done. */
101
 
#endif
 
103
  freelog(LOG_DEBUG, "ggz changed state to %d.", new_state);
 
104
 
 
105
  if (*old_state == GGZDMOD_STATE_CREATED) {
 
106
    const char *savegame = ggzdmod_get_savedgame(ggz);
 
107
 
 
108
    /* If a savegame is given, load it. */
 
109
    freelog(LOG_DEBUG, "Instructed to load \"%s\".", savegame);
 
110
    if (savegame) {
 
111
      if (!load_command(NULL, savegame, FALSE)) {
 
112
        /* no error handling? */
 
113
        server_quit();
 
114
      }
 
115
    }
 
116
 
 
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)) {
 
122
        freelog(LOG_ERROR,
 
123
                _("Unable to make temporary directory for GGZ game.\n"));
 
124
        server_quit();
 
125
      }
 
126
    }
 
127
 
 
128
    /* Change into the server directory */
 
129
    if (chdir(srvarg.serverid) < 0) {
 
130
      freelog(LOG_ERROR,
 
131
              _("Unable to change into temporary server "
 
132
                "directory %s.\n"), srvarg.serverid);
 
133
      server_quit();
 
134
    }
 
135
    freelog(LOG_DEBUG, "Changed into directory %s.", srvarg.serverid);
 
136
  }
102
137
}
103
138
 
104
139
/****************************************************************************
105
140
  Handles a seat-change event as reported by the GGZ server.
 
141
 
 
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;
143
183
 
144
184
    if (leaving) {
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);
149
189
    } else {
150
 
      printf("Couldn't match player %s.\n", old_seat->name);
 
190
      freelog(LOG_ERROR, "Couldn't match player %s.", old_seat->name);
151
191
    }
152
192
  }
153
193
}
173
213
}
174
214
 
175
215
/****************************************************************************
 
216
  Handles a ggzdmod error.  This simply exits the server with an error
 
217
  message.
 
218
****************************************************************************/
 
219
static void handle_ggz_error(GGZdMod * ggz, GGZdModEvent event,
 
220
                             const void *data)
 
221
{
 
222
  const char *err = data;
 
223
 
 
224
  freelog(LOG_ERROR, "Error in ggz: %s", err);
 
225
  server_quit();
 
226
}
 
227
 
 
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,
 
260
                        &handle_ggz_error);
202
261
    if (ggzdmod_connect(ggzdmod) < 0) {
203
262
      exit(EXIT_FAILURE);
204
263
    }
332
391
  num_victors = 0; /* In case there's another game. */
333
392
}
334
393
 
 
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)
 
400
{
 
401
  char full_filename[strlen(filename) + strlen(srvarg.serverid) + 2];
 
402
 
 
403
  if (!path_is_absolute(filename)) {
 
404
    snprintf(full_filename, sizeof(full_filename), "%s/%s",
 
405
             srvarg.serverid, filename);
 
406
  } else {
 
407
    sz_strlcpy(full_filename, filename);
 
408
  }
 
409
  freelog(LOG_DEBUG, "Reporting filename %s => %s to ggz.",
 
410
          filename, full_filename);
 
411
 
 
412
  if (with_ggz) {
 
413
    ggzdmod_report_savegame(ggzdmod, full_filename);
 
414
  }
 
415
}
 
416
 
335
417
#endif