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 wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/.hgignore	Mon May 12 21:24:20 2008 +0200
@@ -0,0 +1,10 @@
+syntax: glob
+*~
+*.swp
+
+*.o
+test
+bin
+
+*.tar.gz
+ChangeLog
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/clock.c	Mon May 12 21:24:20 2008 +0200
@@ -0,0 +1,87 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <mysql.h>
+
+#include "db.h"
+
+char query[512];
+
+
+int check_game(char* gamename) {
+	int rows;
+
+	sprintf(query, "select id from simulation where name = '%s' ", gamename);
+	db_query(query);
+	result = mysql_store_result(conn);
+	rows = mysql_num_rows(result);
+	mysql_free_result(result);
+	return rows;
+}
+
+
+void inc_time(char* gamename) {
+	int time = 0;
+
+	/* get current time */
+	sprintf(query, "select time from simulation where name = '%s' ", gamename);
+	db_query(query);
+	result = mysql_store_result(conn);
+	if (mysql_num_rows(result)) {
+		row = mysql_fetch_row(result);
+		time = atoi(row[0]);
+	}
+	mysql_free_result(result);
+
+
+	/* increment time */
+	sprintf(query, " update simulation set time = '%d' where name = '%s' ", ++time, gamename);
+	db_query(query);
+	if (mysql_affected_rows(conn) > 0) {
+		printf("time update successful\n");
+		printf("simulation time: %d\n", time);
+	} else {
+		printf("E: time update failed\n");
+	}
+
+}
+
+
+int main(int argc, char* argv[]) {
+	int i;
+	char* gamename;
+
+	/* init */
+	if (argc != 2) {
+		printf("usage: %s <game>\n", argv[0]);
+		exit(1);
+	}
+	gamename = argv[1];
+
+	printf("gamename: %s\n", gamename);
+
+	db_connect();
+
+	if (check_game(gamename) != 1) {
+		printf("game '%s' does not exist\n", gamename);
+		exit(1);
+	}
+
+	inc_time(gamename);
+
+/*
+	while ((row = mysql_fetch_row(result)) != NULL) {
+		for (i = 0; i < mysql_num_fields(result); i++) {
+			printf("%10s  ", row[i]);
+		}
+		printf("\n");
+	}
+	*/
+
+
+
+
+	db_close();
+
+	printf("the garten program\n");
+	return 0;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/db.h	Mon May 12 21:24:20 2008 +0200
@@ -0,0 +1,40 @@
+/*
+ * data for the database connection
+ */
+
+char* server = "localhost";
+char* user = "garten";
+char* password = "gras";
+char* database = "garten";
+
+MYSQL* conn;
+MYSQL_RES* result;
+MYSQL_ROW row;
+
+
+void db_connect() {
+	conn = mysql_init(NULL);
+
+	/* Connect to database */
+	if (!mysql_real_connect(conn, server, user, password, database, 0, NULL, 0)) {
+		fprintf(stderr, "%s\n", mysql_error(conn));
+		exit(1);
+	}
+}
+
+
+void db_close() {
+	/* close connection */
+	mysql_close(conn);
+}
+
+
+void db_query(char* query) {
+	int error;
+	/* send SQL query */
+	error = mysql_query(conn, query);
+	printf("query: %s\nerror: %d / %d\n", query, error, mysql_errno(conn));
+	if (error != 0) {
+		fprintf(stderr, "E: %s\n", mysql_error(conn));
+	}
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/runtick.sh	Mon May 12 21:24:20 2008 +0200
@@ -0,0 +1,23 @@
+#!/bin/sh
+#
+# runs one cycle
+#
+
+if [ $# -ne 1 ] ; then
+	echo "usage: runtick.sh <gamename>"
+	exit 1
+fi
+
+gamename="$1"
+
+
+echo "started backend cycle at `date +%F\ %H:%M:%S`"
+
+./bin/clock $gamename
+#./bin/weather $gamename
+#./bin/environmant $gamename
+#./bin/market $gamename
+#./bin/growth $gamename
+#./bin/orderexec $gamename
+
+echo "finished backend cycle at `date +%F\ %H:%M:%S`"