Back to index

extremetuxracer  0.5beta
Classes | Public Member Functions | Static Public Member Functions | Public Attributes | Static Public Attributes | Private Member Functions | Static Private Attributes
highscore Class Reference

#include <highscore.h>

Collaboration diagram for highscore:
Collaboration graph
[legend]

List of all members.

Classes

struct  level_highscore
struct  scorepost

Public Member Functions

 highscore ()
int addScore (std::string level, std::string nick, int score)
int posFromString (std::string level)
std::list< std::string > getCourseList ()
std::list< HighscoreDatagetCourseList_hd ()
bool saveData ()
bool loadData ()
void debug ()
void printlist ()
void redim ()

Static Public Member Functions

static std::string posToStr (int pos)

Public Attributes

level_highscorelevel_hs
int level_hs_length

Static Public Attributes

static bool useHighscore = true
static bool hsDebug = false
static bool anyHighscore = false

Private Member Functions

int findLevel (std::string level)
void sort (int level)
std::string toOutputFormat (std::string str)
std::string fromOutputFormat (std::string str)

Static Private Attributes

static const int version = 2

Detailed Description

Definition at line 38 of file highscore.h.


Constructor & Destructor Documentation

Definition at line 47 of file highscore.cpp.


Member Function Documentation

int highscore::addScore ( std::string  level,
std::string  nick,
int  score 
)

Definition at line 52 of file highscore.cpp.

                                                             {
       int n=findLevel(level);
       if(n == -1) {
              redim();
              level_hs[level_hs_length-1].level = level;
              n = level_hs_length-1;
       }
       //std::cout<<score << " > " << level_hs[n].post[9].score<<std::endl;
       if(score > level_hs[n].post[9].score) {
              /*
                     the list should be sorted, so if score is greater than the lowest score
                     we replace that post with this new one. Then we sort the list
              */
              level_hs[n].post[9].score = score;
              level_hs[n].post[9].nick.assign(nick,0,nick.length());
              if(level_hs[n].posts < 10)
                     level_hs[n].posts++;
              sort(n);
              anyHighscore=true;
              //printlist();
              for(int i=0;i<level_hs[n].posts;i++) {
                     if(level_hs[n].post[i].score == score) {
                            return (i+1);
                     }
              }
              return level_hs[n].posts;
       } else {
              return -1;
       }
              
}

Here is the call graph for this function:

Here is the caller graph for this function:

void highscore::debug ( )

Definition at line 278 of file highscore.cpp.

                 {
       addScore("DEBUG","TUX_DEBUG",100);
       printlist();
}

Here is the call graph for this function:

int highscore::findLevel ( std::string  level) [private]

Definition at line 127 of file highscore.cpp.

                                    {
       for(int i=0;i<level_hs_length;i++) {
              if(level_hs[i].level.compare(level) == 0) {
                     return i;
              }
       }
       return -1;
}

Here is the caller graph for this function:

std::string highscore::fromOutputFormat ( std::string  str) [private]

Definition at line 145 of file highscore.cpp.

                                         {
       while(str.find("_") != std::string::npos) {
              str.replace(str.find("_"),1," ");
       }
       return str;
}

Here is the caller graph for this function:

std::list< std::string > highscore::getCourseList ( )

Definition at line 297 of file highscore.cpp.

                         {
       std::list<std::string> tmp;
       for(int i=0;i<level_hs_length;i++) {
              tmp.push_back(level_hs[i].level);
       }
       return tmp;
}

Here is the caller graph for this function:

Definition at line 306 of file highscore.cpp.

                            {
       std::list<HighscoreData> tmp;
       if(level_hs_length>0) {
              for(int i=0;i<level_hs_length;i++) {
                     HighscoreData tmp2;
                     tmp2.name = level_hs[i].level;
                     tmp2.highscore = level_hs[i].level;
                     tmp.push_back(tmp2);
              }
       } else {
              HighscoreData tmp2;
              tmp2.name = _("No records");
              tmp2.highscore = "";
              tmp.push_back(tmp2);
       }
       return tmp;
}

Here is the caller graph for this function:

Definition at line 191 of file highscore.cpp.

{
       char buff[256];

       if (get_config_dir_name( buff, 255 ) != 0) {
              return false;
       }
       
       std::string filename(buff);
       
       filename+="/highscore.dat";
       
       
       std::ifstream sfile(filename.c_str());
       
       if(!sfile) return false;
       
       int _version;
       int _posts;
       
                     
       sfile >> buff >> buff >> _version;
       if(_version == version) {
              bool end=false;
              bool loop=false;
              while(!end) {
                     memset(buff,256,'\0');
                     sfile >> buff;
                     if(std::string("<level>").compare(buff)==0) {
                            int n;
                            redim();
                            n = level_hs_length-1;
                            memset(buff,256,'\0');
                            sfile >> buff;
                            level_hs[n].level.assign(fromOutputFormat(buff));
                            loop=true;
                            for(_posts=0;loop&&_posts<10;_posts++) {
                                   char nick[256];
                                   memset(nick,256,'\0');
                                   int score;
                                   sfile >> nick >> score;
                                   if(std::string("</level>").compare(nick)!=0) {
                                          level_hs[n].post[_posts].nick.assign(fromOutputFormat(nick));
                                          level_hs[n].post[_posts].score = score;
                                   } else {
                                          _posts--;
                                          loop=false;
                                   }
                            }
                            level_hs[n].posts = _posts;
                     } else if(std::string("EOF").compare(buff)==0) {
                            end=true;
                     }
              }
              anyHighscore=true;
              return true;
       } else {
              std::cout<<"Highscore file version doesnt match current!"<<std::endl;
              return false;
       }
}

