garten

view weather.c @ 10:13c6828bd4a5

switched from mysql to sqlite; (+ some cleanups)
author meillo@marmaro.de
date Wed, 23 Jul 2008 11:40:45 +0200
parents 0c19ad487f02
children 176ee28e7464
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <mysql.h>
4 #include <time.h>
6 #include "db.h"
7 #include "game.h"
10 enum {
11 Nlast = 2,
12 };
14 enum { Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec };
16 struct weather {
17 float temp;
18 float sun;
19 float rain;
20 float wind;
21 float hum;
22 };
24 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};
25 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};
26 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};
27 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};
28 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};
31 float rand_limit(float limit) {
32 /* generates random number between -limit and +limit */
33 int r;
35 r = rand() / ( ((float) RAND_MAX + 1) / (2*limit) );
36 return r - limit;
37 }
42 void getlastweather(struct weather lastw[], int nlast) {
43 char query[512];
44 int i;
46 sprintf(query,
47 " select temp, sun, rain, wind, hum from weather "
48 " where game_id = '%d' "
49 " order by tick desc "
50 " limit %d "
51 , gameid, nlast);
52 db_query(query);
53 result = mysql_store_result(conn);
54 for (i = 0; i < nlast && (row = mysql_fetch_row(result)); i++) {
55 lastw[i].temp = atof(row[0]);
56 lastw[i].sun = atof(row[1]);
57 lastw[i].rain = atof(row[2]);
58 lastw[i].wind = atof(row[3]);
59 lastw[i].hum = atof(row[4]);
60 }
61 mysql_free_result(result);
63 }
66 void genweather(struct weather* w, struct weather lastw[], int nlast, int month) {
67 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);
68 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);
69 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);
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);
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);
72 }
75 int setweather(struct weather* w) {
76 char query[512];
77 sprintf(query,
78 " insert into weather "
79 " (tick, game_id, temp, sun, rain, wind, hum) "
80 " values ('%d', '%d', '%f', '%f', '%f', '%f', '%f') "
81 , gametime, gameid, w->temp, w->sun, w->rain, w->wind, w->hum);
82 db_query(query);
83 puts(query);
84 return mysql_affected_rows(conn);
85 }
88 int main(int argc, char* argv[]) {
89 printf(" --> weather\n");
91 /* init */
92 if (argc != 2) {
93 printf("usage: %s <game>\n", argv[0]);
94 exit(1);
95 }
96 gamename = argv[1];
98 db_connect();
99 check_game();
101 srand((unsigned int) time(NULL));
104 struct weather w;
105 struct weather lastn[Nlast];
107 getlastweather(lastn, Nlast);
109 genweather(&w, lastn, Nlast, May);
111 if (setweather(&w) > 0) {
112 printf("weather successful inserted\n");
113 } else {
114 printf("E: weather insertion failed\n");
115 }
117 /* set_weather(); */
120 db_close();
122 printf(" --< weather\n");
123 return 0;
124 }