# HG changeset patch # User meillo@marmaro.de # Date 1216827655 -7200 # Node ID 5937504619f26398fe3939cf6df5e9015ed293cc # Parent 5e6c9260913a2840df6f082d131dd3f7ffebc6c9 rename game.h -> modules.h; added some error handling; and more diff -r 5e6c9260913a -r 5937504619f2 Makefile --- a/Makefile Wed Jul 23 17:14:38 2008 +0200 +++ b/Makefile Wed Jul 23 17:40:55 2008 +0200 @@ -21,7 +21,7 @@ LDFLAGS = -Wall ${DEBUG} -lsqlite3 # files -DEP = db.h game.h +DEP = db.h SRC = main.c clock.c environment.c weather.c growth.c db.c OBJ = $(SRC:.c=.o) @@ -45,6 +45,8 @@ $(OBJ): $(SRC) $(DEP) +main.o: main.c $(DEP) modules.h + ${NAME}: $(DEP) $(OBJ) gcc -o $(NAME) $(LDFLAGS) $(OBJ) diff -r 5e6c9260913a -r 5937504619f2 clock.c --- a/clock.c Wed Jul 23 17:14:38 2008 +0200 +++ b/clock.c Wed Jul 23 17:40:55 2008 +0200 @@ -1,10 +1,9 @@ #include #include #include "db.h" -#include "game.h" -void inc_time() { +int inc_time() { /* get current time */ sprintf(query, "select time from game;"); stmt = db_query(query); @@ -12,22 +11,29 @@ gametime = sqlite3_column_int(stmt, 0); } else { db_error("get current time"); + return 0; } sqlite3_finalize(stmt); /* increment time */ sprintf(query, - "update game" + "update game " "set time = '%d';" , ++gametime ); if (!db_update(query)) { db_error("time update"); + return 0; } + return 1; } -void worldclock(void) { - inc_time(); +int worldclock(void) { + int ret; + + ret = inc_time(); printf("gametime: %d\n", gametime); + + return ret; } diff -r 5e6c9260913a -r 5937504619f2 db.c --- a/db.c Wed Jul 23 17:14:38 2008 +0200 +++ b/db.c Wed Jul 23 17:40:55 2008 +0200 @@ -1,7 +1,6 @@ #include #include #include "db.h" -#include "game.h" diff -r 5e6c9260913a -r 5937504619f2 environment.c --- a/environment.c Wed Jul 23 17:14:38 2008 +0200 +++ b/environment.c Wed Jul 23 17:40:55 2008 +0200 @@ -1,10 +1,9 @@ #include #include #include "db.h" -#include "game.h" -void set_environment() { +int set_environment() { int groundwater, slugs, earthworms; /* get weather and last environments to calculate the next one * @@ -22,7 +21,6 @@ slugs = 5; earthworms = 10; - /* set weather */ sprintf(query, "insert into environment " @@ -35,11 +33,12 @@ ); if (!db_update(query)) { db_error("environment insertion"); + return 0; } - + return 1; } -void environment(void) { - set_environment(); +int environment(void) { + return set_environment(); } diff -r 5e6c9260913a -r 5937504619f2 game.h --- a/game.h Wed Jul 23 17:14:38 2008 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,5 +0,0 @@ -void worldclock(void); -void environment(void); -void weather(void); -void growth(void); - diff -r 5e6c9260913a -r 5937504619f2 growth.c --- a/growth.c Wed Jul 23 17:14:38 2008 +0200 +++ b/growth.c Wed Jul 23 17:40:55 2008 +0200 @@ -1,20 +1,21 @@ #include #include #include "db.h" -#include "game.h" -void grow_plants() { +int grow_plants() { sprintf(query, "update field " "set age = age+1, size = size+1 " ); if (!db_update(query)) { db_error("grow plants"); + return 0; } + return 1; } -void growth(void) { - grow_plants(); +int growth(void) { + return grow_plants(); } diff -r 5e6c9260913a -r 5937504619f2 main.c --- a/main.c Wed Jul 23 17:14:38 2008 +0200 +++ b/main.c Wed Jul 23 17:40:55 2008 +0200 @@ -1,12 +1,29 @@ #include #include #include "db.h" -#include "game.h" +#include "modules.h" -void run_module(char* name, void (*module)(void)) { +void init(void) { + /* echo "started backend cycle at `date +%F\ %H:%M:%S`" */ + db_connect(); + read_time(); +} + + +void cleanup(void) { + db_close(); + /* echo "finished backend cycle at `date +%F\ %H:%M:%S`" */ +} + + +void run_module(char* name, int (*module)(void)) { printf(" --> %s\n", name); - module(); + if (!module()) { + fprintf(stderr, "failure in module '%s'\n", name); + cleanup(); + exit(2); + } printf(" --< %s\n", name); } @@ -18,11 +35,7 @@ } database = argv[1]; printf("database '%s'\n", database); - - /* echo "started backend cycle at `date +%F\ %H:%M:%S`" */ - db_connect(); - read_time(); - + init(); /* modules */ run_module("clock", worldclock); @@ -32,9 +45,6 @@ run_module("growth", growth); /* run_module("orderexec", orderexec); */ - - db_close(); - /* echo "finished backend cycle at `date +%F\ %H:%M:%S`" */ - + cleanup(); return 0; } diff -r 5e6c9260913a -r 5937504619f2 modules.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/modules.h Wed Jul 23 17:40:55 2008 +0200 @@ -0,0 +1,4 @@ +int worldclock(void); +int environment(void); +int weather(void); +int growth(void); diff -r 5e6c9260913a -r 5937504619f2 weather.c --- a/weather.c Wed Jul 23 17:14:38 2008 +0200 +++ b/weather.c Wed Jul 23 17:40:55 2008 +0200 @@ -2,7 +2,6 @@ #include #include #include "db.h" -#include "game.h" enum { @@ -68,7 +67,7 @@ } -void setweather(struct weather* w) { +int setweather(struct weather* w) { char query[512]; sprintf(query, " insert into weather " @@ -78,15 +77,18 @@ ); if (!db_update(query)) { db_error("weather insertion"); + return 0; } + return 1; } -void weather(void) { +int weather(void) { struct weather w; struct weather lastn[Nlast]; getlastweather(lastn, Nlast); genweather(&w, lastn, Nlast, May); - setweather(&w); + return setweather(&w); + /* FIXME: include all functions in return value */ }