A historical commit: replaced highscores lisp files with xml,

allowing us to completely remove the lisp code from stk - hooray!


git-svn-id: svn+ssh://svn.code.sf.net/p/supertuxkart/code/main/trunk@5072 178a84e3-b1eb-0310-8ba1-8eac791a3b58
This commit is contained in:
hikerstk 2010-03-25 22:29:47 +00:00
parent 4fa02b5325
commit e95d5ad5dd
10 changed files with 161 additions and 239 deletions

View File

@ -172,14 +172,6 @@ supertuxkart_SOURCES = \
karts/kart_properties_manager.hpp \
karts/moveable.cpp \
karts/moveable.hpp \
lisp/lexer.cpp \
lisp/lexer.hpp \
lisp/lisp.cpp \
lisp/lisp.hpp \
lisp/parser.cpp \
lisp/parser.hpp \
lisp/writer.cpp \
lisp/writer.hpp \
modes/follow_the_leader.cpp \
modes/follow_the_leader.hpp \
modes/linear_world.cpp \

View File

@ -285,26 +285,6 @@
</File>
</Filter>
</Filter>
<Filter
Name="lisp"
>
<File
RelativePath="../../../src\lisp\lexer.cpp"
>
</File>
<File
RelativePath="../../../src\lisp\lisp.cpp"
>
</File>
<File
RelativePath="../../../src\lisp\parser.cpp"
>
</File>
<File
RelativePath="../../../src\lisp\writer.cpp"
>
</File>
</Filter>
<Filter
Name="challenges"
>
@ -1019,26 +999,6 @@
</File>
</Filter>
</Filter>
<Filter
Name="lisp"
>
<File
RelativePath="../../../src\lisp\lexer.hpp"
>
</File>
<File
RelativePath="../../../src\lisp\lisp.hpp"
>
</File>
<File
RelativePath="../../../src\lisp\parser.hpp"
>
</File>
<File
RelativePath="../../../src\lisp\writer.hpp"
>
</File>
</Filter>
<Filter
Name="challenges"
>

View File

@ -445,18 +445,18 @@ void World::update(float dt)
// ----------------------------------------------------------------------------
HighscoreEntry* World::getHighscores() const
Highscores* World::getHighscores() const
{
if(!m_use_highscores) return NULL;
const HighscoreEntry::HighscoreType type = "HST_" + getIdent();
const Highscores::HighscoreType type = "HST_" + getIdent();
HighscoreEntry* highscores =
highscore_manager->getHighscoreEntry(type,
getNumKarts(),
race_manager->getDifficulty(),
race_manager->getTrackName(),
race_manager->getNumLaps());
Highscores * highscores =
highscore_manager->getHighscores(type,
getNumKarts(),
race_manager->getDifficulty(),
race_manager->getTrackName(),
race_manager->getNumLaps());
return highscores;
} // getHighscores
@ -515,14 +515,14 @@ void World::updateHighscores()
assert(index[pos] < m_karts.size());
Kart *k = (Kart*)m_karts[index[pos]];
HighscoreEntry* highscores = getHighscores();
Highscores* highscores = getHighscores();
PlayerController *controller = (PlayerController*)(k->getController());
if(highscores->addData(k->getIdent(),
controller->getPlayer()->getProfile()->getName(),
k->getFinishTime())>0 )
{
highscore_manager->Save();
highscore_manager->saveHighscores();
}
} // next position
delete []index;

View File

@ -179,7 +179,7 @@ public:
float getFastestLapTime() const { return m_fastest_lap; }
void setFastestLap(Kart *k, float time){ m_fastest_kart = k;
m_fastest_lap = time; }
HighscoreEntry *getHighscores() const;
Highscores *getHighscores() const;
virtual void terminateRace();

View File

