<URL: http://bugs.freeciv.org/Ticket/Display.html?id=40317 >

New patch.

Some version checks will be necessary as only newer ggzd versions have
savegame support.  And it's still not quite working.

-jason

Index: configure.ac
===================================================================
--- configure.ac	(revision 14950)
+++ configure.ac	(working copy)
@@ -535,6 +535,9 @@
 else
   AC_GGZ_CHECK
 fi
+if test "$ggz_server" = "yes" ; then
+  AC_CHECK_FUNCS([chdir mkdtemp])
+fi
 
 AC_SUBST(gui_sources)
 AC_SUBST(CLIENT_CFLAGS)
Index: server/srv_main.c
===================================================================
--- server/srv_main.c	(revision 14950)
+++ server/srv_main.c	(working copy)
@@ -973,6 +973,8 @@
   freelog(LOG_VERBOSE, "Save time: %g seconds (%g apparent)",
 	  read_timer_seconds_free(timer_cpu),
 	  read_timer_seconds_free(timer_user));
+
+  ggz_game_saved(filename);
 }
 
 /**************************************************************************
Index: server/ggzserver.c
===================================================================
--- server/ggzserver.c	(revision 14950)
+++ server/ggzserver.c	(working copy)
@@ -17,6 +17,9 @@
 
 #ifdef GGZ_SERVER
 
+#include <limits.h>
+#include <unistd.h>
+
 #include <ggzdmod.h>
 
 #include "fciconv.h"
@@ -31,6 +34,8 @@
 #include "ggzserver.h"
 #include "score.h"
 #include "sernet.h"
+#include "srv_main.h"
+#include "stdinhand.h"
 
 bool with_ggz = FALSE;
 
@@ -93,12 +98,42 @@
 static void handle_ggz_state_event(GGZdMod * ggz, GGZdModEvent event,
 				   const void *data)
 {
-#if 0
   const GGZdModState *old_state = data;
   GGZdModState new_state = ggzdmod_get_state(ggz);
 
-  /* Currently no handling is done. */
-#endif
+  freelog(LOG_DEBUG, "ggz changed state to %d.", new_state);
+
+  if (*old_state == GGZDMOD_STATE_CREATED) {
+    const char *savegame = ggzdmod_get_savedgame(ggz);
+
+    /* If a savegame is given, load it. */
+    freelog(LOG_DEBUG, "Instructed to load \"%s\".", savegame);
+    if (savegame) {
+      if (!load_command(NULL, savegame, FALSE)) {
+	/* no error handling? */
+	server_quit();
+      }
+    }
+
+    /* If we loaded a game that'll include the serverid.  If not we
+     * generate one here. */
+    if (strlen(srvarg.serverid) == 0) {
+      strcpy(srvarg.serverid, "ggz-civ-XXXXXX");
+      if (!mkdtemp(srvarg.serverid)) {
+	fc_fprintf(stderr,
+		   _("Unable to make temporary directory for GGZ game.\n"));
+	exit(EXIT_FAILURE);
+      }
+    }
+
+    /* Change into the server directory */
+    if (chdir(srvarg.serverid) < 0) {
+      fc_fprintf(stderr, _("Unable to change into temporary server "
+			   "directory %s.\n"), srvarg.serverid);
+      server_quit();
+    }
+    freelog(LOG_DEBUG, "Changed into directory %s.", srvarg.serverid);
+  }
 }
 
 /****************************************************************************
@@ -199,6 +234,10 @@
 			&handle_ggz_seat_event);
     ggzdmod_set_handler(ggzdmod, GGZDMOD_EVENT_SPECTATOR_JOIN,
 			&handle_ggz_spectator_seat_event);
+    ggzdmod_set_handler(ggzdmod, GGZDMOD_EVENT_SPECTATOR_LEAVE,
+			&handle_ggz_spectator_seat_event);
+    ggzdmod_set_handler(ggzdmod, GGZDMOD_EVENT_SPECTATOR_SEAT,
+			&handle_ggz_spectator_seat_event);
     if (ggzdmod_connect(ggzdmod) < 0) {
       exit(EXIT_FAILURE);
     }
@@ -332,4 +371,29 @@
   num_victors = 0; /* In case there's another game. */
 }
 
