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