garten
diff growth.c @ 7:b1e309dc0b98
added growth module (quite static and limited implementation)
author | meillo@marmaro.de |
---|---|
date | Thu, 15 May 2008 21:42:01 +0200 |
parents | |
children | 176ee28e7464 |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/growth.c Thu May 15 21:42:01 2008 +0200 1.3 @@ -0,0 +1,71 @@ 1.4 +#include <stdio.h> 1.5 +#include <stdlib.h> 1.6 +#include <mysql.h> 1.7 + 1.8 +#include "db.h" 1.9 +#include "game.h" 1.10 + 1.11 + 1.12 + 1.13 + 1.14 +void grow_plants() { 1.15 + int i; 1.16 + unsigned char r[6]; 1.17 + 1.18 + /* get weather and last environments to calculate the next one */ 1.19 + sprintf(query, "select\ 1.20 + f.id, f.size, f.age, p.size, p.growspeed, p.age\ 1.21 + from field f\ 1.22 + join plant p on f.plant_id = p.id\ 1.23 + where f.game_id = '%d' ", 1.24 + gameid); 1.25 + db_query(query); 1.26 + result = mysql_store_result(conn); 1.27 + printf("number of plants to process: %d\n", (int)mysql_num_rows(result)); 1.28 + while ((row = mysql_fetch_row(result)) != NULL) { 1.29 + for (i = 0; i < 6; i++) { 1.30 + r[i] = atoi(row[i]); 1.31 + } 1.32 + 1.33 + sprintf(query, "update field set \ 1.34 + size = '%d', age = '%d' \ 1.35 + where id = '%d' ", 1.36 + (r[1] + 1), r[2]+1, r[0]); 1.37 + db_query(query); 1.38 + 1.39 + if (mysql_affected_rows(conn) > 0) { 1.40 + printf("update successful ++ %s\n", query); 1.41 + } else { 1.42 + printf("E: update failed ++ %s\n", query); 1.43 + } 1.44 + 1.45 + } 1.46 + mysql_free_result(result); 1.47 + 1.48 + 1.49 + 1.50 + 1.51 +} 1.52 + 1.53 + 1.54 +int main(int argc, char* argv[]) { 1.55 + printf(" --> %s\n", argv[0]); 1.56 + 1.57 + /* init */ 1.58 + if (argc != 2) { 1.59 + printf("usage: %s <game>\n", argv[0]); 1.60 + exit(1); 1.61 + } 1.62 + gamename = argv[1]; 1.63 + 1.64 + db_connect(); 1.65 + check_game(); 1.66 + 1.67 + grow_plants(); 1.68 + 1.69 + 1.70 + db_close(); 1.71 + 1.72 + printf(" --< %s\n", argv[0]); 1.73 + return 0; 1.74 +}