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

GGZ savegame support is incomplete but this patch adds it.

Basically there's two things to do: report games that we save to ggz, 
and handle ggz savedgame events that tell us to load games.

Along the way I gave each game a unique directory within the ggz freeciv 
directory.  This follows the serverid system as I remember if from 
pubserver.  The serverid is also saved in the savegame.

The portability of mkdtemp is a concern (a replacement would be easily 
written).  It also lacks some documentation (general explanations).

-jason

Index: configure.ac
===================================================================
--- configure.ac	(revision 14862)
+++ configure.ac	(working copy)
@@ -532,6 +532,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 14862)
+++ 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 14862)
+++ server/ggzserver.c	(working copy)
@@ -17,6 +17,8 @@
 
 #ifdef GGZ_SERVER
 
+#include <unistd.h>
+
 #include <ggzdmod.h>
 
 #include "fciconv.h"
@@ -31,6 +33,8 @@
 #include "ggzserver.h"
 #include "score.h"
 #include "sernet.h"
+#include "srv_main.h"
+#include "stdinhand.h"
 
 bool with_ggz = FALSE;
 
@@ -173,6 +177,24 @@
 }
 
 /****************************************************************************
+  When a saved game is to be loaded, this event is activateed.
+****************************************************************************/
+static void handle_ggz_savedgame(GGZdMod *ggz, GGZdModEvent event,
+				 const void *data)
+{
+  if (!load_command(NULL, data, FALSE)) {
+    /* no error handling? */
+    server_quit();
+  }
+
+  if (chdir(srvarg.serverid) < 0) {
+    fc_fprintf(stderr, _("Unable to change into temporary server "
+			 "directory %s.\n"), srvarg.serverid);
+    server_quit();
+  }
+}
+
+/****************************************************************************
   Connect to the GGZ server, if GGZ is being used.
 ****************************************************************************/
 void ggz_initialize(void)
@@ -199,6 +221,12 @@
 			&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);
+    ggzdmod_set_handler(ggzdmod, GGZDMOD_EVENT_SAVEDGAME,
+			&handle_ggz_savedgame);
     if (ggzdmod_connect(ggzdmod) < 0) {
       exit(EXIT_FAILURE);
     }
@@ -208,6 +236,19 @@
 			   " in ggz mode!\n"));
       exit(EXIT_FAILURE);
     }
+
+    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);
+    }
+
+    if (chdir(srvarg.serverid) < 0) {
+      fc_fprintf(stderr, _("Unable to change into temporary server "
+			   "directory %s.\n"), srvarg.serverid);
+      exit(EXIT_FAILURE);
+    }
   }
 }
 
@@ -332,4 +373,13 @@
   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)
+{
+  ggzdmod_report_savegame(ggzdmod, filename);
+}
+
 #endif
Index: server/ggzserver.h
===================================================================
--- server/ggzserver.h	(revision 14862)
+++ 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() (void)0
 
 #endif
 
Index: server/stdinhand.c
===================================================================
--- server/stdinhand.c	(revision 14863)
+++ 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 14862)
+++ 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 14862)
+++ server/savegame.c	(working copy)
@@ -3840,6 +3840,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");
@@ -4551,8 +4553,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");
_______________________________________________
Freeciv-dev mailing list
Freeciv-dev@gna.org
https://mail.gna.org/listinfo/freeciv-dev

Reply via email to