@ -20,12 +20,10 @@
#include "race/highscore_manager.hpp"
#include <stdexcept>
#include <sstream>
#include <fstream>
#include "config/user_config.hpp"
#include "io/file_manager.hpp"
#include "lisp/parser.hpp"
#include "lisp/writer.hpp"
#include "race/race_manager.hpp"
#include "utils/string_utils.hpp"
#include "utils/translation.hpp"
@ -35,53 +33,45 @@ HighscoreManager* highscore_manager=0;
HighscoreManager::HighscoreManager()
{
m_can_write=true;
SetFilename();
Load();
setFilename();
loadHighscores();
} // HighscoreManager
// -----------------------------------------------------------------------------
HighscoreManager::~HighscoreManager()
{
Save();
for(type_all_scores::iterator i = m_allScores.begin();
i != m_allScores.end(); i++)
saveHighscores();
for(type_all_scores::iterator i = m_all_scores.begin();
i != m_all_scores.end(); i++)
delete *i;
} // ~HighscoreManager
// -----------------------------------------------------------------------------
/** Determines the path to store the highscore file in
*/
void HighscoreManager::SetFilename()
void HighscoreManager::setFilename()
{
if ( getenv("SUPERTUXKART_HIGHSCOREDIR") != NULL )
{
m_filename = getenv("SUPERTUXKART_HIGHSCOREDIR")
+ std::string("/highscore.data");
+ std::string("/highscore.xml");
}
else
{
m_filename=file_manager->getHighscoreFile("highscore.data");
m_filename=file_manager->getHighscoreFile("highscore.xml");
}
return;
} // SetFilename
// -----------------------------------------------------------------------------
void HighscoreManager::Load()
void HighscoreManager::loadHighscores()
{
const lisp::Lisp* root = 0;
std::exception err;
try
XMLNode *root = NULL;
root = file_manager->createXMLTree(m_filename);
if(!root)
{
lisp::Parser parser;
root = parser.parse(m_filename);
}
catch(std::exception& err)
{
(void)err; // remove warning about unused variable
Save();
saveHighscores();
if(m_can_write)
{
fprintf(stderr, "New highscore file '%s' created.\n",
@ -90,55 +80,39 @@ void HighscoreManager::Load()
delete root;
return;
}
try
{
// check for opening 'highscores' nodes
const lisp::Lisp* const node = root->getLisp("highscores");
if(!node)
{
if(!root || root->getName()!="highscores")
throw std::runtime_error("No 'highscore' node found.");
}
// check file version
int v;
if (!node->get("file-version",v) || v<(int)CURRENT_HSCORE_FILE_VERSION)
if (!root->get("version", &v) || v<(int)CURRENT_HSCORE_FILE_VERSION)
{
fprintf(stderr, "Highscore file format too old, a new one will be created.\n");
irr::core::stringw warning = _("The highscore file was too old,\nall highscores have been erased.");
fprintf(stderr,
"Highscore file format too old, a new one will be created.\n");
irr::core::stringw warning =
_("The highscore file was too old,\nall highscores have been erased.");
user_config->setWarning( warning );
// since we haven't had the chance to load the current scores yet,
// calling Save() now will generate an empty file.
Save();
// calling Save() now will generate an empty file with the right format.
saveHighscores();
return;
}
// get number of entries
int n;
if (!node->get("number-entries",n))
// read all entries one by one and store them in 'm_all_scores'
for(unsigned int i=0; i<root->getNumNodes(); i++)
{
throw std::runtime_error("No 'number-entries' node found.");
}
// read all entries one by one and store them in 'm_allScores'
for(int i=0; i<n; i++)
{
std::ostringstream record_name;
record_name << "record-" << i;
const lisp::Lisp* const node_record=node->getLisp(record_name.str());
if(!node_record)
{
std::ostringstream msg;
msg << "Can't find record '" << i << "' in '"
<< m_filename << "'";
throw std::runtime_error(msg.str());
}
HighscoreEntry *highscores = new HighscoreEntry(node_record);
m_allScores.push_back(highscores);
//highscores->Read(node_record);
}// next entry
const XMLNode *node = root->getNode(i);
Highscores *highscores = new Highscores(*node);
m_all_scores.push_back(highscores);
} // next entry
fprintf(stderr, "Highscores will be saved in '%s'.\n",m_filename.c_str());
if(UserConfigParams::m_verbosity>=4)
fprintf(stderr, "Highscores will be saved in '%s'.\n",
m_filename.c_str());
}
catch(std::exception& err)
{
@ -149,78 +123,60 @@ void HighscoreManager::Load()
fprintf(stderr, "No old highscores will be available.\n");
}
delete root;
} // Load
} // loadHighscores
// -----------------------------------------------------------------------------
void HighscoreManager::Save()
void HighscoreManager::saveHighscores()
{
// Print error message only once
if(!m_can_write) return;
#if 0
try
{
std::ofstream highscore_file;
highscore_file.open(m_filename);
highscore_file.open(m_filename.c_str());
highscore_file << "<?xml version=\"1.0\"?>\n";
configfile << "<stkconfig version=\"" << CURRENT_CONFIG_VERSION << "\" >\n\n";
highscore_file << "<highscores version=\"" << CURRENT_HSCORE_FILE_VERSION<< "\">\n";
}
#else
try
{
lisp::Writer writer(m_filename);
writer.beginList("highscores");
writer.writeComment("File format version");
writer.write("file-version\t", CURRENT_HSCORE_FILE_VERSION);
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++)
for(unsigned int i=0; i<m_all_scores.size(); i++)
{
std::ostringstream record_name;
record_name << "record-" << record_number << "\t";
record_number++;
writer.beginList(record_name.str());
(*i)->Write(&writer);
writer.endList(record_name.str());
} // next score
writer.endList("highscores");
m_can_write=true;
} // try
m_all_scores[i]->writeEntry(highscore_file);
}
highscore_file << "</highscores>\n";
highscore_file.close();
}
catch(std::exception &e)
{
printf("Problems saving highscores in '%s'\n",
m_filename.c_str());
printf("Problems saving highscores in '%s'\n", m_filename.c_str());
puts(e.what());
m_can_write=false;
}
#endif
} // Save
} // saveHighscores
// -----------------------------------------------------------------------------
/*
* Returns the high scores entry for a specific type of race. Creates one if none exists yet.
*/
HighscoreEntry* HighscoreManager::getHighscoreEntry(const HighscoreEntry::HighscoreType highscore_type,
int num_karts, const RaceManager::Difficulty difficulty,
const std::string trackName, const int number_of_laps)
Highscores* HighscoreManager::getHighscores(const Highscores::HighscoreType highscore_type,
int num_karts, const RaceManager::Difficulty difficulty,
const std::string trackName, const int number_of_laps)
{
HighscoreEntry *highscores = 0;
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++)
for(type_all_scores::iterator i = m_all_scores.begin();
i != m_all_scores.end(); i++)
{
if((*i)->matches(highscore_type, num_karts, difficulty, trackName, number_of_laps) )
{
// we found one entry for this kind of race, return it
return (*i);
}
} // for i in m_allScores
} // for i in m_all_scores
// we don't have an entry for such a race currently. Create one.
highscores = new HighscoreEntry(highscore_type, num_karts, difficulty, trackName, number_of_laps);
m_allScores.push_back(highscores);
highscores = new Highscores(highscore_type, num_karts, difficulty, trackName, number_of_laps);
m_all_scores.push_back(highscores);
return highscores;
} // getHighscoreEntry
} // getHighscores

