QTfrontend/util/namegen.cpp
changeset 6160 863d3edf5690
parent 6060 fdfc01419815
child 6167 728cabee2c9f
equal deleted inserted replaced
6159:c780b8cf4d75 6160:863d3edf5690
    19 
    19 
    20 #include <QFile>
    20 #include <QFile>
    21 #include <QTextStream>
    21 #include <QTextStream>
    22 #include <QStringList>
    22 #include <QStringList>
    23 #include <QLineEdit>
    23 #include <QLineEdit>
       
    24 
       
    25 #include "hwform.h"
       
    26 #include "HWDataManager.h"
       
    27 
    24 #include "namegen.h"
    28 #include "namegen.h"
    25 #include "hwform.h"
       
    26 #include "hwconsts.h"
       
    27 
       
    28 
    29 
    29 HWNamegen::HWNamegen() {}
    30 HWNamegen::HWNamegen() {}
    30 
    31 
    31 QList<QStringList> HWNamegen::TypesTeamnames;
    32 QList<QStringList> HWNamegen::TypesTeamnames;
    32 QList<QStringList> HWNamegen::TypesHatnames;
    33 QList<QStringList> HWNamegen::TypesHatnames;
   121 
   122 
   122 QStringList HWNamegen::dictContents(const QString filename)
   123 QStringList HWNamegen::dictContents(const QString filename)
   123 {
   124 {
   124     QStringList list;
   125     QStringList list;
   125 
   126 
   126     QFile file;
   127     // find .txt to load the names from
   127 
   128     QFile * file = HWDataManager::instance().findFileForRead(QString(
   128     // find .cfg to load the names from
   129                                                 "Names/%1.txt").arg(filename));
   129     file.setFileName(QString("%1/Data/Names/%2.txt").arg(cfgdir->absolutePath()).arg(filename));
   130 
   130     if (!file.exists())
   131     if (file->exists() && file->open(QIODevice::ReadOnly | QIODevice::Text))
   131         file.setFileName(QString("%1/Names/%2.txt").arg(datadir->absolutePath()).arg(filename));
   132     {
   132 
   133         QTextStream in(file);
   133     if (file.open(QIODevice::ReadOnly | QIODevice::Text))
       
   134     {
       
   135 
       
   136         QTextStream in(&file);
       
   137         while (!in.atEnd())
   134         while (!in.atEnd())
   138         {
   135         {
   139             QString line = in.readLine();
   136             QString line = in.readLine();
   140             if(!line.isEmpty())
   137             if(!line.isEmpty())
   141                 list.append(line);
   138                 list.append(line);
   142         }
   139         }
   143     }
   140     }
   144 
   141 
       
   142     // this QFile isn't needed any further
       
   143     delete file;
       
   144 
   145     if (list.size() == 0)
   145     if (list.size() == 0)
   146          list.append(filename);
   146          list.append(filename);
   147 
   147 
   148     return list;
   148     return list;
   149 }
   149 }
   151 
   151 
   152 QStringList HWNamegen::dictsForHat(const QString hatname)
   152 QStringList HWNamegen::dictsForHat(const QString hatname)
   153 {
   153 {
   154     QStringList list;
   154     QStringList list;
   155 
   155 
   156     QFile file;
   156     // find .cfg to load the dicts from
   157 
   157     QFile * file = HWDataManager::instance().findFileForRead(QString(
   158     // find .cfg to load the names from
   158                                                 "Names/%1.cfg").arg(hatname));
   159     file.setFileName(QString("%1/Data/Names/%2.cfg").arg(cfgdir->absolutePath()).arg(hatname));
   159 
   160     if (!file.exists())
   160     if (file->exists() && file->open(QIODevice::ReadOnly | QIODevice::Text))
   161         file.setFileName(QString("%1/Names/%2.cfg").arg(datadir->absolutePath()).arg(hatname));
   161     {
   162 
   162         QTextStream in(file);
   163 
       
   164     if (file.open(QIODevice::ReadOnly | QIODevice::Text))
       
   165     {
       
   166         QTextStream in(&file);
       
   167         while (!in.atEnd())
   163         while (!in.atEnd())
   168         {
   164         {
   169             QString line = in.readLine();
   165             QString line = in.readLine();
   170             if(!line.isEmpty())
   166             if(!line.isEmpty())
   171                 list.append(line);
   167                 list.append(line);
   172         }
   168         }
   173     }
   169     }
   174 
   170 
       
   171     // this QFile isn't needed any further
       
   172     delete file;
       
   173 
   175     if (list.size() == 0)
   174     if (list.size() == 0)
   176          list.append(QString("generic"));
   175          list.append(QString("generic"));
   177 
   176 
   178     return list;
   177     return list;
   179 }
   178 }
   180 
   179 
   181 // loades types from ini files. returns true on success.
   180 // loades types from ini files. returns true on success.
   182 bool HWNamegen::loadTypes()
   181 bool HWNamegen::loadTypes()
   183 {
   182 {
   184     QFile file;
   183     typesAvailable = false;
   185 
   184 
   186     // find .cfg to load the names from
   185     // find .ini to load the names from
   187     file.setFileName(QString("%1/Data/Names/types.ini").arg(cfgdir->absolutePath()));
   186     QFile * file =
   188     if (!file.exists())
   187           HWDataManager::instance().findFileForRead(QString("Names/types.ini"));
   189         file.setFileName(QString("%1/Names/types.ini").arg(datadir->absolutePath()));
   188 
   190 
   189 
   191 
   190     if (file->exists() && file->open(QIODevice::ReadOnly | QIODevice::Text))
   192     if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
   191     {
   193         return false;
   192 
   194 
   193         int counter = 0; //counter starts with 0 (teamnames mode)
   195     int counter = 0; //counter starts with 0 (teamnames mode)
   194         TypesTeamnames.append(QStringList());
   196     TypesTeamnames.append(QStringList());
   195         TypesHatnames.append(QStringList());
   197     TypesHatnames.append(QStringList());
   196 
   198 
   197         QTextStream in(file);
   199     QTextStream in(&file);
   198         while (!in.atEnd())
   200     while (!in.atEnd())
   199         {
   201     {
   200             QString line = in.readLine();
   202         QString line = in.readLine();
   201             if (line == QString("#####"))
   203         if (line == QString("#####"))
       
   204         {
       
   205             counter++; //toggle mode (teamnames || hats)
       
   206             if ((counter%2) == 0)
       
   207             {
   202             {
   208                 TypesTeamnames.append(QStringList());
   203                 counter++; //toggle mode (teamnames || hats)
   209                 TypesHatnames.append(QStringList());
   204                 if ((counter%2) == 0)
       
   205                 {
       
   206                     TypesTeamnames.append(QStringList());
       
   207                     TypesHatnames.append(QStringList());
       
   208                 }
   210             }
   209             }
   211         }
   210             else if ((line == QString("*****")) || (line == QString("*END*")))
   212         else if ((line == QString("*****")) || (line == QString("*END*")))
       
   213         {
       
   214             typesAvailable = true;
       
   215             return true; // bye bye
       
   216         }
       
   217         else
       
   218         {
       
   219             if ((counter%2) == 0)
       
   220             {
   211             {
   221                 // even => teamnames mode
   212                 typesAvailable = true;
   222                 TypesTeamnames[(counter/2)].append(line);
   213                 return true; // bye bye
   223             }
   214             }
   224             else
   215             else
   225             {
   216             {
   226                 // odd => hats mode
   217                 if ((counter%2) == 0)
   227                 TypesHatnames[((counter-1)/2)].append(line);
   218                 {
       
   219                     // even => teamnames mode
       
   220                     TypesTeamnames[(counter/2)].append(line);
       
   221                 }
       
   222                 else
       
   223                 {
       
   224                     // odd => hats mode
       
   225                     TypesHatnames[((counter-1)/2)].append(line);
       
   226                 }
   228             }
   227             }
   229         }
   228         }
   230     }
   229 
   231 
   230         typesAvailable = true;
   232     typesAvailable = true;
   231     }
   233     return true;
   232 
       
   233     // this QFile isn't needed any further
       
   234     delete file;
       
   235 
       
   236     return typesAvailable;
   234 }
   237 }
   235 
   238 
   236 
   239 
   237 
   240 
   238 QString HWNamegen::getRandomGrave()
   241 QString HWNamegen::getRandomGrave()
   239 {
   242 {
   240     QStringList Graves;
   243     QStringList Graves;
   241 
   244 
   242     //list all available Graves
   245     //list all available Graves
   243     QDir tmpdir;
   246     Graves.append(HWDataManager::instance().entryList(
   244     tmpdir.cd(cfgdir->absolutePath());
   247                          "Graphics/Graves",
   245     tmpdir.cd("Data/Graphics/Graves");
   248                          QDir::Files,
   246     tmpdir.setFilter(QDir::Files);
   249                          QStringList("*.png")
   247     Graves.append(tmpdir.entryList(QStringList("*.png")).replaceInStrings(QRegExp("^(.*)\\.png"), "\\1"));
   250                      ).replaceInStrings(QRegExp("^(.*)\\.png"), "\\1")
   248 
   251                  );
   249     tmpdir.cd(datadir->absolutePath());
       
   250     tmpdir.cd("Graphics/Graves");
       
   251     tmpdir.setFilter(QDir::Files);
       
   252     QStringList tmpList = tmpdir.entryList(QStringList("*.png")).replaceInStrings(QRegExp("^(.*)\\.png"), "\\1");
       
   253     for (QStringList::Iterator it = tmpList.begin(); it != tmpList.end(); ++it) 
       
   254         if (!Graves.contains(*it,Qt::CaseInsensitive)) Graves.append(*it);
       
   255 
   252 
   256     if(Graves.size()==0)
   253     if(Graves.size()==0)
   257     {
   254     {
   258         //do some serious error handling
   255         // TODO do some serious error handling
   259         return "Error";
   256         return "Error";
   260     }
   257     }
   261 
   258 
   262     //pick a random grave
   259     //pick a random grave
   263     return Graves[rand()%(Graves.size())];
   260     return Graves[rand()%(Graves.size())];
   266 QString HWNamegen::getRandomFort()
   263 QString HWNamegen::getRandomFort()
   267 {
   264 {
   268     QStringList Forts;
   265     QStringList Forts;
   269 
   266 
   270     //list all available Forts
   267     //list all available Forts
   271     QDir tmpdir;
   268     Forts.append(HWDataManager::instance().entryList(
   272     tmpdir.cd(datadir->absolutePath());
   269                         "Forts",
   273     tmpdir.cd("Forts");
   270                         QDir::Files,
   274     tmpdir.setFilter(QDir::Files);
   271                         QStringList("*L.png")
   275     Forts.append(tmpdir.entryList(QStringList("*L.png")).replaceInStrings(QRegExp("^(.*)L\\.png"), "\\1"));
   272                     ).replaceInStrings(QRegExp("^(.*)L\\.png"), "\\1")
       
   273                 );
   276 
   274 
   277     if(Forts.size()==0)
   275     if(Forts.size()==0)
   278     {
   276     {
   279         //do some serious error handling
   277         // TODO do some serious error handling
   280         return "Error";
   278         return "Error";
   281     }
   279     }
   282 
   280 
   283     //pick a random fort
   281     //pick a random fort
   284     return Forts[rand()%(Forts.size())];
   282     return Forts[rand()%(Forts.size())];