Back to index

supertuxkart  0.5+dfsg1
Public Member Functions | Private Types | Private Member Functions | Private Attributes
HighscoreManager Class Reference

#include <highscore_manager.hpp>

Collaboration diagram for HighscoreManager:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 HighscoreManager ()
 ~HighscoreManager ()
void Save ()
HighscoresgetHighscores (const Highscores::HighscoreType highscore_type)
HighscoresaddResult (const Highscores::HighscoreType highscore_type, const std::string kart_name, const std::string name, const float time)

Private Types

typedef std::vector< Highscores * > type_all_scores

Private Member Functions

void Load ()
void SetFilename ()
 Determines the path to store the highscore file in.

Private Attributes

type_all_scores m_allScores
std::string m_filename
bool m_can_write

Detailed Description

Definition at line 29 of file highscore_manager.hpp.


Member Typedef Documentation

typedef std::vector<Highscores*> HighscoreManager::type_all_scores [private]

Definition at line 33 of file highscore_manager.hpp.


Constructor & Destructor Documentation

Definition at line 34 of file highscore_manager.cpp.

{
    m_can_write=true;
    SetFilename();
    Load();
}   // HighscoreManager

Here is the call graph for this function:

Definition at line 42 of file highscore_manager.cpp.

{
    Save();
    for(type_all_scores::iterator i  = m_allScores.begin(); 
                                  i != m_allScores.end();  i++)
        delete *i;
}   // ~HighscoreManager

Here is the call graph for this function:


Member Function Documentation

Highscores * HighscoreManager::addResult ( const Highscores::HighscoreType  highscore_type,
const std::string  kart_name,
const std::string  name,
const float  time 
)

Definition at line 200 of file highscore_manager.cpp.

{
    Highscores *highscores = getHighscores(highscore_type);

    if(highscores->addData(highscore_type, kart_name, name, time) >0)
    {
        Save();
    }
    return highscores;
}   // addResult

Here is the call graph for this function:

Definition at line 174 of file highscore_manager.cpp.

{
    Highscores *highscores = 0;

    // See if we already have a record for this type

    for(type_all_scores::iterator i  = m_allScores.begin(); 
                                  i != m_allScores.end();  i++)
    {
        if((*i)->matches(highscore_type, race_manager->getNumKarts(), 
                         race_manager->getDifficulty(), race_manager->getTrackName(),
                         race_manager->getNumLaps()))
        {
            return (*i);
        }
    }   // for i in m_allScores

    highscores = new Highscores();
    m_allScores.push_back(highscores);
    return highscores;
}   // getHighscores

Here is the call graph for this function:

Here is the caller graph for this function:

void HighscoreManager::Load ( ) [private]

Definition at line 69 of file highscore_manager.cpp.

{

    const lisp::Lisp* root = 0;
       std::exception err;

    try
    {
        lisp::Parser parser;
        root = parser.parse(m_filename);
    }
    catch(std::exception& err)
    {
        (void)err;   // remove warning about unused variable
        Save();
        if(m_can_write)
        {
            fprintf(stderr, "New highscore file '%s' created.\n", 
                    m_filename.c_str());
        }
        delete root;
        return;
    }
    try
    {
        const lisp::Lisp* const node = root->getLisp("highscores");
        if(!node)
        {
            char msg[MAX_ERROR_MESSAGE_LENGTH];
            snprintf(msg, sizeof(msg), "No 'highscore' node found.");
            throw std::runtime_error(msg);
        }
        int n;
        if (!node->get("number-entries",n))
        {
            char msg[MAX_ERROR_MESSAGE_LENGTH];
            snprintf(msg, sizeof(msg), "No 'number-entries' node found.");
            throw std::runtime_error(msg);
        }

        for(int i=0; i<n; i++)
        {
            char record_name[255];
            snprintf(record_name, sizeof(record_name), "record-%d", i);
            const lisp::Lisp* const node_record=node->getLisp(record_name);
            if(!node_record) 
            {
                char msg[MAX_ERROR_MESSAGE_LENGTH];
                snprintf(msg, sizeof(msg),"Can't find record '%d' in '%s'",
                         i,m_filename.c_str());
                throw std::runtime_error(msg);
            }
            Highscores *highscores = new Highscores();
            m_allScores.push_back(highscores);
            highscores->Read(node_record);
        }
    fprintf(stderr, "Highscores will be saved in '%s'.\n",m_filename.c_str());
    }
    catch(std::exception& err)
    {
        fprintf(stderr, "Error while parsing highscore file '%s':\n", 
                m_filename.c_str());
        fprintf(stderr, err.what());
        fprintf(stderr, "\n");
        fprintf(stderr, "No old highscores will be available.\n");
    }
    delete root;
}   // Load

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 139 of file highscore_manager.cpp.

{
    // Print error message only once
    if(!m_can_write) return;
    try
    {
        lisp::Writer writer(m_filename);
        writer.beginList("highscores");
          writer.writeComment("Number of highscores in this file");
          writer.write("number-entries\t",(unsigned int)m_allScores.size());
          int record_number=0;
          for(type_all_scores::iterator i  = m_allScores.begin(); 
              i != m_allScores.end();  i++)
          {
              char record_name[255];
              snprintf(record_name, sizeof(record_name),"record-%d\t",record_number);
              record_number++;
              writer.beginList(record_name);
              (*i)->Write(&writer);
              writer.endList(record_name);
          }   // for i
        writer.endList("highscores");
        m_can_write=true;
    }   // try
    catch(std::exception &e)
    {
        printf("Problems saving highscores in '%s'\n",
               m_filename.c_str());
        printf(e.what());
        printf("\n");
        m_can_write=false;
    }
}   // Save

Here is the call graph for this function:

Here is the caller graph for this function:

void HighscoreManager::SetFilename ( ) [private]

Determines the path to store the highscore file in.

Definition at line 53 of file highscore_manager.cpp.

{
    if ( getenv("SUPERTUXKART_HIGHSCOREDIR") != NULL )
    {
        m_filename = getenv("SUPERTUXKART_HIGHSCOREDIR")
                 + std::string("/highscore.data");
    }
    else 
    {
        m_filename=file_manager->getHighscoreFile("highscore.data");
    }

    return;
}   // SetFilename

Here is the call graph for this function:

Here is the caller graph for this function:


Member Data Documentation

Definition at line 34 of file highscore_manager.hpp.

Definition at line 37 of file highscore_manager.hpp.

std::string HighscoreManager::m_filename [private]

Definition at line 36 of file highscore_manager.hpp.


The documentation for this class was generated from the following files: