comparison 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
comparison
equal deleted inserted replaced
1:3ff1fbe4693d 2:8369454d4ec9
1 #include <stdio.h> 1 #include <stdio.h>
2 #include <stdlib.h> 2 #include <stdlib.h>
3 #include <mysql.h> 3 #include <mysql.h>
4 4
5 #include "db.h" 5 #include "db.h"
6 6 #include "game.h"
7 char query[512];
8 7
9 8
10 int check_game(char* gamename) {
11 int rows;
12 9
13 sprintf(query, "select id from simulation where name = '%s' ", gamename); 10 void inc_time() {
14 db_query(query);
15 result = mysql_store_result(conn);
16 rows = mysql_num_rows(result);
17 mysql_free_result(result);
18 return rows;
19 }
20
21
22 void inc_time(char* gamename) {
23 int time = 0;
24 11
25 /* get current time */ 12 /* get current time */
26 sprintf(query, "select time from simulation where name = '%s' ", gamename); 13 sprintf(query, "select time from game where name = '%s' ", gamename);
27 db_query(query); 14 db_query(query);
28 result = mysql_store_result(conn); 15 result = mysql_store_result(conn);
29 if (mysql_num_rows(result)) { 16 if (mysql_num_rows(result)) {
30 row = mysql_fetch_row(result); 17 row = mysql_fetch_row(result);
31 time = atoi(row[0]); 18 gametime = atoi(row[0]);
32 } 19 }
33 mysql_free_result(result); 20 mysql_free_result(result);
34 21
35 22
36 /* increment time */ 23 /* increment time */
37 sprintf(query, " update simulation set time = '%d' where name = '%s' ", ++time, gamename); 24 sprintf(query, " update game set time = '%d' where name = '%s' ", ++gametime, gamename);
38 db_query(query); 25 db_query(query);
39 if (mysql_affected_rows(conn) > 0) { 26 if (mysql_affected_rows(conn) > 0) {
40 printf("time update successful\n"); 27 printf("time update successful\n");
41 printf("simulation time: %d\n", time); 28 printf("virtual time: %d\n", gametime);
42 } else { 29 } else {
43 printf("E: time update failed\n"); 30 printf("E: time update failed\n");
44 } 31 }
45 32
46 } 33 }
47 34
48 35
49 int main(int argc, char* argv[]) { 36 int main(int argc, char* argv[]) {
50 int i; 37 printf(" --> clock\n");
51 char* gamename;
52 38
53 /* init */ 39 /* init */
54 if (argc != 2) { 40 if (argc != 2) {
55 printf("usage: %s <game>\n", argv[0]); 41 printf("usage: %s <game>\n", argv[0]);
56 exit(1); 42 exit(1);
57 } 43 }
58 gamename = argv[1]; 44 gamename = argv[1];
59 45
60 printf("gamename: %s\n", gamename); 46 db_connect();
47 check_game();
61 48
62 db_connect(); 49 inc_time();
63
64 if (check_game(gamename) != 1) {
65 printf("game '%s' does not exist\n", gamename);
66 exit(1);
67 }
68
69 inc_time(gamename);
70 50
71 /* 51 /*
72 while ((row = mysql_fetch_row(result)) != NULL) { 52 while ((row = mysql_fetch_row(result)) != NULL) {
73 for (i = 0; i < mysql_num_fields(result); i++) { 53 for (i = 0; i < mysql_num_fields(result); i++) {
74 printf("%10s ", row[i]); 54 printf("%10s ", row[i]);
80 60
81 61
82 62
83 db_close(); 63 db_close();
84 64
85 printf("the garten program\n"); 65 printf(" --< clock\n");
86 return 0; 66 return 0;
87 } 67 }