garten

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 diff
     1.1 --- a/Makefile	Wed Jul 23 17:14:38 2008 +0200
     1.2 +++ b/Makefile	Wed Jul 23 17:40:55 2008 +0200
     1.3 @@ -21,7 +21,7 @@
     1.4  LDFLAGS = -Wall ${DEBUG} -lsqlite3
     1.5  
     1.6  # files
     1.7 -DEP = db.h game.h
     1.8 +DEP = db.h
     1.9  SRC = main.c clock.c environment.c weather.c growth.c db.c
    1.10  OBJ = $(SRC:.c=.o)
    1.11  
    1.12 @@ -45,6 +45,8 @@
    1.13  
    1.14  $(OBJ): $(SRC) $(DEP)
    1.15  
    1.16 +main.o: main.c $(DEP) modules.h
    1.17 +
    1.18  ${NAME}: $(DEP) $(OBJ)
    1.19  	gcc -o $(NAME) $(LDFLAGS) $(OBJ)
    1.20  
     2.1 --- a/clock.c	Wed Jul 23 17:14:38 2008 +0200
     2.2 +++ b/clock.c	Wed Jul 23 17:40:55 2008 +0200
     2.3 @@ -1,10 +1,9 @@
     2.4  #include <stdio.h>
     2.5  #include <stdlib.h>
     2.6  #include "db.h"
     2.7 -#include "game.h"
     2.8  
     2.9  
    2.10 -void inc_time() {
    2.11 +int inc_time() {
    2.12  	/* get current time */
    2.13  	sprintf(query, "select time from game;");
    2.14  	stmt = db_query(query);
    2.15 @@ -12,22 +11,29 @@
    2.16  		gametime = sqlite3_column_int(stmt, 0);
    2.17  	} else {
    2.18  		db_error("get current time");
    2.19 +		return 0;
    2.20  	}
    2.21  	sqlite3_finalize(stmt);
    2.22  
    2.23  	/* increment time */
    2.24  	sprintf(query,
    2.25 -			"update game"
    2.26 +			"update game "
    2.27  			"set time = '%d';"
    2.28  			, ++gametime
    2.29  			);
    2.30  	if (!db_update(query)) {
    2.31  		db_error("time update");
    2.32 +		return 0;
    2.33  	}
    2.34 +	return 1;
    2.35  }
    2.36  
    2.37  
    2.38 -void worldclock(void) {
    2.39 -	inc_time();
    2.40 +int worldclock(void) {
    2.41 +	int ret;
    2.42 +
    2.43 +	ret = inc_time();
    2.44  	printf("gametime: %d\n", gametime);
    2.45 +
    2.46 +	return ret;
    2.47  }
     3.1 --- a/db.c	Wed Jul 23 17:14:38 2008 +0200
     3.2 +++ b/db.c	Wed Jul 23 17:40:55 2008 +0200
     3.3 @@ -1,7 +1,6 @@
     3.4  #include <stdio.h>
     3.5  #include <stdlib.h>
     3.6  #include "db.h"
     3.7 -#include "game.h"
     3.8  
     3.9  
    3.10  
     4.1 --- a/environment.c	Wed Jul 23 17:14:38 2008 +0200
     4.2 +++ b/environment.c	Wed Jul 23 17:40:55 2008 +0200
     4.3 @@ -1,10 +1,9 @@
     4.4  #include <stdio.h>
     4.5  #include <stdlib.h>
     4.6  #include "db.h"
     4.7 -#include "game.h"
     4.8  
     4.9  
    4.10 -void set_environment() {
    4.11 +int set_environment() {
    4.12  	int groundwater, slugs, earthworms;
    4.13  
    4.14  	/* get weather and last environments to calculate the next one *
    4.15 @@ -22,7 +21,6 @@
    4.16  	slugs = 5;
    4.17  	earthworms = 10;
    4.18  
    4.19 -
    4.20  	/* set weather */
    4.21  	sprintf(query,
    4.22  			"insert into environment "
    4.23 @@ -35,11 +33,12 @@
    4.24  			);
    4.25  	if (!db_update(query)) {
    4.26  		db_error("environment insertion");
    4.27 +		return 0;
    4.28  	}
    4.29 -
    4.30 +	return 1;
    4.31  }
    4.32  
    4.33  
    4.34 -void environment(void) {
    4.35 -	set_environment();
    4.36 +int environment(void) {
    4.37 +	return set_environment();
    4.38  }
     5.1 --- a/game.h	Wed Jul 23 17:14:38 2008 +0200
     5.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.3 @@ -1,5 +0,0 @@
     5.4 -void worldclock(void);
     5.5 -void environment(void);
     5.6 -void weather(void);
     5.7 -void growth(void);
     5.8 -
     6.1 --- a/growth.c	Wed Jul 23 17:14:38 2008 +0200
     6.2 +++ b/growth.c	Wed Jul 23 17:40:55 2008 +0200
     6.3 @@ -1,20 +1,21 @@
     6.4  #include <stdio.h>
     6.5  #include <stdlib.h>
     6.6  #include "db.h"
     6.7 -#include "game.h"
     6.8  
     6.9  
    6.10 -void grow_plants() {
    6.11 +int grow_plants() {
    6.12  	sprintf(query,
    6.13  			"update field "
    6.14  			"set age = age+1, size = size+1 "
    6.15  			);
    6.16  	if (!db_update(query)) {
    6.17  		db_error("grow plants");
    6.18 +		return 0;
    6.19  	}
    6.20 +	return 1;
    6.21  }
    6.22  
    6.23  
    6.24 -void growth(void) {
    6.25 -	grow_plants();
    6.26 +int growth(void) {
    6.27 +	return grow_plants();
    6.28  }
     7.1 --- a/main.c	Wed Jul 23 17:14:38 2008 +0200
     7.2 +++ b/main.c	Wed Jul 23 17:40:55 2008 +0200
     7.3 @@ -1,12 +1,29 @@
     7.4  #include <stdio.h>
     7.5  #include <stdlib.h>
     7.6  #include "db.h"
     7.7 -#include "game.h"
     7.8 +#include "modules.h"
     7.9  
    7.10  
    7.11 -void run_module(char* name, void (*module)(void)) {
    7.12 +void init(void) {
    7.13 +	/* echo "started backend cycle at `date +%F\ %H:%M:%S`" */
    7.14 +	db_connect();
    7.15 +	read_time();
    7.16 +}
    7.17 +
    7.18 +
    7.19 +void cleanup(void) {
    7.20 +	db_close();
    7.21 +	/* echo "finished backend cycle at `date +%F\ %H:%M:%S`" */
    7.22 +}
    7.23 +
    7.24 +
    7.25 +void run_module(char* name, int (*module)(void)) {
    7.26  	printf("  --> %s\n", name);
    7.27 -	module();
    7.28 +	if (!module()) {
    7.29 +		fprintf(stderr, "failure in module '%s'\n", name);
    7.30 +		cleanup();
    7.31 +		exit(2);
    7.32 +	}
    7.33  	printf("  --< %s\n", name);
    7.34  }
    7.35  
    7.36 @@ -18,11 +35,7 @@
    7.37  	}
    7.38  	database = argv[1];
    7.39  	printf("database '%s'\n", database);
    7.40 -
    7.41 -	/* echo "started backend cycle at `date +%F\ %H:%M:%S`" */
    7.42 -	db_connect();
    7.43 -	read_time();
    7.44 -
    7.45 +	init();
    7.46  
    7.47  	/* modules */
    7.48  	run_module("clock", worldclock);
    7.49 @@ -32,9 +45,6 @@
    7.50  	run_module("growth", growth);
    7.51  	/* run_module("orderexec", orderexec); */
    7.52  
    7.53 -
    7.54 -	db_close();
    7.55 -	/* echo "finished backend cycle at `date +%F\ %H:%M:%S`" */
    7.56 -
    7.57 +	cleanup();
    7.58  	return 0;
    7.59  }
     8.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     8.2 +++ b/modules.h	Wed Jul 23 17:40:55 2008 +0200
     8.3 @@ -0,0 +1,4 @@
     8.4 +int worldclock(void);
     8.5 +int environment(void);
     8.6 +int weather(void);
     8.7 +int growth(void);
     9.1 --- a/weather.c	Wed Jul 23 17:14:38 2008 +0200
     9.2 +++ b/weather.c	Wed Jul 23 17:40:55 2008 +0200
     9.3 @@ -2,7 +2,6 @@
     9.4  #include <stdlib.h>
     9.5  #include <time.h>
     9.6  #include "db.h"
     9.7 -#include "game.h"
     9.8  
     9.9  
    9.10  enum {
    9.11 @@ -68,7 +67,7 @@
    9.12  }
    9.13  
    9.14  
    9.15 -void setweather(struct weather* w) {
    9.16 +int setweather(struct weather* w) {
    9.17  	char query[512];
    9.18  	sprintf(query,
    9.19  			" insert into weather "
    9.20 @@ -78,15 +77,18 @@
    9.21  			);
    9.22  	if (!db_update(query)) {
    9.23  		db_error("weather insertion");
    9.24 +		return 0;
    9.25  	}
    9.26 +	return 1;
    9.27  }
    9.28  
    9.29  
    9.30 -void weather(void) {
    9.31 +int weather(void) {
    9.32  	struct weather w;
    9.33  	struct weather lastn[Nlast];
    9.34  
    9.35  	getlastweather(lastn, Nlast);
    9.36  	genweather(&w, lastn, Nlast, May);
    9.37 -	setweather(&w);
    9.38 +	return setweather(&w);
    9.39 +	/* FIXME: include all functions in return value */
    9.40  }