diff --git a/src/db/Db2DbiXX.cpp b/src/db/Db2DbiXX.cpp index 406acf9..24ba381 100644 --- a/src/db/Db2DbiXX.cpp +++ b/src/db/Db2DbiXX.cpp @@ -73,173 +73,203 @@ static string S_CHALLENGES = CHALLENGES; static string booltext[2] = {"n","y"}; -void Db2DbiXX::InitStrings(string backend) { - last_value = false; - if(backend == "pgsql") { - last_value = true; //Now also use last for PostgreSQL - } - if(backend == "mysql") { - last_value = true; - S_GETLASTGAMENUMBER = GETLASTGAMENUMBER_MYSQL; - } - sql_backend = backend; +void Db2DbiXX::InitStrings(string backend) +{ + last_value = false; + if(backend == "pgsql") + { + last_value = true; //Now also use last for PostgreSQL + } + if(backend == "mysql") + { + last_value = true; + S_GETLASTGAMENUMBER = GETLASTGAMENUMBER_MYSQL; + } + sql_backend = backend; } -void Db2DbiXX::ReadConfigFromDb() { - result res; - row r; - string value; - *sql<> value; - uservars2save.insert(value); - } - *sql<< SELECT_CVARS2SAVE,res; - while(res.next(r)) { - r >> value; - cvars2save.insert(value); - } +void Db2DbiXX::ReadConfigFromDb() +{ + result res; + row r; + string value; + *sql<> value; + uservars2save.insert(value); + } + *sql<< SELECT_CVARS2SAVE,res; + while(res.next(r)) + { + r >> value; + cvars2save.insert(value); + } } -bool Db2DbiXX::IsDuplicate(string servername, tm thetime) { - result res; - *sql << SELECT_DUPLICATE,servername,thetime,res; - if(res.rows()) - return true; - else - return false; +bool Db2DbiXX::IsDuplicate(string servername, tm thetime) +{ + result res; + *sql << SELECT_DUPLICATE,servername,thetime,res; + if(res.rows()) + return true; + else + return false; } -Db2DbiXX::Db2DbiXX() { - sql = new session("pgsql"); - InitStrings("pgsql"); - sql->param("dbname","oastat"); - sql->connect(); - ReadConfigFromDb(); - commitlock = new transaction(*sql); - debug = false; +Db2DbiXX::Db2DbiXX() +{ + sql = new session("pgsql"); + InitStrings("pgsql"); + sql->param("dbname","oastat"); + sql->connect(); + ReadConfigFromDb(); + commitlock = new transaction(*sql); + debug = false; } Db2DbiXX::Db2DbiXX(string dbargs) { - stringstream stream(stringstream::in | stringstream::out); - stream << dbargs; - string holder; - stream >> holder; - sql = new session(holder); - InitStrings(holder); - while(!stream.eof()) { - stream >> holder; - if(!stream.eof()) { - string param; - stream >> param; - sql->param(holder,param); - } - } - sql->connect(); - ReadConfigFromDb(); - commitlock = new transaction(*sql); - debug = false; - //sql(dbargs); + stringstream stream(stringstream::in | stringstream::out); + stream << dbargs; + string holder; + stream >> holder; + sql = new session(holder); + InitStrings(holder); + while(!stream.eof()) + { + stream >> holder; + if(!stream.eof()) + { + string param; + stream >> param; + sql->param(holder,param); + } + } + sql->connect(); + ReadConfigFromDb(); + commitlock = new transaction(*sql); + debug = false; + //sql(dbargs); } -Db2DbiXX::Db2DbiXX(const Db2DbiXX& orig) { - throw "May not make copy of Db2DbiXX"; +Db2DbiXX::Db2DbiXX(const Db2DbiXX& orig) +{ + throw "May not make copy of Db2DbiXX"; } -Db2DbiXX::~Db2DbiXX() { - delete commitlock; - delete sql; +Db2DbiXX::~Db2DbiXX() +{ + delete commitlock; + delete sql; } -void Db2DbiXX::createTables() { - +void Db2DbiXX::createTables() +{ + } -void Db2DbiXX::startGame(int gametype, string mapname, string basegame, string servername, OaStatStruct *oss) { - sql->reconnect(); - Rollback(); //in case there was some garbage that could be comitted (like warmup or an unfinished game) - SetOk(true); - if(oss->restOfLine.find("\\isWarmup\\1") != string::npos) - { - SetOk(false); - cout << "Warmup: " << servername << ", " << oss->getTimeStamp() << endl; - return; - } - if(IsDuplicate(servername,oss->getDateTime())) { - SetOk(false); - cout << "Duplicate:" << servername << ", " << oss->getTimeStamp() << endl; - return; - } - if(last_value) { - *sql << S_STARTGAME_LASTVALUE,gametype,mapname,basegame,servername,oss->getDateTime(),exec(); - gamenumber = getLastGameNumber(); - if(gamenumber < 1) { - SetOk(false); - cout << "Must FAIL!" << endl; - } - } else { - gamenumber = getNextGameNumber(); - *sql << S_STARTGAME,gamenumber,gametype,mapname,basegame,servername,oss->getDateTime(),exec(); - } - DebugMessage("startGame"); +void Db2DbiXX::startGame(int gametype, string mapname, string basegame, string servername, OaStatStruct *oss) +{ + sql->reconnect(); + Rollback(); //in case there was some garbage that could be comitted (like warmup or an unfinished game) + SetOk(true); + if(oss->restOfLine.find("\\isWarmup\\1") != string::npos) + { + SetOk(false); + cout << "Warmup: " << servername << ", " << oss->getTimeStamp() << endl; + return; + } + if(IsDuplicate(servername,oss->getDateTime())) + { + SetOk(false); + cout << "Duplicate:" << servername << ", " << oss->getTimeStamp() << endl; + return; + } + if(last_value) + { + *sql << S_STARTGAME_LASTVALUE,gametype,mapname,basegame,servername,oss->getDateTime(),exec(); + gamenumber = getLastGameNumber(); + if(gamenumber < 1) + { + SetOk(false); + cout << "Must FAIL!" << endl; + } + } + else + { + gamenumber = getNextGameNumber(); + *sql << S_STARTGAME,gamenumber,gametype,mapname,basegame,servername,oss->getDateTime(),exec(); + } + DebugMessage("startGame"); } -void Db2DbiXX::addGameCvar(string cvar, string value) { - if(!isok) - return; - if(cvars2save.find(cvar) == cvars2save.end()) - return; //not to be saved - *sql << S_ADDCVAR,gamenumber,cvar,value,0,exec(); - DebugMessage("addCvar"); +void Db2DbiXX::addGameCvar(string cvar, string value) +{ + if(!isok) + return; + if(cvars2save.find(cvar) == cvars2save.end()) + return; //not to be saved + *sql << S_ADDCVAR,gamenumber,cvar,value,0,exec(); + DebugMessage("addCvar"); } /* endGame is called then we finnish the game. This also means that it is safe to commit */ -void Db2DbiXX::endGame(int second) { - if(!isok) - return; - *sql << S_ENDGAME,second,gamenumber,exec(); - Commit(); //Game have ended, transaction is in a stable state - DebugMessage("endgame"); +void Db2DbiXX::endGame(int second) +{ + if(!isok) + return; + *sql << S_ENDGAME,second,gamenumber,exec(); + Commit(); //Game have ended, transaction is in a stable state + DebugMessage("endgame"); } -int Db2DbiXX::getGameNumber() { - return gamenumber; +int Db2DbiXX::getGameNumber() +{ + return gamenumber; } -void Db2DbiXX::setPlayerInfo(string guid, string nickname, bool isBot, int second, int team, string model, string headmodel, int skill, OaStatStruct *oss) { - if(!isok) - return; - if(team>-1) - { - try{ - *sql << "SAVEPOINT SETPLAYER",exec(); - *sql << S_PLAYERSINSERT,guid,nickname,oss->getDateTime(),booltext[isBot],model,headmodel,exec(); - *sql << "RELEASE SAVEPOINT SETPLAYER",exec(); //Needed by postgresql - }catch (dbixx_error &e) { - DebugMessage("Already inserted? "+(string)e.what()); - *sql << "ROLLBACK TO SAVEPOINT SETPLAYER",exec(); - } - *sql << S_PLAYERSUPDATE,nickname,oss->getDateTime(),booltext[isBot],model,headmodel,guid,oss->getDateTime(),exec(); - } - try{ - *sql << "SAVEPOINT SETUSERINFO",exec(); - *sql << S_USERINFOINSERT,gamenumber,second,guid,team,model,skill,exec(); - *sql << "RELEASE SAVEPOINT SETUSERINFO",exec(); //Needed by postgresql - }catch (dbixx_error &e) { - *sql << "ROLLBACK TO SAVEPOINT SETUSERINFO",exec(); - *sql << S_USERINFOUPDATE,team,model,skill,gamenumber,second,guid,exec(); - } - DebugMessage("setPlayerInfo for "+nickname+" with GUID: "+guid); +void Db2DbiXX::setPlayerInfo(string guid, string nickname, bool isBot, int second, int team, string model, string headmodel, int skill, OaStatStruct *oss) +{ + if(!isok) + return; + if(team>-1) + { + try + { + *sql << "SAVEPOINT SETPLAYER",exec(); + *sql << S_PLAYERSINSERT,guid,nickname,oss->getDateTime(),booltext[isBot],model,headmodel,exec(); + *sql << "RELEASE SAVEPOINT SETPLAYER",exec(); //Needed by postgresql + } + catch (dbixx_error &e) + { + DebugMessage("Already inserted? "+(string)e.what()); + *sql << "ROLLBACK TO SAVEPOINT SETPLAYER",exec(); + } + *sql << S_PLAYERSUPDATE,nickname,oss->getDateTime(),booltext[isBot],model,headmodel,guid,oss->getDateTime(),exec(); + } + try + { + *sql << "SAVEPOINT SETUSERINFO",exec(); + *sql << S_USERINFOINSERT,gamenumber,second,guid,team,model,skill,exec(); + *sql << "RELEASE SAVEPOINT SETUSERINFO",exec(); //Needed by postgresql + } + catch (dbixx_error &e) + { + *sql << "ROLLBACK TO SAVEPOINT SETUSERINFO",exec(); + *sql << S_USERINFOUPDATE,team,model,skill,gamenumber,second,guid,exec(); + } + DebugMessage("setPlayerInfo for "+nickname+" with GUID: "+guid); } -void Db2DbiXX::addKill(int second, string attackerID, string targetID, int type) { - if(!isok) - return; - *sql<< S_KILL,gamenumber,second,attackerID,targetID,type,exec(); - DebugMessage("addKill"); +void Db2DbiXX::addKill(int second, string attackerID, string targetID, int type) +{ + if(!isok) + return; + *sql<< S_KILL,gamenumber,second,attackerID,targetID,type,exec(); + DebugMessage("addKill"); } /*void Db2DbiXX::addCapture(int second, string player, int team) { @@ -247,128 +277,149 @@ void Db2DbiXX::addKill(int second, string attackerID, string targetID, int type) DebugMessage("addCapture"); }*/ -void Db2DbiXX::addAward(int second, string player, int award) { - if(!isok) - return; - *sql << S_AWARD,gamenumber,second,player,award,exec(); - DebugMessage("addAward"); +void Db2DbiXX::addAward(int second, string player, int award) +{ + if(!isok) + return; + *sql << S_AWARD,gamenumber,second,player,award,exec(); + DebugMessage("addAward"); } -void Db2DbiXX::addScoreInfo(int second, string player, int score) { - if(!isok) - return; - *sql << S_POINT,gamenumber,second,player,score,exec(); - DebugMessage("addScoreInfo"); +void Db2DbiXX::addScoreInfo(int second, string player, int score) +{ + if(!isok) + return; + *sql << S_POINT,gamenumber,second,player,score,exec(); + DebugMessage("addScoreInfo"); } -void Db2DbiXX::addCtf(int second, string player, int team, int event) { - if(!isok) - return; - *sql << S_CTF,gamenumber,second,team,player,event,exec(); - DebugMessage("addCtf"); +void Db2DbiXX::addCtf(int second, string player, int team, int event) +{ + if(!isok) + return; + *sql << S_CTF,gamenumber,second,team,player,event,exec(); + DebugMessage("addCtf"); } -void Db2DbiXX::addCtf1f(int second, string player, int team, int event) { - if(!isok) - return; - *sql << S_CTF1F,gamenumber,second,team,player,event,exec(); - DebugMessage("addCtf1f"); +void Db2DbiXX::addCtf1f(int second, string player, int team, int event) +{ + if(!isok) + return; + *sql << S_CTF1F,gamenumber,second,team,player,event,exec(); + DebugMessage("addCtf1f"); } -void Db2DbiXX::addElimination(int second, int roundnumber, int team, int event) { - if(!isok) - return; - *sql << S_ELIMINATION,gamenumber,second,team,event,roundnumber,exec(); - DebugMessage("addElimination"); +void Db2DbiXX::addElimination(int second, int roundnumber, int team, int event) +{ + if(!isok) + return; + *sql << S_ELIMINATION,gamenumber,second,team,event,roundnumber,exec(); + DebugMessage("addElimination"); } -void Db2DbiXX::addCtfElimination(int second, int roundnumber, string player, int team, int event) { - if(!isok) - return; - *sql << S_CTF_ELIM,gamenumber,second,team,player,event,roundnumber,exec(); - DebugMessage("addCtfElimination"); +void Db2DbiXX::addCtfElimination(int second, int roundnumber, string player, int team, int event) +{ + if(!isok) + return; + *sql << S_CTF_ELIM,gamenumber,second,team,player,event,roundnumber,exec(); + DebugMessage("addCtfElimination"); } -void Db2DbiXX::addHarvester(int second, string player1, string player2, int team, int event, int score) { - if(!isok) - return; - *sql << S_HARVESTER,gamenumber,second,team,player1,player2,event,score,exec(); - DebugMessage("addHarvester"); +void Db2DbiXX::addHarvester(int second, string player1, string player2, int team, int event, int score) +{ + if(!isok) + return; + *sql << S_HARVESTER,gamenumber,second,team,player1,player2,event,score,exec(); + DebugMessage("addHarvester"); } -void Db2DbiXX::addChallenge(int second, string player, int challenge, int amount) { - if(!isok) - return; - *sql << S_CHALLENGES,gamenumber,player,challenge,amount,exec(); - DebugMessage("addChallenge"); +void Db2DbiXX::addChallenge(int second, string player, int challenge, int amount) +{ + if(!isok) + return; + *sql << S_CHALLENGES,gamenumber,player,challenge,amount,exec(); + DebugMessage("addChallenge"); } -int Db2DbiXX::getNextGameNumber() { - int result = -1; - row r; - *sql<< GETNEXTGAMENUMBER; - DebugMessage("Gettings next game number"); - if(sql->single(r)) { - r>> result; - return result; - } - else { - throw "Could not get next gamenumber"; - } +int Db2DbiXX::getNextGameNumber() +{ + int result = -1; + row r; + *sql<< GETNEXTGAMENUMBER; + DebugMessage("Gettings next game number"); + if(sql->single(r)) + { + r>> result; + return result; + } + else + { + throw "Could not get next gamenumber"; + } } -int Db2DbiXX::getLastGameNumber() { - int result = -1; - row r; - *sql<< S_GETLASTGAMENUMBER; - DebugMessage("Gettings last game number"); - if(sql->single(r)) { - r>> result; - cout << "Game number: " << result << endl; - return result; - } - else { - throw "Could not get last gamenumber"; - } +int Db2DbiXX::getLastGameNumber() +{ + int result = -1; + row r; + *sql<< S_GETLASTGAMENUMBER; + DebugMessage("Gettings last game number"); + if(sql->single(r)) + { + r>> result; + cout << "Game number: " << result << endl; + return result; + } + else + { + throw "Could not get last gamenumber"; + } } /* This is a helper function so we never forget to start a new transaction after a commit. */ -void Db2DbiXX::Commit() { - if(!isok) - { - Rollback(); - return; - } - commitlock->commit(); - delete commitlock; - commitlock = new transaction(*sql); - DebugMessage("Commited"); +void Db2DbiXX::Commit() +{ + if(!isok) + { + Rollback(); + return; + } + commitlock->commit(); + delete commitlock; + commitlock = new transaction(*sql); + DebugMessage("Commited"); } -void Db2DbiXX::Rollback() { - commitlock->rollback(); - delete commitlock; - commitlock = new transaction(*sql); - DebugMessage("Rollback"); +void Db2DbiXX::Rollback() +{ + commitlock->rollback(); + delete commitlock; + commitlock = new transaction(*sql); + DebugMessage("Rollback"); } -bool Db2DbiXX::Ok() { - return isok; +bool Db2DbiXX::Ok() +{ + return isok; } -void Db2DbiXX::SetOk(bool ok) { - isok = ok; +void Db2DbiXX::SetOk(bool ok) +{ + isok = ok; } -void Db2DbiXX::doNotCommit() { - SetOk(false); +void Db2DbiXX::doNotCommit() +{ + SetOk(false); } -void Db2DbiXX::DebugMessage(string msg) { - if(debug) { - cout << "oastat: " << msg << endl; - } +void Db2DbiXX::DebugMessage(string msg) +{ + if(debug) + { + cout << "oastat: " << msg << endl; + } } diff --git a/src/db/Db2DbiXX.hpp b/src/db/Db2DbiXX.hpp index b7e6921..90c0f4e 100644 --- a/src/db/Db2DbiXX.hpp +++ b/src/db/Db2DbiXX.hpp @@ -34,49 +34,50 @@ http://code.google.com/p/oastat/ using namespace std; using namespace dbixx; -class Db2DbiXX : public Database { +class Db2DbiXX : public Database +{ public: - Db2DbiXX(); - Db2DbiXX(string dbargs); - Db2DbiXX(const Db2DbiXX& orig); - virtual ~Db2DbiXX(); - void createTables(); - void startGame(int gametype, string mapname, string basegame, string servername, OaStatStruct *oss); - void addGameCvar(string cvar, string value); - void endGame(int second); - int getGameNumber(); - void setPlayerInfo(string guid, string nickname, bool isBot, int second, int team, string model, string headmodel, int skill, OaStatStruct *oss); - void addKill(int second, string attackerID, string targetID, int type); - //void addCapture(int second, string player, int team); - void addAward(int second, string player, int award); - void addScoreInfo(int second, string player, int score); - void addCtf(int second, string player, int team, int event); - void addCtf1f(int second, string player, int team, int event); - void addElimination(int second, int roundnumber, int team, int event); - void addCtfElimination(int second, int roundnumber, string player, int team, int event); - void addHarvester(int second, string player1, string player2, int team, int event, int score); - void addChallenge(int second, string player, int challenge, int amount); - void doNotCommit(); + Db2DbiXX(); + Db2DbiXX(string dbargs); + Db2DbiXX(const Db2DbiXX& orig); + virtual ~Db2DbiXX(); + void createTables(); + void startGame(int gametype, string mapname, string basegame, string servername, OaStatStruct *oss); + void addGameCvar(string cvar, string value); + void endGame(int second); + int getGameNumber(); + void setPlayerInfo(string guid, string nickname, bool isBot, int second, int team, string model, string headmodel, int skill, OaStatStruct *oss); + void addKill(int second, string attackerID, string targetID, int type); + //void addCapture(int second, string player, int team); + void addAward(int second, string player, int award); + void addScoreInfo(int second, string player, int score); + void addCtf(int second, string player, int team, int event); + void addCtf1f(int second, string player, int team, int event); + void addElimination(int second, int roundnumber, int team, int event); + void addCtfElimination(int second, int roundnumber, string player, int team, int event); + void addHarvester(int second, string player1, string player2, int team, int event, int score); + void addChallenge(int second, string player, int challenge, int amount); + void doNotCommit(); private: - session *sql; - transaction *commitlock; - bool isok; - bool debug; - int gamenumber; - int getNextGameNumber(); - int getLastGameNumber(); - void Commit(); - void Rollback(); - bool Ok(); - void SetOk(bool ok); - void DebugMessage(string msg); - void InitStrings(string backend); - void ReadConfigFromDb(); - bool IsDuplicate(string servername, tm thetime); - string sql_backend; - bool last_value; - set cvars2save; - set uservars2save; + session *sql; + transaction *commitlock; + bool isok; + bool debug; + int gamenumber; + int getNextGameNumber(); + int getLastGameNumber(); + void Commit(); + void Rollback(); + bool Ok(); + void SetOk(bool ok); + void DebugMessage(string msg); + void InitStrings(string backend); + void ReadConfigFromDb(); + bool IsDuplicate(string servername, tm thetime); + string sql_backend; + bool last_value; + set cvars2save; + set uservars2save; }; #endif /* _DB2DBIXX_HPP */ diff --git a/src/db/Db2PostgreSQL.cpp b/src/db/Db2PostgreSQL.cpp index fcc535c..a0aec81 100644 --- a/src/db/Db2PostgreSQL.cpp +++ b/src/db/Db2PostgreSQL.cpp @@ -49,31 +49,35 @@ static char* booltext[2] = {"false","true"}; * Escapes special chaecters. The best solution should be to use PQescapeStringConn * but unfortunatly I had limited success likely because the C++ string class * already handles encoding. The only char postgreSql can be cheated by is the - * single quete, so that is the only one we removes. + * single quete, so that is the only one we removes. * * @param unescaped string * @return escaped string */ -string Db2PostgreSQL::sqlescape(string sql) { - #if 1 - string output = ""; - string::iterator itr; - for(itr = sql.begin();itr=0 if succes and/or >0 if returned tubles */ -int Db2PostgreSQL::simpleQuery(const char* query) { - res = PQexec(conn, query_string); - int ret; - if (PQresultStatus(res) != PGRES_COMMAND_OK) { - if (PQresultStatus(res) == PGRES_TUPLES_OK ) { - //If a SELECT STATEMENT count results - ret = PQntuples(res); - PQclear(res); - return ret; - } - PQclear(res); - cout << "FAIL: " << query << endl; - return -1; - } - PQclear(res); //We don't relly care about the result - cout << "SUCCES: " << query << endl; - return 0; +int Db2PostgreSQL::simpleQuery(const char* query) +{ + res = PQexec(conn, query_string); + int ret; + if (PQresultStatus(res) != PGRES_COMMAND_OK) + { + if (PQresultStatus(res) == PGRES_TUPLES_OK ) + { + //If a SELECT STATEMENT count results + ret = PQntuples(res); + PQclear(res); + return ret; + } + PQclear(res); + cout << "FAIL: " << query << endl; + return -1; + } + PQclear(res); //We don't relly care about the result + cout << "SUCCES: " << query << endl; + return 0; } -Db2PostgreSQL::Db2PostgreSQL() { - conn = PQconnectdb("dbname=oastat"); //Connect to the database - if (PQstatus(conn) == CONNECTION_BAD) - throw "Failed to open connection to DB!"; - gamenumber = -1; - cout << "CONNECTED" << endl; +Db2PostgreSQL::Db2PostgreSQL() +{ + conn = PQconnectdb("dbname=oastat"); //Connect to the database + if (PQstatus(conn) == CONNECTION_BAD) + throw "Failed to open connection to DB!"; + gamenumber = -1; + cout << "CONNECTED" << endl; } -Db2PostgreSQL::Db2PostgreSQL(string args) { - conn = PQconnectdb(args.c_str()); //Connect to the database - if (PQstatus(conn) == CONNECTION_BAD) - throw "Failed to open connection to DB!"; - cout << "CONNECTED" << endl; +Db2PostgreSQL::Db2PostgreSQL(string args) +{ + conn = PQconnectdb(args.c_str()); //Connect to the database + if (PQstatus(conn) == CONNECTION_BAD) + throw "Failed to open connection to DB!"; + cout << "CONNECTED" << endl; } -Db2PostgreSQL::Db2PostgreSQL(const Db2PostgreSQL& orig) { - throw "May not make copy of Db2PostgreSQL"; +Db2PostgreSQL::Db2PostgreSQL(const Db2PostgreSQL& orig) +{ + throw "May not make copy of Db2PostgreSQL"; } -Db2PostgreSQL::~Db2PostgreSQL() { - PQfinish(conn); +Db2PostgreSQL::~Db2PostgreSQL() +{ + PQfinish(conn); } void Db2PostgreSQL::createTables() { - //Nothing to do + //Nothing to do } void Db2PostgreSQL::startGame(int gametype, string mapname, string basegame) { - simpleQuery("ROLLBACK"); //In case we have just completed warmup or - simpleQuery("BEGIN"); - //simpleQuery("LOCK TABLE OASTAT_GAMES IN SHARE ROW EXCLUSIVE"); //Hold a lock until we have selected the just inserted line - gamenumber = -1; //Prevent any following sql commands from completing until this is set! - sprintf(query_string,STARTGAME,gametype,mapname.c_str(),basegame.c_str()); - res = PQexec(conn, query_string); - if (PQresultStatus(res) != PGRES_COMMAND_OK) { - PQclear(res); - cout << "FAIL: " << query_string << endl; - simpleQuery("ROLLBACK"); - return; - } - PQclear(res); - //res = PQexec(conn, "SELECT MAX(gamenumber) FROM OASTAT_GAMES"); - res = PQexec(conn, "SELECT currval('oastat_games_gamenumber_seq')"); - if (PQresultStatus(res) != PGRES_TUPLES_OK ) { - PQclear(res); - simpleQuery("ROLLBACK"); - return; - } - gamenumber = atoi(PQgetvalue(res,0,0)); - cout << "GAME: " << PQgetvalue(res,0,0) << ":" << gamenumber << endl; - PQclear(res); - //simpleQuery("COMMIT"); //release lock - //simpleQuery("BEGIN"); //This is just for performace... + simpleQuery("ROLLBACK"); //In case we have just completed warmup or + simpleQuery("BEGIN"); + //simpleQuery("LOCK TABLE OASTAT_GAMES IN SHARE ROW EXCLUSIVE"); //Hold a lock until we have selected the just inserted line + gamenumber = -1; //Prevent any following sql commands from completing until this is set! + sprintf(query_string,STARTGAME,gametype,mapname.c_str(),basegame.c_str()); + res = PQexec(conn, query_string); + if (PQresultStatus(res) != PGRES_COMMAND_OK) + { + PQclear(res); + cout << "FAIL: " << query_string << endl; + simpleQuery("ROLLBACK"); + return; + } + PQclear(res); + //res = PQexec(conn, "SELECT MAX(gamenumber) FROM OASTAT_GAMES"); + res = PQexec(conn, "SELECT currval('oastat_games_gamenumber_seq')"); + if (PQresultStatus(res) != PGRES_TUPLES_OK ) + { + PQclear(res); + simpleQuery("ROLLBACK"); + return; + } + gamenumber = atoi(PQgetvalue(res,0,0)); + cout << "GAME: " << PQgetvalue(res,0,0) << ":" << gamenumber << endl; + PQclear(res); + //simpleQuery("COMMIT"); //release lock + //simpleQuery("BEGIN"); //This is just for performace... } int Db2PostgreSQL::getGameNumber() { - return gamenumber; //Only to standard out, normally this should be read from db!!! + return gamenumber; //Only to standard out, normally this should be read from db!!! } void Db2PostgreSQL::setPlayerInfo(string guid, string nickname, bool isBot, int second, int team, string model, string headmodel, int skill) { - //If team < 0 then it is a disconnect event that need not to be in - if(team>-1) - { - /* - Note: There is a possible race condition here! - * There should be - */ - //simpleQuery("COMMIT"); - sprintf(query_string,"SELECT * FROM oastat_players WHERE GUID = '%s'",sqlescape(guid).c_str()); - if(simpleQuery(query_string)<1) { - sprintf(query_string,PLAYERSINSERT,sqlescape(guid).c_str(),sqlescape(nickname).c_str(),booltext[isBot],sqlescape(model).c_str(),sqlescape(headmodel).c_str()); - simpleQuery(query_string); - } - sprintf(query_string,PLAYERSUPDATE,sqlescape(nickname).c_str(),booltext[isBot],sqlescape(model).c_str(),sqlescape(headmodel).c_str(),guid.c_str()); - simpleQuery(query_string); - //simpleQuery("BEGIN"); - } - sprintf(query_string,USERINFOINSERT,getGameNumber(),second,sqlescape(guid).c_str(),team,sqlescape(model).c_str(),skill); - simpleQuery(query_string); + //If team < 0 then it is a disconnect event that need not to be in + if(team>-1) + { + /* + Note: There is a possible race condition here! + * There should be + */ + //simpleQuery("COMMIT"); + sprintf(query_string,"SELECT * FROM oastat_players WHERE GUID = '%s'",sqlescape(guid).c_str()); + if(simpleQuery(query_string)<1) + { + sprintf(query_string,PLAYERSINSERT,sqlescape(guid).c_str(),sqlescape(nickname).c_str(),booltext[isBot],sqlescape(model).c_str(),sqlescape(headmodel).c_str()); + simpleQuery(query_string); + } + sprintf(query_string,PLAYERSUPDATE,sqlescape(nickname).c_str(),booltext[isBot],sqlescape(model).c_str(),sqlescape(headmodel).c_str(),guid.c_str()); + simpleQuery(query_string); + //simpleQuery("BEGIN"); + } + sprintf(query_string,USERINFOINSERT,getGameNumber(),second,sqlescape(guid).c_str(),team,sqlescape(model).c_str(),skill); + simpleQuery(query_string); } void Db2PostgreSQL::addKill(int second, string attackerID, string targetID, int type) { - sprintf(query_string,KILL,gamenumber,second,sqlescape(attackerID).c_str(),sqlescape(targetID).c_str(),type); - simpleQuery(query_string); + sprintf(query_string,KILL,gamenumber,second,sqlescape(attackerID).c_str(),sqlescape(targetID).c_str(),type); + simpleQuery(query_string); } void Db2PostgreSQL::endGame(int second) { - sprintf(query_string,ENDGAME,second,gamenumber); - simpleQuery(query_string); - simpleQuery("COMMIT"); //This is just for performace... + sprintf(query_string,ENDGAME,second,gamenumber); + simpleQuery(query_string); + simpleQuery("COMMIT"); //This is just for performace... } void Db2PostgreSQL::addCapture(int second, string player, int team) { - sprintf(query_string,CAPTURE,gamenumber,second,sqlescape(player).c_str(),team); - simpleQuery(query_string); + sprintf(query_string,CAPTURE,gamenumber,second,sqlescape(player).c_str(),team); + simpleQuery(query_string); } void Db2PostgreSQL::addAward(int second, string player, int award) { - sprintf(query_string,AWARD,gamenumber,second,sqlescape(player).c_str(),award); - simpleQuery(query_string); + sprintf(query_string,AWARD,gamenumber,second,sqlescape(player).c_str(),award); + simpleQuery(query_string); } \ No newline at end of file diff --git a/src/db/Db2PostgreSQL.h b/src/db/Db2PostgreSQL.h index ca6ae0f..d220673 100644 --- a/src/db/Db2PostgreSQL.h +++ b/src/db/Db2PostgreSQL.h @@ -29,27 +29,28 @@ http://code.google.com/p/oastat/ using namespace std; -class Db2PostgreSQL : public Database{ +class Db2PostgreSQL : public Database +{ public: - Db2PostgreSQL(); - Db2PostgreSQL(string args); - Db2PostgreSQL(const Db2PostgreSQL& orig); - virtual ~Db2PostgreSQL(); - void createTables(); - void startGame(int gametype, string mapname, string basegame); - void endGame(int second); - int getGameNumber(); - void setPlayerInfo(string guid, string nickname, bool isBot, int second, int team, string model, string headmodel, int skill); - void addKill(int second, string attackerID, string targetID, int type); - void addCapture(int second, string player, int team); - void addAward(int second, string player, int award); + Db2PostgreSQL(); + Db2PostgreSQL(string args); + Db2PostgreSQL(const Db2PostgreSQL& orig); + virtual ~Db2PostgreSQL(); + void createTables(); + void startGame(int gametype, string mapname, string basegame); + void endGame(int second); + int getGameNumber(); + void setPlayerInfo(string guid, string nickname, bool isBot, int second, int team, string model, string headmodel, int skill); + void addKill(int second, string attackerID, string targetID, int type); + void addCapture(int second, string player, int team); + void addAward(int second, string player, int award); private: - int simpleQuery(const char *); - string sqlescape(string sql); - PGconn *conn; - PGresult *res; - int gamenumber; - char query_string[4096]; + int simpleQuery(const char *); + string sqlescape(string sql); + PGconn *conn; + PGresult *res; + int gamenumber; + char query_string[4096]; }; #endif /* _DB2POSTGRESQL_H */ diff --git a/src/db/Db2Xml.cpp b/src/db/Db2Xml.cpp index 9d7b779..a10dc1e 100644 --- a/src/db/Db2Xml.cpp +++ b/src/db/Db2Xml.cpp @@ -24,34 +24,36 @@ http://code.google.com/p/oastat/ #include "Db2Xml.hpp" -Db2Xml::Db2Xml() +Db2Xml::Db2Xml() { psoudo_playerids.clear(); nextId = 1; - boost::format path("%1%/oastat"); - path % getenv("HOME"); - p_output_dir = path.str(); - boost::format fmkdir("mkdir -p %1%"); - fmkdir % p_output_dir; - system(fmkdir.str().c_str()); + boost::format path("%1%/oastat"); + path % getenv("HOME"); + p_output_dir = path.str(); + boost::format fmkdir("mkdir -p %1%"); + fmkdir % p_output_dir; + system(fmkdir.str().c_str()); } Db2Xml::Db2Xml(string dbargs) { boost::format path("%1%/oastat"); - path % getenv("HOME"); - p_output_dir = path.str(); - p_postscript = ""; + path % getenv("HOME"); + p_output_dir = path.str(); + p_postscript = ""; psoudo_playerids.clear(); nextId = 1; //mkdir(p_output_dir.c_str()); - + stringstream stream(stringstream::in | stringstream::out); stream << dbargs; string holder; //stream >> holder; - while(!stream.eof()) { + while(!stream.eof()) + { stream >> holder; - if(!stream.eof()) { + if(!stream.eof()) + { string param; stream >> param; if(holder == "outputdir") @@ -60,9 +62,9 @@ Db2Xml::Db2Xml(string dbargs) p_postscript = param; } } - boost::format fmkdir("mkdir -p %1%"); - fmkdir % p_output_dir; - system(fmkdir.str().c_str()); + boost::format fmkdir("mkdir -p %1%"); + fmkdir % p_output_dir; + system(fmkdir.str().c_str()); } Db2Xml::Db2Xml(const Db2Xml& orig) { @@ -70,7 +72,7 @@ Db2Xml::Db2Xml(const Db2Xml& orig) } Db2Xml::~Db2Xml() { - + } void Db2Xml::startGame(int gametype, string mapname, string basegame, string servername, OaStatStruct *oss) { @@ -83,9 +85,9 @@ void Db2Xml::startGame(int gametype, string mapname, string basegame, string ser p_xmlcontent += (boost::format(" %1%\n") % gametype).str(); p_xmlcontent += (boost::format(" %1%\n") % getXmlEscaped(mapname) ).str(); p_xmlcontent += (boost::format(" %1%\n") % getXmlEscaped(basegame) ).str(); - p_xmlcontent += (boost::format(" %04i-%02i-%02iT%02i:%02i:%02i\n") - % (p_gametime.tm_year+1900) % (p_gametime.tm_mon+1) % (p_gametime.tm_mday) - % (p_gametime.tm_hour) % (p_gametime.tm_min) % (p_gametime.tm_sec) ).str(); + p_xmlcontent += (boost::format(" %04i-%02i-%02iT%02i:%02i:%02i\n") + % (p_gametime.tm_year+1900) % (p_gametime.tm_mon+1) % (p_gametime.tm_mday) + % (p_gametime.tm_hour) % (p_gametime.tm_min) % (p_gametime.tm_sec) ).str(); } void Db2Xml::addGameCvar(string cvar, string value) { @@ -98,16 +100,17 @@ void Db2Xml::addGameCvar(string cvar, string value) } void Db2Xml::endGame(int second) { - if(isOk) { + if(isOk) + { p_xmlcontent += (boost::format(" %1%\n") % second).str(); p_xmlcontent += "\n"; string filename = (boost::format("%1%/%2%_%3%-%4%-%5%_%6%-%7%-%8%.xml") % p_output_dir % p_servername - % (p_gametime.tm_year+1900) % (p_gametime.tm_mon+1) % (p_gametime.tm_mday) - % (p_gametime.tm_hour) % (p_gametime.tm_min) % (p_gametime.tm_sec) ).str(); + % (p_gametime.tm_year+1900) % (p_gametime.tm_mon+1) % (p_gametime.tm_mday) + % (p_gametime.tm_hour) % (p_gametime.tm_min) % (p_gametime.tm_sec) ).str(); ofstream outfile; outfile.open (filename.c_str()); - if(outfile.fail()) - cout << "could not create " << filename << endl; + if(outfile.fail()) + cout << "could not create " << filename << endl; outfile << p_xmlcontent; outfile.close(); cout << "End game at " << second << "with size " << p_xmlcontent.length() << " written to " << filename << endl; @@ -119,7 +122,7 @@ int Db2Xml::getGameNumber() } void Db2Xml::setPlayerInfo(string guid, string nickname, bool isBot, int second, int team, string model, string headmodel, int skill, OaStatStruct *oss) { - if(!isOk) + if(!isOk) return; p_xmlcontent += " \n"; p_xmlcontent += (boost::format(" %1%\n") % guid).str(); @@ -135,7 +138,7 @@ void Db2Xml::setPlayerInfo(string guid, string nickname, bool isBot, int second, } void Db2Xml::addKill(int second, string attackerID, string targetID, int type) { - if(!isOk) + if(!isOk) return; p_xmlcontent += " \n"; p_xmlcontent += (boost::format(" %1%\n") % second).str(); @@ -147,7 +150,7 @@ void Db2Xml::addKill(int second, string attackerID, string targetID, int type) //void addCapture(int second, string player, int team); void Db2Xml::addAward(int second, string player, int award) { - if(!isOk) + if(!isOk) return; p_xmlcontent += " \n"; p_xmlcontent += (boost::format(" %1%\n") % second).str(); @@ -157,7 +160,7 @@ void Db2Xml::addAward(int second, string player, int award) } void Db2Xml::addScoreInfo(int second, string player, int score) { - if(!isOk) + if(!isOk) return; p_xmlcontent += " \n"; p_xmlcontent += (boost::format(" %1%\n") % second).str(); @@ -167,7 +170,7 @@ void Db2Xml::addScoreInfo(int second, string player, int score) } void Db2Xml::addCtf(int second, string player, int team, int event) { - if(!isOk) + if(!isOk) return; p_xmlcontent += " \n"; p_xmlcontent += (boost::format(" %1%\n") % second).str(); @@ -178,7 +181,7 @@ void Db2Xml::addCtf(int second, string player, int team, int event) } void Db2Xml::addCtf1f(int second, string player, int team, int event) { - if(!isOk) + if(!isOk) return; p_xmlcontent += " \n"; p_xmlcontent += (boost::format(" %1%\n") % second).str(); @@ -189,7 +192,7 @@ void Db2Xml::addCtf1f(int second, string player, int team, int event) } void Db2Xml::addElimination(int second, int roundnumber, int team, int event) { - if(!isOk) + if(!isOk) return; p_xmlcontent += " \n"; p_xmlcontent += (boost::format(" %1%\n") % second).str(); @@ -200,7 +203,7 @@ void Db2Xml::addElimination(int second, int roundnumber, int team, int event) } void Db2Xml::addCtfElimination(int second, int roundnumber, string player, int team, int event) { - if(!isOk) + if(!isOk) return; p_xmlcontent += " \n"; p_xmlcontent += (boost::format(" %1%\n") % second).str(); @@ -208,11 +211,11 @@ void Db2Xml::addCtfElimination(int second, int roundnumber, string player, int t p_xmlcontent += (boost::format(" %1%\n") % player).str(); p_xmlcontent += (boost::format(" %1%\n") % team).str(); p_xmlcontent += (boost::format(" %1%\n") % event).str(); - p_xmlcontent += " \n"; + p_xmlcontent += " \n"; } void Db2Xml::addHarvester(int second, string player1, string player2, int team, int event, int score) { - if(!isOk) + if(!isOk) return; p_xmlcontent += " \n"; p_xmlcontent += (boost::format(" %1%\n") % second).str(); @@ -221,11 +224,11 @@ void Db2Xml::addHarvester(int second, string player1, string player2, int team, p_xmlcontent += (boost::format(" %1%\n") % team).str(); p_xmlcontent += (boost::format(" %1%\n") % event).str(); p_xmlcontent += (boost::format(" %1%\n") % score).str(); - p_xmlcontent += " \n"; + p_xmlcontent += " \n"; } void Db2Xml::addChallenge(int second, string player, int challenge, int amount) { - if(!isOk) + if(!isOk) return; p_xmlcontent += " \n"; p_xmlcontent += (boost::format(" %1%\n") % second).str(); @@ -239,7 +242,7 @@ void Db2Xml::doNotCommit() isOk = false; } -int Db2Xml::getPsoudoId(string guid) +int Db2Xml::getPsoudoId(string guid) { int ret = psoudo_playerids[guid]; if(ret > 0) @@ -251,7 +254,7 @@ int Db2Xml::getPsoudoId(string guid) return ret; } -string Db2Xml::getXmlEscaped(string org) +string Db2Xml::getXmlEscaped(string org) { string result = org; boost::algorithm::replace_all(result,(string)"&",(string)"&"); @@ -259,10 +262,11 @@ string Db2Xml::getXmlEscaped(string org) boost::algorithm::replace_all(result,(string)">",(string)">"); boost::algorithm::replace_all(result,(string)"\"",(string)"""); boost::algorithm::replace_all(result,(string)"'",(string)"'"); - + return result; } -void Db2Xml::createTables() { - +void Db2Xml::createTables() +{ + } \ No newline at end of file diff --git a/src/db/Db2Xml.hpp b/src/db/Db2Xml.hpp index fd1f898..85ecdfb 100644 --- a/src/db/Db2Xml.hpp +++ b/src/db/Db2Xml.hpp @@ -23,7 +23,7 @@ http://code.google.com/p/oastat/ /* * This file generates a file - * + * */ #ifndef DB2XML_HPP @@ -36,44 +36,45 @@ http://code.google.com/p/oastat/ #include #include -class Db2Xml : public Database { +class Db2Xml : public Database +{ public: - Db2Xml(); - Db2Xml(string dbargs); - Db2Xml(const Db2Xml& orig); - virtual ~Db2Xml(); + Db2Xml(); + Db2Xml(string dbargs); + Db2Xml(const Db2Xml& orig); + virtual ~Db2Xml(); void createTables(); - void startGame(int gametype, string mapname, string basegame, string servername, OaStatStruct *oss); - void addGameCvar(string cvar, string value); - void endGame(int second); - int getGameNumber(); - void setPlayerInfo(string guid, string nickname, bool isBot, int second, int team, string model, string headmodel, int skill, OaStatStruct *oss); - void addKill(int second, string attackerID, string targetID, int type); - //void addCapture(int second, string player, int team); - void addAward(int second, string player, int award); - void addScoreInfo(int second, string player, int score); - void addCtf(int second, string player, int team, int event); - void addCtf1f(int second, string player, int team, int event); - void addElimination(int second, int roundnumber, int team, int event); - void addCtfElimination(int second, int roundnumber, string player, int team, int event); - void addHarvester(int second, string player1, string player2, int team, int event, int score); - void addChallenge(int second, string player, int challenge, int amount); - void doNotCommit(); + void startGame(int gametype, string mapname, string basegame, string servername, OaStatStruct *oss); + void addGameCvar(string cvar, string value); + void endGame(int second); + int getGameNumber(); + void setPlayerInfo(string guid, string nickname, bool isBot, int second, int team, string model, string headmodel, int skill, OaStatStruct *oss); + void addKill(int second, string attackerID, string targetID, int type); + //void addCapture(int second, string player, int team); + void addAward(int second, string player, int award); + void addScoreInfo(int second, string player, int score); + void addCtf(int second, string player, int team, int event); + void addCtf1f(int second, string player, int team, int event); + void addElimination(int second, int roundnumber, int team, int event); + void addCtfElimination(int second, int roundnumber, string player, int team, int event); + void addHarvester(int second, string player1, string player2, int team, int event, int score); + void addChallenge(int second, string player, int challenge, int amount); + void doNotCommit(); private: - string p_output_dir; + string p_output_dir; string p_servername; //Used for filename tm p_gametime; //Used for filename - string p_xmlcontent; - string p_postscript; - bool isOk; - + string p_xmlcontent; + string p_postscript; + bool isOk; + /* * To make it easier to parse the XML file oastat will generate a unique id for each game * This is used then converting back to a log file */ map psoudo_playerids; int nextId; - + int getPsoudoId(string guid); string getXmlEscaped(string org); }; diff --git a/src/db/database.hpp b/src/db/database.hpp index f1b2d2e..b30fc0b 100644 --- a/src/db/database.hpp +++ b/src/db/database.hpp @@ -33,165 +33,166 @@ using namespace std; /* This is the class that all database connections need to inherit to be able to recieve data from the program */ -class Database { - public: - virtual void createTables() = 0; - - /** - * Starts a new game in the database might also start a transaction - * - * @param gametype - integer of the gametype DM=0,CTF=4, Elim=8 and so on - * @param mapname - the mapname used for this game - * @param basegame - the mod... normally baseoa - */ - virtual void startGame(int gametype, string mapname, string basegame, string servername, OaStatStruct *oss) = 0; - - /** - * Adds information about a cvar to the last started game - * - * @param cvar - The name of the cvar (limited to 100 chars) - * @param value - The value of the cvar as a string - */ - virtual void addGameCvar(string cvar, string value) = 0; - - /** - * Called then a game ends. - * - * @param second - relative time since game start - */ - virtual void endGame(int second) = 0; - - /** - * Document getGameNumber() here... - * - * @return the gamenumber that we are working on. Retrieved from the db. - */ - virtual int getGameNumber() = 0; - - /** - * If this is called the game will be not be written to the database. This is good for warmup or games ended prematurely. - */ - virtual void doNotCommit() = 0; - - /** - * Document setPlayerInfo(string,string,bool,int,int,string,string,int) here... - * - * @param guid - the hashed GUID of the player - * @param nickname - The players nickname - * @param isBot - true if a bot, false otherwise - * @param second - relative time for the event - * @param team - team number to join, -1 if leaving - * @param model - model used - * @param headmodel - headmodel used - * @param skill - bot skill, bots only - */ - virtual void setPlayerInfo(string guid, string nickname, bool isBot, int second, int team, string model, string headmodel, int skill, OaStatStruct *oss) = 0; - - /** - * Represents a kill - * - * @param second time of the kill - * @param attackerID hashed guid of the attacker - * @param targetID hashed guid of the victim - * @param type weapon number - */ - virtual void addKill(int second, string attackerID, string targetID, int type) = 0; - - /** - * A flag/skull capture or obelisk destruction - * - * @param second time of the event - * @param player guid of the player if any, empty if none - * @param team - */ - //virtual void addCapture(int second, string player, int team) = 0; - - /** - * Add an award: Like Defence, Excelent, Gauntlet and so on - * - * @param second time of the - * @param player hashed guid of the player - * @param award the award, numbers must be found in code - */ - virtual void addAward(int second, string player, int award) = 0; - - /** - * Adds information about a players score that is changing. - * Notice that the score is the total amount of points the - * player has for the entire match - * - * @param second time of the event - * @param player hashed guid of the player - * @param score the Total - */ - virtual void addScoreInfo(int second, string player, int score) = 0; - - /** - * Adds an event from a CTF-game. - * - * Note that event: - * 0 | Flag is taken - * 1 | Flag is captured - * 2 | Flag is returned - * 3 | Flagcarrier got killed - * - * @param second - * @param player - * @param team - * @param event - */ - virtual void addCtf(int second, string player, int team, int event) = 0; - - /** - * Adds an event from a 1FCTF-game. - * - * Note that event: - * 0 | Flag is taken - * 1 | Flag is captured - * 2 | Flag is returned (like timeout) - * 3 | Flagcarrier got killed - * - * @param second - * @param player - * @param team - * @param event - */ - virtual void addCtf1f(int second, string player, int team, int event) = 0; - - virtual void addElimination(int second, int roundnumber, int team, int event) = 0; - - /** - * Events for the CTF Elimination gametype - * - * @param second second from game start - * @param roundnumber the round number - * @param player the player the event is about. Possible blank - * @param team the team the event is about - * @param event the event type - */ - virtual void addCtfElimination(int second, int roundnumber, string player, int team, int event) = 0; - - /** - * Document addHarvester(int,string,string,int,int,int) here... - * - * @param second time from gamestart - * @param player1 the player the event is about - * @param player2 the second player the event is about (player2 is fragcarrier) - * @param team the team from the logging event - * @param event the event number - * @param score number of skulls brought in in one capture - */ - virtual void addHarvester(int second, string player1, string player2, int team, int event, int score) = 0; - - /** - * Add information about a scored challenge - * - * @param second time from gamestart - * @param player player getting the challenge - * @param challenge the challenge gotten (see challenges.h) - * @param amount normally 1 - */ - virtual void addChallenge(int second, string player, int challenge, int amount) = 0; +class Database +{ +public: + virtual void createTables() = 0; + + /** + * Starts a new game in the database might also start a transaction + * + * @param gametype - integer of the gametype DM=0,CTF=4, Elim=8 and so on + * @param mapname - the mapname used for this game + * @param basegame - the mod... normally baseoa + */ + virtual void startGame(int gametype, string mapname, string basegame, string servername, OaStatStruct *oss) = 0; + + /** + * Adds information about a cvar to the last started game + * + * @param cvar - The name of the cvar (limited to 100 chars) + * @param value - The value of the cvar as a string + */ + virtual void addGameCvar(string cvar, string value) = 0; + + /** + * Called then a game ends. + * + * @param second - relative time since game start + */ + virtual void endGame(int second) = 0; + + /** + * Document getGameNumber() here... + * + * @return the gamenumber that we are working on. Retrieved from the db. + */ + virtual int getGameNumber() = 0; + + /** + * If this is called the game will be not be written to the database. This is good for warmup or games ended prematurely. + */ + virtual void doNotCommit() = 0; + + /** + * Document setPlayerInfo(string,string,bool,int,int,string,string,int) here... + * + * @param guid - the hashed GUID of the player + * @param nickname - The players nickname + * @param isBot - true if a bot, false otherwise + * @param second - relative time for the event + * @param team - team number to join, -1 if leaving + * @param model - model used + * @param headmodel - headmodel used + * @param skill - bot skill, bots only + */ + virtual void setPlayerInfo(string guid, string nickname, bool isBot, int second, int team, string model, string headmodel, int skill, OaStatStruct *oss) = 0; + + /** + * Represents a kill + * + * @param second time of the kill + * @param attackerID hashed guid of the attacker + * @param targetID hashed guid of the victim + * @param type weapon number + */ + virtual void addKill(int second, string attackerID, string targetID, int type) = 0; + + /** + * A flag/skull capture or obelisk destruction + * + * @param second time of the event + * @param player guid of the player if any, empty if none + * @param team + */ + //virtual void addCapture(int second, string player, int team) = 0; + + /** + * Add an award: Like Defence, Excelent, Gauntlet and so on + * + * @param second time of the + * @param player hashed guid of the player + * @param award the award, numbers must be found in code + */ + virtual void addAward(int second, string player, int award) = 0; + + /** + * Adds information about a players score that is changing. + * Notice that the score is the total amount of points the + * player has for the entire match + * + * @param second time of the event + * @param player hashed guid of the player + * @param score the Total + */ + virtual void addScoreInfo(int second, string player, int score) = 0; + + /** + * Adds an event from a CTF-game. + * + * Note that event: + * 0 | Flag is taken + * 1 | Flag is captured + * 2 | Flag is returned + * 3 | Flagcarrier got killed + * + * @param second + * @param player + * @param team + * @param event + */ + virtual void addCtf(int second, string player, int team, int event) = 0; + + /** + * Adds an event from a 1FCTF-game. + * + * Note that event: + * 0 | Flag is taken + * 1 | Flag is captured + * 2 | Flag is returned (like timeout) + * 3 | Flagcarrier got killed + * + * @param second + * @param player + * @param team + * @param event + */ + virtual void addCtf1f(int second, string player, int team, int event) = 0; + + virtual void addElimination(int second, int roundnumber, int team, int event) = 0; + + /** + * Events for the CTF Elimination gametype + * + * @param second second from game start + * @param roundnumber the round number + * @param player the player the event is about. Possible blank + * @param team the team the event is about + * @param event the event type + */ + virtual void addCtfElimination(int second, int roundnumber, string player, int team, int event) = 0; + + /** + * Document addHarvester(int,string,string,int,int,int) here... + * + * @param second time from gamestart + * @param player1 the player the event is about + * @param player2 the second player the event is about (player2 is fragcarrier) + * @param team the team from the logging event + * @param event the event number + * @param score number of skulls brought in in one capture + */ + virtual void addHarvester(int second, string player1, string player2, int team, int event, int score) = 0; + + /** + * Add information about a scored challenge + * + * @param second time from gamestart + * @param player player getting the challenge + * @param challenge the challenge gotten (see challenges.h) + * @param amount normally 1 + */ + virtual void addChallenge(int second, string player, int challenge, int amount) = 0; }; #endif //_DATABASE_H diff --git a/src/db/db2stdout.cpp b/src/db/db2stdout.cpp index 32c9ac0..69acb01 100644 --- a/src/db/db2stdout.cpp +++ b/src/db/db2stdout.cpp @@ -39,81 +39,88 @@ static int gamecount = 1; //Only to standard out, normally this should be read f static char* booltext[2] = {"false","true"}; -static string sqlescape(string sql) { - string output = ""; - string::iterator itr; - for(itr = sql.begin();itr-1) - { - printf(PLAYERSINSERT,guid.c_str(),sqlescape(nickname).c_str(),booltext[isBot],sqlescape(model).c_str(),sqlescape(headmodel).c_str()); - cout << endl; - printf(PLAYERSUPDATE,sqlescape(nickname).c_str(),booltext[isBot],sqlescape(model).c_str(),sqlescape(headmodel).c_str(),guid.c_str()); - cout << endl; - } - printf(USERINFOINSERT,getGameNumber(),second,guid.c_str(),team,sqlescape(model).c_str(),skill); - cout << endl; + //If team < 0 then it is a disconnect event that need not to be in + if(team>-1) + { + printf(PLAYERSINSERT,guid.c_str(),sqlescape(nickname).c_str(),booltext[isBot],sqlescape(model).c_str(),sqlescape(headmodel).c_str()); + cout << endl; + printf(PLAYERSUPDATE,sqlescape(nickname).c_str(),booltext[isBot],sqlescape(model).c_str(),sqlescape(headmodel).c_str(),guid.c_str()); + cout << endl; + } + printf(USERINFOINSERT,getGameNumber(),second,guid.c_str(),team,sqlescape(model).c_str(),skill); + cout << endl; } void DB2stdout::addKill(int second, string attackerID, string targetID, int type) { - printf(KILL,gamenumber,second,attackerID.c_str(),targetID.c_str(),type); - cout << endl; + printf(KILL,gamenumber,second,attackerID.c_str(),targetID.c_str(),type); + cout << endl; } void DB2stdout::endGame(int second) { - printf(ENDGAME,second,gamenumber); - cout << endl; + printf(ENDGAME,second,gamenumber); + cout << endl; } void DB2stdout::addCapture(int second, string player, int team) { - printf(CAPTURE,gamenumber,second,player.c_str(),team); - cout << endl; + printf(CAPTURE,gamenumber,second,player.c_str(),team); + cout << endl; } void DB2stdout::addAward(int second, string player, int award) { - printf(AWARD,gamenumber,second,player.c_str(),award); - cout << endl; + printf(AWARD,gamenumber,second,player.c_str(),award); + cout << endl; } -void addCtf(int second, string player, int team, int event) { - cout << "missing" << endl; +void addCtf(int second, string player, int team, int event) +{ + cout << "missing" << endl; } \ No newline at end of file diff --git a/src/db/db2stdout.h b/src/db/db2stdout.h index 8399ec1..640613e 100644 --- a/src/db/db2stdout.h +++ b/src/db/db2stdout.h @@ -28,21 +28,22 @@ http://code.google.com/p/oastat/ using namespace std; -class DB2stdout : public Database { +class DB2stdout : public Database +{ public: - DB2stdout(); - ~DB2stdout(); - void createTables(); - void startGame(int gametype, string mapname, string basegame); - void endGame(int second); - int getGameNumber(); - void setPlayerInfo(string guid, string nickname, bool isBot, int second, int team, string model, string headmodel, int skill); - void addKill(int second, string attackerID, string targetID, int type); - void addCapture(int second, string player, int team); - void addAward(int second, string player, int award); - void addCtf(int second, string player, int team, int event); + DB2stdout(); + ~DB2stdout(); + void createTables(); + void startGame(int gametype, string mapname, string basegame); + void endGame(int second); + int getGameNumber(); + void setPlayerInfo(string guid, string nickname, bool isBot, int second, int team, string model, string headmodel, int skill); + void addKill(int second, string attackerID, string targetID, int type); + void addCapture(int second, string player, int team); + void addAward(int second, string player, int award); + void addCtf(int second, string player, int team, int event); private: - int gamenumber; + int gamenumber; }; #endif /* _DB2STDOUT_H */ diff --git a/src/oastat.cpp b/src/oastat.cpp index 7807c5a..e906c10 100644 --- a/src/oastat.cpp +++ b/src/oastat.cpp @@ -80,83 +80,92 @@ vector commands; */ void addCommands() { - //Add new commands here - commands.push_back(new Kill2Db()); - commands.push_back(new Init2Db()); - commands.push_back(new Shutdown2Db()); - commands.push_back(new Userinfo2Db()); - commands.push_back(new Disconnect2Db()); - commands.push_back(new Award2Db()); - commands.push_back(new Ctf2Db()); - commands.push_back(new Point2Db()); - commands.push_back(new Ctf1f2Db()); - commands.push_back(new Elimination2Db()); - commands.push_back(new CtfElimination2Db()); - commands.push_back(new Harvester2Db()); - commands.push_back(new Challenge2Db()); - commands.push_back(new Warmup2Db()); - //Add more commands just above here + //Add new commands here + commands.push_back(new Kill2Db()); + commands.push_back(new Init2Db()); + commands.push_back(new Shutdown2Db()); + commands.push_back(new Userinfo2Db()); + commands.push_back(new Disconnect2Db()); + commands.push_back(new Award2Db()); + commands.push_back(new Ctf2Db()); + commands.push_back(new Point2Db()); + commands.push_back(new Ctf1f2Db()); + commands.push_back(new Elimination2Db()); + commands.push_back(new CtfElimination2Db()); + commands.push_back(new Harvester2Db()); + commands.push_back(new Challenge2Db()); + commands.push_back(new Warmup2Db()); + //Add more commands just above here - for(int i=0;isetDb(db); - } + for(int i=0; isetDb(db); + } } int main (int argc, const char* argv[]) { - string dbargs = ""; - string filename = ""; - string backend = "Xml"; + string dbargs = ""; + string filename = ""; + string backend = "Xml"; bool useTail = false; - ///////////// - //dbargs = "mysql dbname oastat"; - boost::format f("%1%/.openarena/baseoa/games.log"); - f % getenv("HOME"); - filename = f.str(); - //////////// - for(int i=1;i0) { - if(useTail) { + if(filename.length()>0) + { + if(useTail) + { redi::ipstream in("tail -s 1 -f "+filename); processStdIn(&in); - } else { + } + else + { ifstream in(filename.c_str(),ifstream::in); processStdIn(&in); } - } else - processStdIn(&cin); + } + else + processStdIn(&cin); - }catch (const char *s) { - cout << "Crashed: " << s << endl; - return -1; - } + } + catch (const char *s) + { + cout << "Crashed: " << s << endl; + return -1; + } - return 0; + return 0; } -static int processStdIn(istream* in_p) { - bool done = true; - OaStatStruct *startstruct; - startstruct = NULL; - do - { - string line = ""; - OaStatStruct oss; - list osslist; - try{ - while( getline(*in_p,line) ) - { - oss.clear(); - oss.parseLine(line); - osslist.push_back(oss); - if(oss.command=="InitGame") - startstruct = &osslist.back(); - if(oss.command=="Warmup" && startstruct) - startstruct->restOfLine += "\\isWarmup\\1"; //Workaround to stop warmup - if(oss.command=="ShutdownGame") - { - while(!osslist.empty()) - { - //cout << "next: " << oss.command << endl; - oss = osslist.front(); - //cout << "gotten, now popping" << endl; - osslist.pop_front(); - //cout << "popping complete" << endl; - for(int i=0;igetCommand() << endl; - if(commands.at(i)->canProcess(oss)) { - //cout << "Execturedg by " << commands.at(i)->getCommand(); - commands.at(i)->process(oss); - } - /*} catch (exception &e) - { - cerr << "oastat: Sql_error at line: \"" << line << "\"" << endl << - "oastat: Error is: " << e.what() << - "oastat: Last error will be ignored" << endl; - }*/ - } - //cout << "returned" << endl; - } - startstruct = NULL; - } - } - } catch (exception &e2) { - /* - If there is an error write it in the log and try again continue - */ - osslist.clear(); - cerr << "oastat: Crashed (NEAR FATAL EXCEPTION) at line: \"" << line << "\"" << endl << - "oastat: Error is: " << e2.what() << endl; - done = false; - } - } while (!done); - return 0; +static int processStdIn(istream* in_p) +{ + bool done = true; + OaStatStruct *startstruct; + startstruct = NULL; + do + { + string line = ""; + OaStatStruct oss; + list osslist; + try + { + while( getline(*in_p,line) ) + { + oss.clear(); + oss.parseLine(line); + osslist.push_back(oss); + if(oss.command=="InitGame") + startstruct = &osslist.back(); + if(oss.command=="Warmup" && startstruct) + startstruct->restOfLine += "\\isWarmup\\1"; //Workaround to stop warmup + if(oss.command=="ShutdownGame") + { + while(!osslist.empty()) + { + //cout << "next: " << oss.command << endl; + oss = osslist.front(); + //cout << "gotten, now popping" << endl; + osslist.pop_front(); + //cout << "popping complete" << endl; + for(int i=0; igetCommand() << endl; + if(commands.at(i)->canProcess(oss)) + { + //cout << "Execturedg by " << commands.at(i)->getCommand(); + commands.at(i)->process(oss); + } + /*} catch (exception &e) + { + cerr << "oastat: Sql_error at line: \"" << line << "\"" << endl << + "oastat: Error is: " << e.what() << + "oastat: Last error will be ignored" << endl; + }*/ + } + //cout << "returned" << endl; + } + startstruct = NULL; + } + } + } + catch (exception &e2) + { + /* + If there is an error write it in the log and try again continue + */ + osslist.clear(); + cerr << "oastat: Crashed (NEAR FATAL EXCEPTION) at line: \"" << line << "\"" << endl << + "oastat: Error is: " << e2.what() << endl; + done = false; + } + } + while (!done); + return 0; } /** @@ -258,21 +281,23 @@ static int processStdIn(istream* in_p) { * @param unhashedID - The unhashed id * @return the hashed id */ -string getHashedId(string unhashedID) { - int msg_len = unhashedID.length(); - int hash_len = gcry_md_get_algo_dlen( GCRY_MD_SHA1 ); - unsigned char hash_binary[ hash_len ]; - char hash_hex[ hash_len*2+1 ]; //surpriseingly this works - char *out = hash_hex; //(char *) malloc( sizeof(char) * ((hash_len*2)+1) ); - char *p = out; +string getHashedId(string unhashedID) +{ + int msg_len = unhashedID.length(); + int hash_len = gcry_md_get_algo_dlen( GCRY_MD_SHA1 ); + unsigned char hash_binary[ hash_len ]; + char hash_hex[ hash_len*2+1 ]; //surpriseingly this works + char *out = hash_hex; //(char *) malloc( sizeof(char) * ((hash_len*2)+1) ); + char *p = out; - gcry_md_hash_buffer( GCRY_MD_SHA1, hash_binary, unhashedID.c_str(), msg_len ); - for ( int i = 0; i < hash_len; i++, p += 2 ) { - snprintf ( p, 3, "%02x", hash_binary[i] ); - } + gcry_md_hash_buffer( GCRY_MD_SHA1, hash_binary, unhashedID.c_str(), msg_len ); + for ( int i = 0; i < hash_len; i++, p += 2 ) + { + snprintf ( p, 3, "%02x", hash_binary[i] ); + } - unhashedID = hash_hex; + unhashedID = hash_hex; - //free(out); - return unhashedID; //Replace with md5 at some point + //free(out); + return unhashedID; //Replace with md5 at some point } diff --git a/src/oastatstruct.cpp b/src/oastatstruct.cpp index 2782622..1c0fc8e 100644 --- a/src/oastatstruct.cpp +++ b/src/oastatstruct.cpp @@ -31,148 +31,169 @@ using namespace std; tm OaStatStruct::_datetime; -OaStatStruct::OaStatStruct() { - second = 0; - command = ""; - parameters.clear(); - restOfLine = ""; +OaStatStruct::OaStatStruct() +{ + second = 0; + command = ""; + parameters.clear(); + restOfLine = ""; } -OaStatStruct::OaStatStruct(const OaStatStruct& orig) { - second = orig.second; - command = orig.command; - parameters = orig.parameters; - restOfLine = orig.restOfLine; - _datetime = orig._datetime; +OaStatStruct::OaStatStruct(const OaStatStruct& orig) +{ + second = orig.second; + command = orig.command; + parameters = orig.parameters; + restOfLine = orig.restOfLine; + _datetime = orig._datetime; } -OaStatStruct::~OaStatStruct() { +OaStatStruct::~OaStatStruct() +{ } -void OaStatStruct::clear() { - second=0; - command = ""; - parameters.clear(); - restOfLine = ""; +void OaStatStruct::clear() +{ + second=0; + command = ""; + parameters.clear(); + restOfLine = ""; } -void OaStatStruct::parseLine(string line) { - //Parse time - //cout << line << endl; - //try{ - string tempTimeString = line.substr(0,7); - int posColon = tempTimeString.find(":"); - int minute = atoi(tempTimeString.substr(0,posColon).c_str()); - /*time_t thetime = time(NULL); - gmtime_r(&thetime,&_datetime);*/ - second = atoi(tempTimeString.substr(posColon+1,7).c_str()); - second+=minute*60; - line = line.substr(7,line.length()); //Cut the first part of the string. - - //Parse command name: - posColon = line.find(":"); - command = line.substr(0,posColon); - try{ - line = line.substr(posColon+2,line.length()); //Also remove the colon and the first space after it (+2) - - posColon = line.find_first_not_of(" -0123456789"); //First non-number or space - stringstream ss; - ss << line.substr(0,posColon); - int counter = 0; //to ensure that we don't go on forever - while(ss && counter++<10) { - int value; - ss >> value; - parameters.push_back(value); - } - - - if(line.at(posColon) == ':'){ - posColon++; //There is some inconsistensy about then there is a colon here - if(line.at(posColon) == ' ') - posColon++; //If there is a colon there is likely a space - } - line = line.substr(posColon,line.length()); - } catch(...) { - //The last part does not always exist... ignore it - restOfLine = ""; - return; - } - - - restOfLine = line; +void OaStatStruct::parseLine(string line) +{ + //Parse time + //cout << line << endl; + //try{ + string tempTimeString = line.substr(0,7); + int posColon = tempTimeString.find(":"); + int minute = atoi(tempTimeString.substr(0,posColon).c_str()); + /*time_t thetime = time(NULL); + gmtime_r(&thetime,&_datetime);*/ + second = atoi(tempTimeString.substr(posColon+1,7).c_str()); + second+=minute*60; + line = line.substr(7,line.length()); //Cut the first part of the string. + + //Parse command name: + posColon = line.find(":"); + command = line.substr(0,posColon); + try + { + line = line.substr(posColon+2,line.length()); //Also remove the colon and the first space after it (+2) + + posColon = line.find_first_not_of(" -0123456789"); //First non-number or space + stringstream ss; + ss << line.substr(0,posColon); + int counter = 0; //to ensure that we don't go on forever + while(ss && counter++<10) + { + int value; + ss >> value; + parameters.push_back(value); + } + + + if(line.at(posColon) == ':') + { + posColon++; //There is some inconsistensy about then there is a colon here + if(line.at(posColon) == ' ') + posColon++; //If there is a colon there is likely a space + } + line = line.substr(posColon,line.length()); + } + catch(...) + { + //The last part does not always exist... ignore it + restOfLine = ""; + return; + } + + + restOfLine = line; } -void makeLower(string &x) { - for(int i=0; i= 'A' && x[i] <= 'X') - x[i] = x[i]-'A'+'a'; - } +void makeLower(string &x) +{ + for(int i=0; i= 'A' && x[i] <= 'X') + x[i] = x[i]-'A'+'a'; + } } -map OaStatStruct::GetInfostring(string restOfLine) { - map list; - int curPos = 0, lastPos = 0; - bool iskey = true; - string key, value; - if(restOfLine[0] == '\\') - lastPos++; - while(lastPos(key,value)); - makeLower(key); - if(key.length()>0) - list[key] = value; - //cout << key << ":" << value << endl; - } - iskey = !iskey; - lastPos = curPos+1; - } - return list; +map OaStatStruct::GetInfostring(string restOfLine) +{ + map list; + int curPos = 0, lastPos = 0; + bool iskey = true; + string key, value; + if(restOfLine[0] == '\\') + lastPos++; + while(lastPos(key,value)); + makeLower(key); + if(key.length()>0) + list[key] = value; + //cout << key << ":" << value << endl; + } + iskey = !iskey; + lastPos = curPos+1; + } + return list; } -map OaStatStruct::GetInfostring() { - return GetInfostring(restOfLine); +map OaStatStruct::GetInfostring() +{ + return GetInfostring(restOfLine); } -tm OaStatStruct::getDateTime() { - return _datetime; +tm OaStatStruct::getDateTime() +{ + return _datetime; } string ZeroPadNumber(int num, int size = 2) { - std::ostringstream ss; - ss.clear(); - ss << setw( size ) << setfill( '0' ) << num; - return ss.str(); + std::ostringstream ss; + ss.clear(); + ss << setw( size ) << setfill( '0' ) << num; + return ss.str(); } -string OaStatStruct::getTimeStamp() { - string s; - s = "TIMESTAMP \'" + ZeroPadNumber(_datetime.tm_year+1900,4) + "-" + ZeroPadNumber(_datetime.tm_mon+1) + "-" - + ZeroPadNumber(_datetime.tm_mday) + " " + ZeroPadNumber(_datetime.tm_hour) + ":" - + ZeroPadNumber(_datetime.tm_min) + ":" + ZeroPadNumber(_datetime.tm_sec) + "\'"; - return s; +string OaStatStruct::getTimeStamp() +{ + string s; + s = "TIMESTAMP \'" + ZeroPadNumber(_datetime.tm_year+1900,4) + "-" + ZeroPadNumber(_datetime.tm_mon+1) + "-" + + ZeroPadNumber(_datetime.tm_mday) + " " + ZeroPadNumber(_datetime.tm_hour) + ":" + + ZeroPadNumber(_datetime.tm_min) + ":" + ZeroPadNumber(_datetime.tm_sec) + "\'"; + return s; } -void OaStatStruct::setTimeStamp(string timestring) { - std::stringstream ss(timestring); - string tmp; - getline(ss,tmp,'-'); - _datetime.tm_year = atoi(tmp.c_str())-1900; - getline(ss,tmp,'-'); - _datetime.tm_mon = atoi(tmp.c_str())-1; - getline(ss,tmp,' '); - _datetime.tm_mday = atoi(tmp.c_str()); - getline(ss,tmp,':'); - _datetime.tm_hour = atoi(tmp.c_str()); - getline(ss,tmp,':'); - _datetime.tm_min = atoi(tmp.c_str()); - getline(ss,tmp); - _datetime.tm_sec = atoi(tmp.c_str()); +void OaStatStruct::setTimeStamp(string timestring) +{ + std::stringstream ss(timestring); + string tmp; + getline(ss,tmp,'-'); + _datetime.tm_year = atoi(tmp.c_str())-1900; + getline(ss,tmp,'-'); + _datetime.tm_mon = atoi(tmp.c_str())-1; + getline(ss,tmp,' '); + _datetime.tm_mday = atoi(tmp.c_str()); + getline(ss,tmp,':'); + _datetime.tm_hour = atoi(tmp.c_str()); + getline(ss,tmp,':'); + _datetime.tm_min = atoi(tmp.c_str()); + getline(ss,tmp); + _datetime.tm_sec = atoi(tmp.c_str()); } diff --git a/src/oastatstruct.h b/src/oastatstruct.h index 01fb81b..f2fbe71 100644 --- a/src/oastatstruct.h +++ b/src/oastatstruct.h @@ -35,44 +35,45 @@ using namespace std; * This is the most important class in oastat. This takes a line and stores all * information from that line in easy to process parts. */ -class OaStatStruct { +class OaStatStruct +{ public: - OaStatStruct(); - OaStatStruct(const OaStatStruct& orig); - virtual ~OaStatStruct(); - - //There are no reason to make any variables private. - int second; //number of seconds - string command; //Like 'ClientConnect', 'Award' and 'Kill' - vector parameters; //The parameters for the command. - string restOfLine; - tm getDateTime(); - - void parseLine(string line); - void clear(); - - /* - *Parses the rest of the line to an info string - */ - map GetInfostring(string restOfLine); - - /* - *Same but uses this.restOfLine - */ - map GetInfostring(); - - /* - Returns a string with the time in the format: - * TIMESTAMP 'YYYY-MM- - */ - string getTimeStamp(); - - /* - Sets _datetime. Requires input in the format: "YYYY-MM-DD HH:MM:SS" - */ - void setTimeStamp(string timestring); + OaStatStruct(); + OaStatStruct(const OaStatStruct& orig); + virtual ~OaStatStruct(); + + //There are no reason to make any variables private. + int second; //number of seconds + string command; //Like 'ClientConnect', 'Award' and 'Kill' + vector parameters; //The parameters for the command. + string restOfLine; + tm getDateTime(); + + void parseLine(string line); + void clear(); + + /* + *Parses the rest of the line to an info string + */ + map GetInfostring(string restOfLine); + + /* + *Same but uses this.restOfLine + */ + map GetInfostring(); + + /* + Returns a string with the time in the format: + * TIMESTAMP 'YYYY-MM- + */ + string getTimeStamp(); + + /* + Sets _datetime. Requires input in the format: "YYYY-MM-DD HH:MM:SS" + */ + void setTimeStamp(string timestring); private: - static tm _datetime; + static tm _datetime; }; #endif /* _OASTATSTRUCT_H */ diff --git a/src/oss2db/Award2Db.cpp b/src/oss2db/Award2Db.cpp index 485523f..fba3643 100644 --- a/src/oss2db/Award2Db.cpp +++ b/src/oss2db/Award2Db.cpp @@ -25,24 +25,27 @@ http://code.google.com/p/oastat/ -string Award2Db::getCommand() { - return "Award"; +string Award2Db::getCommand() +{ + return "Award"; } -bool Award2Db::canProcess(OaStatStruct oss) { - if(oss.command != getCommand() || oss.parameters.size()<2 || oss.parameters.at(0)<0) - return false; - return true; +bool Award2Db::canProcess(OaStatStruct oss) +{ + if(oss.command != getCommand() || oss.parameters.size()<2 || oss.parameters.at(0)<0) + return false; + return true; } -void Award2Db::process(OaStatStruct oss) { - if(!canProcess(oss)) - return; //Invalid oss +void Award2Db::process(OaStatStruct oss) +{ + if(!canProcess(oss)) + return; //Invalid oss - if(oss.parameters.at(0)==-1) - return; //Not valid player - - string player = clientIdMap.at(oss.parameters.at(0)); + if(oss.parameters.at(0)==-1) + return; //Not valid player - dp->addAward(oss.second,player,oss.parameters.at(1)); + string player = clientIdMap.at(oss.parameters.at(0)); + + dp->addAward(oss.second,player,oss.parameters.at(1)); } \ No newline at end of file diff --git a/src/oss2db/Award2Db.h b/src/oss2db/Award2Db.h index aa3cbf4..407a38c 100644 --- a/src/oss2db/Award2Db.h +++ b/src/oss2db/Award2Db.h @@ -27,13 +27,14 @@ http://code.google.com/p/oastat/ #include "struct2db.h" #include "../local.h" -class Award2Db : public Struct2Db { +class Award2Db : public Struct2Db +{ public: - string getCommand(); - bool canProcess(OaStatStruct oss); + string getCommand(); + bool canProcess(OaStatStruct oss); - void process(OaStatStruct oss); + void process(OaStatStruct oss); private: }; diff --git a/src/oss2db/Challenge2Db.cpp b/src/oss2db/Challenge2Db.cpp index a02caa7..7eb1025 100644 --- a/src/oss2db/Challenge2Db.cpp +++ b/src/oss2db/Challenge2Db.cpp @@ -23,25 +23,28 @@ http://code.google.com/p/oastat/ #include "Challenge2Db.hpp" -string Challenge2Db::getCommand() { - return "Challenge"; +string Challenge2Db::getCommand() +{ + return "Challenge"; } -bool Challenge2Db::canProcess(OaStatStruct oss) { - if(oss.command != getCommand() || oss.parameters.size()<3) - return false; - return true; +bool Challenge2Db::canProcess(OaStatStruct oss) +{ + if(oss.command != getCommand() || oss.parameters.size()<3) + return false; + return true; } -void Challenge2Db::process(OaStatStruct oss) { - if(!canProcess(oss)) - return; //Invalid oss - - if(oss.parameters.at(0)==-1) - return; //Not valid player +void Challenge2Db::process(OaStatStruct oss) +{ + if(!canProcess(oss)) + return; //Invalid oss - string player = clientIdMap.at(oss.parameters.at(0)); + if(oss.parameters.at(0)==-1) + return; //Not valid player - dp->addChallenge(oss.second,player,oss.parameters.at(1),oss.parameters.at(2)); + string player = clientIdMap.at(oss.parameters.at(0)); + + dp->addChallenge(oss.second,player,oss.parameters.at(1),oss.parameters.at(2)); } \ No newline at end of file diff --git a/src/oss2db/Challenge2Db.hpp b/src/oss2db/Challenge2Db.hpp index fa02ce4..6f49b13 100644 --- a/src/oss2db/Challenge2Db.hpp +++ b/src/oss2db/Challenge2Db.hpp @@ -27,12 +27,13 @@ http://code.google.com/p/oastat/ #include "struct2db.h" #include "../local.h" -class Challenge2Db : public Struct2Db { +class Challenge2Db : public Struct2Db +{ public: - string getCommand(); - bool canProcess(OaStatStruct oss); + string getCommand(); + bool canProcess(OaStatStruct oss); - void process(OaStatStruct oss); + void process(OaStatStruct oss); private: }; diff --git a/src/oss2db/Ctf1f2Db.cpp b/src/oss2db/Ctf1f2Db.cpp index 228736d..b1180be 100644 --- a/src/oss2db/Ctf1f2Db.cpp +++ b/src/oss2db/Ctf1f2Db.cpp @@ -24,23 +24,26 @@ http://code.google.com/p/oastat/ #include "Ctf1f2Db.hpp" -string Ctf1f2Db::getCommand() { - return "1FCTF"; +string Ctf1f2Db::getCommand() +{ + return "1FCTF"; } -bool Ctf1f2Db::canProcess(OaStatStruct oss) { - if(oss.command != getCommand() || oss.parameters.size()<3) - return false; - return true; +bool Ctf1f2Db::canProcess(OaStatStruct oss) +{ + if(oss.command != getCommand() || oss.parameters.size()<3) + return false; + return true; } -void Ctf1f2Db::process(OaStatStruct oss) { - if(!canProcess(oss)) - return; //Invalid oss +void Ctf1f2Db::process(OaStatStruct oss) +{ + if(!canProcess(oss)) + return; //Invalid oss - string player = ""; - if(oss.parameters.at(0)!=-1) - player= clientIdMap.at(oss.parameters.at(0)); + string player = ""; + if(oss.parameters.at(0)!=-1) + player= clientIdMap.at(oss.parameters.at(0)); - dp->addCtf1f(oss.second,player,oss.parameters.at(1) /*team*/, oss.parameters.at(2) /*event*/); + dp->addCtf1f(oss.second,player,oss.parameters.at(1) /*team*/, oss.parameters.at(2) /*event*/); } diff --git a/src/oss2db/Ctf1f2Db.hpp b/src/oss2db/Ctf1f2Db.hpp index 7581ff9..2de61f4 100644 --- a/src/oss2db/Ctf1f2Db.hpp +++ b/src/oss2db/Ctf1f2Db.hpp @@ -27,13 +27,14 @@ http://code.google.com/p/oastat/ #include "struct2db.h" #include "../local.h" -class Ctf1f2Db : public Struct2Db { +class Ctf1f2Db : public Struct2Db +{ public: - string getCommand(); - bool canProcess(OaStatStruct oss); + string getCommand(); + bool canProcess(OaStatStruct oss); - void process(OaStatStruct oss); + void process(OaStatStruct oss); private: }; diff --git a/src/oss2db/Ctf2Db.cpp b/src/oss2db/Ctf2Db.cpp index 9baf3cf..54200e8 100644 --- a/src/oss2db/Ctf2Db.cpp +++ b/src/oss2db/Ctf2Db.cpp @@ -24,23 +24,26 @@ http://code.google.com/p/oastat/ #include "Ctf2Db.hpp" -string Ctf2Db::getCommand() { - return "CTF"; +string Ctf2Db::getCommand() +{ + return "CTF"; } -bool Ctf2Db::canProcess(OaStatStruct oss) { - if(oss.command != getCommand() || oss.parameters.size()<3) - return false; - return true; +bool Ctf2Db::canProcess(OaStatStruct oss) +{ + if(oss.command != getCommand() || oss.parameters.size()<3) + return false; + return true; } -void Ctf2Db::process(OaStatStruct oss) { - if(!canProcess(oss)) - return; //Invalid oss +void Ctf2Db::process(OaStatStruct oss) +{ + if(!canProcess(oss)) + return; //Invalid oss - string player = ""; - if(oss.parameters.at(0)!=-1) - player= clientIdMap.at(oss.parameters.at(0)); + string player = ""; + if(oss.parameters.at(0)!=-1) + player= clientIdMap.at(oss.parameters.at(0)); - dp->addCtf(oss.second,player,oss.parameters.at(1) /*team*/, oss.parameters.at(2) /*event*/); + dp->addCtf(oss.second,player,oss.parameters.at(1) /*team*/, oss.parameters.at(2) /*event*/); } diff --git a/src/oss2db/Ctf2Db.hpp b/src/oss2db/Ctf2Db.hpp index 21f35f6..6ef1a99 100644 --- a/src/oss2db/Ctf2Db.hpp +++ b/src/oss2db/Ctf2Db.hpp @@ -27,13 +27,14 @@ http://code.google.com/p/oastat/ #include "struct2db.h" #include "../local.h" -class Ctf2Db : public Struct2Db { +class Ctf2Db : public Struct2Db +{ public: - string getCommand(); - bool canProcess(OaStatStruct oss); + string getCommand(); + bool canProcess(OaStatStruct oss); - void process(OaStatStruct oss); + void process(OaStatStruct oss); private: }; diff --git a/src/oss2db/CtfElimination2Db.cpp b/src/oss2db/CtfElimination2Db.cpp index 5cb0d53..218b684 100644 --- a/src/oss2db/CtfElimination2Db.cpp +++ b/src/oss2db/CtfElimination2Db.cpp @@ -23,24 +23,27 @@ http://code.google.com/p/oastat/ #include "CtfElimination2Db.hpp" -string CtfElimination2Db::getCommand() { - return "ELIMINATION"; +string CtfElimination2Db::getCommand() +{ + return "ELIMINATION"; } -bool CtfElimination2Db::canProcess(OaStatStruct oss) { - if(oss.command != getCommand() || oss.parameters.size()<3 ) - return false; - return true; +bool CtfElimination2Db::canProcess(OaStatStruct oss) +{ + if(oss.command != getCommand() || oss.parameters.size()<3 ) + return false; + return true; } -void CtfElimination2Db::process(OaStatStruct oss) { - if(!canProcess(oss)) - return; //Invalid oss +void CtfElimination2Db::process(OaStatStruct oss) +{ + if(!canProcess(oss)) + return; //Invalid oss - string player = ""; - if(oss.parameters.at(1)!=-1) - player= clientIdMap.at(oss.parameters.at(1)); + string player = ""; + if(oss.parameters.at(1)!=-1) + player= clientIdMap.at(oss.parameters.at(1)); - dp->addCtfElimination(oss.second,oss.parameters.at(0) /*roundnumber*/, - player,oss.parameters.at(2) /*team*/, oss.parameters.at(3) /*event*/); + dp->addCtfElimination(oss.second,oss.parameters.at(0) /*roundnumber*/, + player,oss.parameters.at(2) /*team*/, oss.parameters.at(3) /*event*/); } diff --git a/src/oss2db/CtfElimination2Db.hpp b/src/oss2db/CtfElimination2Db.hpp index 8147ebe..ef86787 100644 --- a/src/oss2db/CtfElimination2Db.hpp +++ b/src/oss2db/CtfElimination2Db.hpp @@ -28,12 +28,13 @@ http://code.google.com/p/oastat/ #include "struct2db.h" #include "../local.h" -class CtfElimination2Db : public Struct2Db { +class CtfElimination2Db : public Struct2Db +{ public: - string getCommand(); - bool canProcess(OaStatStruct oss); + string getCommand(); + bool canProcess(OaStatStruct oss); - void process(OaStatStruct oss); + void process(OaStatStruct oss); private: diff --git a/src/oss2db/Disconnect2Db.cpp b/src/oss2db/Disconnect2Db.cpp index b6b55ed..cde26f3 100644 --- a/src/oss2db/Disconnect2Db.cpp +++ b/src/oss2db/Disconnect2Db.cpp @@ -24,21 +24,24 @@ http://code.google.com/p/oastat/ #include "Disconnect2Db.h" -string Disconnect2Db::getCommand() { - return "ClientDisconnect"; +string Disconnect2Db::getCommand() +{ + return "ClientDisconnect"; } -bool Disconnect2Db::canProcess(OaStatStruct oss) { - if(oss.command != getCommand() || oss.parameters.size()<1) - return false; - return true; +bool Disconnect2Db::canProcess(OaStatStruct oss) +{ + if(oss.command != getCommand() || oss.parameters.size()<1) + return false; + return true; } -void Disconnect2Db::process(OaStatStruct oss) { - if(!canProcess(oss)) - return; //Invalid oss +void Disconnect2Db::process(OaStatStruct oss) +{ + if(!canProcess(oss)) + return; //Invalid oss - string player = clientIdMap.at(oss.parameters.at(0)); + string player = clientIdMap.at(oss.parameters.at(0)); - dp->setPlayerInfo(player,"",false,oss.second,-1,"","",-1,&oss); + dp->setPlayerInfo(player,"",false,oss.second,-1,"","",-1,&oss); } diff --git a/src/oss2db/Disconnect2Db.h b/src/oss2db/Disconnect2Db.h index 03454e5..34089c3 100644 --- a/src/oss2db/Disconnect2Db.h +++ b/src/oss2db/Disconnect2Db.h @@ -27,13 +27,14 @@ http://code.google.com/p/oastat/ #include "struct2db.h" #include "../local.h" -class Disconnect2Db : public Struct2Db { +class Disconnect2Db : public Struct2Db +{ public: - string getCommand(); - bool canProcess(OaStatStruct oss); + string getCommand(); + bool canProcess(OaStatStruct oss); - void process(OaStatStruct oss); + void process(OaStatStruct oss); private: }; diff --git a/src/oss2db/Elimination2Db.cpp b/src/oss2db/Elimination2Db.cpp index defebbd..0d20cba 100644 --- a/src/oss2db/Elimination2Db.cpp +++ b/src/oss2db/Elimination2Db.cpp @@ -24,19 +24,22 @@ http://code.google.com/p/oastat/ #include "Elimination2Db.hpp" -string Elimination2Db::getCommand() { - return "ELIMINATION"; +string Elimination2Db::getCommand() +{ + return "ELIMINATION"; } -bool Elimination2Db::canProcess(OaStatStruct oss) { - if(oss.command != getCommand() || oss.parameters.size()<3 ) - return false; - return true; +bool Elimination2Db::canProcess(OaStatStruct oss) +{ + if(oss.command != getCommand() || oss.parameters.size()<3 ) + return false; + return true; } -void Elimination2Db::process(OaStatStruct oss) { - if(!canProcess(oss)) - return; //Invalid oss +void Elimination2Db::process(OaStatStruct oss) +{ + if(!canProcess(oss)) + return; //Invalid oss - dp->addElimination(oss.second,oss.parameters.at(0) /*roundnumber*/,oss.parameters.at(1) /*team*/, oss.parameters.at(2) /*event*/); + dp->addElimination(oss.second,oss.parameters.at(0) /*roundnumber*/,oss.parameters.at(1) /*team*/, oss.parameters.at(2) /*event*/); } \ No newline at end of file diff --git a/src/oss2db/Elimination2Db.hpp b/src/oss2db/Elimination2Db.hpp index 07a9204..a32a390 100644 --- a/src/oss2db/Elimination2Db.hpp +++ b/src/oss2db/Elimination2Db.hpp @@ -27,12 +27,13 @@ http://code.google.com/p/oastat/ #include "struct2db.h" #include "../local.h" -class Elimination2Db : public Struct2Db { +class Elimination2Db : public Struct2Db +{ public: - string getCommand(); - bool canProcess(OaStatStruct oss); + string getCommand(); + bool canProcess(OaStatStruct oss); - void process(OaStatStruct oss); + void process(OaStatStruct oss); private: }; diff --git a/src/oss2db/Harvester2Db.cpp b/src/oss2db/Harvester2Db.cpp index 8d3ca00..e4c9d52 100644 --- a/src/oss2db/Harvester2Db.cpp +++ b/src/oss2db/Harvester2Db.cpp @@ -23,35 +23,44 @@ http://code.google.com/p/oastat/ #include "Harvester2Db.hpp" -string Harvester2Db::getCommand() { - return "HARVESTER"; +string Harvester2Db::getCommand() +{ + return "HARVESTER"; } -bool Harvester2Db::canProcess(OaStatStruct oss) { - if(oss.command != getCommand() || oss.parameters.size()<5 ) - return false; - return true; +bool Harvester2Db::canProcess(OaStatStruct oss) +{ + if(oss.command != getCommand() || oss.parameters.size()<5 ) + return false; + return true; } -void Harvester2Db::process(OaStatStruct oss) { - if(!canProcess(oss)) - return; //Invalid oss +void Harvester2Db::process(OaStatStruct oss) +{ + if(!canProcess(oss)) + return; //Invalid oss - string player1 = ""; //Parameter 0 - string player2 = ""; //Parameter 3 + string player1 = ""; //Parameter 0 + string player2 = ""; //Parameter 3 - if(oss.parameters.at(0)>-1 && oss.parameters.at(0)-1 && oss.parameters.at(0)-1 && oss.parameters.at(3)-1 && oss.parameters.at(3)addHarvester(oss.second,player1,player2,oss.parameters.at(1) /*team*/,oss.parameters.at(2) /*event*/, oss.parameters.at(4) /*score*/ ); + dp->addHarvester(oss.second,player1,player2,oss.parameters.at(1) /*team*/,oss.parameters.at(2) /*event*/, oss.parameters.at(4) /*score*/ ); } diff --git a/src/oss2db/Harvester2Db.hpp b/src/oss2db/Harvester2Db.hpp index 2bce414..8bfd0f6 100644 --- a/src/oss2db/Harvester2Db.hpp +++ b/src/oss2db/Harvester2Db.hpp @@ -27,12 +27,13 @@ http://code.google.com/p/oastat/ #include "struct2db.h" #include "../local.h" -class Harvester2Db : public Struct2Db { +class Harvester2Db : public Struct2Db +{ public: - string getCommand(); - bool canProcess(OaStatStruct oss); + string getCommand(); + bool canProcess(OaStatStruct oss); - void process(OaStatStruct oss); + void process(OaStatStruct oss); private: }; diff --git a/src/oss2db/Point2Db.cpp b/src/oss2db/Point2Db.cpp index b5555ac..a0e424c 100644 --- a/src/oss2db/Point2Db.cpp +++ b/src/oss2db/Point2Db.cpp @@ -24,21 +24,24 @@ http://code.google.com/p/oastat/ #include "Point2Db.hpp" -string Point2Db::getCommand() { - return "PlayerScore"; +string Point2Db::getCommand() +{ + return "PlayerScore"; } -bool Point2Db::canProcess(OaStatStruct oss) { - if(oss.command != getCommand() || oss.parameters.size()<2 || oss.parameters.at(0)<0) - return false; - return true; +bool Point2Db::canProcess(OaStatStruct oss) +{ + if(oss.command != getCommand() || oss.parameters.size()<2 || oss.parameters.at(0)<0) + return false; + return true; } -void Point2Db::process(OaStatStruct oss) { - if(!canProcess(oss)) - return; //Invalid oss +void Point2Db::process(OaStatStruct oss) +{ + if(!canProcess(oss)) + return; //Invalid oss - string player = clientIdMap.at(oss.parameters.at(0)); + string player = clientIdMap.at(oss.parameters.at(0)); - dp->addScoreInfo(oss.second,player,oss.parameters.at(1)); + dp->addScoreInfo(oss.second,player,oss.parameters.at(1)); } diff --git a/src/oss2db/Point2Db.hpp b/src/oss2db/Point2Db.hpp index f82719e..722f9b3 100644 --- a/src/oss2db/Point2Db.hpp +++ b/src/oss2db/Point2Db.hpp @@ -27,12 +27,13 @@ http://code.google.com/p/oastat/ #include "struct2db.h" #include "../local.h" -class Point2Db : public Struct2Db { +class Point2Db : public Struct2Db +{ public: - string getCommand(); - bool canProcess(OaStatStruct oss); + string getCommand(); + bool canProcess(OaStatStruct oss); - void process(OaStatStruct oss); + void process(OaStatStruct oss); private: }; diff --git a/src/oss2db/Warmup2Db.cpp b/src/oss2db/Warmup2Db.cpp index 950c8cb..2ba3e3d 100644 --- a/src/oss2db/Warmup2Db.cpp +++ b/src/oss2db/Warmup2Db.cpp @@ -23,19 +23,22 @@ http://code.google.com/p/oastat/ #include "Warmup2Db.hpp" -string Warmup2Db::getCommand() { - return "Warmup"; +string Warmup2Db::getCommand() +{ + return "Warmup"; } -bool Warmup2Db::canProcess(OaStatStruct oss) { - if(oss.command != getCommand()) - return false; - return true; +bool Warmup2Db::canProcess(OaStatStruct oss) +{ + if(oss.command != getCommand()) + return false; + return true; } -void Warmup2Db::process(OaStatStruct oss) { - if(!canProcess(oss)) - return; //Invalid oss +void Warmup2Db::process(OaStatStruct oss) +{ + if(!canProcess(oss)) + return; //Invalid oss - dp->doNotCommit(); + dp->doNotCommit(); } \ No newline at end of file diff --git a/src/oss2db/Warmup2Db.hpp b/src/oss2db/Warmup2Db.hpp index 8bff63f..cfc31e3 100644 --- a/src/oss2db/Warmup2Db.hpp +++ b/src/oss2db/Warmup2Db.hpp @@ -27,12 +27,13 @@ http://code.google.com/p/oastat/ #include "struct2db.h" #include "../local.h" -class Warmup2Db : public Struct2Db { +class Warmup2Db : public Struct2Db +{ public: - string getCommand(); - bool canProcess(OaStatStruct oss); + string getCommand(); + bool canProcess(OaStatStruct oss); - void process(OaStatStruct oss); + void process(OaStatStruct oss); private: }; diff --git a/src/oss2db/init2db.cpp b/src/oss2db/init2db.cpp index 253f5ea..6e33e80 100644 --- a/src/oss2db/init2db.cpp +++ b/src/oss2db/init2db.cpp @@ -25,37 +25,41 @@ http://code.google.com/p/oastat/ #include -string Init2Db::getCommand() { - return "InitGame"; +string Init2Db::getCommand() +{ + return "InitGame"; } -bool Init2Db::canProcess(OaStatStruct oss) { - if(oss.command != getCommand()) - return false; - return true; +bool Init2Db::canProcess(OaStatStruct oss) +{ + if(oss.command != getCommand()) + return false; + return true; } -void Init2Db::process(OaStatStruct oss) { - if(!canProcess(oss)) - return; //Invalid oss - map arguments = oss.GetInfostring(); - int gametype = atoi(arguments["g_gametype"].c_str()); - string basegame = arguments["gamename"]; - string mapname = arguments["mapname"]; - string servername = arguments["sv_hostname"]; - oss.setTimeStamp(arguments["g_timestamp"]); - - dp->startGame(gametype,mapname,basegame,servername,&oss); - map::iterator it; - for(it = arguments.begin(); it != arguments.end(); it++) { - if( - it->first == "g_gametype" || - it->first == "gamename" || - it->first == "mapname" || - it->first == "sv_hostname" /*|| +void Init2Db::process(OaStatStruct oss) +{ + if(!canProcess(oss)) + return; //Invalid oss + map arguments = oss.GetInfostring(); + int gametype = atoi(arguments["g_gametype"].c_str()); + string basegame = arguments["gamename"]; + string mapname = arguments["mapname"]; + string servername = arguments["sv_hostname"]; + oss.setTimeStamp(arguments["g_timestamp"]); + + dp->startGame(gametype,mapname,basegame,servername,&oss); + map::iterator it; + for(it = arguments.begin(); it != arguments.end(); it++) + { + if( + it->first == "g_gametype" || + it->first == "gamename" || + it->first == "mapname" || + it->first == "sv_hostname" /*|| it->first == "g_timestamp" do not skip this one*/ - ) - continue; //Skip the ones on the games-table - dp->addGameCvar(it->first,it->second); - } + ) + continue; //Skip the ones on the games-table + dp->addGameCvar(it->first,it->second); + } } diff --git a/src/oss2db/init2db.h b/src/oss2db/init2db.h index e77277c..c7404a7 100644 --- a/src/oss2db/init2db.h +++ b/src/oss2db/init2db.h @@ -27,13 +27,14 @@ http://code.google.com/p/oastat/ #include "struct2db.h" #include "../local.h" -class Init2Db : public Struct2Db { +class Init2Db : public Struct2Db +{ public: - string getCommand(); - bool canProcess(OaStatStruct oss); + string getCommand(); + bool canProcess(OaStatStruct oss); - void process(OaStatStruct oss); + void process(OaStatStruct oss); private: }; diff --git a/src/oss2db/kill2db.cpp b/src/oss2db/kill2db.cpp index 4019579..90360cf 100644 --- a/src/oss2db/kill2db.cpp +++ b/src/oss2db/kill2db.cpp @@ -24,31 +24,38 @@ http://code.google.com/p/oastat/ #include "kill2db.h" -string Kill2Db::getCommand() { - return "Kill"; +string Kill2Db::getCommand() +{ + return "Kill"; } -bool Kill2Db::canProcess(OaStatStruct oss) { - if(oss.command != getCommand() || oss.parameters.size()<=3) - return false; - return true; +bool Kill2Db::canProcess(OaStatStruct oss) +{ + if(oss.command != getCommand() || oss.parameters.size()<=3) + return false; + return true; } -void Kill2Db::process(OaStatStruct oss) { - if(!canProcess(oss)) - return; //Invalid oss - string target = ""; //Parameter 1 - string killer = ""; //Parameter 0 - //mod is parameter 2 - if(oss.parameters.at(1)>-1 && oss.parameters.at(1)-1 && oss.parameters.at(0)addKill(oss.second,killer,target,oss.parameters.at(2)); +void Kill2Db::process(OaStatStruct oss) +{ + if(!canProcess(oss)) + return; //Invalid oss + string target = ""; //Parameter 1 + string killer = ""; //Parameter 0 + //mod is parameter 2 + if(oss.parameters.at(1)>-1 && oss.parameters.at(1)-1 && oss.parameters.at(0)addKill(oss.second,killer,target,oss.parameters.at(2)); } diff --git a/src/oss2db/kill2db.h b/src/oss2db/kill2db.h index 034acf1..df0c0cc 100644 --- a/src/oss2db/kill2db.h +++ b/src/oss2db/kill2db.h @@ -27,13 +27,14 @@ http://code.google.com/p/oastat/ #include "struct2db.h" #include "../local.h" -class Kill2Db : public Struct2Db { +class Kill2Db : public Struct2Db +{ - string getCommand(); - bool canProcess(OaStatStruct oss); + string getCommand(); + bool canProcess(OaStatStruct oss); + + void process(OaStatStruct oss); - void process(OaStatStruct oss); - }; diff --git a/src/oss2db/shutdown2db.cpp b/src/oss2db/shutdown2db.cpp index ac6aa8a..d13f67b 100644 --- a/src/oss2db/shutdown2db.cpp +++ b/src/oss2db/shutdown2db.cpp @@ -25,22 +25,26 @@ http://code.google.com/p/oastat/ -string Shutdown2Db::getCommand() { - return "ShutdownGame"; +string Shutdown2Db::getCommand() +{ + return "ShutdownGame"; } -bool Shutdown2Db::canProcess(OaStatStruct oss) { - if(oss.command != getCommand()) - return false; - return true; +bool Shutdown2Db::canProcess(OaStatStruct oss) +{ + if(oss.command != getCommand()) + return false; + return true; } -void Shutdown2Db::process(OaStatStruct oss) { - if(!canProcess(oss)) - return; - dp->endGame(oss.second); +void Shutdown2Db::process(OaStatStruct oss) +{ + if(!canProcess(oss)) + return; + dp->endGame(oss.second); } -bool Shutdown2Db::shouldCommit() { - return true; +bool Shutdown2Db::shouldCommit() +{ + return true; } diff --git a/src/oss2db/shutdown2db.h b/src/oss2db/shutdown2db.h index 907fadf..23d16ee 100644 --- a/src/oss2db/shutdown2db.h +++ b/src/oss2db/shutdown2db.h @@ -27,15 +27,16 @@ http://code.google.com/p/oastat/ #include "struct2db.h" #include "../local.h" -class Shutdown2Db : public Struct2Db{ +class Shutdown2Db : public Struct2Db +{ public: - string getCommand(); - bool canProcess(OaStatStruct oss); + string getCommand(); + bool canProcess(OaStatStruct oss); - void process(OaStatStruct oss); + void process(OaStatStruct oss); - bool shouldCommit(); + bool shouldCommit(); private: }; diff --git a/src/oss2db/struct2db.h b/src/oss2db/struct2db.h index 27abc28..fbee2e1 100644 --- a/src/oss2db/struct2db.h +++ b/src/oss2db/struct2db.h @@ -33,39 +33,44 @@ using namespace std; /* This is the struct to database class that all classes that interpret a log line must inherit */ -class Struct2Db { - public: - /* - * The object will need a database interface to cummunicate to. This is given here - */ - void setDb(Database *d) { - dp = d; - } - - /* - * The object need a way to tell what command it is capreble to preocess. - * - * @returns The command the object can process - */ - virtual string getCommand() = 0; - - /* - * The object needs a way to tell if it can process a command. - * - * This is better than the last getCommand, because it can check for multiple commands at once, and can - * discard subevents for supported commands. - */ - virtual bool canProcess(OaStatStruct oss) = 0; - - /* - * Parses s OaStatStruct to the object to process - */ - virtual void process(OaStatStruct oss) = 0; - - virtual bool shouldCommit() { return false; }; - - protected: - Database *dp; //Pointer to the used db +class Struct2Db +{ +public: + /* + * The object will need a database interface to cummunicate to. This is given here + */ + void setDb(Database *d) + { + dp = d; + } + + /* + * The object need a way to tell what command it is capreble to preocess. + * + * @returns The command the object can process + */ + virtual string getCommand() = 0; + + /* + * The object needs a way to tell if it can process a command. + * + * This is better than the last getCommand, because it can check for multiple commands at once, and can + * discard subevents for supported commands. + */ + virtual bool canProcess(OaStatStruct oss) = 0; + + /* + * Parses s OaStatStruct to the object to process + */ + virtual void process(OaStatStruct oss) = 0; + + virtual bool shouldCommit() + { + return false; + }; + +protected: + Database *dp; //Pointer to the used db }; diff --git a/src/oss2db/userinfo2db.cpp b/src/oss2db/userinfo2db.cpp index e0bb052..7562abb 100644 --- a/src/oss2db/userinfo2db.cpp +++ b/src/oss2db/userinfo2db.cpp @@ -23,42 +23,46 @@ http://code.google.com/p/oastat/ #include "userinfo2db.h" #include -#include +#include #include -string Userinfo2Db::getCommand() { - return "ClientUserinfoChanged"; +string Userinfo2Db::getCommand() +{ + return "ClientUserinfoChanged"; } -bool Userinfo2Db::canProcess(OaStatStruct oss) { - if(oss.command != getCommand() || oss.parameters.size()<1) - return false; - return true; +bool Userinfo2Db::canProcess(OaStatStruct oss) +{ + if(oss.command != getCommand() || oss.parameters.size()<1) + return false; + return true; } -void Userinfo2Db::process(OaStatStruct oss) { - if(!canProcess(oss)) - return; //Invalid oss - bool isBot = false; - map arguments = oss.GetInfostring(); +void Userinfo2Db::process(OaStatStruct oss) +{ + if(!canProcess(oss)) + return; //Invalid oss + bool isBot = false; + map arguments = oss.GetInfostring(); //grow clientIdMap until oss.parameters.at(0)+1 while(clientIdMap.size() < oss.parameters.at(0)+2) clientIdMap.push_back(""); - - if(arguments["id"].length()>0 || arguments["hashedid"].length() > 0) { //Not bot + + if(arguments["id"].length()>0 || arguments["hashedid"].length() > 0) //Not bot + { if(arguments["id"].length()>0) clientIdMap[oss.parameters.at(0)] = getHashedId(arguments["id"]); else clientIdMap[oss.parameters.at(0)] = arguments["hashedid"]; } - else //bot - { - clientIdMap[oss.parameters.at(0)] = (boost::format("%1%_client%2%") % arguments["n"] % oss.parameters.at(0) ).str(); - isBot = true; - } - string player = clientIdMap[oss.parameters.at(0)]; + else //bot + { + clientIdMap[oss.parameters.at(0)] = (boost::format("%1%_client%2%") % arguments["n"] % oss.parameters.at(0) ).str(); + isBot = true; + } + string player = clientIdMap[oss.parameters.at(0)]; - dp->setPlayerInfo(player,arguments["n"],isBot,oss.second,atoi(arguments["t"].c_str()), arguments["model"],arguments["hmodel"],-1,&oss); + dp->setPlayerInfo(player,arguments["n"],isBot,oss.second,atoi(arguments["t"].c_str()), arguments["model"],arguments["hmodel"],-1,&oss); } diff --git a/src/oss2db/userinfo2db.h b/src/oss2db/userinfo2db.h index aae968e..3b2ed22 100644 --- a/src/oss2db/userinfo2db.h +++ b/src/oss2db/userinfo2db.h @@ -27,14 +27,15 @@ http://code.google.com/p/oastat/ #include "struct2db.h" #include "../local.h" -class Userinfo2Db : public Struct2Db{ +class Userinfo2Db : public Struct2Db +{ public: - string getCommand(); + string getCommand(); - bool canProcess(OaStatStruct oss); + bool canProcess(OaStatStruct oss); - void process(OaStatStruct oss); + void process(OaStatStruct oss); private: };