Add an interface for fetching the list of countries

This commit introduces the concept of Bygfoot backends.  The only
backend currently supported is the filesystem backend.  An example of
other backends that might be added include: server or database backend.

Creating an interface for fetching the country list will make it easier
to interface with multiple backends.
This commit is contained in:
Tom Stellard 2020-12-31 13:25:10 -08:00
parent 0e72704a73
commit bdd28eceb2
9 changed files with 41 additions and 14 deletions

View File

@ -1,5 +1,6 @@
#include "bygfoot.h"
#include "file.h"
#include "gui.h"
#include "misc.h"
#include "start_end.h"
@ -7,7 +8,8 @@
#include "xml_country.h"
void
bygfoot_init(Bygfoot *bygfoot, enum BygfootFrontend frontend)
bygfoot_init(Bygfoot *bygfoot, enum BygfootFrontend frontend,
enum BygfootBackend backend)
{
memset(bygfoot, 0, sizeof(*bygfoot));
switch(frontend) {
@ -15,6 +17,11 @@ bygfoot_init(Bygfoot *bygfoot, enum BygfootFrontend frontend)
bygfoot->show_progress = gui_show_progress;
break;
}
switch (backend) {
case BYGFOOT_BACKEND_FILESYSTEM:
bygfoot->get_country_list = file_get_country_files;
break;
}
}
Country *bygfoot_load_country(Bygfoot *bygfoot, const gchar *country_name)
@ -51,3 +58,8 @@ void bygfoot_show_progress(const Bygfoot *bygfoot, gfloat value, const gchar *te
if (bygfoot->show_progress)
bygfoot->show_progress(value, text, pictype);
}
GPtrArray *bygfoot_get_country_list(const Bygfoot *bygfoot)
{
return bygfoot->get_country_list(bygfoot);
}

View File

@ -210,9 +210,11 @@ typedef struct {
} CommandLineArgs;
void bygfoot_init(Bygfoot *bygfoot, enum BygfootFrontend frontend);
void bygfoot_init(Bygfoot *bygfoot, enum BygfootFrontend frontend,
enum BygfootBackend backend);
Country *bygfoot_load_country(Bygfoot *bygfoot, const gchar *country_name);
User *bygfoot_add_user(Bygfoot *bygfoot, const gchar *username, Team *tm);
void bygfoot_start_game(Bygfoot *bygfoot);
void bygfoot_show_progress(const Bygfoot *bygfoot, gfloat value, const gchar *text, gint pictype);
#endif
GPtrArray *bygfoot_get_country_list(const Bygfoot *bygfoot);

View File

@ -5,10 +5,14 @@ enum BygfootFrontend {
BYGFOOT_FRONTEND_GTK2
};
enum BygfootBackend {
BYGFOOT_BACKEND_FILESYSTEM
};
/** This struct holds all of the global state for a bygfoot game. The goal
* is for ths struct to eventually replace all the global variables.
*/
typedef struct
typedef struct bygfoot
{
gchar *id;
@ -16,6 +20,15 @@ typedef struct
/* @{ */
void (*show_progress)(gfloat, const gchar *, gint);
/* @} */
/** @name Backend functions */
/* @{ */
/* This function returns an array of gchar* that describe the countries
* a user can choose from. The string format for the countries is
* <Continent>/<...>/<country>
*/
GPtrArray *(*get_country_list)(const struct bygfoot *);
/* @} */
} Bygfoot;
#endif

View File

@ -484,7 +484,7 @@ file_dir_get_contents(const gchar *dir_name, const gchar *prefix, const gchar *s
/** Return the country definition files found in the support dirs.
* The files are appended with the directories*/
GPtrArray*
file_get_country_files(void)
file_get_country_files(const Bygfoot *bygfoot)
{
#ifdef DEBUG
printf("file_get_country_files\n");

View File

@ -60,7 +60,7 @@ gboolean
file_my_fopen(const gchar *filename, gchar *bits, FILE **fil, gboolean abort_program);
GPtrArray*
file_get_country_files(void);
file_get_country_files(const Bygfoot *bygfoot);
void
file_load_conf_files(void);

View File

@ -71,19 +71,19 @@ job_update(Bygfoot *bygfoot)
for(i=0;i<new_offers - int_offers;i++)
job_add_new_national();
job_add_new_international(int_offers);
job_add_new_international(bygfoot, int_offers);
}
/** Add some new international job offers to the job exchange. */
void
job_add_new_international(gint num_of_new)
job_add_new_international(const Bygfoot *bygfoot, gint num_of_new)
{
#ifdef DEBUG
printf("job_add_new_international\n");
#endif
gint i, k, rndom, idx;
GPtrArray *country_files = file_get_country_files();
GPtrArray *country_files = bygfoot_get_country_list(bygfoot);
Country countries[num_of_new];
Team *tm = NULL;
League *league = NULL;

View File

@ -36,7 +36,7 @@ void
job_update(Bygfoot *bygfoot);
void
job_add_new_international(gint num_of_new);
job_add_new_international(const Bygfoot *bygfoot, gint num_of_new);
gint
job_country_is_in_list(const gchar *country_file,

View File

@ -338,9 +338,9 @@ static void validate_country_file(gpointer country_file, gpointer user_data)
xml_country_read(country_file, &country);
}
static void validate_country_files()
static void validate_country_files(const Bygfoot *bygfoot)
{
GPtrArray *country_files = file_get_country_files();
GPtrArray *country_files = file_get_country_files(bygfoot);
if(country_files->len == 0)
main_exit_program(EXIT_NO_COUNTRY_FILES,
@ -426,10 +426,10 @@ main (gint argc, gchar *argv[])
int fd2 = open ("stderr.log", O_CREAT|O_WRONLY|O_TRUNC, 0666);
dup2 (fd2, 2);
#endif
bygfoot_init(&bygfoot, BYGFOOT_FRONTEND_GTK2);
bygfoot_init(&bygfoot, BYGFOOT_FRONTEND_GTK2, BYGFOOT_BACKEND_FILESYSTEM);
main_init(&argc, &argv);
validate_country_files();
validate_country_files(&bygfoot);
gtk_init (&argc, &argv);

View File

@ -304,7 +304,7 @@ window_show_startup(Bygfoot *bygfoot)
GtkCellRenderer *renderer = NULL;
gchar *last_country = file_load_text_from_saves("last_country");
country_files = file_get_country_files();
country_files = bygfoot_get_country_list(bygfoot);
if(country_files->len == 0)
main_exit_program(EXIT_NO_COUNTRY_FILES,