garten

annotate growth.c @ 10:13c6828bd4a5

switched from mysql to sqlite; (+ some cleanups)
author meillo@marmaro.de
date Wed, 23 Jul 2008 11:40:45 +0200
parents
children 176ee28e7464
rev   line source
meillo@7 1 #include <stdio.h>
meillo@7 2 #include <stdlib.h>
meillo@7 3 #include <mysql.h>
meillo@7 4
meillo@7 5 #include "db.h"
meillo@7 6 #include "game.h"
meillo@7 7
meillo@7 8
meillo@7 9
meillo@7 10
meillo@7 11 void grow_plants() {
meillo@7 12 int i;
meillo@7 13 unsigned char r[6];
meillo@7 14
meillo@7 15 /* get weather and last environments to calculate the next one */
meillo@7 16 sprintf(query, "select\
meillo@7 17 f.id, f.size, f.age, p.size, p.growspeed, p.age\
meillo@7 18 from field f\
meillo@7 19 join plant p on f.plant_id = p.id\
meillo@7 20 where f.game_id = '%d' ",
meillo@7 21 gameid);
meillo@7 22 db_query(query);
meillo@7 23 result = mysql_store_result(conn);
meillo@7 24 printf("number of plants to process: %d\n", (int)mysql_num_rows(result));
meillo@7 25 while ((row = mysql_fetch_row(result)) != NULL) {
meillo@7 26 for (i = 0; i < 6; i++) {
meillo@7 27 r[i] = atoi(row[i]);
meillo@7 28 }
meillo@7 29
meillo@7 30 sprintf(query, "update field set \
meillo@7 31 size = '%d', age = '%d' \
meillo@7 32 where id = '%d' ",
meillo@7 33 (r[1] + 1), r[2]+1, r[0]);
meillo@7 34 db_query(query);
meillo@7 35
meillo@7 36 if (mysql_affected_rows(conn) > 0) {
meillo@7 37 printf("update successful ++ %s\n", query);
meillo@7 38 } else {
meillo@7 39 printf("E: update failed ++ %s\n", query);
meillo@7 40 }
meillo@7 41
meillo@7 42 }
meillo@7 43 mysql_free_result(result);
meillo@7 44
meillo@7 45
meillo@7 46
meillo@7 47
meillo@7 48 }
meillo@7 49
meillo@7 50
meillo@7 51 int main(int argc, char* argv[]) {
meillo@7 52 printf(" --> %s\n", argv[0]);
meillo@7 53
meillo@7 54 /* init */
meillo@7 55 if (argc != 2) {
meillo@7 56 printf("usage: %s <game>\n", argv[0]);
meillo@7 57 exit(1);
meillo@7 58 }
meillo@7 59 gamename = argv[1];
meillo@7 60
meillo@7 61 db_connect();
meillo@7 62 check_game();
meillo@7 63
meillo@7 64 grow_plants();
meillo@7 65
meillo@7 66
meillo@7 67 db_close();
meillo@7 68
meillo@7 69 printf(" --< %s\n", argv[0]);
meillo@7 70 return 0;
meillo@7 71 }