~ubuntu-branches/ubuntu/precise/supertuxkart/precise

« back to all changes in this revision

Viewing changes to src/race/highscore_manager.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Christoph Egger
  • Date: 2011-02-24 22:36:25 UTC
  • mfrom: (1.1.9 upstream) (6.1.4 sid)
  • Revision ID: james.westby@ubuntu.com-20110224223625-ygrjfpg92obovuch
Tags: 0.7+dfsg1-1
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//  $Id: highscores.hpp 921 2007-02-28 05:43:34Z hiker $
 
2
//
 
3
//  SuperTuxKart - a fun racing game with go-kart
 
4
//  Copyright (C) 2006 Joerg Henrichs
 
5
//
 
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.
 
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 Free Software
 
18
//  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
19
 
 
20
#include "race/highscore_manager.hpp"
 
21
 
 
22
#include <stdexcept>
 
23
#include <fstream>
 
24
 
 
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"
 
30
 
 
31
HighscoreManager* highscore_manager=0;
 
32
 
 
33
HighscoreManager::HighscoreManager()
 
34
{
 
35
    m_can_write=true;
 
36
    setFilename();
 
37
    loadHighscores();
 
38
}   // HighscoreManager
 
39
 
 
40
// -----------------------------------------------------------------------------
 
41
HighscoreManager::~HighscoreManager()
 
42
{
 
43
    saveHighscores();
 
44
    for(type_all_scores::iterator i  = m_all_scores.begin(); 
 
45
                                  i != m_all_scores.end();  i++)
 
46
        delete *i;
 
47
}   // ~HighscoreManager
 
48
 
 
49
// -----------------------------------------------------------------------------
 
50
/** Determines the path to store the highscore file in
 
51
 */
 
52
void HighscoreManager::setFilename()
 
53
{
 
54
    if ( getenv("SUPERTUXKART_HIGHSCOREDIR") != NULL )
 
55
    {
 
56
        m_filename = getenv("SUPERTUXKART_HIGHSCOREDIR")
 
57
                   + std::string("/highscore.xml");
 
58
    }
 
59
    else 
 
60
    {
 
61
        m_filename=file_manager->getHighscoreFile("highscore.xml");
 
62
    }
 
63
 
 
64
    return;
 
65
}   // SetFilename
 
66
 
 
67
// -----------------------------------------------------------------------------
 
68
void HighscoreManager::loadHighscores()
 
69
{
 
70
    XMLNode *root = NULL;
 
71
    root = file_manager->createXMLTree(m_filename);
 
72
    if(!root)
 
73
    {
 
74
        saveHighscores();
 
75
        if(m_can_write)
 
76
        {
 
77
            fprintf(stderr, "New highscore file '%s' created.\n", 
 
78
                    m_filename.c_str());
 
79
        }
 
80
        delete root;
 
81
        return;
 
82
    }
 
83
 
 
84
    try
 
85
    {
 
86
        if(!root || root->getName()!="highscores")
 
87
            throw std::runtime_error("No 'highscore' node found.");
 
88
        
 
89
        // check file version
 
90
        int v;
 
91
        if (!root->get("version", &v) || v<(int)CURRENT_HSCORE_FILE_VERSION)
 
92
        {
 
93
            fprintf(stderr, 
 
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 );
 
98
            
 
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.
 
101
            saveHighscores();
 
102
            return;
 
103
        }
 
104
        
 
105
        // read all entries one by one and store them in 'm_all_scores'
 
106
        for(unsigned int i=0; i<root->getNumNodes(); i++)
 
107
        {
 
108
            const XMLNode *node = root->getNode(i);
 
109
            Highscores *highscores;
 
110
            try
 
111
            {
 
112
                highscores = new Highscores(*node);
 
113
            }
 
114
            catch (std::logic_error& e)
 
115
            {
 
116
                fprintf(stderr, "Invalid highscore entry will be skipped : %s\n", e.what());
 
117
                continue;
 
118
            }
 
119
            m_all_scores.push_back(highscores);
 
120
        }   // next entry
 
121
        
 
122
        if(UserConfigParams::m_verbosity>=4)
 
123
            fprintf(stderr, "Highscores will be saved in '%s'.\n",
 
124
                    m_filename.c_str());
 
125
    }
 
126
    catch(std::exception& err)
 
127
    {
 
128
        fprintf(stderr, "Error while parsing highscore file '%s':\n", 
 
129
                m_filename.c_str());
 
130
        fprintf(stderr, "%s", err.what());
 
131
        fprintf(stderr, "\n");
 
132
        fprintf(stderr, "No old highscores will be available.\n");
 
133
    }
 
134
    delete root;
 
135
}   // loadHighscores
 
136
 
 
137
// -----------------------------------------------------------------------------
 
138
void HighscoreManager::saveHighscores()
 
139
{
 
140
    // Print error message only once
 
141
    if(!m_can_write) return;
 
142
 
 
143
    try
 
144
    {
 
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";
 
149
 
 
150
        for(unsigned int i=0; i<m_all_scores.size(); i++)
 
151
        {
 
152
            m_all_scores[i]->writeEntry(highscore_file);
 
153
        }
 
154
        highscore_file << "</highscores>\n";
 
155
        highscore_file.close();
 
156
    }
 
157
    catch(std::exception &e)
 
158
    {
 
159
        printf("Problems saving highscores in '%s'\n", m_filename.c_str());
 
160
        puts(e.what());
 
161
        m_can_write=false;
 
162
    }
 
163
 
 
164
}   // saveHighscores
 
165
 
 
166
// -----------------------------------------------------------------------------
 
167
/*
 
168
 * Returns the high scores entry for a specific type of race. Creates one if none exists yet.
 
169
 */
 
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)
 
173
{
 
174
    Highscores *highscores = 0;
 
175
 
 
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++)
 
179
    {
 
180
        if((*i)->matches(highscore_type, num_karts, difficulty, trackName, number_of_laps) )
 
181
        {
 
182
            // we found one entry for this kind of race, return it
 
183
            return (*i);
 
184
        }
 
185
    }   // for i in m_all_scores
 
186
 
 
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);
 
190
    return highscores;
 
191
}   // getHighscores