// $Header: /cvsroot/openyahtzee/OpenYahtzee/src/HighScoreTableDB.cpp,v 1.5 2006/12/19 21:32:15 guyru Exp $ /*************************************************************************** * Copyright (C) 2006 by Guy Rutenberg * * guyrutenberg@gmail.com * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the * * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ #include "HighScoreTableDB.h" #include #include using namespace std; ///Default constructor HighScoreTableDB::HighScoreTableDB() { /* Open the database which holds the settings. */ m_path = ((wxFileName::GetHomeDir()).mb_str()); m_path += "/"; //IMPORTANT! only works under linux/unix/*nix m_path += DBFILENAME; #ifdef DEBUG if (this->Open(m_path)==SQLITE_OK) cerr<<"file opened: "<Open(m_path); #endif /* Initialize the database table */ CreateTable(); } /** \brief Destructor * * Closes the connection to the database */ HighScoreTableDB::~HighScoreTableDB() { Close(); } /** * Creates the highscore database table if the table doesn't exist already. * */ void HighScoreTableDB::CreateTable() { //Create the database table. Query("CREATE TABLE IF NOT EXISTS highscore (place INTEGER PRIMARY KEY, name TEXT, date TEXT, score INTEGER)"); // create the table //Create the unique index on the 'key' column. Query("CREATE UNIQUE INDEX IF NOT EXISTS place ON highscore ( place )"); } int HighScoreTableDB::GetSize() { return m_size; } void HighScoreTableDB::SetSize(int size) { string tmp_query; std::ostringstream sstr; for(int i = 1; i<=size; i++){ sstr.str(""); tmp_query="INSERT OR IGNORE INTO highscore (place,name,score,date) VALUES( "; sstr< scores; int temp_score; scores = Query("SELECT score FROM highscore"); list::iterator p = scores.begin(); int i = 1; while (p != scores.end()) { temp_score = atoi(p->c_str()); if(score > temp_score) break; i++; p++; } if (i<=m_size) return i; return 0; // the score doesn't qualifiy } int HighScoreTableDB::SendHighScore(std::string name, std::string date, int score) { int place; std::ostringstream sstr; std::string tmp_date, tmpswap_date="", tmp_name,tmpswap_name=""; int tmp_score, tmpswap_score=0; place = IsHighScore(score); if(!place) //the score doesn't qualify return 0; string tmp_query; list result; list::iterator p; //first move all the rows starting at 'place' one row down for (int i = place; i <= m_size ; i++){ sstr.str(""); //Get the current score and details of the row and then move them one row below; tmp_query="SELECT name,date,score FROM highscore WHERE place="; sstr<c_str()); sstr.str(""); tmp_query="REPLACE INTO highscore (place,name,date,score) VALUES("; sstr< HighScoreTableDB::GetHighScoreTable() { std::ostringstream sstr; sstr.str(""); string tmp_query = "SELECT name,date,score FROM highscore LIMIT "; sstr<