comparison weather.c @ 11:176ee28e7464

switched from mysql to sqlite; (+ some cleanups)
author meillo@marmaro.de
date Wed, 23 Jul 2008 11:41:38 +0200
parents 9bd0a2100694
children 8db6497d6065
comparison
equal deleted inserted replaced
10:13c6828bd4a5 11:176ee28e7464
1 #include <stdio.h> 1 #include <stdio.h>
2 #include <stdlib.h> 2 #include <stdlib.h>
3 #include <mysql.h>
4 #include <time.h> 3 #include <time.h>
5 4
6 #include "db.h" 5 #include "db.h"
7 #include "game.h" 6 #include "game.h"
8 7
43 char query[512]; 42 char query[512];
44 int i; 43 int i;
45 44
46 sprintf(query, 45 sprintf(query,
47 " select temp, sun, rain, wind, hum from weather " 46 " select temp, sun, rain, wind, hum from weather "
48 " where game_id = '%d' "
49 " order by tick desc " 47 " order by tick desc "
50 " limit %d " 48 " limit %d "
51 , gameid, nlast); 49 , nlast);
52 db_query(query); 50 db_query(query);
51 for (i = 0; i < nlast && sqlite3_step(stmt) == SQLITE_ROW; i++) {
52 lastw[i].temp = sqlite3_column_double(stmt, 0);
53 lastw[i].sun = sqlite3_column_double(stmt, 1);
54 lastw[i].rain = sqlite3_column_double(stmt, 2);
55 lastw[i].wind = sqlite3_column_double(stmt, 3);
56 lastw[i].hum = sqlite3_column_double(stmt, 4);
57 }
58 sqlite3_finalize(stmt);
59
60 /*
53 result = mysql_store_result(conn); 61 result = mysql_store_result(conn);
54 for (i = 0; i < nlast && (row = mysql_fetch_row(result)); i++) { 62 for (i = 0; i < nlast && (row = mysql_fetch_row(result)); i++) {
55 lastw[i].temp = atof(row[0]); 63 lastw[i].temp = atof(row[0]);
56 lastw[i].sun = atof(row[1]); 64 lastw[i].sun = atof(row[1]);
57 lastw[i].rain = atof(row[2]); 65 lastw[i].rain = atof(row[2]);
58 lastw[i].wind = atof(row[3]); 66 lastw[i].wind = atof(row[3]);
59 lastw[i].hum = atof(row[4]); 67 lastw[i].hum = atof(row[4]);
60 } 68 }
61 mysql_free_result(result); 69 mysql_free_result(result);
70 */
62 71
63 } 72 }
64 73
65 74
66 void genweather(struct weather* w, struct weather lastw[], int nlast, int month) { 75 void genweather(struct weather* w, struct weather lastw[], int nlast, int month) {
70 w->wind = 0.2 * mean_wind[month] + 0.4 * lastw[0].wind + 0.2 * lastw[1].wind + 0.08 * rand_limit(10); // + 0.02 * rand_limit(100); 79 w->wind = 0.2 * mean_wind[month] + 0.4 * lastw[0].wind + 0.2 * lastw[1].wind + 0.08 * rand_limit(10); // + 0.02 * rand_limit(100);
71 w->hum = 0.2 * mean_hum[month] + 0.4 * lastw[0].hum + 0.2 * lastw[1].hum + 0.08 * rand_limit(10); // + 0.02 * rand_limit(100); 80 w->hum = 0.2 * mean_hum[month] + 0.4 * lastw[0].hum + 0.2 * lastw[1].hum + 0.08 * rand_limit(10); // + 0.02 * rand_limit(100);
72 } 81 }
73 82
74 83
75 int setweather(struct weather* w) { 84 void setweather(struct weather* w) {
76 char query[512]; 85 char query[512];
77 sprintf(query, 86 sprintf(query,
78 " insert into weather " 87 " insert into weather "
79 " (tick, game_id, temp, sun, rain, wind, hum) " 88 " (tick, temp, sun, rain, wind, hum) "
80 " values ('%d', '%d', '%f', '%f', '%f', '%f', '%f') " 89 " values ('%d', '%f', '%f', '%f', '%f', '%f') "
81 , gametime, gameid, w->temp, w->sun, w->rain, w->wind, w->hum); 90 , gametime, w->temp, w->sun, w->rain, w->wind, w->hum);
82 db_query(query); 91 db_query(query);
83 puts(query); 92 puts(query);
84 return mysql_affected_rows(conn); 93 if (sqlite3_step(stmt) == SQLITE_DONE) {
94 printf("weather successful inserted\n");
95 } else {
96 printf("error: weather insertion failed: %s\n", sqlite3_errmsg(db));
97 }
85 } 98 }
86 99
87 100
88 int main(int argc, char* argv[]) { 101 int main(int argc, char* argv[]) {
89 printf(" --> weather\n"); 102 printf(" --> weather\n");
90 103
91 /* init */ 104 /* init */
92 if (argc != 2) { 105 if (argc != 2) {
93 printf("usage: %s <game>\n", argv[0]); 106 printf("usage: %s <database>\n", argv[0]);
94 exit(1); 107 exit(1);
95 } 108 }
96 gamename = argv[1]; 109 database = argv[1];
97 110
98 db_connect(); 111 db_connect();
99 check_game(); 112 read_time();
100 113
101 srand((unsigned int) time(NULL)); 114 srand((unsigned int) time(NULL));
102 115
103 116
104 struct weather w; 117 struct weather w;
105 struct weather lastn[Nlast]; 118 struct weather lastn[Nlast];
106 119
107 getlastweather(lastn, Nlast); 120 getlastweather(lastn, Nlast);
108
109 genweather(&w, lastn, Nlast, May); 121 genweather(&w, lastn, Nlast, May);
110 122 setweather(&w);
111 if (setweather(&w) > 0) {
112 printf("weather successful inserted\n");
113 } else {
114 printf("E: weather insertion failed\n");
115 }
116
117 /* set_weather(); */
118
119 123
120 db_close(); 124 db_close();
121 125
122 printf(" --< weather\n"); 126 printf(" --< weather\n");
123 return 0; 127 return 0;