garten

diff clock.c @ 2:8369454d4ec9

with outsourced gamecheck and minor stuff
author meillo@marmaro.de
date Wed, 14 May 2008 21:08:25 +0200
parents 831599184108
children 176ee28e7464
line diff
     1.1 --- a/clock.c	Wed May 14 21:07:33 2008 +0200
     1.2 +++ b/clock.c	Wed May 14 21:08:25 2008 +0200
     1.3 @@ -3,42 +3,29 @@
     1.4  #include <mysql.h>
     1.5  
     1.6  #include "db.h"
     1.7 +#include "game.h"
     1.8  
     1.9 -char query[512];
    1.10  
    1.11  
    1.12 -int check_game(char* gamename) {
    1.13 -	int rows;
    1.14 -
    1.15 -	sprintf(query, "select id from simulation where name = '%s' ", gamename);
    1.16 -	db_query(query);
    1.17 -	result = mysql_store_result(conn);
    1.18 -	rows = mysql_num_rows(result);
    1.19 -	mysql_free_result(result);
    1.20 -	return rows;
    1.21 -}
    1.22 -
    1.23 -
    1.24 -void inc_time(char* gamename) {
    1.25 -	int time = 0;
    1.26 +void inc_time() {
    1.27  
    1.28  	/* get current time */
    1.29 -	sprintf(query, "select time from simulation where name = '%s' ", gamename);
    1.30 +	sprintf(query, "select time from game where name = '%s' ", gamename);
    1.31  	db_query(query);
    1.32  	result = mysql_store_result(conn);
    1.33  	if (mysql_num_rows(result)) {
    1.34  		row = mysql_fetch_row(result);
    1.35 -		time = atoi(row[0]);
    1.36 +		gametime = atoi(row[0]);
    1.37  	}
    1.38  	mysql_free_result(result);
    1.39  
    1.40  
    1.41  	/* increment time */
    1.42 -	sprintf(query, " update simulation set time = '%d' where name = '%s' ", ++time, gamename);
    1.43 +	sprintf(query, " update game set time = '%d' where name = '%s' ", ++gametime, gamename);
    1.44  	db_query(query);
    1.45  	if (mysql_affected_rows(conn) > 0) {
    1.46  		printf("time update successful\n");
    1.47 -		printf("simulation time: %d\n", time);
    1.48 +		printf("virtual time: %d\n", gametime);
    1.49  	} else {
    1.50  		printf("E: time update failed\n");
    1.51  	}
    1.52 @@ -47,8 +34,7 @@
    1.53  
    1.54  
    1.55  int main(int argc, char* argv[]) {
    1.56 -	int i;
    1.57 -	char* gamename;
    1.58 +	printf("  --> clock\n");
    1.59  
    1.60  	/* init */
    1.61  	if (argc != 2) {
    1.62 @@ -57,16 +43,10 @@
    1.63  	}
    1.64  	gamename = argv[1];
    1.65  
    1.66 -	printf("gamename: %s\n", gamename);
    1.67 +	db_connect();
    1.68 +	check_game();
    1.69  
    1.70 -	db_connect();
    1.71 -
    1.72 -	if (check_game(gamename) != 1) {
    1.73 -		printf("game '%s' does not exist\n", gamename);
    1.74 -		exit(1);
    1.75 -	}
    1.76 -
    1.77 -	inc_time(gamename);
    1.78 +	inc_time();
    1.79  
    1.80  /*
    1.81  	while ((row = mysql_fetch_row(result)) != NULL) {
    1.82 @@ -82,6 +62,6 @@
    1.83  
    1.84  	db_close();
    1.85  
    1.86 -	printf("the garten program\n");
    1.87 +	printf("  --< clock\n");
    1.88  	return 0;
    1.89  }