garten

changeset 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 3ff1fbe4693d
files .hgignore clock.c db.h runtick.sh
diffstat 4 files changed, 160 insertions(+), 0 deletions(-) [+]
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/.hgignore	Mon May 12 21:24:20 2008 +0200
     1.3 @@ -0,0 +1,10 @@
     1.4 +syntax: glob
     1.5 +*~
     1.6 +*.swp
     1.7 +
     1.8 +*.o
     1.9 +test
    1.10 +bin
    1.11 +
    1.12 +*.tar.gz
    1.13 +ChangeLog
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/clock.c	Mon May 12 21:24:20 2008 +0200
     2.3 @@ -0,0 +1,87 @@
     2.4 +#include <stdio.h>
     2.5 +#include <stdlib.h>
     2.6 +#include <mysql.h>
     2.7 +
     2.8 +#include "db.h"
     2.9 +
    2.10 +char query[512];
    2.11 +
    2.12 +
    2.13 +int check_game(char* gamename) {
    2.14 +	int rows;
    2.15 +
    2.16 +	sprintf(query, "select id from simulation where name = '%s' ", gamename);
    2.17 +	db_query(query);
    2.18 +	result = mysql_store_result(conn);
    2.19 +	rows = mysql_num_rows(result);
    2.20 +	mysql_free_result(result);
    2.21 +	return rows;
    2.22 +}
    2.23 +
    2.24 +
    2.25 +void inc_time(char* gamename) {
    2.26 +	int time = 0;
    2.27 +
    2.28 +	/* get current time */
    2.29 +	sprintf(query, "select time from simulation where name = '%s' ", gamename);
    2.30 +	db_query(query);
    2.31 +	result = mysql_store_result(conn);
    2.32 +	if (mysql_num_rows(result)) {
    2.33 +		row = mysql_fetch_row(result);
    2.34 +		time = atoi(row[0]);
    2.35 +	}
    2.36 +	mysql_free_result(result);
    2.37 +
    2.38 +
    2.39 +	/* increment time */
    2.40 +	sprintf(query, " update simulation set time = '%d' where name = '%s' ", ++time, gamename);
    2.41 +	db_query(query);
    2.42 +	if (mysql_affected_rows(conn) > 0) {
    2.43 +		printf("time update successful\n");
    2.44 +		printf("simulation time: %d\n", time);
    2.45 +	} else {
    2.46 +		printf("E: time update failed\n");
    2.47 +	}
    2.48 +
    2.49 +}
    2.50 +
    2.51 +
    2.52 +int main(int argc, char* argv[]) {
    2.53 +	int i;
    2.54 +	char* gamename;
    2.55 +
    2.56 +	/* init */
    2.57 +	if (argc != 2) {
    2.58 +		printf("usage: %s <game>\n", argv[0]);
    2.59 +		exit(1);
    2.60 +	}
    2.61 +	gamename = argv[1];
    2.62 +
    2.63 +	printf("gamename: %s\n", gamename);
    2.64 +
    2.65 +	db_connect();
    2.66 +
    2.67 +	if (check_game(gamename) != 1) {
    2.68 +		printf("game '%s' does not exist\n", gamename);
    2.69 +		exit(1);
    2.70 +	}
    2.71 +
    2.72 +	inc_time(gamename);
    2.73 +
    2.74 +/*
    2.75 +	while ((row = mysql_fetch_row(result)) != NULL) {
    2.76 +		for (i = 0; i < mysql_num_fields(result); i++) {
    2.77 +			printf("%10s  ", row[i]);
    2.78 +		}
    2.79 +		printf("\n");
    2.80 +	}
    2.81 +	*/
    2.82 +
    2.83 +
    2.84 +
    2.85 +
    2.86 +	db_close();
    2.87 +
    2.88 +	printf("the garten program\n");
    2.89 +	return 0;
    2.90 +}
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/db.h	Mon May 12 21:24:20 2008 +0200
     3.3 @@ -0,0 +1,40 @@
     3.4 +/*
     3.5 + * data for the database connection
     3.6 + */
     3.7 +
     3.8 +char* server = "localhost";
     3.9 +char* user = "garten";
    3.10 +char* password = "gras";
    3.11 +char* database = "garten";
    3.12 +
    3.13 +MYSQL* conn;
    3.14 +MYSQL_RES* result;
    3.15 +MYSQL_ROW row;
    3.16 +
    3.17 +
    3.18 +void db_connect() {
    3.19 +	conn = mysql_init(NULL);
    3.20 +
    3.21 +	/* Connect to database */
    3.22 +	if (!mysql_real_connect(conn, server, user, password, database, 0, NULL, 0)) {
    3.23 +		fprintf(stderr, "%s\n", mysql_error(conn));
    3.24 +		exit(1);
    3.25 +	}
    3.26 +}
    3.27 +
    3.28 +
    3.29 +void db_close() {
    3.30 +	/* close connection */
    3.31 +	mysql_close(conn);
    3.32 +}
    3.33 +
    3.34 +
    3.35 +void db_query(char* query) {
    3.36 +	int error;
    3.37 +	/* send SQL query */
    3.38 +	error = mysql_query(conn, query);
    3.39 +	printf("query: %s\nerror: %d / %d\n", query, error, mysql_errno(conn));
    3.40 +	if (error != 0) {
    3.41 +		fprintf(stderr, "E: %s\n", mysql_error(conn));
    3.42 +	}
    3.43 +}
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/runtick.sh	Mon May 12 21:24:20 2008 +0200
     4.3 @@ -0,0 +1,23 @@
     4.4 +#!/bin/sh
     4.5 +#
     4.6 +# runs one cycle
     4.7 +#
     4.8 +
     4.9 +if [ $# -ne 1 ] ; then
    4.10 +	echo "usage: runtick.sh <gamename>"
    4.11 +	exit 1
    4.12 +fi
    4.13 +
    4.14 +gamename="$1"
    4.15 +
    4.16 +
    4.17 +echo "started backend cycle at `date +%F\ %H:%M:%S`"
    4.18 +
    4.19 +./bin/clock $gamename
    4.20 +#./bin/weather $gamename
    4.21 +#./bin/environmant $gamename
    4.22 +#./bin/market $gamename
    4.23 +#./bin/growth $gamename
    4.24 +#./bin/orderexec $gamename
    4.25 +
    4.26 +echo "finished backend cycle at `date +%F\ %H:%M:%S`"