| 1 | // $Header$ |
|---|
| 2 | /*************************************************************************** |
|---|
| 3 | * Copyright (C) 2006 by Guy Rutenberg * |
|---|
| 4 | * guyrutenberg@gmail.com * |
|---|
| 5 | * * |
|---|
| 6 | * This program is free software; you can redistribute it and/or modify * |
|---|
| 7 | * it under the terms of the GNU General Public License as published by * |
|---|
| 8 | * the Free Software Foundation; either version 2 of the License, or * |
|---|
| 9 | * (at your option) any later version. * |
|---|
| 10 | * * |
|---|
| 11 | * This program is distributed in the hope that it will be useful, * |
|---|
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
|---|
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
|---|
| 14 | * GNU General Public License for more details. * |
|---|
| 15 | * * |
|---|
| 16 | * You should have received a copy of the GNU General Public License * |
|---|
| 17 | * along with this program; if not, write to the * |
|---|
| 18 | * Free Software Foundation, Inc., * |
|---|
| 19 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
|---|
| 20 | ***************************************************************************/ |
|---|
| 21 | |
|---|
| 22 | #include "HighScoreTableDB.h" |
|---|
| 23 | #include <sstream> |
|---|
| 24 | #include <iostream> |
|---|
| 25 | |
|---|
| 26 | using namespace std; |
|---|
| 27 | |
|---|
| 28 | ///Default constructor |
|---|
| 29 | HighScoreTableDB::HighScoreTableDB() |
|---|
| 30 | { |
|---|
| 31 | /* Open the database which holds the settings. */ |
|---|
| 32 | m_path = ((wxFileName::GetHomeDir()).mb_str()); |
|---|
| 33 | m_path += "/"; //IMPORTANT! only works under linux/unix/*nix |
|---|
| 34 | m_path += DBFILENAME; |
|---|
| 35 | |
|---|
| 36 | #ifdef DEBUG |
|---|
| 37 | if (this->Open(m_path)==SQLITE_OK) |
|---|
| 38 | cerr<<"file opened: "<<m_path<<endl; |
|---|
| 39 | #else |
|---|
| 40 | this->Open(m_path); |
|---|
| 41 | #endif |
|---|
| 42 | /* Initialize the database table */ |
|---|
| 43 | CreateTable(); |
|---|
| 44 | |
|---|
| 45 | } |
|---|
| 46 | |
|---|
| 47 | /** |
|---|
| 48 | \brief Destructor |
|---|
| 49 | * |
|---|
| 50 | * Closes the connection to the database |
|---|
| 51 | */ |
|---|
| 52 | HighScoreTableDB::~HighScoreTableDB() |
|---|
| 53 | { |
|---|
| 54 | Close(); |
|---|
| 55 | } |
|---|
| 56 | |
|---|
| 57 | /** |
|---|
| 58 | * Creates the highscore database table if the table doesn't exist already. |
|---|
| 59 | * |
|---|
| 60 | */ |
|---|
| 61 | void HighScoreTableDB::CreateTable() |
|---|
| 62 | { |
|---|
| 63 | //Create the database table. |
|---|
| 64 | Query("CREATE TABLE IF NOT EXISTS highscore (place INTEGER PRIMARY KEY, name TEXT, date TEXT, score INTEGER)"); // create the table |
|---|
| 65 | |
|---|
| 66 | //Create the unique index on the 'key' column. |
|---|
| 67 | Query("CREATE UNIQUE INDEX IF NOT EXISTS place ON highscore ( place )"); |
|---|
| 68 | } |
|---|
| 69 | |
|---|
| 70 | void HighScoreTableDB::SetSize(int size) |
|---|
| 71 | { |
|---|
| 72 | string tmp_query; |
|---|
| 73 | std::ostringstream sstr; |
|---|
| 74 | |
|---|
| 75 | for(int i = 1; i<=size; i++){ |
|---|
| 76 | sstr.str(""); |
|---|
| 77 | tmp_query="INSERT OR IGNORE INTO highscore (place,name,score,date) VALUES( "; |
|---|
| 78 | sstr<<i<<flush; //make sure the number realiy gets into the stream |
|---|
| 79 | tmp_query += sstr.str(); |
|---|
| 80 | tmp_query += ",\"anonymous\", 0, \"never\")"; |
|---|
| 81 | Query(tmp_query); |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | sstr.str(""); |
|---|
| 85 | sstr << size<<flush; |
|---|
| 86 | tmp_query = "DELETE FROM highscore WHERE place > " + sstr.str(); |
|---|
| 87 | Query(tmp_query); |
|---|
| 88 | |
|---|
| 89 | m_size = size; |
|---|
| 90 | |
|---|
| 91 | } |
|---|
| 92 | |
|---|
| 93 | /** |
|---|
| 94 | * Check if a score qualifies for the highscore table |
|---|
| 95 | *\param score the score to be checked |
|---|
| 96 | *\return highscore position or 0 if doesn't qualify |
|---|
| 97 | */ |
|---|
| 98 | int HighScoreTableDB::IsHighScore(int score) |
|---|
| 99 | { |
|---|
| 100 | list<string> scores; |
|---|
| 101 | int temp_score; |
|---|
| 102 | scores = Query("SELECT score FROM highscore"); |
|---|
| 103 | |
|---|
| 104 | list<string>::iterator p = scores.begin(); |
|---|
| 105 | int i = 1; |
|---|
| 106 | while (p != scores.end()) { |
|---|
| 107 | temp_score = atoi(p->c_str()); |
|---|
| 108 | if(score > temp_score) |
|---|
| 109 | break; |
|---|
| 110 | i++; |
|---|
| 111 | p++; |
|---|
| 112 | } |
|---|
| 113 | if (i<=m_size) |
|---|
| 114 | return i; |
|---|
| 115 | return 0; // the score doesn't qualifiy |
|---|
| 116 | } |
|---|
| 117 | |
|---|
| 118 | int HighScoreTableDB::SendHighScore(std::string name, std::string date, int score) |
|---|
| 119 | { |
|---|
| 120 | int place; |
|---|
| 121 | std::ostringstream sstr; |
|---|
| 122 | |
|---|
| 123 | std::string tmp_date, tmpswap_date="", tmp_name,tmpswap_name=""; |
|---|
| 124 | int tmp_score, tmpswap_score=0; |
|---|
| 125 | |
|---|
| 126 | place = IsHighScore(score); |
|---|
| 127 | if(!place) //the score doesn't qualify |
|---|
| 128 | return 0; |
|---|
| 129 | |
|---|
| 130 | string tmp_query; |
|---|
| 131 | list<string> result; |
|---|
| 132 | list<string>::iterator p; |
|---|
| 133 | |
|---|
| 134 | |
|---|
| 135 | //first move all the rows starting at 'place' one row down |
|---|
| 136 | for (int i = place; i <= m_size ; i++){ |
|---|
| 137 | sstr.str(""); |
|---|
| 138 | //Get the current score and details of the row and then move them one row below; |
|---|
| 139 | tmp_query="SELECT name,date,score FROM highscore WHERE place="; |
|---|
| 140 | sstr<<i<<flush; //make sure the number realiy gets into the stream |
|---|
| 141 | tmp_query += sstr.str(); |
|---|
| 142 | result = Query(tmp_query); |
|---|
| 143 | p = result.begin(); |
|---|
| 144 | tmp_name=*(p); |
|---|
| 145 | tmp_date=*(++p); |
|---|
| 146 | tmp_score= atoi((++p)->c_str()); |
|---|
| 147 | |
|---|
| 148 | sstr.str(""); |
|---|
| 149 | tmp_query="REPLACE INTO highscore (place,name,date,score) VALUES("; |
|---|
| 150 | sstr<<i<<flush; //make sure the number realiy gets into the stream |
|---|
| 151 | tmp_query += sstr.str(); |
|---|
| 152 | tmp_query += ","; |
|---|
| 153 | tmp_query +="\""+tmpswap_name+"\"" +", " + "\""+tmpswap_date+"\"" + ", "; |
|---|
| 154 | |
|---|
| 155 | sstr.str(""); |
|---|
| 156 | sstr<<tmpswap_score<<flush; //make sure the number realiy gets into the stream |
|---|
| 157 | tmp_query += sstr.str()+")"; |
|---|
| 158 | result = Query(tmp_query); |
|---|
| 159 | |
|---|
| 160 | //pass the info to the next item |
|---|
| 161 | tmpswap_score = tmp_score; |
|---|
| 162 | tmpswap_name = tmp_name; |
|---|
| 163 | tmpswap_date = tmp_date; |
|---|
| 164 | } |
|---|
| 165 | //now replace the row at 'place' |
|---|
| 166 | sstr.str(""); |
|---|
| 167 | tmp_query="REPLACE INTO highscore (place,name,date,score) VALUES("; |
|---|
| 168 | sstr<<place<<flush; //make sure the number realiy gets into the stream |
|---|
| 169 | tmp_query += sstr.str(); |
|---|
| 170 | sstr.str(""); |
|---|
| 171 | tmp_query += ","; |
|---|
| 172 | tmp_query +="\""+name+"\"" +", " + "\""+date+"\"" + ", "; |
|---|
| 173 | sstr<<score<<flush; //make sure the number realiy gets into the stream |
|---|
| 174 | tmp_query += sstr.str()+")"; |
|---|
| 175 | result = Query(tmp_query); |
|---|
| 176 | |
|---|
| 177 | return place; |
|---|
| 178 | } |
|---|
| 179 | |
|---|
| 180 | |
|---|
| 181 | std::list<std::string> HighScoreTableDB::GetHighScoreTable() |
|---|
| 182 | { |
|---|
| 183 | std::ostringstream sstr; |
|---|
| 184 | sstr.str(""); |
|---|
| 185 | string tmp_query = "SELECT name,date,score FROM highscore LIMIT "; |
|---|
| 186 | sstr<<m_size<<flush; |
|---|
| 187 | tmp_query += sstr.str(); |
|---|
| 188 | |
|---|
| 189 | return Query(tmp_query); |
|---|
| 190 | } |
|---|
| 191 | |
|---|