QTfrontend/team.cpp
changeset 3333 560e2766c445
parent 3236 4ab3917d7d44
child 3344 b18a166e6ca4
equal deleted inserted replaced
3332:3c90a923f156 3333:560e2766c445
    20 #include <QTextStream>
    20 #include <QTextStream>
    21 #include <QApplication>
    21 #include <QApplication>
    22 #include <QStringList>
    22 #include <QStringList>
    23 #include <QLineEdit>
    23 #include <QLineEdit>
    24 #include <QCryptographicHash>
    24 #include <QCryptographicHash>
       
    25 #include <QSettings>
    25 #include "team.h"
    26 #include "team.h"
    26 #include "hwform.h"
    27 #include "hwform.h"
    27 #include "pages.h"
    28 #include "pages.h"
    28 #include "hwconsts.h"
    29 #include "hwconsts.h"
    29 #include "hats.h"
    30 #include "hats.h"
    99 }
   100 }
   100 
   101 
   101 
   102 
   102 bool HWTeam::LoadFromFile()
   103 bool HWTeam::LoadFromFile()
   103 {
   104 {
   104     numHedgehogs=4;
   105     QSettings teamfile(cfgdir->absolutePath() + "/Teams/" + TeamName + ".ini", QSettings::IniFormat, 0);
   105     QFile cfgfile(cfgdir->absolutePath() + "/" + TeamName + ".cfg");
   106     teamfile.setIniCodec("UTF-8");
   106     if (!cfgfile.open(QIODevice::ReadOnly)) return false;
   107     TeamName = teamfile.value("Team/Name", TeamName).toString();
   107     QTextStream stream(&cfgfile);
   108     Grave = teamfile.value("Team/Grave", "Statue").toString();
   108     stream.setCodec("UTF-8");
   109     Fort = teamfile.value("Team/Fort", "Plane").toString();
   109     QString str;
   110     Voicepack = teamfile.value("Team/Voicepack", "Default").toString();
   110     QString action;
   111     Flag = teamfile.value("Team/Flag", "hedgewars").toString();
   111 
   112     difficulty = teamfile.value("Team/Difficulty", 0).toInt();
   112     while (!stream.atEnd())
   113     for(int i = 0; i < 8; i++)
   113     {
   114     {
   114         str = stream.readLine();
   115         QString hh = QString("Hedgehog%1/").arg(i);
   115         if (str.startsWith(";")) continue;
   116         HHName[i] = teamfile.value(hh + "Name", QString("hedgehog %1").arg(i)).toString();
   116         /*if (str.startsWith("name team "))
   117         HHHat[i] = teamfile.value(hh + "Hat", "NoHat").toString();
   117         {
   118         //teamfile.setValue(hh + "Kills", 0);
   118             str.remove(0, 10);
   119         //teamfile.setValue(hh + "Deaths", 0);
   119             TeamName = str;
   120         //teamfile.setValue(hh + "Rounds", 0);
   120         } else*/
   121         //teamfile.setValue(hh + "Suicides", 0);
   121         if (str.startsWith("name hh"))
   122     }
   122         {
   123     for(int i = 0; i < BINDS_NUMBER; i++)
   123             str.remove(0, 7);
   124         binds[i].action = teamfile.value(QString("Binds/%1").arg(binds[i].strbind), cbinds[i].action).toString();
   124             long i = str.left(1).toLong();
       
   125             if ((i < 0) || (i > 7)) continue;
       
   126             str.remove(0, 2);
       
   127             HHName[i] = str;
       
   128         } else
       
   129         if (str.startsWith("hat"))
       
   130         {
       
   131             str.remove(0, 3);
       
   132             long i = str.left(1).toLong();
       
   133             if ((i < 0) || (i > 7)) continue;
       
   134             str.remove(0, 2);
       
   135             HHHat[i] = str;
       
   136 // Somehow claymore managed an empty hat.  Until we figure out how, this should avoid a repeat
       
   137             if (HHHat[i].length() == 0) HHHat[i] = "NoHat"; 
       
   138         } else
       
   139         if (str.startsWith("grave "))
       
   140         {
       
   141             str.remove(0, 6);
       
   142             Grave = str;
       
   143         } else
       
   144         if (str.startsWith("fort "))
       
   145         {
       
   146             str.remove(0, 5);
       
   147             Fort = str;
       
   148         } else
       
   149         if (str.startsWith("flag "))
       
   150         {
       
   151             str.remove(0, 5);
       
   152             Flag = str;
       
   153         } else
       
   154         if (str.startsWith("voicepack "))
       
   155         {
       
   156             str.remove(0, 10);
       
   157             Voicepack = str;
       
   158         } else
       
   159         if (str.startsWith("bind "))
       
   160         {
       
   161             str.remove(0, 5);
       
   162             action = str.section(' ', 1);
       
   163             str = str.section(' ', 0, 0);
       
   164             str.truncate(15);
       
   165             for (int i = 0; i < BINDS_NUMBER; i++)
       
   166                 if (action == binds[i].action)
       
   167                 {
       
   168                     binds[i].strbind = str;
       
   169                     break;
       
   170                 }
       
   171         } else
       
   172         if (str.startsWith("difficulty "))
       
   173         {
       
   174           str.remove(0, 11);
       
   175           difficulty=str.toUInt();
       
   176           if (difficulty>5) difficulty=0; // this shouldn't normally happen
       
   177         }
       
   178     }
       
   179     cfgfile.close();
       
   180     return true;
   125     return true;
   181 }
   126 }
   182 
   127 
   183 bool HWTeam::DeleteFile()
   128 bool HWTeam::DeleteFile()
   184 {
   129 {
   185     if(m_isNetTeam)
   130     if(m_isNetTeam)
   186         return false;
   131         return false;
   187 
   132     QFile cfgfile(cfgdir->absolutePath() + "/Teams/" + TeamName + ".ini");
   188     QFile cfgfile(cfgdir->absolutePath() + "/" + TeamName + ".cfg");
       
   189     cfgfile.remove();
   133     cfgfile.remove();
   190 
       
   191         
       
   192     return true;
   134     return true;
   193 }
   135 }
   194 
   136 
   195 bool HWTeam::SaveToFile()
   137 bool HWTeam::SaveToFile()
   196 {
   138 {
   197     if (OldTeamName != TeamName)
   139     if (OldTeamName != TeamName)
   198     {
   140     {
   199         QFile cfgfile(cfgdir->absolutePath() + "/" + OldTeamName + ".cfg");
   141         QFile cfgfile(cfgdir->absolutePath() + "/Teams/" + OldTeamName + ".ini");
   200         cfgfile.remove();
   142         cfgfile.remove();
   201         OldTeamName = TeamName;
   143         OldTeamName = TeamName;
   202     }
   144     }
   203     QFile cfgfile(cfgdir->absolutePath() + "/" + TeamName + ".cfg");
   145     QSettings teamfile(cfgdir->absolutePath() + "/Teams/" + TeamName + ".ini", QSettings::IniFormat, 0);
   204     if (!cfgfile.open(QIODevice::WriteOnly)) return false;
   146     teamfile.setIniCodec("UTF-8");
   205     QTextStream stream(&cfgfile);
   147     teamfile.setValue("Team/Name", TeamName);
   206     stream.setCodec("UTF-8");
   148     teamfile.setValue("Team/Grave", Grave);
   207     stream << "; Generated by Hedgewars, do not modify" << endl;
   149     teamfile.setValue("Team/Fort", Fort);
   208     stream << "name team " << TeamName << endl;
   150     teamfile.setValue("Team/Voicepack", Voicepack);
   209     for (int i = 0; i < 8; i++)
   151     teamfile.setValue("Team/Flag", Flag);
   210     {
   152     teamfile.setValue("Team/Difficulty", difficulty);
   211         stream << "name hh" << i << " " << HHName[i] << endl;
   153     for(int i = 0; i < 8; i++)
   212         stream << "hat" << i << " " << HHHat[i] << endl;
   154     {
   213     }
   155         QString hh = QString("Hedgehog%1/").arg(i);
   214     stream << "grave " << Grave << endl;
   156         teamfile.setValue(hh + "Name", HHName[i]);
   215     stream << "fort " << Fort << endl;
   157         teamfile.setValue(hh + "Hat", HHHat[i]);
   216     stream << "voicepack " << Voicepack << endl;
   158         teamfile.setValue(hh + "Kills", 0);
   217     stream << "flag " << Flag << endl;
   159         teamfile.setValue(hh + "Deaths", 0);
   218     for(int i = 0; i < BINDS_NUMBER; i++)
   160         teamfile.setValue(hh + "Rounds", 0);
   219     {
   161         teamfile.setValue(hh + "Suicides", 0);
   220         stream << "bind " << binds[i].strbind << " " << binds[i].action << endl;
   162     }
   221     }
   163     for(int i = 0; i < BINDS_NUMBER; i++)
   222     stream << "difficulty " << difficulty << endl;
   164         teamfile.setValue(QString("Binds/%1").arg(binds[i].strbind), binds[i].action);
   223     cfgfile.close();
       
   224     return true;
   165     return true;
   225 }
   166 }
   226 
   167 
   227 void HWTeam::SetToPage(HWForm * hwform)
   168 void HWTeam::SetToPage(HWForm * hwform)
   228 {
   169 {