View File

@ -25,9 +25,6 @@
#include <map>
#include "race/highscores.hpp"
#include "lisp/lisp.hpp"
const unsigned int CURRENT_HSCORE_FILE_VERSION = 1;
/**
* This class reads and writes the 'highscores.data' file, and also takes
@ -38,22 +35,25 @@ class HighscoreManager
{
public:
private:
typedef std::vector<HighscoreEntry*> type_all_scores;
type_all_scores m_allScores;
static const unsigned int CURRENT_HSCORE_FILE_VERSION = 1;
typedef std::vector<Highscores*> type_all_scores;
type_all_scores m_all_scores;
std::string m_filename;
bool m_can_write;
void Load();
void SetFilename();
void loadHighscores();
void setFilename();
public:
HighscoreManager();
~HighscoreManager();
void Save();
HighscoreEntry *getHighscoreEntry(const HighscoreEntry::HighscoreType highscore_type,
int num_karts, const RaceManager::Difficulty difficulty,
const std::string trackName, const int number_of_laps);
HighscoreManager();
~HighscoreManager();
void saveHighscores();
Highscores *getHighscores(const Highscores::HighscoreType highscore_type,
int num_karts,
const RaceManager::Difficulty difficulty,
const std::string trackName,
const int number_of_laps);
}; // HighscoreManager
extern HighscoreManager* highscore_manager;

View File

@ -20,14 +20,17 @@
#include "race/highscores.hpp"
#include <stdexcept>
#include <sstream>
#include <fstream>
#include "io/xml_node.hpp"
#include "race/race_manager.hpp"
// -----------------------------------------------------------------------------
HighscoreEntry::HighscoreEntry(const HighscoreEntry::HighscoreType highscore_type,
int num_karts, const RaceManager::Difficulty difficulty,
const std::string trackName, const int number_of_laps)
Highscores::Highscores(const HighscoreType highscore_type,
int num_karts,
const RaceManager::Difficulty difficulty,
const std::string trackName,
const int number_of_laps)
{
m_track = trackName;
m_highscore_type = highscore_type;
@ -43,7 +46,7 @@ HighscoreEntry::HighscoreEntry(const HighscoreEntry::HighscoreType highscore_typ
}
}
// -----------------------------------------------------------------------------
HighscoreEntry::HighscoreEntry(const lisp::Lisp* const node)
Highscores::Highscores(const XMLNode &node)
{
m_track = "";
m_highscore_type = "HST_UNDEFINED";
@ -58,54 +61,62 @@ HighscoreEntry::HighscoreEntry(const lisp::Lisp* const node)
m_time[i] = -9.9f;
}
Read(node);
}
readEntry(node);
} // Highscores
// -----------------------------------------------------------------------------
void HighscoreEntry::Read(const lisp::Lisp* const node)
void Highscores::readEntry(const XMLNode &node)
{
node->get("track-name", m_track );
node->get("number-karts", m_number_of_karts );
node.get("track-name", &m_track );
node.get("number-karts", &m_number_of_karts );
std::string hst="HST_UNDEFINED";
node->get("hscore-type", hst );
node.get("hscore-type", &hst );
m_highscore_type = (HighscoreType)hst;
node->get("difficulty", m_difficulty );
node->get("number-of-laps", m_number_of_laps );
node.get("difficulty", &m_difficulty );
node.get("number-of-laps", &m_number_of_laps );
for(unsigned int i=0; i<node.getNumNodes(); i++)
{
const XMLNode *entry = node.getNode(i);
entry->get("time", &m_time[i] );
entry->get("name", &m_name[i] );
entry->get("kart-name", &m_kart_name[i] );
}
} // readEntry
// -----------------------------------------------------------------------------
/** Writes the highscores in this entry to the writer. It will only write
* anything if there is actually a highscore recored (i.e. time >=0). Empty
* entries are created e.g. when changing the number of laps in the GUI,
* resulting in empty entries here.
* \param writer The file stream to write the data to.
*/
void Highscores::writeEntry(std::ofstream &writer)
{
// Only
bool one_is_set = false;
for(unsigned int i=0; i<HIGHSCORE_LEN; i++)
one_is_set |= m_time[i]>=0;
if(!one_is_set) return;
writer << " <highscore track-name =\"" <<m_track <<"\"\n";
writer << " number-karts =\"" <<m_number_of_karts<<"\"\n";
writer << " difficulty =\"" <<m_difficulty <<"\"\n";
writer << " hscore-type =\"" <<m_highscore_type <<"\"\n";
writer << " number-of-laps=\"" <<m_number_of_laps <<"\">\n";
for(int i=0; i<HIGHSCORE_LEN; i++)
{
std::ostringstream s;
s << "time-" << i;
node->get(s.str(),m_time[i] );
s.str(""); s << "name-" << i;
node->get(s.str(),m_name[i] );
s.str(""); s << "kartname-" << i;
node->get(s.str(), m_kart_name[i] );
}
} // Read
writer << " <entry time =\""<<m_time[i]<<"\"\n";
writer << " name =\""<<m_name[i]<<"\"\n";
writer << " kartname=\""<<m_kart_name[i]
<< "\"/>\n";
} // for i
writer << " </highscore>\n";
} // writeEntry
// -----------------------------------------------------------------------------
void HighscoreEntry::Write(lisp::Writer *writer)
{
writer->write("track-name\t", m_track );
writer->write("number-karts\t", m_number_of_karts );
writer->write("difficulty\t\t", m_difficulty );
writer->write("hscore-type\t\t", m_highscore_type );
writer->write("number-of-laps\t", m_number_of_laps );
for(int j=0; j<HIGHSCORE_LEN; j++)
{
std::ostringstream s;
s << "time-" << j << "\t\t";
writer->write(s.str(), m_time[j] );
s.str(""); s << "name-" << j << "\t\t";
writer->write(s.str(), m_name[j] );
s.str(""); s << "kartname-" << j << "\t\t";
writer->write(s.str(), m_kart_name[j] );
} // for j
} // Write
// -----------------------------------------------------------------------------
int HighscoreEntry::matches(HighscoreType highscore_type,
int Highscores::matches(HighscoreType highscore_type,
int num_karts, RaceManager::Difficulty difficulty,
const std::string track, const int number_of_laps)
{
@ -115,13 +126,14 @@ int HighscoreEntry::matches(HighscoreType highscore_type,
m_number_of_laps == number_of_laps &&
m_number_of_karts == num_karts );
} // matches
// -----------------------------------------------------------------------------
/** Inserts the data into the highscore list.
* If the new entry is fast enough to
* be in the highscore list, the new position (1-HIGHSCORE_LEN) is returned,
* otherwise a 0.
*/
int HighscoreEntry::addData(const std::string& kart_name,
int Highscores::addData(const std::string& kart_name,
const std::string& name, const float time)
{
int position=-1;
@ -163,7 +175,7 @@ int HighscoreEntry::addData(const std::string& kart_name,
} // addData
// -----------------------------------------------------------------------------
int HighscoreEntry::getNumberEntries() const
int Highscores::getNumberEntries() const
{
for(int i=HIGHSCORE_LEN-1; i>=0; i--)
{
@ -173,7 +185,7 @@ int HighscoreEntry::getNumberEntries() const
} // getNumberEntries
// -----------------------------------------------------------------------------
void HighscoreEntry::getEntry(int number, std::string &kart_name,
void Highscores::getEntry(int number, std::string &kart_name,
std::string &name, float *const time) const
{
if(number<0 || number>getNumberEntries())

View File

@ -22,15 +22,17 @@
#include <string>
#include <vector>
#include <map>
#include <fstream>
#include "lisp/lisp.hpp"
#include "lisp/writer.hpp"
#include "race/race_manager.hpp"
class XMLNode;
/**
* Represents one highscore entry.
*/
class HighscoreEntry
* Represents one highscore entry, i.e. the (atm up to three) highscores
* for a particular setting (track, #karts, difficulty etc).
*/
class Highscores
{
public:
typedef std::string HighscoreType;
@ -48,15 +50,15 @@ private:
public:
/** Creates a new entry
*/
HighscoreEntry (const HighscoreEntry::HighscoreType highscore_type,
int num_karts, const RaceManager::Difficulty difficulty,
const std::string trackName, const int number_of_laps);
Highscores (const Highscores::HighscoreType highscore_type,
int num_karts, const RaceManager::Difficulty difficulty,
const std::string trackName, const int number_of_laps);
/** Creates an entry from a file
*/
HighscoreEntry (const lisp::Lisp* const node);
Highscores (const XMLNode &node);
void Read (const lisp::Lisp* const node);
void Write (lisp::Writer* writer);
void readEntry (const XMLNode &node);
void writeEntry(std::ofstream &writer);
int matches (HighscoreType highscore_type, int num_karts,
const RaceManager::Difficulty difficulty,
const std::string track, const int number_of_laps);
@ -65,6 +67,6 @@ public:
int getNumberEntries() const;
void getEntry (int number, std::string &kart_name,
std::string &name, float *const time) const;
}; // HighscoreEntry
}; // Highscores
#endif

View File

@ -181,7 +181,7 @@ RaceOverDialog::RaceOverDialog(const float percentWidth,
// ---- Highscores
if (show_highscores)
{
const HighscoreEntry *hs = World::getWorld()->getHighscores();
const Highscores *hs = World::getWorld()->getHighscores();
if (hs != NULL)
{
core::rect< s32 > hsarea(m_area.getWidth()*2/3, 0, m_area.getWidth(), text_height);

View File

@ -213,14 +213,14 @@ void TrackInfoDialog::addHighScoreWidgets(const int hscores_y_from, const int hs
void TrackInfoDialog::updateHighScores()
{
std::string game_mode_ident = RaceManager::getIdentOf( race_manager->getMinorMode() );
const HighscoreEntry::HighscoreType type = "HST_" + game_mode_ident;
const Highscores::HighscoreType type = "HST_" + game_mode_ident;
HighscoreEntry* highscores =
highscore_manager->getHighscoreEntry(type,
race_manager->getNumberOfKarts(),
race_manager->getDifficulty(),
m_track_ident,
race_manager->getNumLaps() );
Highscores* highscores =
highscore_manager->getHighscores(type,
race_manager->getNumberOfKarts(),
race_manager->getDifficulty(),
m_track_ident,
race_manager->getNumLaps() );
const int amount = highscores->getNumberEntries();
std::string kart_name;