garten

diff clock.c @ 0:831599184108

inital commit can increment the time in the database
author meillo@marmaro.de
date Mon, 12 May 2008 21:24:20 +0200
parents
children 8369454d4ec9
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/clock.c	Mon May 12 21:24:20 2008 +0200
     1.3 @@ -0,0 +1,87 @@
     1.4 +#include <stdio.h>
     1.5 +#include <stdlib.h>
     1.6 +#include <mysql.h>
     1.7 +
     1.8 +#include "db.h"
     1.9 +
    1.10 +char query[512];
    1.11 +
    1.12 +
    1.13 +int check_game(char* gamename) {
    1.14 +	int rows;
    1.15 +
    1.16 +	sprintf(query, "select id from simulation where name = '%s' ", gamename);
    1.17 +	db_query(query);
    1.18 +	result = mysql_store_result(conn);
    1.19 +	rows = mysql_num_rows(result);
    1.20 +	mysql_free_result(result);
    1.21 +	return rows;
    1.22 +}
    1.23 +
    1.24 +
    1.25 +void inc_time(char* gamename) {
    1.26 +	int time = 0;
    1.27 +
    1.28 +	/* get current time */
    1.29 +	sprintf(query, "select time from simulation where name = '%s' ", gamename);
    1.30 +	db_query(query);
    1.31 +	result = mysql_store_result(conn);
    1.32 +	if (mysql_num_rows(result)) {
    1.33 +		row = mysql_fetch_row(result);
    1.34 +		time = atoi(row[0]);
    1.35 +	}
    1.36 +	mysql_free_result(result);
    1.37 +
    1.38 +
    1.39 +	/* increment time */
    1.40 +	sprintf(query, " update simulation set time = '%d' where name = '%s' ", ++time, gamename);
    1.41 +	db_query(query);
    1.42 +	if (mysql_affected_rows(conn) > 0) {
    1.43 +		printf("time update successful\n");
    1.44 +		printf("simulation time: %d\n", time);
    1.45 +	} else {
    1.46 +		printf("E: time update failed\n");
    1.47 +	}
    1.48 +
    1.49 +}
    1.50 +
    1.51 +
    1.52 +int main(int argc, char* argv[]) {
    1.53 +	int i;
    1.54 +	char* gamename;
    1.55 +
    1.56 +	/* init */
    1.57 +	if (argc != 2) {
    1.58 +		printf("usage: %s <game>\n", argv[0]);
    1.59 +		exit(1);
    1.60 +	}
    1.61 +	gamename = argv[1];
    1.62 +
    1.63 +	printf("gamename: %s\n", gamename);
    1.64 +
    1.65 +	db_connect();
    1.66 +
    1.67 +	if (check_game(gamename) != 1) {
    1.68 +		printf("game '%s' does not exist\n", gamename);
    1.69 +		exit(1);
    1.70 +	}
    1.71 +
    1.72 +	inc_time(gamename);
    1.73 +
    1.74 +/*
    1.75 +	while ((row = mysql_fetch_row(result)) != NULL) {
    1.76 +		for (i = 0; i < mysql_num_fields(result); i++) {
    1.77 +			printf("%10s  ", row[i]);
    1.78 +		}
    1.79 +		printf("\n");
    1.80 +	}
    1.81 +	*/
    1.82 +
    1.83 +
    1.84 +
    1.85 +
    1.86 +	db_close();
    1.87 +
    1.88 +	printf("the garten program\n");
    1.89 +	return 0;
    1.90 +}