+/****************************************************************************
+  Reports a savegame file to the GGZ server.  In theory GGZ will allow
+  reloading from a file later.
+****************************************************************************/
+void ggz_game_saved(const char *filename)
+{
+  char full_filename[PATH_MAX];
+
+  if (!path_is_absolute(filename)) {
+    if (!getcwd(full_filename, sizeof(full_filename))) {
+      freelog(LOG_FATAL, "Could not get cwd.");
+      exit(EXIT_FAILURE);
+    }
+    sz_strlcat(full_filename, "/");
+    sz_strlcat(full_filename, filename);
+  } else {
+    sz_strlcpy(full_filename, filename);
+  }
+  freelog(LOG_DEBUG, "Reporting filename %s to ggz.", full_filename);
+
+  if (with_ggz) {
+    ggzdmod_report_savegame(ggzdmod, full_filename);
+  }
+}
+
 #endif
Index: server/ggzserver.h
===================================================================
--- server/ggzserver.h	(revision 14950)
+++ server/ggzserver.h	(working copy)
@@ -28,12 +28,15 @@
 void ggz_report_victor(const struct player *winner);
 void ggz_report_victory(void);
 
+void ggz_game_saved(const char *filename);
+
 #else
 
 #  define with_ggz FALSE
 #  define ggz_initialize() (void)0
 #  define ggz_report_victor(pplayer) (void)0
 #  define ggz_report_victory() (void)0
+#  define ggz_game_saved(filename) (void)0
 
 #endif
 
Index: server/stdinhand.c
===================================================================
--- server/stdinhand.c	(revision 14950)
+++ server/stdinhand.c	(working copy)
@@ -3301,7 +3301,7 @@
     legitimate but has inconsistencies) and would lead to a broken server
     afterwards.
 **************************************************************************/
-bool load_command(struct connection *caller, char *filename, bool check)
+bool load_command(struct connection *caller, const char *filename, bool check)
 {
   struct timer *loadtimer, *uloadtimer;  
   struct section_file file;
Index: server/stdinhand.h
===================================================================
--- server/stdinhand.h	(revision 14950)
+++ server/stdinhand.h	(working copy)
@@ -32,7 +32,8 @@
                       bool from_cmdline);
 void show_players(struct connection *caller);
 
-bool load_command(struct connection *caller, char *arg, bool check);
+bool load_command(struct connection *caller,
+		  const char *filename, bool check);
 bool start_command(struct connection *caller, bool check, bool notify);
 
 void toggle_ai_player_direct(struct connection *caller,
Index: server/savegame.c
===================================================================
--- server/savegame.c	(revision 14950)
+++ server/savegame.c	(working copy)
@@ -3843,6 +3843,8 @@
     sz_strlcpy(srvarg.metaserver_addr,
 	       secfile_lookup_str_default(file, DEFAULT_META_SERVER_ADDR,
 					  "game.metaserver"));
+    sz_strlcpy(srvarg.serverid,
+	       secfile_lookup_str_default(file, "", "game.serverid"));
 
     game.info.gold          = secfile_lookup_int(file, "game.gold");
     game.info.tech          = secfile_lookup_int(file, "game.tech");
@@ -4554,8 +4556,8 @@
     secfile_insert_str(file, user_message, "game.metamessage");
   }
   secfile_insert_str(file, meta_addr_port(), "game.metaserver");
+  secfile_insert_str(file, srvarg.serverid, "game.serverid");
   
-  
   secfile_insert_int(file, game.info.gold, "game.gold");
   secfile_insert_int(file, game.info.tech, "game.tech");
   secfile_insert_int(file, game.info.skill_level, "game.skill_level");
Index: data/civserver.dsc.in
===================================================================
--- data/civserver.dsc.in	(revision 14950)
+++ data/civserver.dsc.in	(working copy)
@@ -22,6 +22,7 @@
 # Because of -q 180 -e options, above
 KillWhenEmpty = 0
 AllowSpectators = 0
+AllowRestore = 1
 
 [Statistics]
 Records = 1
Index: common/game.h
===================================================================
--- common/game.h	(revision 14950)
+++ common/game.h	(working copy)
@@ -319,7 +319,7 @@
 #define GAME_DEFAULT_RULESETDIR      "default"
 
 #define GAME_DEFAULT_SAVE_NAME       "civgame"
-#define GAME_DEFAULT_SAVETURNS       10
+#define GAME_DEFAULT_SAVETURNS       1
 
 #define GAME_DEFAULT_SKILL_LEVEL 3      /* easy */
 #define GAME_OLD_DEFAULT_SKILL_LEVEL 5  /* normal; for old save games */
_______________________________________________
Freeciv-dev mailing list
Freeciv-dev@gna.org
https://mail.gna.org/listinfo/freeciv-dev

Reply via email to