Here is the call graph for this function:

Here is the caller graph for this function:

int highscore::posFromString ( std::string  level)

Definition at line 254 of file highscore.cpp.

                                        {
       for(int i=0;i<level_hs_length;i++) {
              if(level_hs[i].level.compare(level) == 0) {
                     return i;
              }
       }
       return -1;
}

Here is the caller graph for this function:

std::string highscore::posToStr ( int  pos) [static]

Definition at line 85 of file highscore.cpp.

                           {
       switch(pos) {
              case 1:
                     return _("1:st");
              case 2:
                     return _("2:nd");
              case 3:
                     return _("3:rd");
              case 4:
                     return _("4:th");
              case 5:
                     return _("5:th");
              case 6:
                     return _("6:th");
              case 7:
                     return _("7:th");
              case 8:
                     return _("8:th");
              case 9:
                     return _("9:th");
              case 10:
                     return _("10:th");
              default:
                     return "";
                     
       }
}

Here is the caller graph for this function:

Definition at line 264 of file highscore.cpp.

                     {
       std::cout<<"== HIGHSCORE =="<<std::endl;
       for(int n=0;n<level_hs_length;n++) {
              std::cout<<"LEVEL: " << level_hs[n].level<<std::endl;
              for(int i=0;i<level_hs[n].posts;i++) {
                     std::cout<<level_hs[n].post[i].nick << ": " << level_hs[n].post[i].score<<std::endl;
              }
              std::cout<<"-----------"<<std::endl;
       }
       std::cout<<"== EOF HIGHSCORE =="<<std::endl;
}

Here is the caller graph for this function:

void highscore::redim ( )

Definition at line 284 of file highscore.cpp.

                 {
       //std::cout<<"Highscore: redimming..."<<std::endl;
       level_highscore *tmp = level_hs;
       level_hs = new level_highscore[++level_hs_length];

       if(tmp != NULL) {
              for(int i=0;i<level_hs_length-1;i++) {
                     level_hs[i] = tmp[i];
              }
       }
}

Here is the caller graph for this function:

Definition at line 153 of file highscore.cpp.

{
       if(!anyHighscore) {
              //std::cout << "No highscore to write, skipping"<<std::endl;
              return true;
       }
       
    char buff[256];

    if (get_config_dir_name( buff, 255 ) != 0) {
              return false;
    }
       
       std::string filename(buff);
       
       filename+="/highscore.dat";
       
       //std::cout <<"Writing highscore: "<<filename<<std::endl;
       
       std::ofstream sfile(filename.c_str());
       
       sfile << "ETRacer HIGHSCORE "<< version << std::endl;
       
       for(int n=0;n<level_hs_length;n++) {
              sfile << "<level>" << std::endl;
              sfile << toOutputFormat(level_hs[n].level) << std::endl;
              for(int i=0;i<level_hs[n].posts;i++){
                     sfile << toOutputFormat(level_hs[n].post[i].nick) << " " << level_hs[n].post[i].score << std::endl;
              }
              sfile << "</level> 0" << std::endl;
       }
       sfile << "EOF" << std::endl;
       
       
       return true;
}

Here is the call graph for this function:

Here is the caller graph for this function:

void highscore::sort ( int  level) [private]

Definition at line 114 of file highscore.cpp.

                     {
              for(int i=1; i < 10; i++) {
                     int pos = i;
                     scorepost tmp = level_hs[l].post[i];
                     while(pos > 0 && level_hs[l].post[pos-1].score < tmp.score) {
                            level_hs[l].post[pos] = level_hs[l].post[pos-1];
                            pos--;
                     }
                     level_hs[l].post[pos] = tmp;
              }
}

Here is the caller graph for this function:

std::string highscore::toOutputFormat ( std::string  str) [private]

Definition at line 137 of file highscore.cpp.

                                       {
       while(str.find(" ") != std::string::npos) {
              str.replace(str.find(" "),1,"_");
       }
       return str;
}

Here is the caller graph for this function:


Member Data Documentation

bool highscore::anyHighscore = false [static]

Definition at line 54 of file highscore.h.

bool highscore::hsDebug = false [static]

Definition at line 53 of file highscore.h.

Definition at line 80 of file highscore.h.

Definition at line 81 of file highscore.h.

bool highscore::useHighscore = true [static]

Definition at line 52 of file highscore.h.

const int highscore::version = 2 [static, private]

Definition at line 41 of file highscore.h.


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