/* * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; version 2 of the License. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * 10-09-2008 0h57 by Vamps (vamps@alterinet.org) * * */ #include #include #include #include #include #include #include #include #include #include #include #include "include/parsage.h" #include "include/mysql.h" #include "include/log.h" #include "include/config.h" /*#define DEBUG*/ int main(int argc, char *argv[]) { int fifo; int i; char *buffer; char *buf; struct game game_t; MYSQL_ROW row_stats; unsigned short error; MYSQL *mysql; MYSQL_ROW row; struct time time_p[MAX_PLAYER]; time_t time_tmp; unsigned short current_struct_time; double ptime; struct config config_t; config_t.read = 0; short daemon = 0; char buffer_tmp[SIZE_BUFFER]; char *liste_options = "c:l:hdv"; /* Option List */ int option; if (argc < 2) { printf("Error, see ./teestat -h for help\n"); exit(1); } LogType(0); /* desactive log by default */ while ((option = getopt(argc, argv, liste_options)) != -1) { switch (option) { case 'c': /* Config file option */ if (LoadConf(optarg, &config_t) != 0) exit(1); config_t.read = 1; break; case 'l': /* Log file option */ LoadLog(optarg); LogType(1); break; case 'd': /* Daemon option */ daemon = 1; break; case 'v': /* Verbose log option */ LogType(2); break; case 'h': /* Help */ printf("Usage : teestat [options] fifo\n"); printf("Options...\n\ -h Print this help\n\ -c Config file\n\ -l Log file\n\ -d Daemonize\n\ -v Verbose log\n"); exit(0); break; case '?': /* Unknown */ exit(1); break; } } if (daemon == 1) { i = fork(); if (i < 0) exit(1); if (i > 0) exit(0); } if (config_t.read == 0) { LoadConf(DEFAULT_CONFIG_FILE, &config_t); } if (argv[optind] != NULL && (fifo = open(argv[optind], O_RDONLY)) < 0) { perror("open"); return 1; } buffer = malloc(SIZE_BUFFER * sizeof(char)); if (buffer == NULL) { perror("malloc"); return 1; } /* MySQL initialisation */ if ((mysql = mysql_init(NULL)) == NULL) { printf("MySQL initialisation failed\n"); return 1; } if (mysql_real_connect (mysql, config_t.mysql_server, config_t.mysql_user, config_t.mysql_password, config_t.mysql_base, config_t.mysql_port, NULL, 0) == NULL) { printf("Connection failed : %s\n", mysql_error(mysql)); return 1; } /* Database selection */ mysql_select_db(mysql, config_t.mysql_base); game_t.party = 2; /* Time initialisation */ for (i = 0; i < MAX_PLAYER; i++) { memset(time_p[i].player, 0, SIZE_BUFFER_LOW); time_p[i].mutex = 0; } while (error != 1) { while (1) { memset(buffer, 0, SIZE_BUFFER); buf = NULL; /* Get data from the tube */ if (read(fifo, buffer, SIZE_BUFFER) == -1) { perror("read"); error = 1; break; } /* Data processing */ if (ParseJoin(buffer, &game_t) == 0) { for (current_struct_time = 0; current_struct_time <= MAX_PLAYER; current_struct_time++) { if (time_p[current_struct_time].mutex == 0) break; } memset(buffer_tmp, 0, SIZE_BUFFER); sprintf(buffer_tmp, "Time structure %d free !", current_struct_time); PutLog(buffer_tmp); time_p[current_struct_time].time = time(NULL); memset(time_p[current_struct_time].player, 0, SIZE_BUFFER_LOW); strcpy(time_p[current_struct_time].player, game_t.player); time_p[current_struct_time].mutex = 1; /* Mutex */ current_struct_time++; if ((row = CheckPlayerDB(mysql, game_t.player)) == NULL) CreatePlayerDB(mysql, game_t.player); game_t.party_id = GetLastIdPartyDB(mysql); game_t.player_id = GetIdPlayerDB(mysql, game_t.player); if ((row_stats = CheckPlayerStatsDB(mysql, game_t.party_id, game_t.player_id)) == NULL) AddPlayerStatsDB(mysql, game_t.party_id, game_t.player_id); } if (ParseKill(buffer, &game_t) == 0) { /* Table : teewars_party * Update killer's table * */ if (((row = CheckPlayerDB(mysql, game_t.killer))) != NULL) { if (strcmp(game_t.victim, game_t.killer) == 0); /* Kill hitself, do nothing */ else AddPlayerKill(mysql, row, game_t.killer); } else { /* If the program restarts during the game with non-registered players */ CreatePlayerDB(mysql, game_t.killer); if (strcmp(game_t.victim, game_t.killer) == 0); else AddPlayerKill(mysql, row, game_t.killer); } /* Table : teewars_party * Update victim's table * */ if (((row = CheckPlayerDB(mysql, game_t.victim))) != NULL) { AddPlayerDeath(mysql, row, game_t.victim); } else { CreatePlayerDB(mysql, game_t.victim); AddPlayerDeath(mysql, row, game_t.victim); } /* Table : teewars_stats * Calculating IDs * */ game_t.party_id = GetLastIdPartyDB(mysql); game_t.killer_id = GetIdPlayerDB(mysql, game_t.killer); game_t.victim_id = GetIdPlayerDB(mysql, game_t.victim); if ((row_stats = CheckPlayerStatsDB(mysql, game_t.party_id, game_t.killer_id)) != NULL) { if (strcmp(game_t.victim, game_t.killer) == 0) /* Kill hitself, remove on point */ RmPlayerKillStats(mysql, row_stats, game_t.party_id, game_t.killer_id); else AddPlayerKillStats(mysql, row_stats, game_t.party_id, game_t.killer_id); } else { AddPlayerStatsDB(mysql, game_t.party_id, game_t.killer_id); if (strcmp(game_t.victim, game_t.killer) == 0) /* Kill hitself, remove on point */ RmPlayerKillStats(mysql, row_stats, game_t.party_id, game_t.killer_id); else AddPlayerKillStats(mysql, row_stats, game_t.party_id, game_t.killer_id); } if ((row_stats = CheckPlayerStatsDB(mysql, game_t.party_id, game_t.victim_id)) != NULL) AddPlayerDeathStats(mysql, row_stats, game_t.party_id, game_t.victim_id); else { AddPlayerStatsDB(mysql, game_t.party_id, game_t.victim_id); AddPlayerDeathStats(mysql, row_stats, game_t.party_id, game_t.victim_id); } } if (ParseLeave(buffer, &game_t) == 0) { for (i = 0; i < MAX_PLAYER; i++) { if (strcmp(time_p[i].player, game_t.player) == 0) { memset(buffer_tmp, 0, SIZE_BUFFER); sprintf(buffer_tmp, "Time structure %d", i); PutLog(buffer_tmp); /* Calculating player's game time */ time_tmp = time(NULL); ptime = difftime(time_tmp, time_p[i].time); if ((row = CheckPlayerDB(mysql, game_t.player)) != NULL); AddPlayerPtime(mysql, row, game_t.player, ptime); game_t.party_id = GetLastIdPartyDB(mysql); game_t.player_id = GetIdPlayerDB(mysql, game_t.player); if ((row_stats = CheckPlayerStatsDB(mysql, game_t.party_id, game_t.player_id)) != NULL) AddPlayerPtimeStats(mysql, row_stats, game_t.party_id, game_t.player_id, ptime); memset(buffer_tmp, 0, SIZE_BUFFER); sprintf(buffer_tmp, "%d has played %d seconds", game_t.player_id, (int) ptime); PutLog(buffer_tmp); memset(time_p[i].player, 0, SIZE_BUFFER_LOW); strcpy(time_p[i].player, "NULL"); time_p[i].mutex = 0; break; } } } if (ParsePartyBegin(buffer, &game_t) == 0) { if (game_t.party == 0) { /* Performed */ /* Set the winner */ game_t.party_id = GetLastIdPartyDB(mysql); game_t.player_id = CheckPlayerWinnerStatsDB(mysql, game_t.party_id); SetPartyWinner(mysql, game_t.party_id, game_t.player_id); } /* Change party, reinitialize time structure. * Update data * */ PutLog("Clear time structure"); for (i = 0; i < MAX_PLAYER; i++) { if (time_p[i].mutex == 1 && strcmp(time_p[i].player, "NULL") != 0) { time_tmp = time(NULL); ptime = difftime(time_tmp, time_p[i].time); /* Calculating player's game time */ if ((row = CheckPlayerDB(mysql, time_p[i].player)) != NULL); AddPlayerPtime(mysql, row, time_p[i].player, ptime); game_t.party_id = GetLastIdPartyDB(mysql); game_t.player_id = GetIdPlayerDB(mysql, time_p[i].player); if ((row_stats = CheckPlayerStatsDB(mysql, game_t.party_id, game_t.player_id)) != NULL) AddPlayerPtimeStats(mysql, row_stats, game_t.party_id, game_t.player_id, ptime); memset(buffer_tmp, 0, SIZE_BUFFER); sprintf(buffer_tmp, "%d has played %d seconds", game_t.player_id, (int) ptime); PutLog(buffer_tmp); memset(time_p[i].player, 0, SIZE_BUFFER_LOW); strcpy(time_p[i].player, "NULL"); time_p[i].mutex = 0; } } game_t.party = 1; } if (ParsePartyType(buffer, &game_t) == 0 && game_t.party == 1) { CreatePartyDB(mysql, &game_t); game_t.party = 0; } } } mysql_close(mysql); free(buffer); close(fifo); FreeLog(); exit(0); }