QTfrontend/team.cpp
changeset 3333 560e2766c445
parent 3236 4ab3917d7d44
child 3344 b18a166e6ca4
--- a/QTfrontend/team.cpp	Sat Apr 10 20:48:09 2010 +0000
+++ b/QTfrontend/team.cpp	Sat Apr 10 21:36:40 2010 +0000
@@ -22,6 +22,7 @@
 #include <QStringList>
 #include <QLineEdit>
 #include <QCryptographicHash>
+#include <QSettings>
 #include "team.h"
 #include "hwform.h"
 #include "pages.h"
@@ -101,82 +102,26 @@
 
 bool HWTeam::LoadFromFile()
 {
-    numHedgehogs=4;
-    QFile cfgfile(cfgdir->absolutePath() + "/" + TeamName + ".cfg");
-    if (!cfgfile.open(QIODevice::ReadOnly)) return false;
-    QTextStream stream(&cfgfile);
-    stream.setCodec("UTF-8");
-    QString str;
-    QString action;
-
-    while (!stream.atEnd())
+    QSettings teamfile(cfgdir->absolutePath() + "/Teams/" + TeamName + ".ini", QSettings::IniFormat, 0);
+    teamfile.setIniCodec("UTF-8");
+    TeamName = teamfile.value("Team/Name", TeamName).toString();
+    Grave = teamfile.value("Team/Grave", "Statue").toString();
+    Fort = teamfile.value("Team/Fort", "Plane").toString();
+    Voicepack = teamfile.value("Team/Voicepack", "Default").toString();
+    Flag = teamfile.value("Team/Flag", "hedgewars").toString();
+    difficulty = teamfile.value("Team/Difficulty", 0).toInt();
+    for(int i = 0; i < 8; i++)
     {
-        str = stream.readLine();
-        if (str.startsWith(";")) continue;
-        /*if (str.startsWith("name team "))
-        {
-            str.remove(0, 10);
-            TeamName = str;
-        } else*/
-        if (str.startsWith("name hh"))
-        {
-            str.remove(0, 7);
-            long i = str.left(1).toLong();
-            if ((i < 0) || (i > 7)) continue;
-            str.remove(0, 2);
-            HHName[i] = str;
-        } else
-        if (str.startsWith("hat"))
-        {
-            str.remove(0, 3);
-            long i = str.left(1).toLong();
-            if ((i < 0) || (i > 7)) continue;
-            str.remove(0, 2);
-            HHHat[i] = str;
-// Somehow claymore managed an empty hat.  Until we figure out how, this should avoid a repeat
-            if (HHHat[i].length() == 0) HHHat[i] = "NoHat"; 
-        } else
-        if (str.startsWith("grave "))
-        {
-            str.remove(0, 6);
-            Grave = str;
-        } else
-        if (str.startsWith("fort "))
-        {
-            str.remove(0, 5);
-            Fort = str;
-        } else
-        if (str.startsWith("flag "))
-        {
-            str.remove(0, 5);
-            Flag = str;
-        } else
-        if (str.startsWith("voicepack "))
-        {
-            str.remove(0, 10);
-            Voicepack = str;
-        } else
-        if (str.startsWith("bind "))
-        {
-            str.remove(0, 5);
-            action = str.section(' ', 1);
-            str = str.section(' ', 0, 0);
-            str.truncate(15);
-            for (int i = 0; i < BINDS_NUMBER; i++)
-                if (action == binds[i].action)
-                {
-                    binds[i].strbind = str;
-                    break;
-                }
-        } else
-        if (str.startsWith("difficulty "))
-        {
-          str.remove(0, 11);
-          difficulty=str.toUInt();
-          if (difficulty>5) difficulty=0; // this shouldn't normally happen
-        }
+        QString hh = QString("Hedgehog%1/").arg(i);
+        HHName[i] = teamfile.value(hh + "Name", QString("hedgehog %1").arg(i)).toString();
+        HHHat[i] = teamfile.value(hh + "Hat", "NoHat").toString();
+        //teamfile.setValue(hh + "Kills", 0);
+        //teamfile.setValue(hh + "Deaths", 0);
+        //teamfile.setValue(hh + "Rounds", 0);
+        //teamfile.setValue(hh + "Suicides", 0);
     }
-    cfgfile.close();
+    for(int i = 0; i < BINDS_NUMBER; i++)
+        binds[i].action = teamfile.value(QString("Binds/%1").arg(binds[i].strbind), cbinds[i].action).toString();
     return true;
 }
 
@@ -184,11 +129,8 @@
 {
     if(m_isNetTeam)
         return false;
-
-    QFile cfgfile(cfgdir->absolutePath() + "/" + TeamName + ".cfg");
+    QFile cfgfile(cfgdir->absolutePath() + "/Teams/" + TeamName + ".ini");
     cfgfile.remove();
-
-        
     return true;
 }
 
@@ -196,31 +138,30 @@
 {
     if (OldTeamName != TeamName)
     {
-        QFile cfgfile(cfgdir->absolutePath() + "/" + OldTeamName + ".cfg");
+        QFile cfgfile(cfgdir->absolutePath() + "/Teams/" + OldTeamName + ".ini");
         cfgfile.remove();
         OldTeamName = TeamName;
     }
-    QFile cfgfile(cfgdir->absolutePath() + "/" + TeamName + ".cfg");
-    if (!cfgfile.open(QIODevice::WriteOnly)) return false;
-    QTextStream stream(&cfgfile);
-    stream.setCodec("UTF-8");
-    stream << "; Generated by Hedgewars, do not modify" << endl;
-    stream << "name team " << TeamName << endl;
-    for (int i = 0; i < 8; i++)
+    QSettings teamfile(cfgdir->absolutePath() + "/Teams/" + TeamName + ".ini", QSettings::IniFormat, 0);
+    teamfile.setIniCodec("UTF-8");
+    teamfile.setValue("Team/Name", TeamName);
+    teamfile.setValue("Team/Grave", Grave);
+    teamfile.setValue("Team/Fort", Fort);
+    teamfile.setValue("Team/Voicepack", Voicepack);
+    teamfile.setValue("Team/Flag", Flag);
+    teamfile.setValue("Team/Difficulty", difficulty);
+    for(int i = 0; i < 8; i++)
     {
-        stream << "name hh" << i << " " << HHName[i] << endl;
-        stream << "hat" << i << " " << HHHat[i] << endl;
+        QString hh = QString("Hedgehog%1/").arg(i);
+        teamfile.setValue(hh + "Name", HHName[i]);
+        teamfile.setValue(hh + "Hat", HHHat[i]);
+        teamfile.setValue(hh + "Kills", 0);
+        teamfile.setValue(hh + "Deaths", 0);
+        teamfile.setValue(hh + "Rounds", 0);
+        teamfile.setValue(hh + "Suicides", 0);
     }
-    stream << "grave " << Grave << endl;
-    stream << "fort " << Fort << endl;
-    stream << "voicepack " << Voicepack << endl;
-    stream << "flag " << Flag << endl;
     for(int i = 0; i < BINDS_NUMBER; i++)
-    {
-        stream << "bind " << binds[i].strbind << " " << binds[i].action << endl;
-    }
-    stream << "difficulty " << difficulty << endl;
-    cfgfile.close();
+        teamfile.setValue(QString("Binds/%1").arg(binds[i].strbind), binds[i].action);
     return true;
 }