Mercurial > garten
annotate 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 |
rev | line source |
---|---|
3 | 1 #include <stdio.h> |
2 #include <stdlib.h> | |
8 | 3 #include <time.h> |
3 | 4 |
5 #include "db.h" | |
6 #include "game.h" | |
7 | |
8 | |
8 | 9 enum { |
10 Nlast = 2, | |
11 }; | |
12 | |
13 enum { Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec }; | |
14 | |
15 struct weather { | |
16 float temp; | |
17 float sun; | |
18 float rain; | |
19 float wind; | |
20 float hum; | |
21 }; | |
22 | |
23 float mean_temp[12] = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0}; | |
24 float mean_sun[12] = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0}; | |
25 float mean_rain[12] = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0}; | |
26 float mean_wind[12] = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0}; | |
27 float mean_hum[12] = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0}; | |
3 | 28 |
29 | |
8 | 30 float rand_limit(float limit) { |
31 /* generates random number between -limit and +limit */ | |
32 int r; | |
33 | |
34 r = rand() / ( ((float) RAND_MAX + 1) / (2*limit) ); | |
35 return r - limit; | |
36 } | |
37 | |
38 | |
39 | |
3 | 40 |
8 | 41 void getlastweather(struct weather lastw[], int nlast) { |
42 char query[512]; | |
43 int i; | |
44 | |
45 sprintf(query, | |
46 " select temp, sun, rain, wind, hum from weather " | |
47 " order by tick desc " | |
48 " limit %d " | |
11
176ee28e7464
switched from mysql to sqlite; (+ some cleanups)
meillo@marmaro.de
parents:
8
diff
changeset
|
49 , nlast); |
3 | 50 db_query(query); |
11
176ee28e7464
switched from mysql to sqlite; (+ some cleanups)
meillo@marmaro.de
parents:
8
diff
changeset
|
51 for (i = 0; i < nlast && sqlite3_step(stmt) == SQLITE_ROW; i++) { |
176ee28e7464
switched from mysql to sqlite; (+ some cleanups)
meillo@marmaro.de
parents:
8
diff
changeset
|
52 lastw[i].temp = sqlite3_column_double(stmt, 0); |
176ee28e7464
switched from mysql to sqlite; (+ some cleanups)
meillo@marmaro.de
parents:
8
diff
changeset
|
53 lastw[i].sun = sqlite3_column_double(stmt, 1); |
176ee28e7464
switched from mysql to sqlite; (+ some cleanups)
meillo@marmaro.de
parents:
8
diff
changeset
|
54 lastw[i].rain = sqlite3_column_double(stmt, 2); |
176ee28e7464
switched from mysql to sqlite; (+ some cleanups)
meillo@marmaro.de
parents:
8
diff
changeset
|
55 lastw[i].wind = sqlite3_column_double(stmt, 3); |
176ee28e7464
switched from mysql to sqlite; (+ some cleanups)
meillo@marmaro.de
parents:
8
diff
changeset
|
56 lastw[i].hum = sqlite3_column_double(stmt, 4); |
176ee28e7464
switched from mysql to sqlite; (+ some cleanups)
meillo@marmaro.de
parents:
8
diff
changeset
|
57 } |
176ee28e7464
switched from mysql to sqlite; (+ some cleanups)
meillo@marmaro.de
parents:
8
diff
changeset
|
58 sqlite3_finalize(stmt); |
176ee28e7464
switched from mysql to sqlite; (+ some cleanups)
meillo@marmaro.de
parents:
8
diff
changeset
|
59 |
176ee28e7464
switched from mysql to sqlite; (+ some cleanups)
meillo@marmaro.de
parents:
8
diff
changeset
|
60 /* |
3 | 61 result = mysql_store_result(conn); |
8 | 62 for (i = 0; i < nlast && (row = mysql_fetch_row(result)); i++) { |
63 lastw[i].temp = atof(row[0]); | |
64 lastw[i].sun = atof(row[1]); | |
65 lastw[i].rain = atof(row[2]); | |
66 lastw[i].wind = atof(row[3]); | |
67 lastw[i].hum = atof(row[4]); | |
3 | 68 } |
69 mysql_free_result(result); | |
11
176ee28e7464
switched from mysql to sqlite; (+ some cleanups)
meillo@marmaro.de
parents:
8
diff
changeset
|
70 */ |
3 | 71 |
8 | 72 } |
3 | 73 |
74 | |
8 | 75 void genweather(struct weather* w, struct weather lastw[], int nlast, int month) { |
76 w->temp = 0.2 * mean_temp[month] + 0.4 * lastw[0].temp + 0.2 * lastw[1].temp + 0.08 * rand_limit(10); // + 0.02 * rand_limit(100); | |
77 w->sun = 0.2 * mean_sun[month] + 0.4 * lastw[0].sun + 0.2 * lastw[1].sun + 0.08 * rand_limit(10); // + 0.02 * rand_limit(100); | |
78 w->rain = 0.2 * mean_rain[month] + 0.4 * lastw[0].rain + 0.2 * lastw[1].rain + 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); | |
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); | |
81 } | |
82 | |
83 | |
11
176ee28e7464
switched from mysql to sqlite; (+ some cleanups)
meillo@marmaro.de
parents:
8
diff
changeset
|
84 void setweather(struct weather* w) { |
8 | 85 char query[512]; |
86 sprintf(query, | |
87 " insert into weather " | |
11
176ee28e7464
switched from mysql to sqlite; (+ some cleanups)
meillo@marmaro.de
parents:
8
diff
changeset
|
88 " (tick, temp, sun, rain, wind, hum) " |
176ee28e7464
switched from mysql to sqlite; (+ some cleanups)
meillo@marmaro.de
parents:
8
diff
changeset
|
89 " values ('%d', '%f', '%f', '%f', '%f', '%f') " |
176ee28e7464
switched from mysql to sqlite; (+ some cleanups)
meillo@marmaro.de
parents:
8
diff
changeset
|
90 , gametime, w->temp, w->sun, w->rain, w->wind, w->hum); |
3 | 91 db_query(query); |
8 | 92 puts(query); |
11
176ee28e7464
switched from mysql to sqlite; (+ some cleanups)
meillo@marmaro.de
parents:
8
diff
changeset
|
93 if (sqlite3_step(stmt) == SQLITE_DONE) { |
176ee28e7464
switched from mysql to sqlite; (+ some cleanups)
meillo@marmaro.de
parents:
8
diff
changeset
|
94 printf("weather successful inserted\n"); |
176ee28e7464
switched from mysql to sqlite; (+ some cleanups)
meillo@marmaro.de
parents:
8
diff
changeset
|
95 } else { |
176ee28e7464
switched from mysql to sqlite; (+ some cleanups)
meillo@marmaro.de
parents:
8
diff
changeset
|
96 printf("error: weather insertion failed: %s\n", sqlite3_errmsg(db)); |
176ee28e7464
switched from mysql to sqlite; (+ some cleanups)
meillo@marmaro.de
parents:
8
diff
changeset
|
97 } |
3 | 98 } |
99 | |
100 | |
101 int main(int argc, char* argv[]) { | |
102 printf(" --> weather\n"); | |
103 | |
104 /* init */ | |
105 if (argc != 2) { | |
11
176ee28e7464
switched from mysql to sqlite; (+ some cleanups)
meillo@marmaro.de
parents:
8
diff
changeset
|
106 printf("usage: %s <database>\n", argv[0]); |
3 | 107 exit(1); |
108 } | |
11
176ee28e7464
switched from mysql to sqlite; (+ some cleanups)
meillo@marmaro.de
parents:
8
diff
changeset
|
109 database = argv[1]; |
3 | 110 |
111 db_connect(); | |
11
176ee28e7464
switched from mysql to sqlite; (+ some cleanups)
meillo@marmaro.de
parents:
8
diff
changeset
|
112 read_time(); |
3 | 113 |
8 | 114 srand((unsigned int) time(NULL)); |
115 | |
116 | |
117 struct weather w; | |
118 struct weather lastn[Nlast]; | |
119 | |
120 getlastweather(lastn, Nlast); | |
121 genweather(&w, lastn, Nlast, May); | |
11
176ee28e7464
switched from mysql to sqlite; (+ some cleanups)
meillo@marmaro.de
parents:
8
diff
changeset
|
122 setweather(&w); |
3 | 123 |
124 db_close(); | |
125 | |
126 printf(" --< weather\n"); | |
127 return 0; | |
128 } |