1 /*
2  * Hedgewars, a free turn based strategy game
3  * Copyright (c) 2004-2018 Andrey Korotaev <unC0Rr@gmail.com>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; version 2 of the License
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17  */
18 
19 #include "mission.h"
20 #include "hwconsts.h"
21 #include "DataManager.h"
22 #include <QSettings>
23 #include <QObject>
24 #include <QLocale>
25 
getMissionTeamFile(QString & missionName,QString & teamName)26 QSettings* getMissionTeamFile(QString & missionName, QString & teamName)
27 {
28     QSettings* teamfile = new QSettings(cfgdir->absolutePath() + "/Teams/" + teamName + ".hwt", QSettings::IniFormat, 0);
29     teamfile->setIniCodec("UTF-8");
30     if (!teamfile->childGroups().contains("Mission " + missionName) &&
31             teamfile->childGroups().contains("Mission " + missionName)){
32         teamfile->beginGroup("Mission " + missionName);
33         QStringList keys = teamfile->childKeys();
34         teamfile->endGroup();
35         for (int i=0;i<keys.size();i++) {
36             QVariant value = teamfile->value("Mission " + missionName + "/" + keys[i]);
37             teamfile->setValue("Mission " + missionName + "/" + keys[i], value);
38         }
39         teamfile->remove("Mission " + missionName);
40     }
41 
42     return teamfile;
43 }
44 
45 /**
46     Returns true if the specified mission has been completed
47     missionName: Name of the mission in question
48     teamName: Name of the playing team
49 */
isMissionWon(QString & missionName,QString & teamName)50 bool isMissionWon(QString & missionName, QString & teamName)
51 {
52     QSettings* teamfile = getMissionTeamFile(missionName, teamName);
53     bool won = teamfile->value("Mission " + missionName + "/Won", false).toBool();
54     return won;
55 }
56 
57 /**
58     Returns true if the mission value adressed with the provided
59     missionName: Name of the mission in question
60     teamName: Name of the playing team
61     key: name of key to check
62 */
missionValueExists(QString & missionName,QString & teamName,QString key)63 bool missionValueExists(QString & missionName, QString & teamName, QString key)
64 {
65     QSettings* teamfile = getMissionTeamFile(missionName, teamName);
66     return teamfile->contains("Mission " + missionName + "/" + key);
67 }
68 /**
69     Returns a mission value.
70     NOTE: Check whether the mission value exists first, using missionValueExists.
71     missionName: Name of the mission in question
72     teamName: Name of the playing team
73     key: name of key to read its value from
74 */
getMissionValue(QString & missionName,QString & teamName,QString key)75 QVariant getMissionValue(QString & missionName, QString & teamName, QString key)
76 {
77     QSettings* teamfile = getMissionTeamFile(missionName, teamName);
78     return teamfile->value("Mission " + missionName + "/" + key);
79 }
80