garten

changeset 7:b1e309dc0b98

added growth module (quite static and limited implementation)
author meillo@marmaro.de
date Thu, 15 May 2008 21:42:01 +0200
parents e3679f888813
children 9bd0a2100694
files Makefile growth.c runtick.sh
diffstat 3 files changed, 76 insertions(+), 2 deletions(-) [+]
line diff
     1.1 --- a/Makefile	Wed May 14 21:59:35 2008 +0200
     1.2 +++ b/Makefile	Thu May 15 21:42:01 2008 +0200
     1.3 @@ -49,8 +49,11 @@
     1.4  bin/environment: environment.c db.h game.h
     1.5  	$(CC) -o $@ $(LDFLAGS) `mysql_config --cflags` environment.c `mysql_config --libs`
     1.6  
     1.7 +bin/growth: growth.c db.h game.h
     1.8 +	$(CC) -o $@ $(LDFLAGS) `mysql_config --cflags` growth.c `mysql_config --libs`
     1.9  
    1.10 -${NAME}: bin/clock bin/weather bin/environment
    1.11 +
    1.12 +${NAME}: bin/clock bin/weather bin/environment bin/growth
    1.13  
    1.14  
    1.15  #${OBJ}: ${DEP}
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/growth.c	Thu May 15 21:42:01 2008 +0200
     2.3 @@ -0,0 +1,71 @@
     2.4 +#include <stdio.h>
     2.5 +#include <stdlib.h>
     2.6 +#include <mysql.h>
     2.7 +
     2.8 +#include "db.h"
     2.9 +#include "game.h"
    2.10 +
    2.11 +
    2.12 +
    2.13 +
    2.14 +void grow_plants() {
    2.15 +	int i;
    2.16 +	unsigned char r[6];
    2.17 +
    2.18 +	/* get weather and last environments to calculate the next one */
    2.19 +	sprintf(query, "select\
    2.20 +			f.id, f.size, f.age, p.size, p.growspeed, p.age\
    2.21 +			from field f\
    2.22 +			join plant p on f.plant_id = p.id\
    2.23 +			where f.game_id = '%d' ",
    2.24 +			gameid);
    2.25 +	db_query(query);
    2.26 +	result = mysql_store_result(conn);
    2.27 +	printf("number of plants to process: %d\n", (int)mysql_num_rows(result));
    2.28 +	while ((row = mysql_fetch_row(result)) != NULL) {
    2.29 +		for (i = 0; i < 6; i++) {
    2.30 +			r[i] = atoi(row[i]);
    2.31 +		}
    2.32 +
    2.33 +		sprintf(query, "update field set \
    2.34 +				size = '%d', age = '%d' \
    2.35 +				where id = '%d' ",
    2.36 +				(r[1] + 1), r[2]+1, r[0]);
    2.37 +		db_query(query);
    2.38 +
    2.39 +		if (mysql_affected_rows(conn) > 0) {
    2.40 +			printf("update successful  ++  %s\n", query);
    2.41 +		} else {
    2.42 +			printf("E: update failed  ++  %s\n", query);
    2.43 +		}
    2.44 +
    2.45 +	}
    2.46 +	mysql_free_result(result);
    2.47 +
    2.48 +
    2.49 +
    2.50 +
    2.51 +}
    2.52 +
    2.53 +
    2.54 +int main(int argc, char* argv[]) {
    2.55 +	printf("  --> %s\n", argv[0]);
    2.56 +
    2.57 +	/* init */
    2.58 +	if (argc != 2) {
    2.59 +		printf("usage: %s <game>\n", argv[0]);
    2.60 +		exit(1);
    2.61 +	}
    2.62 +	gamename = argv[1];
    2.63 +
    2.64 +	db_connect();
    2.65 +	check_game();
    2.66 +
    2.67 +	grow_plants();
    2.68 +
    2.69 +
    2.70 +	db_close();
    2.71 +
    2.72 +	printf("  --< %s\n", argv[0]);
    2.73 +	return 0;
    2.74 +}
     3.1 --- a/runtick.sh	Wed May 14 21:59:35 2008 +0200
     3.2 +++ b/runtick.sh	Thu May 15 21:42:01 2008 +0200
     3.3 @@ -17,7 +17,7 @@
     3.4  ./bin/weather $gamename
     3.5  ./bin/environment $gamename
     3.6  #./bin/market $gamename
     3.7 -#./bin/growth $gamename
     3.8 +./bin/growth $gamename
     3.9  #./bin/orderexec $gamename
    3.10  
    3.11  echo "finished backend cycle at `date +%F\ %H:%M:%S`"