garten

annotate weather.c @ 9:ac67f688ed2e

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