changeset 18:5937504619f2

rename game.h -> modules.h; added some error handling; and more
author meillo@marmaro.de
date Wed, 23 Jul 2008 17:40:55 +0200
parents 5e6c9260913a
children eb8db0d906de
files Makefile clock.c db.c environment.c game.h growth.c main.c modules.h weather.c
diffstat 9 files changed, 56 insertions(+), 38 deletions(-) [+]
line wrap: on
line diff
--- 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)
 
--- 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 <stdio.h>
 #include <stdlib.h>
 #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;
 }
--- 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 <stdio.h>
 #include <stdlib.h>
 #include "db.h"
-#include "game.h"
 
 
 
--- 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 <stdio.h>
 #include <stdlib.h>
 #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();
 }
--- 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);
-
--- 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 <stdio.h>
 #include <stdlib.h>
 #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();
 }
--- 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 <stdio.h>
 #include <stdlib.h>
 #include "db.h"
-#include "game.h"
+#include "modules.h"
+
+
+void init(void) {
+	/* echo "started backend cycle at `date +%F\ %H:%M:%S`" */
+	db_connect();
+	read_time();
+}
 
 
-void run_module(char* name, void (*module)(void)) {
+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;
 }
--- /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);
--- 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 <stdlib.h>
 #include <time.h>
 #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 */
 }