1
// $Id: highscores.hpp 921 2007-02-28 05:43:34Z hiker $
3
// SuperTuxKart - a fun racing game with go-kart
4
// Copyright (C) 2006 Joerg Henrichs
6
// This program is free software; you can redistribute it and/or
7
// modify it under the terms of the GNU General Public License
8
// as published by the Free Software Foundation; either version 3
9
// of the License, or (at your option) any later version.
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.
16
// You should have received a copy of the GNU General Public License
17
// along with this program; if not, write to the Free Software
18
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20
#include "race/highscore_manager.hpp"
25
#include "config/user_config.hpp"
26
#include "io/file_manager.hpp"
27
#include "race/race_manager.hpp"
28
#include "utils/string_utils.hpp"
29
#include "utils/translation.hpp"
31
HighscoreManager* highscore_manager=0;
33
HighscoreManager::HighscoreManager()
40
// -----------------------------------------------------------------------------
41
HighscoreManager::~HighscoreManager()
44
for(type_all_scores::iterator i = m_all_scores.begin();
45
i != m_all_scores.end(); i++)
47
} // ~HighscoreManager
49
// -----------------------------------------------------------------------------
50
/** Determines the path to store the highscore file in
52
void HighscoreManager::setFilename()
54
if ( getenv("SUPERTUXKART_HIGHSCOREDIR") != NULL )
56
m_filename = getenv("SUPERTUXKART_HIGHSCOREDIR")
57
+ std::string("/highscore.xml");
61
m_filename=file_manager->getHighscoreFile("highscore.xml");
67
// -----------------------------------------------------------------------------
68
void HighscoreManager::loadHighscores()
71
root = file_manager->createXMLTree(m_filename);
77
fprintf(stderr, "New highscore file '%s' created.\n",
86
if(!root || root->getName()!="highscores")
87
throw std::runtime_error("No 'highscore' node found.");
91
if (!root->get("version", &v) || v<(int)CURRENT_HSCORE_FILE_VERSION)
94
"Highscore file format too old, a new one will be created.\n");
95
irr::core::stringw warning =
96
_("The highscore file was too old,\nall highscores have been erased.");
97
user_config->setWarning( warning );
99
// since we haven't had the chance to load the current scores yet,
100
// calling Save() now will generate an empty file with the right format.
105
// read all entries one by one and store them in 'm_all_scores'
106
for(unsigned int i=0; i<root->getNumNodes(); i++)
108
const XMLNode *node = root->getNode(i);
109
Highscores *highscores;
112
highscores = new Highscores(*node);
114
catch (std::logic_error& e)
116
fprintf(stderr, "Invalid highscore entry will be skipped : %s\n", e.what());
119
m_all_scores.push_back(highscores);
122
if(UserConfigParams::m_verbosity>=4)
123
fprintf(stderr, "Highscores will be saved in '%s'.\n",
126
catch(std::exception& err)
128
fprintf(stderr, "Error while parsing highscore file '%s':\n",
130
fprintf(stderr, "%s", err.what());
131
fprintf(stderr, "\n");
132
fprintf(stderr, "No old highscores will be available.\n");
137
// -----------------------------------------------------------------------------
138
void HighscoreManager::saveHighscores()
140
// Print error message only once
141
if(!m_can_write) return;
145
std::ofstream highscore_file;
146
highscore_file.open(m_filename.c_str());
147
highscore_file << "<?xml version=\"1.0\"?>\n";
148
highscore_file << "<highscores version=\"" << CURRENT_HSCORE_FILE_VERSION<< "\">\n";
150
for(unsigned int i=0; i<m_all_scores.size(); i++)
152
m_all_scores[i]->writeEntry(highscore_file);
154
highscore_file << "</highscores>\n";
155
highscore_file.close();
157
catch(std::exception &e)
159
printf("Problems saving highscores in '%s'\n", m_filename.c_str());
166
// -----------------------------------------------------------------------------
168
* Returns the high scores entry for a specific type of race. Creates one if none exists yet.
170
Highscores* HighscoreManager::getHighscores(const Highscores::HighscoreType highscore_type,
171
int num_karts, const RaceManager::Difficulty difficulty,
172
const std::string trackName, const int number_of_laps)
174
Highscores *highscores = 0;
176
// See if we already have a record for this type
177
for(type_all_scores::iterator i = m_all_scores.begin();
178
i != m_all_scores.end(); i++)
180
if((*i)->matches(highscore_type, num_karts, difficulty, trackName, number_of_laps) )
182
// we found one entry for this kind of race, return it
185
} // for i in m_all_scores
187
// we don't have an entry for such a race currently. Create one.
188
highscores = new Highscores(highscore_type, num_karts, difficulty, trackName, number_of_laps);
189
m_all_scores.push_back(highscores);