<commit_before>/***************************************************************************
 *   Copyright (C) 2006 by <NAME>   *
 *   <EMAIL>   *
 *                                                                         *
 *   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; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   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.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#include "configfile.h"
#include "game_defs.h"
#include "log.h"
#include "playerinterface.h"
#include <sqlite3.h>
#include <iostream>
#include <dirent.h>
#include <boost/lexical_cast.hpp>

using namespace std;

Log::Log(string logDirString, int logOnOffInt) : curGameID(0), logOnOff(false)
{

    if(SQLITE_LOG) {

        // logging activated
        if(logOnOffInt) {

            logOnOff = true;

            DIR *logDir;
            logDir = opendir(logDirString.c_str());

            // check if logging path exist
            if(logDirString != "" && logDir) {

                int i;
                string sql;
                char *errmsg;

                // detect current time
                char curDateTime[20];
                char curDate[11];
                char curTime[9];
                time_t now = time(NULL);
                tm *z = localtime(&now);
                strftime(curDateTime,20,"%Y-%m-%d_%H.%M.%S",z);
                strftime(curDate,11,"%Y-%m-%d",z);
                strftime(curTime,9,"%H:%M:%S",z);

                string mySqliteLogFileName = boost::lexical_cast<string>(logDirString.c_str());
                       mySqliteLogFileName += "pokerth-log-" + boost::lexical_cast<string>(curDateTime) + ".pdb";

                // open sqlite-db
                sqlite3_open(mySqliteLogFileName.data(), &mySqliteLogDb);
                if( mySqliteLogDb != 0 ) {

                    // create session table
                    sql = "CREATE TABLE Session (";
                        sql += "PokerTH_Version TEXT NOT NULL";
                        sql += ",Date TEXT NOT NULL";
                        sql += ",Time TEXT NOT NULL";
                    sql += ", PRIMARY KEY(Date,Time))";
                    if(sqlite3_exec(mySqliteLogDb, sql.data(), 0, 0, &errmsg) != SQLITE_OK) {
                        cout << "Error in statement: " << sql.data() << "[" << errmsg << "]." << endl;
                    }

                    sql = "INSERT INTO Session (";
                        sql += "PokerTH_Version";
                        sql += ",Date";
                        sql += ",Time";
                    sql += ") VALUES (";
                        sql += "\"" + boost::lexical_cast<string>(POKERTH_BETA_RELEASE_STRING) + "\",";
                        sql += "\"" + boost::lexical_cast<string>(curDate) + "\",";
                        sql += "\"" + boost::lexical_cast<string>(curTime) + "\")";
                    if(sqlite3_exec(mySqliteLogDb, sql.data(), 0, 0, &errmsg) != SQLITE_OK) {
                        cout << "Error in statement: " << sql.data() << "[" << errmsg << "]." << endl;
                    }

                    // create game table
                    sql = "CREATE TABLE Game (";
                        sql += "GameID INTEGER NOT NULL PRIMARY KEY";
                        sql += ",Startmoney INTEGER NOT NULL";
                        sql += ",StartSb INTEGER NOT NULL";
                        sql += ",DealerPos INTEGER NOT NULL";
                        for(i=1; i<=MAX_NUMBER_OF_PLAYERS; i++) {
                            sql += ",Seat_" + boost::lexical_cast<std::string>(i) + " TEXT";
                        }
                        sql += ",Winner_Seat INTEGER";
                    sql += ")";
                    if(sqlite3_exec(mySqliteLogDb, sql.data(), 0, 0, &errmsg) != SQLITE_OK) {
                        cout << "Error in statement: " << sql.data() << "[" << errmsg << "]." << endl;
                    }

                    // create hand table
                    sql = "CREATE TABLE Hand (";
                        sql += "HandID INTEGER NOT NULL";
                        sql += ",GameID INTEGER NOT NULL";
                        sql += ",Dealer_Seat INTEGER";
                        sql += ",Sb_Amount INTEGER NOT NULL";
                        sql += ",Sb_Seat INTEGER NOT NULL";
                        sql += ",Bb_Amount INTEGER NOT NULL";
                        sql += ",Bb_Seat INTEGER NOT NULL";
                        for(i=1; i<=MAX_NUMBER_OF_PLAYERS; i++) {
                            sql += ",Seat_" + boost::lexical_cast<std::string>(i) + "_Cash INTEGER";
                            sql += ",Seat_" + boost::lexical_cast<std::string>(i) + "_Card_1 INTEGER";
                            sql += ",Seat_" + boost::lexical_cast<std::string>(i) + "_Card_2 INTEGER";
                            sql += ",Seat_" + boost::lexical_cast<std::string>(i) + "_Hand_text TEXT";
                            sql += ",Seat_" + boost::lexical_cast<std::string>(i) + "_Hand_int INTEGER";
                        }
                        for(i=1; i<=5; i++) {
                            sql += ",BoardCard_" + boost::lexical_cast<std::string>(i) + " INTEGER";
                        }
                    sql += ",PRIMARY KEY(HandID,GameID))";
                    if(sqlite3_exec(mySqliteLogDb, sql.data(), 0, 0, &errmsg) != SQLITE_OK) {
                        cout << "Error in statement: " << sql.data() << "[" << errmsg << "]." << endl;
                    }

                    // create action table
                    sql = "CREATE TABLE Action (";
                        sql += "ActionID INTEGER PRIMARY KEY AUTOINCREMENT";
                        sql += ",HandID INTEGER NOT NULL";
                        sql += ",GameID INTEGER NOT NULL";
                        sql += ",BeRo INTEGER NOT NULL";
                        sql += ",Player INTEGER NOT NULL";
                        sql += ",Action TEXT NOT NULL";
                        sql += ",Amount INTEGER";
                    sql += ")";
                    if(sqlite3_exec(mySqliteLogDb, sql.data(), 0, 0, &errmsg) != SQLITE_OK) {
                        cout << "Error in statement: " << sql.data() << "[" << errmsg << "]." << endl;
                    }


                }

            }

        }

    }
}

Log::~Log()<commit_msg>Fixing memory leaks (in currently unused code).<commit_after>/***************************************************************************
 *   Copyright (C) 2006 by <NAME>   *
 *   <EMAIL>   *
 *                                                                         *
 *   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; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   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.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#include "configfile.h"
#include "game_defs.h"
#include "log.h"
#include "playerinterface.h"
#include <sqlite3.h>
#include <iostream>
#include <dirent.h>
#include <boost/lexical_cast.hpp>

using namespace std;

Log::Log(string logDirString, int logOnOffInt) : curGameID(0), logOnOff(false)
{

    if(SQLITE_LOG) {

        // logging activated
        if(logOnOffInt) {

            logOnOff = true;

            DIR *logDir;
            logDir = opendir(logDirString.c_str());

            // check if logging path exist
            if(logDirString != "" && logDir) {
				closedir(logDir);

                int i;
                string sql;
                char *errmsg;

                // detect current time
                char curDateTime[20];
                char curDate[11];
                char curTime[9];
                time_t now = time(NULL);
                tm *z = localtime(&now);
                strftime(curDateTime,20,"%Y-%m-%d_%H.%M.%S",z);
                strftime(curDate,11,"%Y-%m-%d",z);
                strftime(curTime,9,"%H:%M:%S",z);

                string mySqliteLogFileName = boost::lexical_cast<string>(logDirString.c_str());
                       mySqliteLogFileName += "pokerth-log-" + boost::lexical_cast<string>(curDateTime) + ".pdb";

                // open sqlite-db
                sqlite3_open(mySqliteLogFileName.data(), &mySqliteLogDb);
                if( mySqliteLogDb != 0 ) {

                    // create session table
                    sql = "CREATE TABLE Session (";
                        sql += "PokerTH_Version TEXT NOT NULL";
                        sql += ",Date TEXT NOT NULL";
                        sql += ",Time TEXT NOT NULL";
                    sql += ", PRIMARY KEY(Date,Time))";
                    if(sqlite3_exec(mySqliteLogDb, sql.data(), 0, 0, &errmsg) != SQLITE_OK) {
                        cout << "Error in statement: " << sql.data() << "[" << errmsg << "]." << endl;
						sqlite3_free(errmsg);
					}

                    sql = "INSERT INTO Session (";
                        sql += "PokerTH_Version";
                        sql += ",Date";
                        sql += ",Time";
                    sql += ") VALUES (";
                        sql += "\"" + boost::lexical_cast<string>(POKERTH_BETA_RELEASE_STRING) + "\",";
                        sql += "\"" + boost::lexical_cast<string>(curDate) + "\",";
                        sql += "\"" + boost::lexical_cast<string>(curTime) + "\")";
                    if(sqlite3_exec(mySqliteLogDb, sql.data(), 0, 0, &errmsg) != SQLITE_OK) {
                        cout << "Error in statement: " << sql.data() << "[" << errmsg << "]." << endl;
						sqlite3_free(errmsg);
					}

                    // create game table
                    sql = "CREATE TABLE Game (";
                        sql += "GameID INTEGER NOT NULL PRIMARY KEY";
                        sql += ",Startmoney INTEGER NOT NULL";
                        sql += ",StartSb INTEGER NOT NULL";
                        sql += ",DealerPos INTEGER NOT NULL";
                        for(i=1; i<=MAX_NUMBER_OF_PLAYERS; i++) {
                            sql += ",Seat_" + boost::lexical_cast<std::string>(i) + " TEXT";
                        }
                        sql += ",Winner_Seat INTEGER";
                    sql += ")";
                    if(sqlite3_exec(mySqliteLogDb, sql.data(), 0, 0, &errmsg) != SQLITE_OK) {
                        cout << "Error in statement: " << sql.data() << "[" << errmsg << "]." << endl;
						sqlite3_free(errmsg);
					}

                    // create hand table
                    sql = "CREATE TABLE Hand (";
                        sql += "HandID INTEGER NOT NULL";
                        sql += ",GameID INTEGER NOT NULL";
                        sql += ",Dealer_Seat INTEGER";
                        sql += ",Sb_Amount INTEGER NOT NULL";
                        sql += ",Sb_Seat INTEGER NOT NULL";
                        sql += ",Bb_Amount INTEGER NOT NULL";
                        sql += ",Bb_Seat INTEGER NOT NULL";
                        for(i=1; i<=MAX_NUMBER_OF_PLAYERS; i++) {
                            sql += ",Seat_" + boost::lexical_cast<std::string>(i) + "_Cash INTEGER";
                            sql += ",Seat_" + boost::lexical_cast<std::string>(i) + "_Card_1 INTEGER";
                            sql += ",Seat_" + boost::lexical_cast<std::string>(i) + "_Card_2 INTEGER";
                            sql += ",Seat_" + boost::lexical_cast<std::string>(i) + "_Hand_text TEXT";
                            sql += ",Seat_" + boost::lexical_cast<std::string>(i) + "_Hand_int INTEGER";
                        }
                        for(i=1; i<=5; i++) {
                            sql += ",BoardCard_" + boost::lexical_cast<std::string>(i) + " INTEGER";
                        }
                    sql += ",PRIMARY KEY(HandID,GameID))";
                    if(sqlite3_exec(mySqliteLogDb, sql.data(), 0, 0, &errmsg) != SQLITE_OK) {
                        cout << "Error in statement: " << sql.data() << "[" << errmsg << "]." << endl;
						sqlite3_free(errmsg);
					}

                    // create action table
                    sql = "CREATE TABLE Action (";
                        sql += "ActionID INTEGER PRIMARY KEY AUTOINCREMENT";
                        sql += ",HandID INTEGER NOT NULL";
                        sql += ",GameID INTEGER NOT NULL";
                        sql += ",BeRo INTEGER NOT NULL";
                        sql += ",Player INTEGER NOT NULL";
                        sql += ",Action TEXT NOT NULL";
                        sql += ",Amount INTEGER";
                    sql += ")";
                    if(sqlite3_exec(mySqliteLogDb, sql.data(), 0, 0, &errmsg) != SQLITE_OK) {
                        cout << "Error in statement: " << sql.data() << "[" << errmsg << "]." << endl;
						sqlite3_free(errmsg);
					}


                }

            }

        }

    }
}

Log::~Log()
{'fraction_non_alphanumeric': 0.14592829444179875, 'fraction_numerical': 0.00768759779576842, 'mean_word_length': 1.2929340196537202, 'pattern_counts': {'":': 0, '<': 165, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 22, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2180153', 'n_tokens_mistral': 4113, 'n_tokens_neox': 3638, 'n_words': 949}